4 * TI OMAP3 ISP - Statistics core
6 * Copyright (C) 2010 Nokia Corporation
7 * Copyright (C) 2009 Texas Instruments, Inc
9 * Contacts: David Cohen <dacohen@gmail.com>
10 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
11 * Sakari Ailus <sakari.ailus@iki.fi>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28 #include <linux/dma-mapping.h>
29 #include <linux/omap-iommu.h>
30 #include <linux/slab.h>
31 #include <linux/uaccess.h>
35 #define IS_COHERENT_BUF(stat) ((stat)->dma_ch >= 0)
38 * MAGIC_SIZE must always be the greatest common divisor of
39 * AEWB_PACKET_SIZE and AF_PAXEL_SIZE.
42 #define MAGIC_NUM 0x55
44 /* HACK: AF module seems to be writing one more paxel data than it should. */
45 #define AF_EXTRA_DATA OMAP3ISP_AF_PAXEL_SIZE
48 * HACK: H3A modules go to an invalid state after have a SBL overflow. It makes
49 * the next buffer to start to be written in the same point where the overflow
50 * occurred instead of the configured address. The only known way to make it to
51 * go back to a valid state is having a valid buffer processing. Of course it
52 * requires at least a doubled buffer size to avoid an access to invalid memory
53 * region. But it does not fix everything. It may happen more than one
54 * consecutive SBL overflows. In that case, it might be unpredictable how many
55 * buffers the allocated memory should fit. For that case, a recover
56 * configuration was created. It produces the minimum buffer size for each H3A
57 * module and decrease the change for more SBL overflows. This recover state
58 * will be enabled every time a SBL overflow occur. As the output buffer size
59 * isn't big, it's possible to have an extra size able to fit many recover
60 * buffers making it extreamily unlikely to have an access to invalid memory
63 #define NUM_H3A_RECOVER_BUFS 10
66 * HACK: Because of HW issues the generic layer sometimes need to have
67 * different behaviour for different statistic modules.
69 #define IS_H3A_AF(stat) ((stat) == &(stat)->isp->isp_af)
70 #define IS_H3A_AEWB(stat) ((stat) == &(stat)->isp->isp_aewb)
71 #define IS_H3A(stat) (IS_H3A_AF(stat) || IS_H3A_AEWB(stat))
73 static void __isp_stat_buf_sync_magic(struct ispstat
*stat
,
74 struct ispstat_buffer
*buf
,
75 u32 buf_size
, enum dma_data_direction dir
,
76 void (*dma_sync
)(struct device
*,
77 dma_addr_t
, unsigned long, size_t,
78 enum dma_data_direction
))
80 struct device
*dev
= stat
->isp
->dev
;
85 /* Initial magic words */
86 pg
= vmalloc_to_page(buf
->virt_addr
);
87 dma_addr
= pfn_to_dma(dev
, page_to_pfn(pg
));
88 dma_sync(dev
, dma_addr
, 0, MAGIC_SIZE
, dir
);
90 /* Final magic words */
91 pg
= vmalloc_to_page(buf
->virt_addr
+ buf_size
);
92 dma_addr
= pfn_to_dma(dev
, page_to_pfn(pg
));
93 offset
= ((u32
)buf
->virt_addr
+ buf_size
) & ~PAGE_MASK
;
94 dma_sync(dev
, dma_addr
, offset
, MAGIC_SIZE
, dir
);
97 static void isp_stat_buf_sync_magic_for_device(struct ispstat
*stat
,
98 struct ispstat_buffer
*buf
,
100 enum dma_data_direction dir
)
102 if (IS_COHERENT_BUF(stat
))
105 __isp_stat_buf_sync_magic(stat
, buf
, buf_size
, dir
,
106 dma_sync_single_range_for_device
);
109 static void isp_stat_buf_sync_magic_for_cpu(struct ispstat
*stat
,
110 struct ispstat_buffer
*buf
,
112 enum dma_data_direction dir
)
114 if (IS_COHERENT_BUF(stat
))
117 __isp_stat_buf_sync_magic(stat
, buf
, buf_size
, dir
,
118 dma_sync_single_range_for_cpu
);
121 static int isp_stat_buf_check_magic(struct ispstat
*stat
,
122 struct ispstat_buffer
*buf
)
124 const u32 buf_size
= IS_H3A_AF(stat
) ?
125 buf
->buf_size
+ AF_EXTRA_DATA
: buf
->buf_size
;
130 isp_stat_buf_sync_magic_for_cpu(stat
, buf
, buf_size
, DMA_FROM_DEVICE
);
132 /* Checking initial magic numbers. They shouldn't be here anymore. */
133 for (w
= buf
->virt_addr
, end
= w
+ MAGIC_SIZE
; w
< end
; w
++)
134 if (likely(*w
!= MAGIC_NUM
))
138 dev_dbg(stat
->isp
->dev
, "%s: beginning magic check does not "
139 "match.\n", stat
->subdev
.name
);
143 /* Checking magic numbers at the end. They must be still here. */
144 for (w
= buf
->virt_addr
+ buf_size
, end
= w
+ MAGIC_SIZE
;
146 if (unlikely(*w
!= MAGIC_NUM
)) {
147 dev_dbg(stat
->isp
->dev
, "%s: endding magic check does "
148 "not match.\n", stat
->subdev
.name
);
153 isp_stat_buf_sync_magic_for_device(stat
, buf
, buf_size
,
159 static void isp_stat_buf_insert_magic(struct ispstat
*stat
,
160 struct ispstat_buffer
*buf
)
162 const u32 buf_size
= IS_H3A_AF(stat
) ?
163 stat
->buf_size
+ AF_EXTRA_DATA
: stat
->buf_size
;
165 isp_stat_buf_sync_magic_for_cpu(stat
, buf
, buf_size
, DMA_FROM_DEVICE
);
168 * Inserting MAGIC_NUM at the beginning and end of the buffer.
169 * buf->buf_size is set only after the buffer is queued. For now the
170 * right buf_size for the current configuration is pointed by
173 memset(buf
->virt_addr
, MAGIC_NUM
, MAGIC_SIZE
);
174 memset(buf
->virt_addr
+ buf_size
, MAGIC_NUM
, MAGIC_SIZE
);
176 isp_stat_buf_sync_magic_for_device(stat
, buf
, buf_size
,
180 static void isp_stat_buf_sync_for_device(struct ispstat
*stat
,
181 struct ispstat_buffer
*buf
)
183 if (IS_COHERENT_BUF(stat
))
186 dma_sync_sg_for_device(stat
->isp
->dev
, buf
->iovm
->sgt
->sgl
,
187 buf
->iovm
->sgt
->nents
, DMA_FROM_DEVICE
);
190 static void isp_stat_buf_sync_for_cpu(struct ispstat
*stat
,
191 struct ispstat_buffer
*buf
)
193 if (IS_COHERENT_BUF(stat
))
196 dma_sync_sg_for_cpu(stat
->isp
->dev
, buf
->iovm
->sgt
->sgl
,
197 buf
->iovm
->sgt
->nents
, DMA_FROM_DEVICE
);
200 static void isp_stat_buf_clear(struct ispstat
*stat
)
204 for (i
= 0; i
< STAT_MAX_BUFS
; i
++)
205 stat
->buf
[i
].empty
= 1;
208 static struct ispstat_buffer
*
209 __isp_stat_buf_find(struct ispstat
*stat
, int look_empty
)
211 struct ispstat_buffer
*found
= NULL
;
214 for (i
= 0; i
< STAT_MAX_BUFS
; i
++) {
215 struct ispstat_buffer
*curr
= &stat
->buf
[i
];
218 * Don't select the buffer which is being copied to
219 * userspace or used by the module.
221 if (curr
== stat
->locked_buf
|| curr
== stat
->active_buf
)
224 /* Don't select uninitialised buffers if it's not required */
225 if (!look_empty
&& curr
->empty
)
228 /* Pick uninitialised buffer over anything else if look_empty */
234 /* Choose the oldest buffer */
236 (s32
)curr
->frame_number
- (s32
)found
->frame_number
< 0)
243 static inline struct ispstat_buffer
*
244 isp_stat_buf_find_oldest(struct ispstat
*stat
)
246 return __isp_stat_buf_find(stat
, 0);
249 static inline struct ispstat_buffer
*
250 isp_stat_buf_find_oldest_or_empty(struct ispstat
*stat
)
252 return __isp_stat_buf_find(stat
, 1);
255 static int isp_stat_buf_queue(struct ispstat
*stat
)
257 if (!stat
->active_buf
)
260 do_gettimeofday(&stat
->active_buf
->ts
);
262 stat
->active_buf
->buf_size
= stat
->buf_size
;
263 if (isp_stat_buf_check_magic(stat
, stat
->active_buf
)) {
264 dev_dbg(stat
->isp
->dev
, "%s: data wasn't properly written.\n",
268 stat
->active_buf
->config_counter
= stat
->config_counter
;
269 stat
->active_buf
->frame_number
= stat
->frame_number
;
270 stat
->active_buf
->empty
= 0;
271 stat
->active_buf
= NULL
;
273 return STAT_BUF_DONE
;
276 /* Get next free buffer to write the statistics to and mark it active. */
277 static void isp_stat_buf_next(struct ispstat
*stat
)
279 if (unlikely(stat
->active_buf
))
280 /* Overwriting unused active buffer */
281 dev_dbg(stat
->isp
->dev
, "%s: new buffer requested without "
282 "queuing active one.\n",
285 stat
->active_buf
= isp_stat_buf_find_oldest_or_empty(stat
);
288 static void isp_stat_buf_release(struct ispstat
*stat
)
292 isp_stat_buf_sync_for_device(stat
, stat
->locked_buf
);
293 spin_lock_irqsave(&stat
->isp
->stat_lock
, flags
);
294 stat
->locked_buf
= NULL
;
295 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
298 /* Get buffer to userspace. */
299 static struct ispstat_buffer
*isp_stat_buf_get(struct ispstat
*stat
,
300 struct omap3isp_stat_data
*data
)
304 struct ispstat_buffer
*buf
;
306 spin_lock_irqsave(&stat
->isp
->stat_lock
, flags
);
309 buf
= isp_stat_buf_find_oldest(stat
);
311 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
312 dev_dbg(stat
->isp
->dev
, "%s: cannot find a buffer.\n",
314 return ERR_PTR(-EBUSY
);
316 if (isp_stat_buf_check_magic(stat
, buf
)) {
317 dev_dbg(stat
->isp
->dev
, "%s: current buffer has "
318 "corrupted data\n.", stat
->subdev
.name
);
319 /* Mark empty because it doesn't have valid data. */
322 /* Buffer isn't corrupted. */
327 stat
->locked_buf
= buf
;
329 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
331 if (buf
->buf_size
> data
->buf_size
) {
332 dev_warn(stat
->isp
->dev
, "%s: userspace's buffer size is "
333 "not enough.\n", stat
->subdev
.name
);
334 isp_stat_buf_release(stat
);
335 return ERR_PTR(-EINVAL
);
338 isp_stat_buf_sync_for_cpu(stat
, buf
);
340 rval
= copy_to_user(data
->buf
,
345 dev_info(stat
->isp
->dev
,
346 "%s: failed copying %d bytes of stat data\n",
347 stat
->subdev
.name
, rval
);
348 buf
= ERR_PTR(-EFAULT
);
349 isp_stat_buf_release(stat
);
355 static void isp_stat_bufs_free(struct ispstat
*stat
)
357 struct isp_device
*isp
= stat
->isp
;
360 for (i
= 0; i
< STAT_MAX_BUFS
; i
++) {
361 struct ispstat_buffer
*buf
= &stat
->buf
[i
];
363 if (!IS_COHERENT_BUF(stat
)) {
364 if (IS_ERR_OR_NULL((void *)buf
->iommu_addr
))
367 dma_unmap_sg(isp
->dev
, buf
->iovm
->sgt
->sgl
,
368 buf
->iovm
->sgt
->nents
,
370 omap_iommu_vfree(isp
->domain
, isp
->dev
,
375 dma_free_coherent(stat
->isp
->dev
, stat
->buf_alloc_size
,
376 buf
->virt_addr
, buf
->dma_addr
);
381 buf
->virt_addr
= NULL
;
385 dev_dbg(stat
->isp
->dev
, "%s: all buffers were freed.\n",
388 stat
->buf_alloc_size
= 0;
389 stat
->active_buf
= NULL
;
392 static int isp_stat_bufs_alloc_iommu(struct ispstat
*stat
, unsigned int size
)
394 struct isp_device
*isp
= stat
->isp
;
397 stat
->buf_alloc_size
= size
;
399 for (i
= 0; i
< STAT_MAX_BUFS
; i
++) {
400 struct ispstat_buffer
*buf
= &stat
->buf
[i
];
401 struct iovm_struct
*iovm
;
403 WARN_ON(buf
->dma_addr
);
404 buf
->iommu_addr
= omap_iommu_vmalloc(isp
->domain
, isp
->dev
, 0,
406 if (IS_ERR((void *)buf
->iommu_addr
)) {
407 dev_err(stat
->isp
->dev
,
408 "%s: Can't acquire memory for "
409 "buffer %d\n", stat
->subdev
.name
, i
);
410 isp_stat_bufs_free(stat
);
414 iovm
= omap_find_iovm_area(isp
->dev
, buf
->iommu_addr
);
416 !dma_map_sg(isp
->dev
, iovm
->sgt
->sgl
, iovm
->sgt
->nents
,
418 isp_stat_bufs_free(stat
);
423 buf
->virt_addr
= omap_da_to_va(stat
->isp
->dev
,
424 (u32
)buf
->iommu_addr
);
426 dev_dbg(stat
->isp
->dev
, "%s: buffer[%d] allocated."
427 "iommu_addr=0x%08lx virt_addr=0x%08lx",
428 stat
->subdev
.name
, i
, buf
->iommu_addr
,
429 (unsigned long)buf
->virt_addr
);
435 static int isp_stat_bufs_alloc_dma(struct ispstat
*stat
, unsigned int size
)
439 stat
->buf_alloc_size
= size
;
441 for (i
= 0; i
< STAT_MAX_BUFS
; i
++) {
442 struct ispstat_buffer
*buf
= &stat
->buf
[i
];
444 WARN_ON(buf
->iommu_addr
);
445 buf
->virt_addr
= dma_alloc_coherent(stat
->isp
->dev
, size
,
446 &buf
->dma_addr
, GFP_KERNEL
| GFP_DMA
);
448 if (!buf
->virt_addr
|| !buf
->dma_addr
) {
449 dev_info(stat
->isp
->dev
,
450 "%s: Can't acquire memory for "
451 "DMA buffer %d\n", stat
->subdev
.name
, i
);
452 isp_stat_bufs_free(stat
);
457 dev_dbg(stat
->isp
->dev
, "%s: buffer[%d] allocated."
458 "dma_addr=0x%08lx virt_addr=0x%08lx\n",
459 stat
->subdev
.name
, i
, (unsigned long)buf
->dma_addr
,
460 (unsigned long)buf
->virt_addr
);
466 static int isp_stat_bufs_alloc(struct ispstat
*stat
, u32 size
)
470 spin_lock_irqsave(&stat
->isp
->stat_lock
, flags
);
472 BUG_ON(stat
->locked_buf
!= NULL
);
474 /* Are the old buffers big enough? */
475 if (stat
->buf_alloc_size
>= size
) {
476 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
480 if (stat
->state
!= ISPSTAT_DISABLED
|| stat
->buf_processing
) {
481 dev_info(stat
->isp
->dev
,
482 "%s: trying to allocate memory when busy\n",
484 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
488 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
490 isp_stat_bufs_free(stat
);
492 if (IS_COHERENT_BUF(stat
))
493 return isp_stat_bufs_alloc_dma(stat
, size
);
495 return isp_stat_bufs_alloc_iommu(stat
, size
);
498 static void isp_stat_queue_event(struct ispstat
*stat
, int err
)
500 struct video_device
*vdev
= stat
->subdev
.devnode
;
501 struct v4l2_event event
;
502 struct omap3isp_stat_event_status
*status
= (void *)event
.u
.data
;
504 memset(&event
, 0, sizeof(event
));
506 status
->frame_number
= stat
->frame_number
;
507 status
->config_counter
= stat
->config_counter
;
511 event
.type
= stat
->event_type
;
512 v4l2_event_queue(vdev
, &event
);
517 * omap3isp_stat_request_statistics - Request statistics.
518 * @data: Pointer to return statistics data.
520 * Returns 0 if successful.
522 int omap3isp_stat_request_statistics(struct ispstat
*stat
,
523 struct omap3isp_stat_data
*data
)
525 struct ispstat_buffer
*buf
;
527 if (stat
->state
!= ISPSTAT_ENABLED
) {
528 dev_dbg(stat
->isp
->dev
, "%s: engine not enabled.\n",
533 mutex_lock(&stat
->ioctl_lock
);
534 buf
= isp_stat_buf_get(stat
, data
);
536 mutex_unlock(&stat
->ioctl_lock
);
541 data
->config_counter
= buf
->config_counter
;
542 data
->frame_number
= buf
->frame_number
;
543 data
->buf_size
= buf
->buf_size
;
546 isp_stat_buf_release(stat
);
547 mutex_unlock(&stat
->ioctl_lock
);
553 * omap3isp_stat_config - Receives new statistic engine configuration.
554 * @new_conf: Pointer to config structure.
556 * Returns 0 if successful, -EINVAL if new_conf pointer is NULL, -ENOMEM if
557 * was unable to allocate memory for the buffer, or other errors if parameters
560 int omap3isp_stat_config(struct ispstat
*stat
, void *new_conf
)
563 unsigned long irqflags
;
564 struct ispstat_generic_config
*user_cfg
= new_conf
;
565 u32 buf_size
= user_cfg
->buf_size
;
568 dev_dbg(stat
->isp
->dev
, "%s: configuration is NULL\n",
573 mutex_lock(&stat
->ioctl_lock
);
575 dev_dbg(stat
->isp
->dev
, "%s: configuring module with buffer "
576 "size=0x%08lx\n", stat
->subdev
.name
, (unsigned long)buf_size
);
578 ret
= stat
->ops
->validate_params(stat
, new_conf
);
580 mutex_unlock(&stat
->ioctl_lock
);
581 dev_dbg(stat
->isp
->dev
, "%s: configuration values are "
582 "invalid.\n", stat
->subdev
.name
);
586 if (buf_size
!= user_cfg
->buf_size
)
587 dev_dbg(stat
->isp
->dev
, "%s: driver has corrected buffer size "
588 "request to 0x%08lx\n", stat
->subdev
.name
,
589 (unsigned long)user_cfg
->buf_size
);
592 * Hack: H3A modules may need a doubled buffer size to avoid access
593 * to a invalid memory address after a SBL overflow.
594 * The buffer size is always PAGE_ALIGNED.
595 * Hack 2: MAGIC_SIZE is added to buf_size so a magic word can be
596 * inserted at the end to data integrity check purpose.
597 * Hack 3: AF module writes one paxel data more than it should, so
598 * the buffer allocation must consider it to avoid invalid memory
600 * Hack 4: H3A need to allocate extra space for the recover state.
603 buf_size
= user_cfg
->buf_size
* 2 + MAGIC_SIZE
;
606 * Adding one extra paxel data size for each recover
607 * buffer + 2 regular ones.
609 buf_size
+= AF_EXTRA_DATA
* (NUM_H3A_RECOVER_BUFS
+ 2);
610 if (stat
->recover_priv
) {
611 struct ispstat_generic_config
*recover_cfg
=
613 buf_size
+= recover_cfg
->buf_size
*
614 NUM_H3A_RECOVER_BUFS
;
616 buf_size
= PAGE_ALIGN(buf_size
);
617 } else { /* Histogram */
618 buf_size
= PAGE_ALIGN(user_cfg
->buf_size
+ MAGIC_SIZE
);
621 ret
= isp_stat_bufs_alloc(stat
, buf_size
);
623 mutex_unlock(&stat
->ioctl_lock
);
627 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
628 stat
->ops
->set_params(stat
, new_conf
);
629 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
632 * Returning the right future config_counter for this setup, so
633 * userspace can *know* when it has been applied.
635 user_cfg
->config_counter
= stat
->config_counter
+ stat
->inc_config
;
637 /* Module has a valid configuration. */
638 stat
->configured
= 1;
639 dev_dbg(stat
->isp
->dev
, "%s: module has been successfully "
640 "configured.\n", stat
->subdev
.name
);
642 mutex_unlock(&stat
->ioctl_lock
);
648 * isp_stat_buf_process - Process statistic buffers.
649 * @buf_state: points out if buffer is ready to be processed. It's necessary
650 * because histogram needs to copy the data from internal memory
651 * before be able to process the buffer.
653 static int isp_stat_buf_process(struct ispstat
*stat
, int buf_state
)
655 int ret
= STAT_NO_BUF
;
657 if (!atomic_add_unless(&stat
->buf_err
, -1, 0) &&
658 buf_state
== STAT_BUF_DONE
&& stat
->state
== ISPSTAT_ENABLED
) {
659 ret
= isp_stat_buf_queue(stat
);
660 isp_stat_buf_next(stat
);
666 int omap3isp_stat_pcr_busy(struct ispstat
*stat
)
668 return stat
->ops
->busy(stat
);
671 int omap3isp_stat_busy(struct ispstat
*stat
)
673 return omap3isp_stat_pcr_busy(stat
) | stat
->buf_processing
|
674 (stat
->state
!= ISPSTAT_DISABLED
);
678 * isp_stat_pcr_enable - Disables/Enables statistic engines.
679 * @pcr_enable: 0/1 - Disables/Enables the engine.
681 * Must be called from ISP driver when the module is idle and synchronized
684 static void isp_stat_pcr_enable(struct ispstat
*stat
, u8 pcr_enable
)
686 if ((stat
->state
!= ISPSTAT_ENABLING
&&
687 stat
->state
!= ISPSTAT_ENABLED
) && pcr_enable
)
688 /* Userspace has disabled the module. Aborting. */
691 stat
->ops
->enable(stat
, pcr_enable
);
692 if (stat
->state
== ISPSTAT_DISABLING
&& !pcr_enable
)
693 stat
->state
= ISPSTAT_DISABLED
;
694 else if (stat
->state
== ISPSTAT_ENABLING
&& pcr_enable
)
695 stat
->state
= ISPSTAT_ENABLED
;
698 void omap3isp_stat_suspend(struct ispstat
*stat
)
702 spin_lock_irqsave(&stat
->isp
->stat_lock
, flags
);
704 if (stat
->state
!= ISPSTAT_DISABLED
)
705 stat
->ops
->enable(stat
, 0);
706 if (stat
->state
== ISPSTAT_ENABLED
)
707 stat
->state
= ISPSTAT_SUSPENDED
;
709 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
712 void omap3isp_stat_resume(struct ispstat
*stat
)
714 /* Module will be re-enabled with its pipeline */
715 if (stat
->state
== ISPSTAT_SUSPENDED
)
716 stat
->state
= ISPSTAT_ENABLING
;
719 static void isp_stat_try_enable(struct ispstat
*stat
)
721 unsigned long irqflags
;
723 if (stat
->priv
== NULL
)
724 /* driver wasn't initialised */
727 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
728 if (stat
->state
== ISPSTAT_ENABLING
&& !stat
->buf_processing
&&
729 stat
->buf_alloc_size
) {
731 * Userspace's requested to enable the engine but it wasn't yet.
735 isp_stat_buf_next(stat
);
736 stat
->ops
->setup_regs(stat
, stat
->priv
);
737 isp_stat_buf_insert_magic(stat
, stat
->active_buf
);
740 * H3A module has some hw issues which forces the driver to
741 * ignore next buffers even if it was disabled in the meantime.
742 * On the other hand, Histogram shouldn't ignore buffers anymore
743 * if it's being enabled.
746 atomic_set(&stat
->buf_err
, 0);
748 isp_stat_pcr_enable(stat
, 1);
749 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
750 dev_dbg(stat
->isp
->dev
, "%s: module is enabled.\n",
753 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
757 void omap3isp_stat_isr_frame_sync(struct ispstat
*stat
)
759 isp_stat_try_enable(stat
);
762 void omap3isp_stat_sbl_overflow(struct ispstat
*stat
)
764 unsigned long irqflags
;
766 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
768 * Due to a H3A hw issue which prevents the next buffer to start from
769 * the correct memory address, 2 buffers must be ignored.
771 atomic_set(&stat
->buf_err
, 2);
774 * If more than one SBL overflow happen in a row, H3A module may access
775 * invalid memory region.
776 * stat->sbl_ovl_recover is set to tell to the driver to temporarily use
777 * a soft configuration which helps to avoid consecutive overflows.
779 if (stat
->recover_priv
)
780 stat
->sbl_ovl_recover
= 1;
781 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
785 * omap3isp_stat_enable - Disable/Enable statistic engine as soon as possible
786 * @enable: 0/1 - Disables/Enables the engine.
788 * Client should configure all the module registers before this.
789 * This function can be called from a userspace request.
791 int omap3isp_stat_enable(struct ispstat
*stat
, u8 enable
)
793 unsigned long irqflags
;
795 dev_dbg(stat
->isp
->dev
, "%s: user wants to %s module.\n",
796 stat
->subdev
.name
, enable
? "enable" : "disable");
798 /* Prevent enabling while configuring */
799 mutex_lock(&stat
->ioctl_lock
);
801 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
803 if (!stat
->configured
&& enable
) {
804 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
805 mutex_unlock(&stat
->ioctl_lock
);
806 dev_dbg(stat
->isp
->dev
, "%s: cannot enable module as it's "
807 "never been successfully configured so far.\n",
813 if (stat
->state
== ISPSTAT_DISABLING
)
814 /* Previous disabling request wasn't done yet */
815 stat
->state
= ISPSTAT_ENABLED
;
816 else if (stat
->state
== ISPSTAT_DISABLED
)
817 /* Module is now being enabled */
818 stat
->state
= ISPSTAT_ENABLING
;
820 if (stat
->state
== ISPSTAT_ENABLING
) {
821 /* Previous enabling request wasn't done yet */
822 stat
->state
= ISPSTAT_DISABLED
;
823 } else if (stat
->state
== ISPSTAT_ENABLED
) {
824 /* Module is now being disabled */
825 stat
->state
= ISPSTAT_DISABLING
;
826 isp_stat_buf_clear(stat
);
830 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
831 mutex_unlock(&stat
->ioctl_lock
);
836 int omap3isp_stat_s_stream(struct v4l2_subdev
*subdev
, int enable
)
838 struct ispstat
*stat
= v4l2_get_subdevdata(subdev
);
842 * Only set enable PCR bit if the module was previously
843 * enabled through ioct.
845 isp_stat_try_enable(stat
);
848 /* Disable PCR bit and config enable field */
849 omap3isp_stat_enable(stat
, 0);
850 spin_lock_irqsave(&stat
->isp
->stat_lock
, flags
);
851 stat
->ops
->enable(stat
, 0);
852 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
855 * If module isn't busy, a new interrupt may come or not to
856 * set the state to DISABLED. As Histogram needs to read its
857 * internal memory to clear it, let interrupt handler
858 * responsible of changing state to DISABLED. If the last
859 * interrupt is coming, it's still safe as the handler will
860 * ignore the second time when state is already set to DISABLED.
861 * It's necessary to synchronize Histogram with streamoff, once
862 * the module may be considered idle before last SDMA transfer
863 * starts if we return here.
865 if (!omap3isp_stat_pcr_busy(stat
))
866 omap3isp_stat_isr(stat
);
868 dev_dbg(stat
->isp
->dev
, "%s: module is being disabled\n",
876 * __stat_isr - Interrupt handler for statistic drivers
878 static void __stat_isr(struct ispstat
*stat
, int from_dma
)
880 int ret
= STAT_BUF_DONE
;
882 unsigned long irqflags
;
883 struct isp_pipeline
*pipe
;
886 * stat->buf_processing must be set before disable module. It's
887 * necessary to not inform too early the buffers aren't busy in case
888 * of SDMA is going to be used.
890 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
891 if (stat
->state
== ISPSTAT_DISABLED
) {
892 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
895 buf_processing
= stat
->buf_processing
;
896 stat
->buf_processing
= 1;
897 stat
->ops
->enable(stat
, 0);
899 if (buf_processing
&& !from_dma
) {
900 if (stat
->state
== ISPSTAT_ENABLED
) {
901 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
902 dev_err(stat
->isp
->dev
,
903 "%s: interrupt occurred when module was still "
904 "processing a buffer.\n", stat
->subdev
.name
);
909 * Interrupt handler was called from streamoff when
910 * the module wasn't busy anymore to ensure it is being
911 * disabled after process last buffer. If such buffer
912 * processing has already started, no need to do
915 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
919 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
921 /* If it's busy we can't process this buffer anymore */
922 if (!omap3isp_stat_pcr_busy(stat
)) {
923 if (!from_dma
&& stat
->ops
->buf_process
)
924 /* Module still need to copy data to buffer. */
925 ret
= stat
->ops
->buf_process(stat
);
926 if (ret
== STAT_BUF_WAITING_DMA
)
927 /* Buffer is not ready yet */
930 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
933 * Histogram needs to read its internal memory to clear it
934 * before be disabled. For that reason, common statistic layer
935 * can return only after call stat's buf_process() operator.
937 if (stat
->state
== ISPSTAT_DISABLING
) {
938 stat
->state
= ISPSTAT_DISABLED
;
939 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
940 stat
->buf_processing
= 0;
943 pipe
= to_isp_pipeline(&stat
->subdev
.entity
);
944 stat
->frame_number
= atomic_read(&pipe
->frame_number
);
947 * Before this point, 'ret' stores the buffer's status if it's
948 * ready to be processed. Afterwards, it holds the status if
949 * it was processed successfully.
951 ret
= isp_stat_buf_process(stat
, ret
);
953 if (likely(!stat
->sbl_ovl_recover
)) {
954 stat
->ops
->setup_regs(stat
, stat
->priv
);
957 * Using recover config to increase the chance to have
958 * a good buffer processing and make the H3A module to
959 * go back to a valid state.
962 stat
->ops
->setup_regs(stat
, stat
->recover_priv
);
963 stat
->sbl_ovl_recover
= 0;
966 * Set 'update' in case of the module needs to use
967 * regular configuration after next buffer.
972 isp_stat_buf_insert_magic(stat
, stat
->active_buf
);
975 * Hack: H3A modules may access invalid memory address or send
976 * corrupted data to userspace if more than 1 SBL overflow
977 * happens in a row without re-writing its buffer's start memory
978 * address in the meantime. Such situation is avoided if the
979 * module is not immediately re-enabled when the ISR misses the
980 * timing to process the buffer and to setup the registers.
981 * Because of that, pcr_enable(1) was moved to inside this 'if'
982 * block. But the next interruption will still happen as during
983 * pcr_enable(0) the module was busy.
985 isp_stat_pcr_enable(stat
, 1);
986 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
989 * If a SBL overflow occurs and the H3A driver misses the timing
990 * to process the buffer, stat->buf_err is set and won't be
991 * cleared now. So the next buffer will be correctly ignored.
992 * It's necessary due to a hw issue which makes the next H3A
993 * buffer to start from the memory address where the previous
994 * one stopped, instead of start where it was configured to.
995 * Do not "stat->buf_err = 0" here.
998 if (stat
->ops
->buf_process
)
1000 * Driver may need to erase current data prior to
1001 * process a new buffer. If it misses the timing, the
1002 * next buffer might be wrong. So should be ignored.
1003 * It happens only for Histogram.
1005 atomic_set(&stat
->buf_err
, 1);
1008 dev_dbg(stat
->isp
->dev
, "%s: cannot process buffer, "
1009 "device is busy.\n", stat
->subdev
.name
);
1013 stat
->buf_processing
= 0;
1014 isp_stat_queue_event(stat
, ret
!= STAT_BUF_DONE
);
1017 void omap3isp_stat_isr(struct ispstat
*stat
)
1019 __stat_isr(stat
, 0);
1022 void omap3isp_stat_dma_isr(struct ispstat
*stat
)
1024 __stat_isr(stat
, 1);
1027 int omap3isp_stat_subscribe_event(struct v4l2_subdev
*subdev
,
1029 struct v4l2_event_subscription
*sub
)
1031 struct ispstat
*stat
= v4l2_get_subdevdata(subdev
);
1033 if (sub
->type
!= stat
->event_type
)
1036 return v4l2_event_subscribe(fh
, sub
, STAT_NEVENTS
, NULL
);
1039 int omap3isp_stat_unsubscribe_event(struct v4l2_subdev
*subdev
,
1041 struct v4l2_event_subscription
*sub
)
1043 return v4l2_event_unsubscribe(fh
, sub
);
1046 void omap3isp_stat_unregister_entities(struct ispstat
*stat
)
1048 v4l2_device_unregister_subdev(&stat
->subdev
);
1051 int omap3isp_stat_register_entities(struct ispstat
*stat
,
1052 struct v4l2_device
*vdev
)
1054 return v4l2_device_register_subdev(vdev
, &stat
->subdev
);
1057 static int isp_stat_init_entities(struct ispstat
*stat
, const char *name
,
1058 const struct v4l2_subdev_ops
*sd_ops
)
1060 struct v4l2_subdev
*subdev
= &stat
->subdev
;
1061 struct media_entity
*me
= &subdev
->entity
;
1063 v4l2_subdev_init(subdev
, sd_ops
);
1064 snprintf(subdev
->name
, V4L2_SUBDEV_NAME_SIZE
, "OMAP3 ISP %s", name
);
1065 subdev
->grp_id
= 1 << 16; /* group ID for isp subdevs */
1066 subdev
->flags
|= V4L2_SUBDEV_FL_HAS_EVENTS
| V4L2_SUBDEV_FL_HAS_DEVNODE
;
1067 v4l2_set_subdevdata(subdev
, stat
);
1069 stat
->pad
.flags
= MEDIA_PAD_FL_SINK
;
1072 return media_entity_init(me
, 1, &stat
->pad
, 0);
1075 int omap3isp_stat_init(struct ispstat
*stat
, const char *name
,
1076 const struct v4l2_subdev_ops
*sd_ops
)
1080 stat
->buf
= kcalloc(STAT_MAX_BUFS
, sizeof(*stat
->buf
), GFP_KERNEL
);
1084 isp_stat_buf_clear(stat
);
1085 mutex_init(&stat
->ioctl_lock
);
1086 atomic_set(&stat
->buf_err
, 0);
1088 ret
= isp_stat_init_entities(stat
, name
, sd_ops
);
1090 mutex_destroy(&stat
->ioctl_lock
);
1097 void omap3isp_stat_cleanup(struct ispstat
*stat
)
1099 media_entity_cleanup(&stat
->subdev
.entity
);
1100 mutex_destroy(&stat
->ioctl_lock
);
1101 isp_stat_bufs_free(stat
);