1 // SPDX-License-Identifier: GPL-2.0-only
5 * TI OMAP3 ISP - Statistics core
7 * Copyright (C) 2010 Nokia Corporation
8 * Copyright (C) 2009 Texas Instruments, Inc
10 * Contacts: David Cohen <dacohen@gmail.com>
11 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
12 * Sakari Ailus <sakari.ailus@iki.fi>
15 #include <linux/dma-mapping.h>
16 #include <linux/slab.h>
17 #include <linux/timekeeping.h>
18 #include <linux/uaccess.h>
22 #define ISP_STAT_USES_DMAENGINE(stat) ((stat)->dma_ch != NULL)
25 * MAGIC_SIZE must always be the greatest common divisor of
26 * AEWB_PACKET_SIZE and AF_PAXEL_SIZE.
29 #define MAGIC_NUM 0x55
31 /* HACK: AF module seems to be writing one more paxel data than it should. */
32 #define AF_EXTRA_DATA OMAP3ISP_AF_PAXEL_SIZE
35 * HACK: H3A modules go to an invalid state after have a SBL overflow. It makes
36 * the next buffer to start to be written in the same point where the overflow
37 * occurred instead of the configured address. The only known way to make it to
38 * go back to a valid state is having a valid buffer processing. Of course it
39 * requires at least a doubled buffer size to avoid an access to invalid memory
40 * region. But it does not fix everything. It may happen more than one
41 * consecutive SBL overflows. In that case, it might be unpredictable how many
42 * buffers the allocated memory should fit. For that case, a recover
43 * configuration was created. It produces the minimum buffer size for each H3A
44 * module and decrease the change for more SBL overflows. This recover state
45 * will be enabled every time a SBL overflow occur. As the output buffer size
46 * isn't big, it's possible to have an extra size able to fit many recover
47 * buffers making it extreamily unlikely to have an access to invalid memory
50 #define NUM_H3A_RECOVER_BUFS 10
53 * HACK: Because of HW issues the generic layer sometimes need to have
54 * different behaviour for different statistic modules.
56 #define IS_H3A_AF(stat) ((stat) == &(stat)->isp->isp_af)
57 #define IS_H3A_AEWB(stat) ((stat) == &(stat)->isp->isp_aewb)
58 #define IS_H3A(stat) (IS_H3A_AF(stat) || IS_H3A_AEWB(stat))
60 static void __isp_stat_buf_sync_magic(struct ispstat
*stat
,
61 struct ispstat_buffer
*buf
,
62 u32 buf_size
, enum dma_data_direction dir
,
63 void (*dma_sync
)(struct device
*,
64 dma_addr_t
, unsigned long, size_t,
65 enum dma_data_direction
))
67 /* Sync the initial and final magic words. */
68 dma_sync(stat
->isp
->dev
, buf
->dma_addr
, 0, MAGIC_SIZE
, dir
);
69 dma_sync(stat
->isp
->dev
, buf
->dma_addr
+ (buf_size
& PAGE_MASK
),
70 buf_size
& ~PAGE_MASK
, MAGIC_SIZE
, dir
);
73 static void isp_stat_buf_sync_magic_for_device(struct ispstat
*stat
,
74 struct ispstat_buffer
*buf
,
76 enum dma_data_direction dir
)
78 if (ISP_STAT_USES_DMAENGINE(stat
))
81 __isp_stat_buf_sync_magic(stat
, buf
, buf_size
, dir
,
82 dma_sync_single_range_for_device
);
85 static void isp_stat_buf_sync_magic_for_cpu(struct ispstat
*stat
,
86 struct ispstat_buffer
*buf
,
88 enum dma_data_direction dir
)
90 if (ISP_STAT_USES_DMAENGINE(stat
))
93 __isp_stat_buf_sync_magic(stat
, buf
, buf_size
, dir
,
94 dma_sync_single_range_for_cpu
);
97 static int isp_stat_buf_check_magic(struct ispstat
*stat
,
98 struct ispstat_buffer
*buf
)
100 const u32 buf_size
= IS_H3A_AF(stat
) ?
101 buf
->buf_size
+ AF_EXTRA_DATA
: buf
->buf_size
;
106 isp_stat_buf_sync_magic_for_cpu(stat
, buf
, buf_size
, DMA_FROM_DEVICE
);
108 /* Checking initial magic numbers. They shouldn't be here anymore. */
109 for (w
= buf
->virt_addr
, end
= w
+ MAGIC_SIZE
; w
< end
; w
++)
110 if (likely(*w
!= MAGIC_NUM
))
114 dev_dbg(stat
->isp
->dev
,
115 "%s: beginning magic check does not match.\n",
120 /* Checking magic numbers at the end. They must be still here. */
121 for (w
= buf
->virt_addr
+ buf_size
, end
= w
+ MAGIC_SIZE
;
123 if (unlikely(*w
!= MAGIC_NUM
)) {
124 dev_dbg(stat
->isp
->dev
,
125 "%s: ending magic check does not match.\n",
131 isp_stat_buf_sync_magic_for_device(stat
, buf
, buf_size
,
137 static void isp_stat_buf_insert_magic(struct ispstat
*stat
,
138 struct ispstat_buffer
*buf
)
140 const u32 buf_size
= IS_H3A_AF(stat
) ?
141 stat
->buf_size
+ AF_EXTRA_DATA
: stat
->buf_size
;
143 isp_stat_buf_sync_magic_for_cpu(stat
, buf
, buf_size
, DMA_FROM_DEVICE
);
146 * Inserting MAGIC_NUM at the beginning and end of the buffer.
147 * buf->buf_size is set only after the buffer is queued. For now the
148 * right buf_size for the current configuration is pointed by
151 memset(buf
->virt_addr
, MAGIC_NUM
, MAGIC_SIZE
);
152 memset(buf
->virt_addr
+ buf_size
, MAGIC_NUM
, MAGIC_SIZE
);
154 isp_stat_buf_sync_magic_for_device(stat
, buf
, buf_size
,
158 static void isp_stat_buf_sync_for_device(struct ispstat
*stat
,
159 struct ispstat_buffer
*buf
)
161 if (ISP_STAT_USES_DMAENGINE(stat
))
164 dma_sync_sg_for_device(stat
->isp
->dev
, buf
->sgt
.sgl
,
165 buf
->sgt
.nents
, DMA_FROM_DEVICE
);
168 static void isp_stat_buf_sync_for_cpu(struct ispstat
*stat
,
169 struct ispstat_buffer
*buf
)
171 if (ISP_STAT_USES_DMAENGINE(stat
))
174 dma_sync_sg_for_cpu(stat
->isp
->dev
, buf
->sgt
.sgl
,
175 buf
->sgt
.nents
, DMA_FROM_DEVICE
);
178 static void isp_stat_buf_clear(struct ispstat
*stat
)
182 for (i
= 0; i
< STAT_MAX_BUFS
; i
++)
183 stat
->buf
[i
].empty
= 1;
186 static struct ispstat_buffer
*
187 __isp_stat_buf_find(struct ispstat
*stat
, int look_empty
)
189 struct ispstat_buffer
*found
= NULL
;
192 for (i
= 0; i
< STAT_MAX_BUFS
; i
++) {
193 struct ispstat_buffer
*curr
= &stat
->buf
[i
];
196 * Don't select the buffer which is being copied to
197 * userspace or used by the module.
199 if (curr
== stat
->locked_buf
|| curr
== stat
->active_buf
)
202 /* Don't select uninitialised buffers if it's not required */
203 if (!look_empty
&& curr
->empty
)
206 /* Pick uninitialised buffer over anything else if look_empty */
212 /* Choose the oldest buffer */
214 (s32
)curr
->frame_number
- (s32
)found
->frame_number
< 0)
221 static inline struct ispstat_buffer
*
222 isp_stat_buf_find_oldest(struct ispstat
*stat
)
224 return __isp_stat_buf_find(stat
, 0);
227 static inline struct ispstat_buffer
*
228 isp_stat_buf_find_oldest_or_empty(struct ispstat
*stat
)
230 return __isp_stat_buf_find(stat
, 1);
233 static int isp_stat_buf_queue(struct ispstat
*stat
)
235 if (!stat
->active_buf
)
238 ktime_get_ts64(&stat
->active_buf
->ts
);
240 stat
->active_buf
->buf_size
= stat
->buf_size
;
241 if (isp_stat_buf_check_magic(stat
, stat
->active_buf
)) {
242 dev_dbg(stat
->isp
->dev
, "%s: data wasn't properly written.\n",
246 stat
->active_buf
->config_counter
= stat
->config_counter
;
247 stat
->active_buf
->frame_number
= stat
->frame_number
;
248 stat
->active_buf
->empty
= 0;
249 stat
->active_buf
= NULL
;
251 return STAT_BUF_DONE
;
254 /* Get next free buffer to write the statistics to and mark it active. */
255 static void isp_stat_buf_next(struct ispstat
*stat
)
257 if (unlikely(stat
->active_buf
))
258 /* Overwriting unused active buffer */
259 dev_dbg(stat
->isp
->dev
,
260 "%s: new buffer requested without queuing active one.\n",
263 stat
->active_buf
= isp_stat_buf_find_oldest_or_empty(stat
);
266 static void isp_stat_buf_release(struct ispstat
*stat
)
270 isp_stat_buf_sync_for_device(stat
, stat
->locked_buf
);
271 spin_lock_irqsave(&stat
->isp
->stat_lock
, flags
);
272 stat
->locked_buf
= NULL
;
273 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
276 /* Get buffer to userspace. */
277 static struct ispstat_buffer
*isp_stat_buf_get(struct ispstat
*stat
,
278 struct omap3isp_stat_data
*data
)
282 struct ispstat_buffer
*buf
;
284 spin_lock_irqsave(&stat
->isp
->stat_lock
, flags
);
287 buf
= isp_stat_buf_find_oldest(stat
);
289 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
290 dev_dbg(stat
->isp
->dev
, "%s: cannot find a buffer.\n",
292 return ERR_PTR(-EBUSY
);
294 if (isp_stat_buf_check_magic(stat
, buf
)) {
295 dev_dbg(stat
->isp
->dev
,
296 "%s: current buffer has corrupted data\n.",
298 /* Mark empty because it doesn't have valid data. */
301 /* Buffer isn't corrupted. */
306 stat
->locked_buf
= buf
;
308 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
310 if (buf
->buf_size
> data
->buf_size
) {
311 dev_warn(stat
->isp
->dev
,
312 "%s: userspace's buffer size is not enough.\n",
314 isp_stat_buf_release(stat
);
315 return ERR_PTR(-EINVAL
);
318 isp_stat_buf_sync_for_cpu(stat
, buf
);
320 rval
= copy_to_user(data
->buf
,
325 dev_info(stat
->isp
->dev
,
326 "%s: failed copying %d bytes of stat data\n",
327 stat
->subdev
.name
, rval
);
328 buf
= ERR_PTR(-EFAULT
);
329 isp_stat_buf_release(stat
);
335 static void isp_stat_bufs_free(struct ispstat
*stat
)
337 struct device
*dev
= ISP_STAT_USES_DMAENGINE(stat
)
338 ? NULL
: stat
->isp
->dev
;
341 for (i
= 0; i
< STAT_MAX_BUFS
; i
++) {
342 struct ispstat_buffer
*buf
= &stat
->buf
[i
];
347 sg_free_table(&buf
->sgt
);
349 dma_free_coherent(dev
, stat
->buf_alloc_size
, buf
->virt_addr
,
353 buf
->virt_addr
= NULL
;
357 dev_dbg(stat
->isp
->dev
, "%s: all buffers were freed.\n",
360 stat
->buf_alloc_size
= 0;
361 stat
->active_buf
= NULL
;
364 static int isp_stat_bufs_alloc_one(struct device
*dev
,
365 struct ispstat_buffer
*buf
,
370 buf
->virt_addr
= dma_alloc_coherent(dev
, size
, &buf
->dma_addr
,
375 ret
= dma_get_sgtable(dev
, &buf
->sgt
, buf
->virt_addr
, buf
->dma_addr
,
378 dma_free_coherent(dev
, size
, buf
->virt_addr
, buf
->dma_addr
);
379 buf
->virt_addr
= NULL
;
388 * The device passed to the DMA API depends on whether the statistics block uses
389 * ISP DMA, external DMA or PIO to transfer data.
391 * The first case (for the AEWB and AF engines) passes the ISP device, resulting
392 * in the DMA buffers being mapped through the ISP IOMMU.
394 * The second case (for the histogram engine) should pass the DMA engine device.
395 * As that device isn't accessible through the OMAP DMA engine API the driver
396 * passes NULL instead, resulting in the buffers being mapped directly as
399 * The third case (for the histogram engine) doesn't require any mapping. The
400 * buffers could be allocated with kmalloc/vmalloc, but we still use
401 * dma_alloc_coherent() for consistency purpose.
403 static int isp_stat_bufs_alloc(struct ispstat
*stat
, u32 size
)
405 struct device
*dev
= ISP_STAT_USES_DMAENGINE(stat
)
406 ? NULL
: stat
->isp
->dev
;
410 spin_lock_irqsave(&stat
->isp
->stat_lock
, flags
);
412 BUG_ON(stat
->locked_buf
!= NULL
);
414 /* Are the old buffers big enough? */
415 if (stat
->buf_alloc_size
>= size
) {
416 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
420 if (stat
->state
!= ISPSTAT_DISABLED
|| stat
->buf_processing
) {
421 dev_info(stat
->isp
->dev
,
422 "%s: trying to allocate memory when busy\n",
424 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
428 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
430 isp_stat_bufs_free(stat
);
432 stat
->buf_alloc_size
= size
;
434 for (i
= 0; i
< STAT_MAX_BUFS
; i
++) {
435 struct ispstat_buffer
*buf
= &stat
->buf
[i
];
438 ret
= isp_stat_bufs_alloc_one(dev
, buf
, size
);
440 dev_err(stat
->isp
->dev
,
441 "%s: Failed to allocate DMA buffer %u\n",
442 stat
->subdev
.name
, i
);
443 isp_stat_bufs_free(stat
);
449 dev_dbg(stat
->isp
->dev
,
450 "%s: buffer[%u] allocated. dma=%pad virt=%p",
451 stat
->subdev
.name
, i
, &buf
->dma_addr
, buf
->virt_addr
);
457 static void isp_stat_queue_event(struct ispstat
*stat
, int err
)
459 struct video_device
*vdev
= stat
->subdev
.devnode
;
460 struct v4l2_event event
;
461 struct omap3isp_stat_event_status
*status
= (void *)event
.u
.data
;
463 memset(&event
, 0, sizeof(event
));
465 status
->frame_number
= stat
->frame_number
;
466 status
->config_counter
= stat
->config_counter
;
470 event
.type
= stat
->event_type
;
471 v4l2_event_queue(vdev
, &event
);
476 * omap3isp_stat_request_statistics - Request statistics.
477 * @data: Pointer to return statistics data.
479 * Returns 0 if successful.
481 int omap3isp_stat_request_statistics(struct ispstat
*stat
,
482 struct omap3isp_stat_data
*data
)
484 struct ispstat_buffer
*buf
;
486 if (stat
->state
!= ISPSTAT_ENABLED
) {
487 dev_dbg(stat
->isp
->dev
, "%s: engine not enabled.\n",
492 mutex_lock(&stat
->ioctl_lock
);
493 buf
= isp_stat_buf_get(stat
, data
);
495 mutex_unlock(&stat
->ioctl_lock
);
499 data
->ts
.tv_sec
= buf
->ts
.tv_sec
;
500 data
->ts
.tv_usec
= buf
->ts
.tv_nsec
/ NSEC_PER_USEC
;
501 data
->config_counter
= buf
->config_counter
;
502 data
->frame_number
= buf
->frame_number
;
503 data
->buf_size
= buf
->buf_size
;
506 isp_stat_buf_release(stat
);
507 mutex_unlock(&stat
->ioctl_lock
);
512 int omap3isp_stat_request_statistics_time32(struct ispstat
*stat
,
513 struct omap3isp_stat_data_time32
*data
)
515 struct omap3isp_stat_data data64
;
518 ret
= omap3isp_stat_request_statistics(stat
, &data64
);
522 data
->ts
.tv_sec
= data64
.ts
.tv_sec
;
523 data
->ts
.tv_usec
= data64
.ts
.tv_usec
;
524 memcpy(&data
->buf
, &data64
.buf
, sizeof(*data
) - sizeof(data
->ts
));
530 * omap3isp_stat_config - Receives new statistic engine configuration.
531 * @new_conf: Pointer to config structure.
533 * Returns 0 if successful, -EINVAL if new_conf pointer is NULL, -ENOMEM if
534 * was unable to allocate memory for the buffer, or other errors if parameters
537 int omap3isp_stat_config(struct ispstat
*stat
, void *new_conf
)
540 unsigned long irqflags
;
541 struct ispstat_generic_config
*user_cfg
= new_conf
;
542 u32 buf_size
= user_cfg
->buf_size
;
544 mutex_lock(&stat
->ioctl_lock
);
546 dev_dbg(stat
->isp
->dev
,
547 "%s: configuring module with buffer size=0x%08lx\n",
548 stat
->subdev
.name
, (unsigned long)buf_size
);
550 ret
= stat
->ops
->validate_params(stat
, new_conf
);
552 mutex_unlock(&stat
->ioctl_lock
);
553 dev_dbg(stat
->isp
->dev
, "%s: configuration values are invalid.\n",
558 if (buf_size
!= user_cfg
->buf_size
)
559 dev_dbg(stat
->isp
->dev
,
560 "%s: driver has corrected buffer size request to 0x%08lx\n",
562 (unsigned long)user_cfg
->buf_size
);
565 * Hack: H3A modules may need a doubled buffer size to avoid access
566 * to a invalid memory address after a SBL overflow.
567 * The buffer size is always PAGE_ALIGNED.
568 * Hack 2: MAGIC_SIZE is added to buf_size so a magic word can be
569 * inserted at the end to data integrity check purpose.
570 * Hack 3: AF module writes one paxel data more than it should, so
571 * the buffer allocation must consider it to avoid invalid memory
573 * Hack 4: H3A need to allocate extra space for the recover state.
576 buf_size
= user_cfg
->buf_size
* 2 + MAGIC_SIZE
;
579 * Adding one extra paxel data size for each recover
580 * buffer + 2 regular ones.
582 buf_size
+= AF_EXTRA_DATA
* (NUM_H3A_RECOVER_BUFS
+ 2);
583 if (stat
->recover_priv
) {
584 struct ispstat_generic_config
*recover_cfg
=
586 buf_size
+= recover_cfg
->buf_size
*
587 NUM_H3A_RECOVER_BUFS
;
589 buf_size
= PAGE_ALIGN(buf_size
);
590 } else { /* Histogram */
591 buf_size
= PAGE_ALIGN(user_cfg
->buf_size
+ MAGIC_SIZE
);
594 ret
= isp_stat_bufs_alloc(stat
, buf_size
);
596 mutex_unlock(&stat
->ioctl_lock
);
600 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
601 stat
->ops
->set_params(stat
, new_conf
);
602 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
605 * Returning the right future config_counter for this setup, so
606 * userspace can *know* when it has been applied.
608 user_cfg
->config_counter
= stat
->config_counter
+ stat
->inc_config
;
610 /* Module has a valid configuration. */
611 stat
->configured
= 1;
612 dev_dbg(stat
->isp
->dev
,
613 "%s: module has been successfully configured.\n",
616 mutex_unlock(&stat
->ioctl_lock
);
622 * isp_stat_buf_process - Process statistic buffers.
623 * @buf_state: points out if buffer is ready to be processed. It's necessary
624 * because histogram needs to copy the data from internal memory
625 * before be able to process the buffer.
627 static int isp_stat_buf_process(struct ispstat
*stat
, int buf_state
)
629 int ret
= STAT_NO_BUF
;
631 if (!atomic_add_unless(&stat
->buf_err
, -1, 0) &&
632 buf_state
== STAT_BUF_DONE
&& stat
->state
== ISPSTAT_ENABLED
) {
633 ret
= isp_stat_buf_queue(stat
);
634 isp_stat_buf_next(stat
);
640 int omap3isp_stat_pcr_busy(struct ispstat
*stat
)
642 return stat
->ops
->busy(stat
);
645 int omap3isp_stat_busy(struct ispstat
*stat
)
647 return omap3isp_stat_pcr_busy(stat
) | stat
->buf_processing
|
648 (stat
->state
!= ISPSTAT_DISABLED
);
652 * isp_stat_pcr_enable - Disables/Enables statistic engines.
653 * @pcr_enable: 0/1 - Disables/Enables the engine.
655 * Must be called from ISP driver when the module is idle and synchronized
658 static void isp_stat_pcr_enable(struct ispstat
*stat
, u8 pcr_enable
)
660 if ((stat
->state
!= ISPSTAT_ENABLING
&&
661 stat
->state
!= ISPSTAT_ENABLED
) && pcr_enable
)
662 /* Userspace has disabled the module. Aborting. */
665 stat
->ops
->enable(stat
, pcr_enable
);
666 if (stat
->state
== ISPSTAT_DISABLING
&& !pcr_enable
)
667 stat
->state
= ISPSTAT_DISABLED
;
668 else if (stat
->state
== ISPSTAT_ENABLING
&& pcr_enable
)
669 stat
->state
= ISPSTAT_ENABLED
;
672 void omap3isp_stat_suspend(struct ispstat
*stat
)
676 spin_lock_irqsave(&stat
->isp
->stat_lock
, flags
);
678 if (stat
->state
!= ISPSTAT_DISABLED
)
679 stat
->ops
->enable(stat
, 0);
680 if (stat
->state
== ISPSTAT_ENABLED
)
681 stat
->state
= ISPSTAT_SUSPENDED
;
683 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
686 void omap3isp_stat_resume(struct ispstat
*stat
)
688 /* Module will be re-enabled with its pipeline */
689 if (stat
->state
== ISPSTAT_SUSPENDED
)
690 stat
->state
= ISPSTAT_ENABLING
;
693 static void isp_stat_try_enable(struct ispstat
*stat
)
695 unsigned long irqflags
;
697 if (stat
->priv
== NULL
)
698 /* driver wasn't initialised */
701 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
702 if (stat
->state
== ISPSTAT_ENABLING
&& !stat
->buf_processing
&&
703 stat
->buf_alloc_size
) {
705 * Userspace's requested to enable the engine but it wasn't yet.
709 isp_stat_buf_next(stat
);
710 stat
->ops
->setup_regs(stat
, stat
->priv
);
711 isp_stat_buf_insert_magic(stat
, stat
->active_buf
);
714 * H3A module has some hw issues which forces the driver to
715 * ignore next buffers even if it was disabled in the meantime.
716 * On the other hand, Histogram shouldn't ignore buffers anymore
717 * if it's being enabled.
720 atomic_set(&stat
->buf_err
, 0);
722 isp_stat_pcr_enable(stat
, 1);
723 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
724 dev_dbg(stat
->isp
->dev
, "%s: module is enabled.\n",
727 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
731 void omap3isp_stat_isr_frame_sync(struct ispstat
*stat
)
733 isp_stat_try_enable(stat
);
736 void omap3isp_stat_sbl_overflow(struct ispstat
*stat
)
738 unsigned long irqflags
;
740 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
742 * Due to a H3A hw issue which prevents the next buffer to start from
743 * the correct memory address, 2 buffers must be ignored.
745 atomic_set(&stat
->buf_err
, 2);
748 * If more than one SBL overflow happen in a row, H3A module may access
749 * invalid memory region.
750 * stat->sbl_ovl_recover is set to tell to the driver to temporarily use
751 * a soft configuration which helps to avoid consecutive overflows.
753 if (stat
->recover_priv
)
754 stat
->sbl_ovl_recover
= 1;
755 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
759 * omap3isp_stat_enable - Disable/Enable statistic engine as soon as possible
760 * @enable: 0/1 - Disables/Enables the engine.
762 * Client should configure all the module registers before this.
763 * This function can be called from a userspace request.
765 int omap3isp_stat_enable(struct ispstat
*stat
, u8 enable
)
767 unsigned long irqflags
;
769 dev_dbg(stat
->isp
->dev
, "%s: user wants to %s module.\n",
770 stat
->subdev
.name
, enable
? "enable" : "disable");
772 /* Prevent enabling while configuring */
773 mutex_lock(&stat
->ioctl_lock
);
775 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
777 if (!stat
->configured
&& enable
) {
778 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
779 mutex_unlock(&stat
->ioctl_lock
);
780 dev_dbg(stat
->isp
->dev
,
781 "%s: cannot enable module as it's never been successfully configured so far.\n",
787 if (stat
->state
== ISPSTAT_DISABLING
)
788 /* Previous disabling request wasn't done yet */
789 stat
->state
= ISPSTAT_ENABLED
;
790 else if (stat
->state
== ISPSTAT_DISABLED
)
791 /* Module is now being enabled */
792 stat
->state
= ISPSTAT_ENABLING
;
794 if (stat
->state
== ISPSTAT_ENABLING
) {
795 /* Previous enabling request wasn't done yet */
796 stat
->state
= ISPSTAT_DISABLED
;
797 } else if (stat
->state
== ISPSTAT_ENABLED
) {
798 /* Module is now being disabled */
799 stat
->state
= ISPSTAT_DISABLING
;
800 isp_stat_buf_clear(stat
);
804 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
805 mutex_unlock(&stat
->ioctl_lock
);
810 int omap3isp_stat_s_stream(struct v4l2_subdev
*subdev
, int enable
)
812 struct ispstat
*stat
= v4l2_get_subdevdata(subdev
);
816 * Only set enable PCR bit if the module was previously
817 * enabled through ioctl.
819 isp_stat_try_enable(stat
);
822 /* Disable PCR bit and config enable field */
823 omap3isp_stat_enable(stat
, 0);
824 spin_lock_irqsave(&stat
->isp
->stat_lock
, flags
);
825 stat
->ops
->enable(stat
, 0);
826 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
829 * If module isn't busy, a new interrupt may come or not to
830 * set the state to DISABLED. As Histogram needs to read its
831 * internal memory to clear it, let interrupt handler
832 * responsible of changing state to DISABLED. If the last
833 * interrupt is coming, it's still safe as the handler will
834 * ignore the second time when state is already set to DISABLED.
835 * It's necessary to synchronize Histogram with streamoff, once
836 * the module may be considered idle before last SDMA transfer
837 * starts if we return here.
839 if (!omap3isp_stat_pcr_busy(stat
))
840 omap3isp_stat_isr(stat
);
842 dev_dbg(stat
->isp
->dev
, "%s: module is being disabled\n",
850 * __stat_isr - Interrupt handler for statistic drivers
852 static void __stat_isr(struct ispstat
*stat
, int from_dma
)
854 int ret
= STAT_BUF_DONE
;
856 unsigned long irqflags
;
857 struct isp_pipeline
*pipe
;
860 * stat->buf_processing must be set before disable module. It's
861 * necessary to not inform too early the buffers aren't busy in case
862 * of SDMA is going to be used.
864 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
865 if (stat
->state
== ISPSTAT_DISABLED
) {
866 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
869 buf_processing
= stat
->buf_processing
;
870 stat
->buf_processing
= 1;
871 stat
->ops
->enable(stat
, 0);
873 if (buf_processing
&& !from_dma
) {
874 if (stat
->state
== ISPSTAT_ENABLED
) {
875 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
876 dev_err(stat
->isp
->dev
,
877 "%s: interrupt occurred when module was still processing a buffer.\n",
883 * Interrupt handler was called from streamoff when
884 * the module wasn't busy anymore to ensure it is being
885 * disabled after process last buffer. If such buffer
886 * processing has already started, no need to do
889 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
893 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
895 /* If it's busy we can't process this buffer anymore */
896 if (!omap3isp_stat_pcr_busy(stat
)) {
897 if (!from_dma
&& stat
->ops
->buf_process
)
898 /* Module still need to copy data to buffer. */
899 ret
= stat
->ops
->buf_process(stat
);
900 if (ret
== STAT_BUF_WAITING_DMA
)
901 /* Buffer is not ready yet */
904 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
907 * Histogram needs to read its internal memory to clear it
908 * before be disabled. For that reason, common statistic layer
909 * can return only after call stat's buf_process() operator.
911 if (stat
->state
== ISPSTAT_DISABLING
) {
912 stat
->state
= ISPSTAT_DISABLED
;
913 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
914 stat
->buf_processing
= 0;
917 pipe
= to_isp_pipeline(&stat
->subdev
.entity
);
918 stat
->frame_number
= atomic_read(&pipe
->frame_number
);
921 * Before this point, 'ret' stores the buffer's status if it's
922 * ready to be processed. Afterwards, it holds the status if
923 * it was processed successfully.
925 ret
= isp_stat_buf_process(stat
, ret
);
927 if (likely(!stat
->sbl_ovl_recover
)) {
928 stat
->ops
->setup_regs(stat
, stat
->priv
);
931 * Using recover config to increase the chance to have
932 * a good buffer processing and make the H3A module to
933 * go back to a valid state.
936 stat
->ops
->setup_regs(stat
, stat
->recover_priv
);
937 stat
->sbl_ovl_recover
= 0;
940 * Set 'update' in case of the module needs to use
941 * regular configuration after next buffer.
946 isp_stat_buf_insert_magic(stat
, stat
->active_buf
);
949 * Hack: H3A modules may access invalid memory address or send
950 * corrupted data to userspace if more than 1 SBL overflow
951 * happens in a row without re-writing its buffer's start memory
952 * address in the meantime. Such situation is avoided if the
953 * module is not immediately re-enabled when the ISR misses the
954 * timing to process the buffer and to setup the registers.
955 * Because of that, pcr_enable(1) was moved to inside this 'if'
956 * block. But the next interruption will still happen as during
957 * pcr_enable(0) the module was busy.
959 isp_stat_pcr_enable(stat
, 1);
960 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
963 * If a SBL overflow occurs and the H3A driver misses the timing
964 * to process the buffer, stat->buf_err is set and won't be
965 * cleared now. So the next buffer will be correctly ignored.
966 * It's necessary due to a hw issue which makes the next H3A
967 * buffer to start from the memory address where the previous
968 * one stopped, instead of start where it was configured to.
969 * Do not "stat->buf_err = 0" here.
972 if (stat
->ops
->buf_process
)
974 * Driver may need to erase current data prior to
975 * process a new buffer. If it misses the timing, the
976 * next buffer might be wrong. So should be ignored.
977 * It happens only for Histogram.
979 atomic_set(&stat
->buf_err
, 1);
982 dev_dbg(stat
->isp
->dev
,
983 "%s: cannot process buffer, device is busy.\n",
988 stat
->buf_processing
= 0;
989 isp_stat_queue_event(stat
, ret
!= STAT_BUF_DONE
);
992 void omap3isp_stat_isr(struct ispstat
*stat
)
997 void omap3isp_stat_dma_isr(struct ispstat
*stat
)
1002 int omap3isp_stat_subscribe_event(struct v4l2_subdev
*subdev
,
1004 struct v4l2_event_subscription
*sub
)
1006 struct ispstat
*stat
= v4l2_get_subdevdata(subdev
);
1008 if (sub
->type
!= stat
->event_type
)
1011 return v4l2_event_subscribe(fh
, sub
, STAT_NEVENTS
, NULL
);
1014 int omap3isp_stat_unsubscribe_event(struct v4l2_subdev
*subdev
,
1016 struct v4l2_event_subscription
*sub
)
1018 return v4l2_event_unsubscribe(fh
, sub
);
1021 void omap3isp_stat_unregister_entities(struct ispstat
*stat
)
1023 v4l2_device_unregister_subdev(&stat
->subdev
);
1026 int omap3isp_stat_register_entities(struct ispstat
*stat
,
1027 struct v4l2_device
*vdev
)
1029 stat
->subdev
.dev
= vdev
->mdev
->dev
;
1031 return v4l2_device_register_subdev(vdev
, &stat
->subdev
);
1034 static int isp_stat_init_entities(struct ispstat
*stat
, const char *name
,
1035 const struct v4l2_subdev_ops
*sd_ops
)
1037 struct v4l2_subdev
*subdev
= &stat
->subdev
;
1038 struct media_entity
*me
= &subdev
->entity
;
1040 v4l2_subdev_init(subdev
, sd_ops
);
1041 snprintf(subdev
->name
, V4L2_SUBDEV_NAME_SIZE
, "OMAP3 ISP %s", name
);
1042 subdev
->grp_id
= BIT(16); /* group ID for isp subdevs */
1043 subdev
->flags
|= V4L2_SUBDEV_FL_HAS_EVENTS
| V4L2_SUBDEV_FL_HAS_DEVNODE
;
1044 v4l2_set_subdevdata(subdev
, stat
);
1046 stat
->pad
.flags
= MEDIA_PAD_FL_SINK
| MEDIA_PAD_FL_MUST_CONNECT
;
1049 return media_entity_pads_init(me
, 1, &stat
->pad
);
1052 int omap3isp_stat_init(struct ispstat
*stat
, const char *name
,
1053 const struct v4l2_subdev_ops
*sd_ops
)
1057 stat
->buf
= kcalloc(STAT_MAX_BUFS
, sizeof(*stat
->buf
), GFP_KERNEL
);
1061 isp_stat_buf_clear(stat
);
1062 mutex_init(&stat
->ioctl_lock
);
1063 atomic_set(&stat
->buf_err
, 0);
1065 ret
= isp_stat_init_entities(stat
, name
, sd_ops
);
1067 mutex_destroy(&stat
->ioctl_lock
);
1074 void omap3isp_stat_cleanup(struct ispstat
*stat
)
1076 media_entity_cleanup(&stat
->subdev
.entity
);
1077 mutex_destroy(&stat
->ioctl_lock
);
1078 isp_stat_bufs_free(stat
);
1081 kfree(stat
->recover_priv
);