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 ktime_get_ts(&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
);
540 data
->ts
.tv_sec
= buf
->ts
.tv_sec
;
541 data
->ts
.tv_usec
= buf
->ts
.tv_nsec
/ NSEC_PER_USEC
;
542 data
->config_counter
= buf
->config_counter
;
543 data
->frame_number
= buf
->frame_number
;
544 data
->buf_size
= buf
->buf_size
;
547 isp_stat_buf_release(stat
);
548 mutex_unlock(&stat
->ioctl_lock
);
554 * omap3isp_stat_config - Receives new statistic engine configuration.
555 * @new_conf: Pointer to config structure.
557 * Returns 0 if successful, -EINVAL if new_conf pointer is NULL, -ENOMEM if
558 * was unable to allocate memory for the buffer, or other errors if parameters
561 int omap3isp_stat_config(struct ispstat
*stat
, void *new_conf
)
564 unsigned long irqflags
;
565 struct ispstat_generic_config
*user_cfg
= new_conf
;
566 u32 buf_size
= user_cfg
->buf_size
;
569 dev_dbg(stat
->isp
->dev
, "%s: configuration is NULL\n",
574 mutex_lock(&stat
->ioctl_lock
);
576 dev_dbg(stat
->isp
->dev
, "%s: configuring module with buffer "
577 "size=0x%08lx\n", stat
->subdev
.name
, (unsigned long)buf_size
);
579 ret
= stat
->ops
->validate_params(stat
, new_conf
);
581 mutex_unlock(&stat
->ioctl_lock
);
582 dev_dbg(stat
->isp
->dev
, "%s: configuration values are "
583 "invalid.\n", stat
->subdev
.name
);
587 if (buf_size
!= user_cfg
->buf_size
)
588 dev_dbg(stat
->isp
->dev
, "%s: driver has corrected buffer size "
589 "request to 0x%08lx\n", stat
->subdev
.name
,
590 (unsigned long)user_cfg
->buf_size
);
593 * Hack: H3A modules may need a doubled buffer size to avoid access
594 * to a invalid memory address after a SBL overflow.
595 * The buffer size is always PAGE_ALIGNED.
596 * Hack 2: MAGIC_SIZE is added to buf_size so a magic word can be
597 * inserted at the end to data integrity check purpose.
598 * Hack 3: AF module writes one paxel data more than it should, so
599 * the buffer allocation must consider it to avoid invalid memory
601 * Hack 4: H3A need to allocate extra space for the recover state.
604 buf_size
= user_cfg
->buf_size
* 2 + MAGIC_SIZE
;
607 * Adding one extra paxel data size for each recover
608 * buffer + 2 regular ones.
610 buf_size
+= AF_EXTRA_DATA
* (NUM_H3A_RECOVER_BUFS
+ 2);
611 if (stat
->recover_priv
) {
612 struct ispstat_generic_config
*recover_cfg
=
614 buf_size
+= recover_cfg
->buf_size
*
615 NUM_H3A_RECOVER_BUFS
;
617 buf_size
= PAGE_ALIGN(buf_size
);
618 } else { /* Histogram */
619 buf_size
= PAGE_ALIGN(user_cfg
->buf_size
+ MAGIC_SIZE
);
622 ret
= isp_stat_bufs_alloc(stat
, buf_size
);
624 mutex_unlock(&stat
->ioctl_lock
);
628 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
629 stat
->ops
->set_params(stat
, new_conf
);
630 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
633 * Returning the right future config_counter for this setup, so
634 * userspace can *know* when it has been applied.
636 user_cfg
->config_counter
= stat
->config_counter
+ stat
->inc_config
;
638 /* Module has a valid configuration. */
639 stat
->configured
= 1;
640 dev_dbg(stat
->isp
->dev
, "%s: module has been successfully "
641 "configured.\n", stat
->subdev
.name
);
643 mutex_unlock(&stat
->ioctl_lock
);
649 * isp_stat_buf_process - Process statistic buffers.
650 * @buf_state: points out if buffer is ready to be processed. It's necessary
651 * because histogram needs to copy the data from internal memory
652 * before be able to process the buffer.
654 static int isp_stat_buf_process(struct ispstat
*stat
, int buf_state
)
656 int ret
= STAT_NO_BUF
;
658 if (!atomic_add_unless(&stat
->buf_err
, -1, 0) &&
659 buf_state
== STAT_BUF_DONE
&& stat
->state
== ISPSTAT_ENABLED
) {
660 ret
= isp_stat_buf_queue(stat
);
661 isp_stat_buf_next(stat
);
667 int omap3isp_stat_pcr_busy(struct ispstat
*stat
)
669 return stat
->ops
->busy(stat
);
672 int omap3isp_stat_busy(struct ispstat
*stat
)
674 return omap3isp_stat_pcr_busy(stat
) | stat
->buf_processing
|
675 (stat
->state
!= ISPSTAT_DISABLED
);
679 * isp_stat_pcr_enable - Disables/Enables statistic engines.
680 * @pcr_enable: 0/1 - Disables/Enables the engine.
682 * Must be called from ISP driver when the module is idle and synchronized
685 static void isp_stat_pcr_enable(struct ispstat
*stat
, u8 pcr_enable
)
687 if ((stat
->state
!= ISPSTAT_ENABLING
&&
688 stat
->state
!= ISPSTAT_ENABLED
) && pcr_enable
)
689 /* Userspace has disabled the module. Aborting. */
692 stat
->ops
->enable(stat
, pcr_enable
);
693 if (stat
->state
== ISPSTAT_DISABLING
&& !pcr_enable
)
694 stat
->state
= ISPSTAT_DISABLED
;
695 else if (stat
->state
== ISPSTAT_ENABLING
&& pcr_enable
)
696 stat
->state
= ISPSTAT_ENABLED
;
699 void omap3isp_stat_suspend(struct ispstat
*stat
)
703 spin_lock_irqsave(&stat
->isp
->stat_lock
, flags
);
705 if (stat
->state
!= ISPSTAT_DISABLED
)
706 stat
->ops
->enable(stat
, 0);
707 if (stat
->state
== ISPSTAT_ENABLED
)
708 stat
->state
= ISPSTAT_SUSPENDED
;
710 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
713 void omap3isp_stat_resume(struct ispstat
*stat
)
715 /* Module will be re-enabled with its pipeline */
716 if (stat
->state
== ISPSTAT_SUSPENDED
)
717 stat
->state
= ISPSTAT_ENABLING
;
720 static void isp_stat_try_enable(struct ispstat
*stat
)
722 unsigned long irqflags
;
724 if (stat
->priv
== NULL
)
725 /* driver wasn't initialised */
728 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
729 if (stat
->state
== ISPSTAT_ENABLING
&& !stat
->buf_processing
&&
730 stat
->buf_alloc_size
) {
732 * Userspace's requested to enable the engine but it wasn't yet.
736 isp_stat_buf_next(stat
);
737 stat
->ops
->setup_regs(stat
, stat
->priv
);
738 isp_stat_buf_insert_magic(stat
, stat
->active_buf
);
741 * H3A module has some hw issues which forces the driver to
742 * ignore next buffers even if it was disabled in the meantime.
743 * On the other hand, Histogram shouldn't ignore buffers anymore
744 * if it's being enabled.
747 atomic_set(&stat
->buf_err
, 0);
749 isp_stat_pcr_enable(stat
, 1);
750 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
751 dev_dbg(stat
->isp
->dev
, "%s: module is enabled.\n",
754 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
758 void omap3isp_stat_isr_frame_sync(struct ispstat
*stat
)
760 isp_stat_try_enable(stat
);
763 void omap3isp_stat_sbl_overflow(struct ispstat
*stat
)
765 unsigned long irqflags
;
767 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
769 * Due to a H3A hw issue which prevents the next buffer to start from
770 * the correct memory address, 2 buffers must be ignored.
772 atomic_set(&stat
->buf_err
, 2);
775 * If more than one SBL overflow happen in a row, H3A module may access
776 * invalid memory region.
777 * stat->sbl_ovl_recover is set to tell to the driver to temporarily use
778 * a soft configuration which helps to avoid consecutive overflows.
780 if (stat
->recover_priv
)
781 stat
->sbl_ovl_recover
= 1;
782 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
786 * omap3isp_stat_enable - Disable/Enable statistic engine as soon as possible
787 * @enable: 0/1 - Disables/Enables the engine.
789 * Client should configure all the module registers before this.
790 * This function can be called from a userspace request.
792 int omap3isp_stat_enable(struct ispstat
*stat
, u8 enable
)
794 unsigned long irqflags
;
796 dev_dbg(stat
->isp
->dev
, "%s: user wants to %s module.\n",
797 stat
->subdev
.name
, enable
? "enable" : "disable");
799 /* Prevent enabling while configuring */
800 mutex_lock(&stat
->ioctl_lock
);
802 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
804 if (!stat
->configured
&& enable
) {
805 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
806 mutex_unlock(&stat
->ioctl_lock
);
807 dev_dbg(stat
->isp
->dev
, "%s: cannot enable module as it's "
808 "never been successfully configured so far.\n",
814 if (stat
->state
== ISPSTAT_DISABLING
)
815 /* Previous disabling request wasn't done yet */
816 stat
->state
= ISPSTAT_ENABLED
;
817 else if (stat
->state
== ISPSTAT_DISABLED
)
818 /* Module is now being enabled */
819 stat
->state
= ISPSTAT_ENABLING
;
821 if (stat
->state
== ISPSTAT_ENABLING
) {
822 /* Previous enabling request wasn't done yet */
823 stat
->state
= ISPSTAT_DISABLED
;
824 } else if (stat
->state
== ISPSTAT_ENABLED
) {
825 /* Module is now being disabled */
826 stat
->state
= ISPSTAT_DISABLING
;
827 isp_stat_buf_clear(stat
);
831 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
832 mutex_unlock(&stat
->ioctl_lock
);
837 int omap3isp_stat_s_stream(struct v4l2_subdev
*subdev
, int enable
)
839 struct ispstat
*stat
= v4l2_get_subdevdata(subdev
);
843 * Only set enable PCR bit if the module was previously
844 * enabled through ioct.
846 isp_stat_try_enable(stat
);
849 /* Disable PCR bit and config enable field */
850 omap3isp_stat_enable(stat
, 0);
851 spin_lock_irqsave(&stat
->isp
->stat_lock
, flags
);
852 stat
->ops
->enable(stat
, 0);
853 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, flags
);
856 * If module isn't busy, a new interrupt may come or not to
857 * set the state to DISABLED. As Histogram needs to read its
858 * internal memory to clear it, let interrupt handler
859 * responsible of changing state to DISABLED. If the last
860 * interrupt is coming, it's still safe as the handler will
861 * ignore the second time when state is already set to DISABLED.
862 * It's necessary to synchronize Histogram with streamoff, once
863 * the module may be considered idle before last SDMA transfer
864 * starts if we return here.
866 if (!omap3isp_stat_pcr_busy(stat
))
867 omap3isp_stat_isr(stat
);
869 dev_dbg(stat
->isp
->dev
, "%s: module is being disabled\n",
877 * __stat_isr - Interrupt handler for statistic drivers
879 static void __stat_isr(struct ispstat
*stat
, int from_dma
)
881 int ret
= STAT_BUF_DONE
;
883 unsigned long irqflags
;
884 struct isp_pipeline
*pipe
;
887 * stat->buf_processing must be set before disable module. It's
888 * necessary to not inform too early the buffers aren't busy in case
889 * of SDMA is going to be used.
891 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
892 if (stat
->state
== ISPSTAT_DISABLED
) {
893 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
896 buf_processing
= stat
->buf_processing
;
897 stat
->buf_processing
= 1;
898 stat
->ops
->enable(stat
, 0);
900 if (buf_processing
&& !from_dma
) {
901 if (stat
->state
== ISPSTAT_ENABLED
) {
902 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
903 dev_err(stat
->isp
->dev
,
904 "%s: interrupt occurred when module was still "
905 "processing a buffer.\n", stat
->subdev
.name
);
910 * Interrupt handler was called from streamoff when
911 * the module wasn't busy anymore to ensure it is being
912 * disabled after process last buffer. If such buffer
913 * processing has already started, no need to do
916 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
920 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
922 /* If it's busy we can't process this buffer anymore */
923 if (!omap3isp_stat_pcr_busy(stat
)) {
924 if (!from_dma
&& stat
->ops
->buf_process
)
925 /* Module still need to copy data to buffer. */
926 ret
= stat
->ops
->buf_process(stat
);
927 if (ret
== STAT_BUF_WAITING_DMA
)
928 /* Buffer is not ready yet */
931 spin_lock_irqsave(&stat
->isp
->stat_lock
, irqflags
);
934 * Histogram needs to read its internal memory to clear it
935 * before be disabled. For that reason, common statistic layer
936 * can return only after call stat's buf_process() operator.
938 if (stat
->state
== ISPSTAT_DISABLING
) {
939 stat
->state
= ISPSTAT_DISABLED
;
940 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
941 stat
->buf_processing
= 0;
944 pipe
= to_isp_pipeline(&stat
->subdev
.entity
);
945 stat
->frame_number
= atomic_read(&pipe
->frame_number
);
948 * Before this point, 'ret' stores the buffer's status if it's
949 * ready to be processed. Afterwards, it holds the status if
950 * it was processed successfully.
952 ret
= isp_stat_buf_process(stat
, ret
);
954 if (likely(!stat
->sbl_ovl_recover
)) {
955 stat
->ops
->setup_regs(stat
, stat
->priv
);
958 * Using recover config to increase the chance to have
959 * a good buffer processing and make the H3A module to
960 * go back to a valid state.
963 stat
->ops
->setup_regs(stat
, stat
->recover_priv
);
964 stat
->sbl_ovl_recover
= 0;
967 * Set 'update' in case of the module needs to use
968 * regular configuration after next buffer.
973 isp_stat_buf_insert_magic(stat
, stat
->active_buf
);
976 * Hack: H3A modules may access invalid memory address or send
977 * corrupted data to userspace if more than 1 SBL overflow
978 * happens in a row without re-writing its buffer's start memory
979 * address in the meantime. Such situation is avoided if the
980 * module is not immediately re-enabled when the ISR misses the
981 * timing to process the buffer and to setup the registers.
982 * Because of that, pcr_enable(1) was moved to inside this 'if'
983 * block. But the next interruption will still happen as during
984 * pcr_enable(0) the module was busy.
986 isp_stat_pcr_enable(stat
, 1);
987 spin_unlock_irqrestore(&stat
->isp
->stat_lock
, irqflags
);
990 * If a SBL overflow occurs and the H3A driver misses the timing
991 * to process the buffer, stat->buf_err is set and won't be
992 * cleared now. So the next buffer will be correctly ignored.
993 * It's necessary due to a hw issue which makes the next H3A
994 * buffer to start from the memory address where the previous
995 * one stopped, instead of start where it was configured to.
996 * Do not "stat->buf_err = 0" here.
999 if (stat
->ops
->buf_process
)
1001 * Driver may need to erase current data prior to
1002 * process a new buffer. If it misses the timing, the
1003 * next buffer might be wrong. So should be ignored.
1004 * It happens only for Histogram.
1006 atomic_set(&stat
->buf_err
, 1);
1009 dev_dbg(stat
->isp
->dev
, "%s: cannot process buffer, "
1010 "device is busy.\n", stat
->subdev
.name
);
1014 stat
->buf_processing
= 0;
1015 isp_stat_queue_event(stat
, ret
!= STAT_BUF_DONE
);
1018 void omap3isp_stat_isr(struct ispstat
*stat
)
1020 __stat_isr(stat
, 0);
1023 void omap3isp_stat_dma_isr(struct ispstat
*stat
)
1025 __stat_isr(stat
, 1);
1028 int omap3isp_stat_subscribe_event(struct v4l2_subdev
*subdev
,
1030 struct v4l2_event_subscription
*sub
)
1032 struct ispstat
*stat
= v4l2_get_subdevdata(subdev
);
1034 if (sub
->type
!= stat
->event_type
)
1037 return v4l2_event_subscribe(fh
, sub
, STAT_NEVENTS
, NULL
);
1040 int omap3isp_stat_unsubscribe_event(struct v4l2_subdev
*subdev
,
1042 struct v4l2_event_subscription
*sub
)
1044 return v4l2_event_unsubscribe(fh
, sub
);
1047 void omap3isp_stat_unregister_entities(struct ispstat
*stat
)
1049 v4l2_device_unregister_subdev(&stat
->subdev
);
1052 int omap3isp_stat_register_entities(struct ispstat
*stat
,
1053 struct v4l2_device
*vdev
)
1055 return v4l2_device_register_subdev(vdev
, &stat
->subdev
);
1058 static int isp_stat_init_entities(struct ispstat
*stat
, const char *name
,
1059 const struct v4l2_subdev_ops
*sd_ops
)
1061 struct v4l2_subdev
*subdev
= &stat
->subdev
;
1062 struct media_entity
*me
= &subdev
->entity
;
1064 v4l2_subdev_init(subdev
, sd_ops
);
1065 snprintf(subdev
->name
, V4L2_SUBDEV_NAME_SIZE
, "OMAP3 ISP %s", name
);
1066 subdev
->grp_id
= 1 << 16; /* group ID for isp subdevs */
1067 subdev
->flags
|= V4L2_SUBDEV_FL_HAS_EVENTS
| V4L2_SUBDEV_FL_HAS_DEVNODE
;
1068 v4l2_set_subdevdata(subdev
, stat
);
1070 stat
->pad
.flags
= MEDIA_PAD_FL_SINK
;
1073 return media_entity_init(me
, 1, &stat
->pad
, 0);
1076 int omap3isp_stat_init(struct ispstat
*stat
, const char *name
,
1077 const struct v4l2_subdev_ops
*sd_ops
)
1081 stat
->buf
= kcalloc(STAT_MAX_BUFS
, sizeof(*stat
->buf
), GFP_KERNEL
);
1085 isp_stat_buf_clear(stat
);
1086 mutex_init(&stat
->ioctl_lock
);
1087 atomic_set(&stat
->buf_err
, 0);
1089 ret
= isp_stat_init_entities(stat
, name
, sd_ops
);
1091 mutex_destroy(&stat
->ioctl_lock
);
1098 void omap3isp_stat_cleanup(struct ispstat
*stat
)
1100 media_entity_cleanup(&stat
->subdev
.entity
);
1101 mutex_destroy(&stat
->ioctl_lock
);
1102 isp_stat_bufs_free(stat
);