Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / drivers / media / platform / omap3isp / ispstat.c
blob1b9217d3b1b6af3b01a2979cc9a7cea28c027718
1 /*
2 * ispstat.c
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.
18 #include <linux/dma-mapping.h>
19 #include <linux/slab.h>
20 #include <linux/uaccess.h>
22 #include "isp.h"
24 #define ISP_STAT_USES_DMAENGINE(stat) ((stat)->dma_ch != NULL)
27 * MAGIC_SIZE must always be the greatest common divisor of
28 * AEWB_PACKET_SIZE and AF_PAXEL_SIZE.
30 #define MAGIC_SIZE 16
31 #define MAGIC_NUM 0x55
33 /* HACK: AF module seems to be writing one more paxel data than it should. */
34 #define AF_EXTRA_DATA OMAP3ISP_AF_PAXEL_SIZE
37 * HACK: H3A modules go to an invalid state after have a SBL overflow. It makes
38 * the next buffer to start to be written in the same point where the overflow
39 * occurred instead of the configured address. The only known way to make it to
40 * go back to a valid state is having a valid buffer processing. Of course it
41 * requires at least a doubled buffer size to avoid an access to invalid memory
42 * region. But it does not fix everything. It may happen more than one
43 * consecutive SBL overflows. In that case, it might be unpredictable how many
44 * buffers the allocated memory should fit. For that case, a recover
45 * configuration was created. It produces the minimum buffer size for each H3A
46 * module and decrease the change for more SBL overflows. This recover state
47 * will be enabled every time a SBL overflow occur. As the output buffer size
48 * isn't big, it's possible to have an extra size able to fit many recover
49 * buffers making it extreamily unlikely to have an access to invalid memory
50 * region.
52 #define NUM_H3A_RECOVER_BUFS 10
55 * HACK: Because of HW issues the generic layer sometimes need to have
56 * different behaviour for different statistic modules.
58 #define IS_H3A_AF(stat) ((stat) == &(stat)->isp->isp_af)
59 #define IS_H3A_AEWB(stat) ((stat) == &(stat)->isp->isp_aewb)
60 #define IS_H3A(stat) (IS_H3A_AF(stat) || IS_H3A_AEWB(stat))
62 static void __isp_stat_buf_sync_magic(struct ispstat *stat,
63 struct ispstat_buffer *buf,
64 u32 buf_size, enum dma_data_direction dir,
65 void (*dma_sync)(struct device *,
66 dma_addr_t, unsigned long, size_t,
67 enum dma_data_direction))
69 /* Sync the initial and final magic words. */
70 dma_sync(stat->isp->dev, buf->dma_addr, 0, MAGIC_SIZE, dir);
71 dma_sync(stat->isp->dev, buf->dma_addr + (buf_size & PAGE_MASK),
72 buf_size & ~PAGE_MASK, MAGIC_SIZE, dir);
75 static void isp_stat_buf_sync_magic_for_device(struct ispstat *stat,
76 struct ispstat_buffer *buf,
77 u32 buf_size,
78 enum dma_data_direction dir)
80 if (ISP_STAT_USES_DMAENGINE(stat))
81 return;
83 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
84 dma_sync_single_range_for_device);
87 static void isp_stat_buf_sync_magic_for_cpu(struct ispstat *stat,
88 struct ispstat_buffer *buf,
89 u32 buf_size,
90 enum dma_data_direction dir)
92 if (ISP_STAT_USES_DMAENGINE(stat))
93 return;
95 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
96 dma_sync_single_range_for_cpu);
99 static int isp_stat_buf_check_magic(struct ispstat *stat,
100 struct ispstat_buffer *buf)
102 const u32 buf_size = IS_H3A_AF(stat) ?
103 buf->buf_size + AF_EXTRA_DATA : buf->buf_size;
104 u8 *w;
105 u8 *end;
106 int ret = -EINVAL;
108 isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
110 /* Checking initial magic numbers. They shouldn't be here anymore. */
111 for (w = buf->virt_addr, end = w + MAGIC_SIZE; w < end; w++)
112 if (likely(*w != MAGIC_NUM))
113 ret = 0;
115 if (ret) {
116 dev_dbg(stat->isp->dev, "%s: beginning magic check does not "
117 "match.\n", stat->subdev.name);
118 return ret;
121 /* Checking magic numbers at the end. They must be still here. */
122 for (w = buf->virt_addr + buf_size, end = w + MAGIC_SIZE;
123 w < end; w++) {
124 if (unlikely(*w != MAGIC_NUM)) {
125 dev_dbg(stat->isp->dev, "%s: ending magic check does "
126 "not match.\n", stat->subdev.name);
127 return -EINVAL;
131 isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
132 DMA_FROM_DEVICE);
134 return 0;
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
149 * stat->buf_size.
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,
155 DMA_BIDIRECTIONAL);
158 static void isp_stat_buf_sync_for_device(struct ispstat *stat,
159 struct ispstat_buffer *buf)
161 if (ISP_STAT_USES_DMAENGINE(stat))
162 return;
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))
172 return;
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)
180 int i;
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;
190 int i;
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)
200 continue;
202 /* Don't select uninitialised buffers if it's not required */
203 if (!look_empty && curr->empty)
204 continue;
206 /* Pick uninitialised buffer over anything else if look_empty */
207 if (curr->empty) {
208 found = curr;
209 break;
212 /* Choose the oldest buffer */
213 if (!found ||
214 (s32)curr->frame_number - (s32)found->frame_number < 0)
215 found = curr;
218 return found;
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)
236 return STAT_NO_BUF;
238 v4l2_get_timestamp(&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",
243 stat->subdev.name);
244 return STAT_NO_BUF;
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, "%s: new buffer requested without "
260 "queuing active one.\n",
261 stat->subdev.name);
262 else
263 stat->active_buf = isp_stat_buf_find_oldest_or_empty(stat);
266 static void isp_stat_buf_release(struct ispstat *stat)
268 unsigned long flags;
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)
280 int rval = 0;
281 unsigned long flags;
282 struct ispstat_buffer *buf;
284 spin_lock_irqsave(&stat->isp->stat_lock, flags);
286 while (1) {
287 buf = isp_stat_buf_find_oldest(stat);
288 if (!buf) {
289 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
290 dev_dbg(stat->isp->dev, "%s: cannot find a buffer.\n",
291 stat->subdev.name);
292 return ERR_PTR(-EBUSY);
294 if (isp_stat_buf_check_magic(stat, buf)) {
295 dev_dbg(stat->isp->dev, "%s: current buffer has "
296 "corrupted data\n.", stat->subdev.name);
297 /* Mark empty because it doesn't have valid data. */
298 buf->empty = 1;
299 } else {
300 /* Buffer isn't corrupted. */
301 break;
305 stat->locked_buf = buf;
307 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
309 if (buf->buf_size > data->buf_size) {
310 dev_warn(stat->isp->dev, "%s: userspace's buffer size is "
311 "not enough.\n", stat->subdev.name);
312 isp_stat_buf_release(stat);
313 return ERR_PTR(-EINVAL);
316 isp_stat_buf_sync_for_cpu(stat, buf);
318 rval = copy_to_user(data->buf,
319 buf->virt_addr,
320 buf->buf_size);
322 if (rval) {
323 dev_info(stat->isp->dev,
324 "%s: failed copying %d bytes of stat data\n",
325 stat->subdev.name, rval);
326 buf = ERR_PTR(-EFAULT);
327 isp_stat_buf_release(stat);
330 return buf;
333 static void isp_stat_bufs_free(struct ispstat *stat)
335 struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
336 ? NULL : stat->isp->dev;
337 unsigned int i;
339 for (i = 0; i < STAT_MAX_BUFS; i++) {
340 struct ispstat_buffer *buf = &stat->buf[i];
342 if (!buf->virt_addr)
343 continue;
345 sg_free_table(&buf->sgt);
347 dma_free_coherent(dev, stat->buf_alloc_size, buf->virt_addr,
348 buf->dma_addr);
350 buf->dma_addr = 0;
351 buf->virt_addr = NULL;
352 buf->empty = 1;
355 dev_dbg(stat->isp->dev, "%s: all buffers were freed.\n",
356 stat->subdev.name);
358 stat->buf_alloc_size = 0;
359 stat->active_buf = NULL;
362 static int isp_stat_bufs_alloc_one(struct device *dev,
363 struct ispstat_buffer *buf,
364 unsigned int size)
366 int ret;
368 buf->virt_addr = dma_alloc_coherent(dev, size, &buf->dma_addr,
369 GFP_KERNEL | GFP_DMA);
370 if (!buf->virt_addr)
371 return -ENOMEM;
373 ret = dma_get_sgtable(dev, &buf->sgt, buf->virt_addr, buf->dma_addr,
374 size);
375 if (ret < 0) {
376 dma_free_coherent(dev, size, buf->virt_addr, buf->dma_addr);
377 buf->virt_addr = NULL;
378 buf->dma_addr = 0;
379 return ret;
382 return 0;
386 * The device passed to the DMA API depends on whether the statistics block uses
387 * ISP DMA, external DMA or PIO to transfer data.
389 * The first case (for the AEWB and AF engines) passes the ISP device, resulting
390 * in the DMA buffers being mapped through the ISP IOMMU.
392 * The second case (for the histogram engine) should pass the DMA engine device.
393 * As that device isn't accessible through the OMAP DMA engine API the driver
394 * passes NULL instead, resulting in the buffers being mapped directly as
395 * physical pages.
397 * The third case (for the histogram engine) doesn't require any mapping. The
398 * buffers could be allocated with kmalloc/vmalloc, but we still use
399 * dma_alloc_coherent() for consistency purpose.
401 static int isp_stat_bufs_alloc(struct ispstat *stat, u32 size)
403 struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
404 ? NULL : stat->isp->dev;
405 unsigned long flags;
406 unsigned int i;
408 spin_lock_irqsave(&stat->isp->stat_lock, flags);
410 BUG_ON(stat->locked_buf != NULL);
412 /* Are the old buffers big enough? */
413 if (stat->buf_alloc_size >= size) {
414 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
415 return 0;
418 if (stat->state != ISPSTAT_DISABLED || stat->buf_processing) {
419 dev_info(stat->isp->dev,
420 "%s: trying to allocate memory when busy\n",
421 stat->subdev.name);
422 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
423 return -EBUSY;
426 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
428 isp_stat_bufs_free(stat);
430 stat->buf_alloc_size = size;
432 for (i = 0; i < STAT_MAX_BUFS; i++) {
433 struct ispstat_buffer *buf = &stat->buf[i];
434 int ret;
436 ret = isp_stat_bufs_alloc_one(dev, buf, size);
437 if (ret < 0) {
438 dev_err(stat->isp->dev,
439 "%s: Failed to allocate DMA buffer %u\n",
440 stat->subdev.name, i);
441 isp_stat_bufs_free(stat);
442 return ret;
445 buf->empty = 1;
447 dev_dbg(stat->isp->dev,
448 "%s: buffer[%u] allocated. dma=0x%08lx virt=0x%08lx",
449 stat->subdev.name, i,
450 (unsigned long)buf->dma_addr,
451 (unsigned long)buf->virt_addr);
454 return 0;
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));
464 if (!err) {
465 status->frame_number = stat->frame_number;
466 status->config_counter = stat->config_counter;
467 } else {
468 status->buf_err = 1;
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",
488 stat->subdev.name);
489 return -EINVAL;
492 mutex_lock(&stat->ioctl_lock);
493 buf = isp_stat_buf_get(stat, data);
494 if (IS_ERR(buf)) {
495 mutex_unlock(&stat->ioctl_lock);
496 return PTR_ERR(buf);
499 data->ts = buf->ts;
500 data->config_counter = buf->config_counter;
501 data->frame_number = buf->frame_number;
502 data->buf_size = buf->buf_size;
504 buf->empty = 1;
505 isp_stat_buf_release(stat);
506 mutex_unlock(&stat->ioctl_lock);
508 return 0;
512 * omap3isp_stat_config - Receives new statistic engine configuration.
513 * @new_conf: Pointer to config structure.
515 * Returns 0 if successful, -EINVAL if new_conf pointer is NULL, -ENOMEM if
516 * was unable to allocate memory for the buffer, or other errors if parameters
517 * are invalid.
519 int omap3isp_stat_config(struct ispstat *stat, void *new_conf)
521 int ret;
522 unsigned long irqflags;
523 struct ispstat_generic_config *user_cfg = new_conf;
524 u32 buf_size = user_cfg->buf_size;
526 if (!new_conf) {
527 dev_dbg(stat->isp->dev, "%s: configuration is NULL\n",
528 stat->subdev.name);
529 return -EINVAL;
532 mutex_lock(&stat->ioctl_lock);
534 dev_dbg(stat->isp->dev, "%s: configuring module with buffer "
535 "size=0x%08lx\n", stat->subdev.name, (unsigned long)buf_size);
537 ret = stat->ops->validate_params(stat, new_conf);
538 if (ret) {
539 mutex_unlock(&stat->ioctl_lock);
540 dev_dbg(stat->isp->dev, "%s: configuration values are "
541 "invalid.\n", stat->subdev.name);
542 return ret;
545 if (buf_size != user_cfg->buf_size)
546 dev_dbg(stat->isp->dev, "%s: driver has corrected buffer size "
547 "request to 0x%08lx\n", stat->subdev.name,
548 (unsigned long)user_cfg->buf_size);
551 * Hack: H3A modules may need a doubled buffer size to avoid access
552 * to a invalid memory address after a SBL overflow.
553 * The buffer size is always PAGE_ALIGNED.
554 * Hack 2: MAGIC_SIZE is added to buf_size so a magic word can be
555 * inserted at the end to data integrity check purpose.
556 * Hack 3: AF module writes one paxel data more than it should, so
557 * the buffer allocation must consider it to avoid invalid memory
558 * access.
559 * Hack 4: H3A need to allocate extra space for the recover state.
561 if (IS_H3A(stat)) {
562 buf_size = user_cfg->buf_size * 2 + MAGIC_SIZE;
563 if (IS_H3A_AF(stat))
565 * Adding one extra paxel data size for each recover
566 * buffer + 2 regular ones.
568 buf_size += AF_EXTRA_DATA * (NUM_H3A_RECOVER_BUFS + 2);
569 if (stat->recover_priv) {
570 struct ispstat_generic_config *recover_cfg =
571 stat->recover_priv;
572 buf_size += recover_cfg->buf_size *
573 NUM_H3A_RECOVER_BUFS;
575 buf_size = PAGE_ALIGN(buf_size);
576 } else { /* Histogram */
577 buf_size = PAGE_ALIGN(user_cfg->buf_size + MAGIC_SIZE);
580 ret = isp_stat_bufs_alloc(stat, buf_size);
581 if (ret) {
582 mutex_unlock(&stat->ioctl_lock);
583 return ret;
586 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
587 stat->ops->set_params(stat, new_conf);
588 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
591 * Returning the right future config_counter for this setup, so
592 * userspace can *know* when it has been applied.
594 user_cfg->config_counter = stat->config_counter + stat->inc_config;
596 /* Module has a valid configuration. */
597 stat->configured = 1;
598 dev_dbg(stat->isp->dev, "%s: module has been successfully "
599 "configured.\n", stat->subdev.name);
601 mutex_unlock(&stat->ioctl_lock);
603 return 0;
607 * isp_stat_buf_process - Process statistic buffers.
608 * @buf_state: points out if buffer is ready to be processed. It's necessary
609 * because histogram needs to copy the data from internal memory
610 * before be able to process the buffer.
612 static int isp_stat_buf_process(struct ispstat *stat, int buf_state)
614 int ret = STAT_NO_BUF;
616 if (!atomic_add_unless(&stat->buf_err, -1, 0) &&
617 buf_state == STAT_BUF_DONE && stat->state == ISPSTAT_ENABLED) {
618 ret = isp_stat_buf_queue(stat);
619 isp_stat_buf_next(stat);
622 return ret;
625 int omap3isp_stat_pcr_busy(struct ispstat *stat)
627 return stat->ops->busy(stat);
630 int omap3isp_stat_busy(struct ispstat *stat)
632 return omap3isp_stat_pcr_busy(stat) | stat->buf_processing |
633 (stat->state != ISPSTAT_DISABLED);
637 * isp_stat_pcr_enable - Disables/Enables statistic engines.
638 * @pcr_enable: 0/1 - Disables/Enables the engine.
640 * Must be called from ISP driver when the module is idle and synchronized
641 * with CCDC.
643 static void isp_stat_pcr_enable(struct ispstat *stat, u8 pcr_enable)
645 if ((stat->state != ISPSTAT_ENABLING &&
646 stat->state != ISPSTAT_ENABLED) && pcr_enable)
647 /* Userspace has disabled the module. Aborting. */
648 return;
650 stat->ops->enable(stat, pcr_enable);
651 if (stat->state == ISPSTAT_DISABLING && !pcr_enable)
652 stat->state = ISPSTAT_DISABLED;
653 else if (stat->state == ISPSTAT_ENABLING && pcr_enable)
654 stat->state = ISPSTAT_ENABLED;
657 void omap3isp_stat_suspend(struct ispstat *stat)
659 unsigned long flags;
661 spin_lock_irqsave(&stat->isp->stat_lock, flags);
663 if (stat->state != ISPSTAT_DISABLED)
664 stat->ops->enable(stat, 0);
665 if (stat->state == ISPSTAT_ENABLED)
666 stat->state = ISPSTAT_SUSPENDED;
668 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
671 void omap3isp_stat_resume(struct ispstat *stat)
673 /* Module will be re-enabled with its pipeline */
674 if (stat->state == ISPSTAT_SUSPENDED)
675 stat->state = ISPSTAT_ENABLING;
678 static void isp_stat_try_enable(struct ispstat *stat)
680 unsigned long irqflags;
682 if (stat->priv == NULL)
683 /* driver wasn't initialised */
684 return;
686 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
687 if (stat->state == ISPSTAT_ENABLING && !stat->buf_processing &&
688 stat->buf_alloc_size) {
690 * Userspace's requested to enable the engine but it wasn't yet.
691 * Let's do that now.
693 stat->update = 1;
694 isp_stat_buf_next(stat);
695 stat->ops->setup_regs(stat, stat->priv);
696 isp_stat_buf_insert_magic(stat, stat->active_buf);
699 * H3A module has some hw issues which forces the driver to
700 * ignore next buffers even if it was disabled in the meantime.
701 * On the other hand, Histogram shouldn't ignore buffers anymore
702 * if it's being enabled.
704 if (!IS_H3A(stat))
705 atomic_set(&stat->buf_err, 0);
707 isp_stat_pcr_enable(stat, 1);
708 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
709 dev_dbg(stat->isp->dev, "%s: module is enabled.\n",
710 stat->subdev.name);
711 } else {
712 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
716 void omap3isp_stat_isr_frame_sync(struct ispstat *stat)
718 isp_stat_try_enable(stat);
721 void omap3isp_stat_sbl_overflow(struct ispstat *stat)
723 unsigned long irqflags;
725 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
727 * Due to a H3A hw issue which prevents the next buffer to start from
728 * the correct memory address, 2 buffers must be ignored.
730 atomic_set(&stat->buf_err, 2);
733 * If more than one SBL overflow happen in a row, H3A module may access
734 * invalid memory region.
735 * stat->sbl_ovl_recover is set to tell to the driver to temporarily use
736 * a soft configuration which helps to avoid consecutive overflows.
738 if (stat->recover_priv)
739 stat->sbl_ovl_recover = 1;
740 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
744 * omap3isp_stat_enable - Disable/Enable statistic engine as soon as possible
745 * @enable: 0/1 - Disables/Enables the engine.
747 * Client should configure all the module registers before this.
748 * This function can be called from a userspace request.
750 int omap3isp_stat_enable(struct ispstat *stat, u8 enable)
752 unsigned long irqflags;
754 dev_dbg(stat->isp->dev, "%s: user wants to %s module.\n",
755 stat->subdev.name, enable ? "enable" : "disable");
757 /* Prevent enabling while configuring */
758 mutex_lock(&stat->ioctl_lock);
760 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
762 if (!stat->configured && enable) {
763 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
764 mutex_unlock(&stat->ioctl_lock);
765 dev_dbg(stat->isp->dev, "%s: cannot enable module as it's "
766 "never been successfully configured so far.\n",
767 stat->subdev.name);
768 return -EINVAL;
771 if (enable) {
772 if (stat->state == ISPSTAT_DISABLING)
773 /* Previous disabling request wasn't done yet */
774 stat->state = ISPSTAT_ENABLED;
775 else if (stat->state == ISPSTAT_DISABLED)
776 /* Module is now being enabled */
777 stat->state = ISPSTAT_ENABLING;
778 } else {
779 if (stat->state == ISPSTAT_ENABLING) {
780 /* Previous enabling request wasn't done yet */
781 stat->state = ISPSTAT_DISABLED;
782 } else if (stat->state == ISPSTAT_ENABLED) {
783 /* Module is now being disabled */
784 stat->state = ISPSTAT_DISABLING;
785 isp_stat_buf_clear(stat);
789 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
790 mutex_unlock(&stat->ioctl_lock);
792 return 0;
795 int omap3isp_stat_s_stream(struct v4l2_subdev *subdev, int enable)
797 struct ispstat *stat = v4l2_get_subdevdata(subdev);
799 if (enable) {
801 * Only set enable PCR bit if the module was previously
802 * enabled through ioctl.
804 isp_stat_try_enable(stat);
805 } else {
806 unsigned long flags;
807 /* Disable PCR bit and config enable field */
808 omap3isp_stat_enable(stat, 0);
809 spin_lock_irqsave(&stat->isp->stat_lock, flags);
810 stat->ops->enable(stat, 0);
811 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
814 * If module isn't busy, a new interrupt may come or not to
815 * set the state to DISABLED. As Histogram needs to read its
816 * internal memory to clear it, let interrupt handler
817 * responsible of changing state to DISABLED. If the last
818 * interrupt is coming, it's still safe as the handler will
819 * ignore the second time when state is already set to DISABLED.
820 * It's necessary to synchronize Histogram with streamoff, once
821 * the module may be considered idle before last SDMA transfer
822 * starts if we return here.
824 if (!omap3isp_stat_pcr_busy(stat))
825 omap3isp_stat_isr(stat);
827 dev_dbg(stat->isp->dev, "%s: module is being disabled\n",
828 stat->subdev.name);
831 return 0;
835 * __stat_isr - Interrupt handler for statistic drivers
837 static void __stat_isr(struct ispstat *stat, int from_dma)
839 int ret = STAT_BUF_DONE;
840 int buf_processing;
841 unsigned long irqflags;
842 struct isp_pipeline *pipe;
845 * stat->buf_processing must be set before disable module. It's
846 * necessary to not inform too early the buffers aren't busy in case
847 * of SDMA is going to be used.
849 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
850 if (stat->state == ISPSTAT_DISABLED) {
851 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
852 return;
854 buf_processing = stat->buf_processing;
855 stat->buf_processing = 1;
856 stat->ops->enable(stat, 0);
858 if (buf_processing && !from_dma) {
859 if (stat->state == ISPSTAT_ENABLED) {
860 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
861 dev_err(stat->isp->dev,
862 "%s: interrupt occurred when module was still "
863 "processing a buffer.\n", stat->subdev.name);
864 ret = STAT_NO_BUF;
865 goto out;
866 } else {
868 * Interrupt handler was called from streamoff when
869 * the module wasn't busy anymore to ensure it is being
870 * disabled after process last buffer. If such buffer
871 * processing has already started, no need to do
872 * anything else.
874 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
875 return;
878 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
880 /* If it's busy we can't process this buffer anymore */
881 if (!omap3isp_stat_pcr_busy(stat)) {
882 if (!from_dma && stat->ops->buf_process)
883 /* Module still need to copy data to buffer. */
884 ret = stat->ops->buf_process(stat);
885 if (ret == STAT_BUF_WAITING_DMA)
886 /* Buffer is not ready yet */
887 return;
889 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
892 * Histogram needs to read its internal memory to clear it
893 * before be disabled. For that reason, common statistic layer
894 * can return only after call stat's buf_process() operator.
896 if (stat->state == ISPSTAT_DISABLING) {
897 stat->state = ISPSTAT_DISABLED;
898 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
899 stat->buf_processing = 0;
900 return;
902 pipe = to_isp_pipeline(&stat->subdev.entity);
903 stat->frame_number = atomic_read(&pipe->frame_number);
906 * Before this point, 'ret' stores the buffer's status if it's
907 * ready to be processed. Afterwards, it holds the status if
908 * it was processed successfully.
910 ret = isp_stat_buf_process(stat, ret);
912 if (likely(!stat->sbl_ovl_recover)) {
913 stat->ops->setup_regs(stat, stat->priv);
914 } else {
916 * Using recover config to increase the chance to have
917 * a good buffer processing and make the H3A module to
918 * go back to a valid state.
920 stat->update = 1;
921 stat->ops->setup_regs(stat, stat->recover_priv);
922 stat->sbl_ovl_recover = 0;
925 * Set 'update' in case of the module needs to use
926 * regular configuration after next buffer.
928 stat->update = 1;
931 isp_stat_buf_insert_magic(stat, stat->active_buf);
934 * Hack: H3A modules may access invalid memory address or send
935 * corrupted data to userspace if more than 1 SBL overflow
936 * happens in a row without re-writing its buffer's start memory
937 * address in the meantime. Such situation is avoided if the
938 * module is not immediately re-enabled when the ISR misses the
939 * timing to process the buffer and to setup the registers.
940 * Because of that, pcr_enable(1) was moved to inside this 'if'
941 * block. But the next interruption will still happen as during
942 * pcr_enable(0) the module was busy.
944 isp_stat_pcr_enable(stat, 1);
945 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
946 } else {
948 * If a SBL overflow occurs and the H3A driver misses the timing
949 * to process the buffer, stat->buf_err is set and won't be
950 * cleared now. So the next buffer will be correctly ignored.
951 * It's necessary due to a hw issue which makes the next H3A
952 * buffer to start from the memory address where the previous
953 * one stopped, instead of start where it was configured to.
954 * Do not "stat->buf_err = 0" here.
957 if (stat->ops->buf_process)
959 * Driver may need to erase current data prior to
960 * process a new buffer. If it misses the timing, the
961 * next buffer might be wrong. So should be ignored.
962 * It happens only for Histogram.
964 atomic_set(&stat->buf_err, 1);
966 ret = STAT_NO_BUF;
967 dev_dbg(stat->isp->dev, "%s: cannot process buffer, "
968 "device is busy.\n", stat->subdev.name);
971 out:
972 stat->buf_processing = 0;
973 isp_stat_queue_event(stat, ret != STAT_BUF_DONE);
976 void omap3isp_stat_isr(struct ispstat *stat)
978 __stat_isr(stat, 0);
981 void omap3isp_stat_dma_isr(struct ispstat *stat)
983 __stat_isr(stat, 1);
986 int omap3isp_stat_subscribe_event(struct v4l2_subdev *subdev,
987 struct v4l2_fh *fh,
988 struct v4l2_event_subscription *sub)
990 struct ispstat *stat = v4l2_get_subdevdata(subdev);
992 if (sub->type != stat->event_type)
993 return -EINVAL;
995 return v4l2_event_subscribe(fh, sub, STAT_NEVENTS, NULL);
998 int omap3isp_stat_unsubscribe_event(struct v4l2_subdev *subdev,
999 struct v4l2_fh *fh,
1000 struct v4l2_event_subscription *sub)
1002 return v4l2_event_unsubscribe(fh, sub);
1005 void omap3isp_stat_unregister_entities(struct ispstat *stat)
1007 v4l2_device_unregister_subdev(&stat->subdev);
1010 int omap3isp_stat_register_entities(struct ispstat *stat,
1011 struct v4l2_device *vdev)
1013 return v4l2_device_register_subdev(vdev, &stat->subdev);
1016 static int isp_stat_init_entities(struct ispstat *stat, const char *name,
1017 const struct v4l2_subdev_ops *sd_ops)
1019 struct v4l2_subdev *subdev = &stat->subdev;
1020 struct media_entity *me = &subdev->entity;
1022 v4l2_subdev_init(subdev, sd_ops);
1023 snprintf(subdev->name, V4L2_SUBDEV_NAME_SIZE, "OMAP3 ISP %s", name);
1024 subdev->grp_id = 1 << 16; /* group ID for isp subdevs */
1025 subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
1026 v4l2_set_subdevdata(subdev, stat);
1028 stat->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
1029 me->ops = NULL;
1031 return media_entity_pads_init(me, 1, &stat->pad);
1034 int omap3isp_stat_init(struct ispstat *stat, const char *name,
1035 const struct v4l2_subdev_ops *sd_ops)
1037 int ret;
1039 stat->buf = kcalloc(STAT_MAX_BUFS, sizeof(*stat->buf), GFP_KERNEL);
1040 if (!stat->buf)
1041 return -ENOMEM;
1043 isp_stat_buf_clear(stat);
1044 mutex_init(&stat->ioctl_lock);
1045 atomic_set(&stat->buf_err, 0);
1047 ret = isp_stat_init_entities(stat, name, sd_ops);
1048 if (ret < 0) {
1049 mutex_destroy(&stat->ioctl_lock);
1050 kfree(stat->buf);
1053 return ret;
1056 void omap3isp_stat_cleanup(struct ispstat *stat)
1058 media_entity_cleanup(&stat->subdev.entity);
1059 mutex_destroy(&stat->ioctl_lock);
1060 isp_stat_bufs_free(stat);
1061 kfree(stat->buf);