18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * ispstat.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * TI OMAP3 ISP - Statistics core
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2010 Nokia Corporation
88c2ecf20Sopenharmony_ci * Copyright (C) 2009 Texas Instruments, Inc
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * Contacts: David Cohen <dacohen@gmail.com>
118c2ecf20Sopenharmony_ci *	     Laurent Pinchart <laurent.pinchart@ideasonboard.com>
128c2ecf20Sopenharmony_ci *	     Sakari Ailus <sakari.ailus@iki.fi>
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/timekeeping.h>
188c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#include "isp.h"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define ISP_STAT_USES_DMAENGINE(stat)	((stat)->dma_ch != NULL)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/*
258c2ecf20Sopenharmony_ci * MAGIC_SIZE must always be the greatest common divisor of
268c2ecf20Sopenharmony_ci * AEWB_PACKET_SIZE and AF_PAXEL_SIZE.
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci#define MAGIC_SIZE		16
298c2ecf20Sopenharmony_ci#define MAGIC_NUM		0x55
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci/* HACK: AF module seems to be writing one more paxel data than it should. */
328c2ecf20Sopenharmony_ci#define AF_EXTRA_DATA		OMAP3ISP_AF_PAXEL_SIZE
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/*
358c2ecf20Sopenharmony_ci * HACK: H3A modules go to an invalid state after have a SBL overflow. It makes
368c2ecf20Sopenharmony_ci * the next buffer to start to be written in the same point where the overflow
378c2ecf20Sopenharmony_ci * occurred instead of the configured address. The only known way to make it to
388c2ecf20Sopenharmony_ci * go back to a valid state is having a valid buffer processing. Of course it
398c2ecf20Sopenharmony_ci * requires at least a doubled buffer size to avoid an access to invalid memory
408c2ecf20Sopenharmony_ci * region. But it does not fix everything. It may happen more than one
418c2ecf20Sopenharmony_ci * consecutive SBL overflows. In that case, it might be unpredictable how many
428c2ecf20Sopenharmony_ci * buffers the allocated memory should fit. For that case, a recover
438c2ecf20Sopenharmony_ci * configuration was created. It produces the minimum buffer size for each H3A
448c2ecf20Sopenharmony_ci * module and decrease the change for more SBL overflows. This recover state
458c2ecf20Sopenharmony_ci * will be enabled every time a SBL overflow occur. As the output buffer size
468c2ecf20Sopenharmony_ci * isn't big, it's possible to have an extra size able to fit many recover
478c2ecf20Sopenharmony_ci * buffers making it extreamily unlikely to have an access to invalid memory
488c2ecf20Sopenharmony_ci * region.
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_ci#define NUM_H3A_RECOVER_BUFS	10
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci/*
538c2ecf20Sopenharmony_ci * HACK: Because of HW issues the generic layer sometimes need to have
548c2ecf20Sopenharmony_ci * different behaviour for different statistic modules.
558c2ecf20Sopenharmony_ci */
568c2ecf20Sopenharmony_ci#define IS_H3A_AF(stat)		((stat) == &(stat)->isp->isp_af)
578c2ecf20Sopenharmony_ci#define IS_H3A_AEWB(stat)	((stat) == &(stat)->isp->isp_aewb)
588c2ecf20Sopenharmony_ci#define IS_H3A(stat)		(IS_H3A_AF(stat) || IS_H3A_AEWB(stat))
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic void __isp_stat_buf_sync_magic(struct ispstat *stat,
618c2ecf20Sopenharmony_ci				      struct ispstat_buffer *buf,
628c2ecf20Sopenharmony_ci				      u32 buf_size, enum dma_data_direction dir,
638c2ecf20Sopenharmony_ci				      void (*dma_sync)(struct device *,
648c2ecf20Sopenharmony_ci					dma_addr_t, unsigned long, size_t,
658c2ecf20Sopenharmony_ci					enum dma_data_direction))
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	/* Sync the initial and final magic words. */
688c2ecf20Sopenharmony_ci	dma_sync(stat->isp->dev, buf->dma_addr, 0, MAGIC_SIZE, dir);
698c2ecf20Sopenharmony_ci	dma_sync(stat->isp->dev, buf->dma_addr + (buf_size & PAGE_MASK),
708c2ecf20Sopenharmony_ci		 buf_size & ~PAGE_MASK, MAGIC_SIZE, dir);
718c2ecf20Sopenharmony_ci}
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_cistatic void isp_stat_buf_sync_magic_for_device(struct ispstat *stat,
748c2ecf20Sopenharmony_ci					       struct ispstat_buffer *buf,
758c2ecf20Sopenharmony_ci					       u32 buf_size,
768c2ecf20Sopenharmony_ci					       enum dma_data_direction dir)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	if (ISP_STAT_USES_DMAENGINE(stat))
798c2ecf20Sopenharmony_ci		return;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	__isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
828c2ecf20Sopenharmony_ci				  dma_sync_single_range_for_device);
838c2ecf20Sopenharmony_ci}
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic void isp_stat_buf_sync_magic_for_cpu(struct ispstat *stat,
868c2ecf20Sopenharmony_ci					    struct ispstat_buffer *buf,
878c2ecf20Sopenharmony_ci					    u32 buf_size,
888c2ecf20Sopenharmony_ci					    enum dma_data_direction dir)
898c2ecf20Sopenharmony_ci{
908c2ecf20Sopenharmony_ci	if (ISP_STAT_USES_DMAENGINE(stat))
918c2ecf20Sopenharmony_ci		return;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	__isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
948c2ecf20Sopenharmony_ci				  dma_sync_single_range_for_cpu);
958c2ecf20Sopenharmony_ci}
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_cistatic int isp_stat_buf_check_magic(struct ispstat *stat,
988c2ecf20Sopenharmony_ci				    struct ispstat_buffer *buf)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	const u32 buf_size = IS_H3A_AF(stat) ?
1018c2ecf20Sopenharmony_ci			     buf->buf_size + AF_EXTRA_DATA : buf->buf_size;
1028c2ecf20Sopenharmony_ci	u8 *w;
1038c2ecf20Sopenharmony_ci	u8 *end;
1048c2ecf20Sopenharmony_ci	int ret = -EINVAL;
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	/* Checking initial magic numbers. They shouldn't be here anymore. */
1098c2ecf20Sopenharmony_ci	for (w = buf->virt_addr, end = w + MAGIC_SIZE; w < end; w++)
1108c2ecf20Sopenharmony_ci		if (likely(*w != MAGIC_NUM))
1118c2ecf20Sopenharmony_ci			ret = 0;
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci	if (ret) {
1148c2ecf20Sopenharmony_ci		dev_dbg(stat->isp->dev,
1158c2ecf20Sopenharmony_ci			"%s: beginning magic check does not match.\n",
1168c2ecf20Sopenharmony_ci			stat->subdev.name);
1178c2ecf20Sopenharmony_ci		return ret;
1188c2ecf20Sopenharmony_ci	}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	/* Checking magic numbers at the end. They must be still here. */
1218c2ecf20Sopenharmony_ci	for (w = buf->virt_addr + buf_size, end = w + MAGIC_SIZE;
1228c2ecf20Sopenharmony_ci	     w < end; w++) {
1238c2ecf20Sopenharmony_ci		if (unlikely(*w != MAGIC_NUM)) {
1248c2ecf20Sopenharmony_ci			dev_dbg(stat->isp->dev,
1258c2ecf20Sopenharmony_ci				"%s: ending magic check does not match.\n",
1268c2ecf20Sopenharmony_ci				stat->subdev.name);
1278c2ecf20Sopenharmony_ci			return -EINVAL;
1288c2ecf20Sopenharmony_ci		}
1298c2ecf20Sopenharmony_ci	}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
1328c2ecf20Sopenharmony_ci					   DMA_FROM_DEVICE);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	return 0;
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic void isp_stat_buf_insert_magic(struct ispstat *stat,
1388c2ecf20Sopenharmony_ci				      struct ispstat_buffer *buf)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	const u32 buf_size = IS_H3A_AF(stat) ?
1418c2ecf20Sopenharmony_ci			     stat->buf_size + AF_EXTRA_DATA : stat->buf_size;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/*
1468c2ecf20Sopenharmony_ci	 * Inserting MAGIC_NUM at the beginning and end of the buffer.
1478c2ecf20Sopenharmony_ci	 * buf->buf_size is set only after the buffer is queued. For now the
1488c2ecf20Sopenharmony_ci	 * right buf_size for the current configuration is pointed by
1498c2ecf20Sopenharmony_ci	 * stat->buf_size.
1508c2ecf20Sopenharmony_ci	 */
1518c2ecf20Sopenharmony_ci	memset(buf->virt_addr, MAGIC_NUM, MAGIC_SIZE);
1528c2ecf20Sopenharmony_ci	memset(buf->virt_addr + buf_size, MAGIC_NUM, MAGIC_SIZE);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
1558c2ecf20Sopenharmony_ci					   DMA_BIDIRECTIONAL);
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic void isp_stat_buf_sync_for_device(struct ispstat *stat,
1598c2ecf20Sopenharmony_ci					 struct ispstat_buffer *buf)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	if (ISP_STAT_USES_DMAENGINE(stat))
1628c2ecf20Sopenharmony_ci		return;
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	dma_sync_sg_for_device(stat->isp->dev, buf->sgt.sgl,
1658c2ecf20Sopenharmony_ci			       buf->sgt.nents, DMA_FROM_DEVICE);
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistatic void isp_stat_buf_sync_for_cpu(struct ispstat *stat,
1698c2ecf20Sopenharmony_ci				      struct ispstat_buffer *buf)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	if (ISP_STAT_USES_DMAENGINE(stat))
1728c2ecf20Sopenharmony_ci		return;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	dma_sync_sg_for_cpu(stat->isp->dev, buf->sgt.sgl,
1758c2ecf20Sopenharmony_ci			    buf->sgt.nents, DMA_FROM_DEVICE);
1768c2ecf20Sopenharmony_ci}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_cistatic void isp_stat_buf_clear(struct ispstat *stat)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	int i;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	for (i = 0; i < STAT_MAX_BUFS; i++)
1838c2ecf20Sopenharmony_ci		stat->buf[i].empty = 1;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic struct ispstat_buffer *
1878c2ecf20Sopenharmony_ci__isp_stat_buf_find(struct ispstat *stat, int look_empty)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	struct ispstat_buffer *found = NULL;
1908c2ecf20Sopenharmony_ci	int i;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	for (i = 0; i < STAT_MAX_BUFS; i++) {
1938c2ecf20Sopenharmony_ci		struct ispstat_buffer *curr = &stat->buf[i];
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci		/*
1968c2ecf20Sopenharmony_ci		 * Don't select the buffer which is being copied to
1978c2ecf20Sopenharmony_ci		 * userspace or used by the module.
1988c2ecf20Sopenharmony_ci		 */
1998c2ecf20Sopenharmony_ci		if (curr == stat->locked_buf || curr == stat->active_buf)
2008c2ecf20Sopenharmony_ci			continue;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci		/* Don't select uninitialised buffers if it's not required */
2038c2ecf20Sopenharmony_ci		if (!look_empty && curr->empty)
2048c2ecf20Sopenharmony_ci			continue;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci		/* Pick uninitialised buffer over anything else if look_empty */
2078c2ecf20Sopenharmony_ci		if (curr->empty) {
2088c2ecf20Sopenharmony_ci			found = curr;
2098c2ecf20Sopenharmony_ci			break;
2108c2ecf20Sopenharmony_ci		}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci		/* Choose the oldest buffer */
2138c2ecf20Sopenharmony_ci		if (!found ||
2148c2ecf20Sopenharmony_ci		    (s32)curr->frame_number - (s32)found->frame_number < 0)
2158c2ecf20Sopenharmony_ci			found = curr;
2168c2ecf20Sopenharmony_ci	}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	return found;
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic inline struct ispstat_buffer *
2228c2ecf20Sopenharmony_ciisp_stat_buf_find_oldest(struct ispstat *stat)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	return __isp_stat_buf_find(stat, 0);
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic inline struct ispstat_buffer *
2288c2ecf20Sopenharmony_ciisp_stat_buf_find_oldest_or_empty(struct ispstat *stat)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	return __isp_stat_buf_find(stat, 1);
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic int isp_stat_buf_queue(struct ispstat *stat)
2348c2ecf20Sopenharmony_ci{
2358c2ecf20Sopenharmony_ci	if (!stat->active_buf)
2368c2ecf20Sopenharmony_ci		return STAT_NO_BUF;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	ktime_get_ts64(&stat->active_buf->ts);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	stat->active_buf->buf_size = stat->buf_size;
2418c2ecf20Sopenharmony_ci	if (isp_stat_buf_check_magic(stat, stat->active_buf)) {
2428c2ecf20Sopenharmony_ci		dev_dbg(stat->isp->dev, "%s: data wasn't properly written.\n",
2438c2ecf20Sopenharmony_ci			stat->subdev.name);
2448c2ecf20Sopenharmony_ci		return STAT_NO_BUF;
2458c2ecf20Sopenharmony_ci	}
2468c2ecf20Sopenharmony_ci	stat->active_buf->config_counter = stat->config_counter;
2478c2ecf20Sopenharmony_ci	stat->active_buf->frame_number = stat->frame_number;
2488c2ecf20Sopenharmony_ci	stat->active_buf->empty = 0;
2498c2ecf20Sopenharmony_ci	stat->active_buf = NULL;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	return STAT_BUF_DONE;
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci/* Get next free buffer to write the statistics to and mark it active. */
2558c2ecf20Sopenharmony_cistatic void isp_stat_buf_next(struct ispstat *stat)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	if (unlikely(stat->active_buf))
2588c2ecf20Sopenharmony_ci		/* Overwriting unused active buffer */
2598c2ecf20Sopenharmony_ci		dev_dbg(stat->isp->dev,
2608c2ecf20Sopenharmony_ci			"%s: new buffer requested without queuing active one.\n",
2618c2ecf20Sopenharmony_ci			stat->subdev.name);
2628c2ecf20Sopenharmony_ci	else
2638c2ecf20Sopenharmony_ci		stat->active_buf = isp_stat_buf_find_oldest_or_empty(stat);
2648c2ecf20Sopenharmony_ci}
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_cistatic void isp_stat_buf_release(struct ispstat *stat)
2678c2ecf20Sopenharmony_ci{
2688c2ecf20Sopenharmony_ci	unsigned long flags;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	isp_stat_buf_sync_for_device(stat, stat->locked_buf);
2718c2ecf20Sopenharmony_ci	spin_lock_irqsave(&stat->isp->stat_lock, flags);
2728c2ecf20Sopenharmony_ci	stat->locked_buf = NULL;
2738c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci/* Get buffer to userspace. */
2778c2ecf20Sopenharmony_cistatic struct ispstat_buffer *isp_stat_buf_get(struct ispstat *stat,
2788c2ecf20Sopenharmony_ci					       struct omap3isp_stat_data *data)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	int rval = 0;
2818c2ecf20Sopenharmony_ci	unsigned long flags;
2828c2ecf20Sopenharmony_ci	struct ispstat_buffer *buf;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	spin_lock_irqsave(&stat->isp->stat_lock, flags);
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	while (1) {
2878c2ecf20Sopenharmony_ci		buf = isp_stat_buf_find_oldest(stat);
2888c2ecf20Sopenharmony_ci		if (!buf) {
2898c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
2908c2ecf20Sopenharmony_ci			dev_dbg(stat->isp->dev, "%s: cannot find a buffer.\n",
2918c2ecf20Sopenharmony_ci				stat->subdev.name);
2928c2ecf20Sopenharmony_ci			return ERR_PTR(-EBUSY);
2938c2ecf20Sopenharmony_ci		}
2948c2ecf20Sopenharmony_ci		if (isp_stat_buf_check_magic(stat, buf)) {
2958c2ecf20Sopenharmony_ci			dev_dbg(stat->isp->dev,
2968c2ecf20Sopenharmony_ci				"%s: current buffer has corrupted data\n.",
2978c2ecf20Sopenharmony_ci				stat->subdev.name);
2988c2ecf20Sopenharmony_ci			/* Mark empty because it doesn't have valid data. */
2998c2ecf20Sopenharmony_ci			buf->empty = 1;
3008c2ecf20Sopenharmony_ci		} else {
3018c2ecf20Sopenharmony_ci			/* Buffer isn't corrupted. */
3028c2ecf20Sopenharmony_ci			break;
3038c2ecf20Sopenharmony_ci		}
3048c2ecf20Sopenharmony_ci	}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	stat->locked_buf = buf;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if (buf->buf_size > data->buf_size) {
3118c2ecf20Sopenharmony_ci		dev_warn(stat->isp->dev,
3128c2ecf20Sopenharmony_ci			 "%s: userspace's buffer size is not enough.\n",
3138c2ecf20Sopenharmony_ci			 stat->subdev.name);
3148c2ecf20Sopenharmony_ci		isp_stat_buf_release(stat);
3158c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
3168c2ecf20Sopenharmony_ci	}
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	isp_stat_buf_sync_for_cpu(stat, buf);
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	rval = copy_to_user(data->buf,
3218c2ecf20Sopenharmony_ci			    buf->virt_addr,
3228c2ecf20Sopenharmony_ci			    buf->buf_size);
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (rval) {
3258c2ecf20Sopenharmony_ci		dev_info(stat->isp->dev,
3268c2ecf20Sopenharmony_ci			 "%s: failed copying %d bytes of stat data\n",
3278c2ecf20Sopenharmony_ci			 stat->subdev.name, rval);
3288c2ecf20Sopenharmony_ci		buf = ERR_PTR(-EFAULT);
3298c2ecf20Sopenharmony_ci		isp_stat_buf_release(stat);
3308c2ecf20Sopenharmony_ci	}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	return buf;
3338c2ecf20Sopenharmony_ci}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic void isp_stat_bufs_free(struct ispstat *stat)
3368c2ecf20Sopenharmony_ci{
3378c2ecf20Sopenharmony_ci	struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
3388c2ecf20Sopenharmony_ci			   ? NULL : stat->isp->dev;
3398c2ecf20Sopenharmony_ci	unsigned int i;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	for (i = 0; i < STAT_MAX_BUFS; i++) {
3428c2ecf20Sopenharmony_ci		struct ispstat_buffer *buf = &stat->buf[i];
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci		if (!buf->virt_addr)
3458c2ecf20Sopenharmony_ci			continue;
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci		sg_free_table(&buf->sgt);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci		dma_free_coherent(dev, stat->buf_alloc_size, buf->virt_addr,
3508c2ecf20Sopenharmony_ci				  buf->dma_addr);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci		buf->dma_addr = 0;
3538c2ecf20Sopenharmony_ci		buf->virt_addr = NULL;
3548c2ecf20Sopenharmony_ci		buf->empty = 1;
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	dev_dbg(stat->isp->dev, "%s: all buffers were freed.\n",
3588c2ecf20Sopenharmony_ci		stat->subdev.name);
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	stat->buf_alloc_size = 0;
3618c2ecf20Sopenharmony_ci	stat->active_buf = NULL;
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_cistatic int isp_stat_bufs_alloc_one(struct device *dev,
3658c2ecf20Sopenharmony_ci				   struct ispstat_buffer *buf,
3668c2ecf20Sopenharmony_ci				   unsigned int size)
3678c2ecf20Sopenharmony_ci{
3688c2ecf20Sopenharmony_ci	int ret;
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci	buf->virt_addr = dma_alloc_coherent(dev, size, &buf->dma_addr,
3718c2ecf20Sopenharmony_ci					    GFP_KERNEL);
3728c2ecf20Sopenharmony_ci	if (!buf->virt_addr)
3738c2ecf20Sopenharmony_ci		return -ENOMEM;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	ret = dma_get_sgtable(dev, &buf->sgt, buf->virt_addr, buf->dma_addr,
3768c2ecf20Sopenharmony_ci			      size);
3778c2ecf20Sopenharmony_ci	if (ret < 0) {
3788c2ecf20Sopenharmony_ci		dma_free_coherent(dev, size, buf->virt_addr, buf->dma_addr);
3798c2ecf20Sopenharmony_ci		buf->virt_addr = NULL;
3808c2ecf20Sopenharmony_ci		buf->dma_addr = 0;
3818c2ecf20Sopenharmony_ci		return ret;
3828c2ecf20Sopenharmony_ci	}
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	return 0;
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci/*
3888c2ecf20Sopenharmony_ci * The device passed to the DMA API depends on whether the statistics block uses
3898c2ecf20Sopenharmony_ci * ISP DMA, external DMA or PIO to transfer data.
3908c2ecf20Sopenharmony_ci *
3918c2ecf20Sopenharmony_ci * The first case (for the AEWB and AF engines) passes the ISP device, resulting
3928c2ecf20Sopenharmony_ci * in the DMA buffers being mapped through the ISP IOMMU.
3938c2ecf20Sopenharmony_ci *
3948c2ecf20Sopenharmony_ci * The second case (for the histogram engine) should pass the DMA engine device.
3958c2ecf20Sopenharmony_ci * As that device isn't accessible through the OMAP DMA engine API the driver
3968c2ecf20Sopenharmony_ci * passes NULL instead, resulting in the buffers being mapped directly as
3978c2ecf20Sopenharmony_ci * physical pages.
3988c2ecf20Sopenharmony_ci *
3998c2ecf20Sopenharmony_ci * The third case (for the histogram engine) doesn't require any mapping. The
4008c2ecf20Sopenharmony_ci * buffers could be allocated with kmalloc/vmalloc, but we still use
4018c2ecf20Sopenharmony_ci * dma_alloc_coherent() for consistency purpose.
4028c2ecf20Sopenharmony_ci */
4038c2ecf20Sopenharmony_cistatic int isp_stat_bufs_alloc(struct ispstat *stat, u32 size)
4048c2ecf20Sopenharmony_ci{
4058c2ecf20Sopenharmony_ci	struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
4068c2ecf20Sopenharmony_ci			   ? NULL : stat->isp->dev;
4078c2ecf20Sopenharmony_ci	unsigned long flags;
4088c2ecf20Sopenharmony_ci	unsigned int i;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	spin_lock_irqsave(&stat->isp->stat_lock, flags);
4118c2ecf20Sopenharmony_ci
4128c2ecf20Sopenharmony_ci	BUG_ON(stat->locked_buf != NULL);
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	/* Are the old buffers big enough? */
4158c2ecf20Sopenharmony_ci	if (stat->buf_alloc_size >= size) {
4168c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
4178c2ecf20Sopenharmony_ci		return 0;
4188c2ecf20Sopenharmony_ci	}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	if (stat->state != ISPSTAT_DISABLED || stat->buf_processing) {
4218c2ecf20Sopenharmony_ci		dev_info(stat->isp->dev,
4228c2ecf20Sopenharmony_ci			 "%s: trying to allocate memory when busy\n",
4238c2ecf20Sopenharmony_ci			 stat->subdev.name);
4248c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
4258c2ecf20Sopenharmony_ci		return -EBUSY;
4268c2ecf20Sopenharmony_ci	}
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	isp_stat_bufs_free(stat);
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	stat->buf_alloc_size = size;
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	for (i = 0; i < STAT_MAX_BUFS; i++) {
4358c2ecf20Sopenharmony_ci		struct ispstat_buffer *buf = &stat->buf[i];
4368c2ecf20Sopenharmony_ci		int ret;
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci		ret = isp_stat_bufs_alloc_one(dev, buf, size);
4398c2ecf20Sopenharmony_ci		if (ret < 0) {
4408c2ecf20Sopenharmony_ci			dev_err(stat->isp->dev,
4418c2ecf20Sopenharmony_ci				"%s: Failed to allocate DMA buffer %u\n",
4428c2ecf20Sopenharmony_ci				stat->subdev.name, i);
4438c2ecf20Sopenharmony_ci			isp_stat_bufs_free(stat);
4448c2ecf20Sopenharmony_ci			return ret;
4458c2ecf20Sopenharmony_ci		}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci		buf->empty = 1;
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci		dev_dbg(stat->isp->dev,
4508c2ecf20Sopenharmony_ci			"%s: buffer[%u] allocated. dma=%pad virt=%p",
4518c2ecf20Sopenharmony_ci			stat->subdev.name, i, &buf->dma_addr, buf->virt_addr);
4528c2ecf20Sopenharmony_ci	}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	return 0;
4558c2ecf20Sopenharmony_ci}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistatic void isp_stat_queue_event(struct ispstat *stat, int err)
4588c2ecf20Sopenharmony_ci{
4598c2ecf20Sopenharmony_ci	struct video_device *vdev = stat->subdev.devnode;
4608c2ecf20Sopenharmony_ci	struct v4l2_event event;
4618c2ecf20Sopenharmony_ci	struct omap3isp_stat_event_status *status = (void *)event.u.data;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	memset(&event, 0, sizeof(event));
4648c2ecf20Sopenharmony_ci	if (!err) {
4658c2ecf20Sopenharmony_ci		status->frame_number = stat->frame_number;
4668c2ecf20Sopenharmony_ci		status->config_counter = stat->config_counter;
4678c2ecf20Sopenharmony_ci	} else {
4688c2ecf20Sopenharmony_ci		status->buf_err = 1;
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci	event.type = stat->event_type;
4718c2ecf20Sopenharmony_ci	v4l2_event_queue(vdev, &event);
4728c2ecf20Sopenharmony_ci}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci/*
4768c2ecf20Sopenharmony_ci * omap3isp_stat_request_statistics - Request statistics.
4778c2ecf20Sopenharmony_ci * @data: Pointer to return statistics data.
4788c2ecf20Sopenharmony_ci *
4798c2ecf20Sopenharmony_ci * Returns 0 if successful.
4808c2ecf20Sopenharmony_ci */
4818c2ecf20Sopenharmony_ciint omap3isp_stat_request_statistics(struct ispstat *stat,
4828c2ecf20Sopenharmony_ci				     struct omap3isp_stat_data *data)
4838c2ecf20Sopenharmony_ci{
4848c2ecf20Sopenharmony_ci	struct ispstat_buffer *buf;
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	if (stat->state != ISPSTAT_ENABLED) {
4878c2ecf20Sopenharmony_ci		dev_dbg(stat->isp->dev, "%s: engine not enabled.\n",
4888c2ecf20Sopenharmony_ci			stat->subdev.name);
4898c2ecf20Sopenharmony_ci		return -EINVAL;
4908c2ecf20Sopenharmony_ci	}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	mutex_lock(&stat->ioctl_lock);
4938c2ecf20Sopenharmony_ci	buf = isp_stat_buf_get(stat, data);
4948c2ecf20Sopenharmony_ci	if (IS_ERR(buf)) {
4958c2ecf20Sopenharmony_ci		mutex_unlock(&stat->ioctl_lock);
4968c2ecf20Sopenharmony_ci		return PTR_ERR(buf);
4978c2ecf20Sopenharmony_ci	}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	data->ts.tv_sec = buf->ts.tv_sec;
5008c2ecf20Sopenharmony_ci	data->ts.tv_usec = buf->ts.tv_nsec / NSEC_PER_USEC;
5018c2ecf20Sopenharmony_ci	data->config_counter = buf->config_counter;
5028c2ecf20Sopenharmony_ci	data->frame_number = buf->frame_number;
5038c2ecf20Sopenharmony_ci	data->buf_size = buf->buf_size;
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	buf->empty = 1;
5068c2ecf20Sopenharmony_ci	isp_stat_buf_release(stat);
5078c2ecf20Sopenharmony_ci	mutex_unlock(&stat->ioctl_lock);
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_ci	return 0;
5108c2ecf20Sopenharmony_ci}
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ciint omap3isp_stat_request_statistics_time32(struct ispstat *stat,
5138c2ecf20Sopenharmony_ci					struct omap3isp_stat_data_time32 *data)
5148c2ecf20Sopenharmony_ci{
5158c2ecf20Sopenharmony_ci	struct omap3isp_stat_data data64;
5168c2ecf20Sopenharmony_ci	int ret;
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	ret = omap3isp_stat_request_statistics(stat, &data64);
5198c2ecf20Sopenharmony_ci	if (ret)
5208c2ecf20Sopenharmony_ci		return ret;
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	data->ts.tv_sec = data64.ts.tv_sec;
5238c2ecf20Sopenharmony_ci	data->ts.tv_usec = data64.ts.tv_usec;
5248c2ecf20Sopenharmony_ci	memcpy(&data->buf, &data64.buf, sizeof(*data) - sizeof(data->ts));
5258c2ecf20Sopenharmony_ci
5268c2ecf20Sopenharmony_ci	return 0;
5278c2ecf20Sopenharmony_ci}
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci/*
5308c2ecf20Sopenharmony_ci * omap3isp_stat_config - Receives new statistic engine configuration.
5318c2ecf20Sopenharmony_ci * @new_conf: Pointer to config structure.
5328c2ecf20Sopenharmony_ci *
5338c2ecf20Sopenharmony_ci * Returns 0 if successful, -EINVAL if new_conf pointer is NULL, -ENOMEM if
5348c2ecf20Sopenharmony_ci * was unable to allocate memory for the buffer, or other errors if parameters
5358c2ecf20Sopenharmony_ci * are invalid.
5368c2ecf20Sopenharmony_ci */
5378c2ecf20Sopenharmony_ciint omap3isp_stat_config(struct ispstat *stat, void *new_conf)
5388c2ecf20Sopenharmony_ci{
5398c2ecf20Sopenharmony_ci	int ret;
5408c2ecf20Sopenharmony_ci	unsigned long irqflags;
5418c2ecf20Sopenharmony_ci	struct ispstat_generic_config *user_cfg = new_conf;
5428c2ecf20Sopenharmony_ci	u32 buf_size = user_cfg->buf_size;
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	mutex_lock(&stat->ioctl_lock);
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	dev_dbg(stat->isp->dev,
5478c2ecf20Sopenharmony_ci		"%s: configuring module with buffer size=0x%08lx\n",
5488c2ecf20Sopenharmony_ci		stat->subdev.name, (unsigned long)buf_size);
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_ci	ret = stat->ops->validate_params(stat, new_conf);
5518c2ecf20Sopenharmony_ci	if (ret) {
5528c2ecf20Sopenharmony_ci		mutex_unlock(&stat->ioctl_lock);
5538c2ecf20Sopenharmony_ci		dev_dbg(stat->isp->dev, "%s: configuration values are invalid.\n",
5548c2ecf20Sopenharmony_ci			stat->subdev.name);
5558c2ecf20Sopenharmony_ci		return ret;
5568c2ecf20Sopenharmony_ci	}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	if (buf_size != user_cfg->buf_size)
5598c2ecf20Sopenharmony_ci		dev_dbg(stat->isp->dev,
5608c2ecf20Sopenharmony_ci			"%s: driver has corrected buffer size request to 0x%08lx\n",
5618c2ecf20Sopenharmony_ci			stat->subdev.name,
5628c2ecf20Sopenharmony_ci			(unsigned long)user_cfg->buf_size);
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_ci	/*
5658c2ecf20Sopenharmony_ci	 * Hack: H3A modules may need a doubled buffer size to avoid access
5668c2ecf20Sopenharmony_ci	 * to a invalid memory address after a SBL overflow.
5678c2ecf20Sopenharmony_ci	 * The buffer size is always PAGE_ALIGNED.
5688c2ecf20Sopenharmony_ci	 * Hack 2: MAGIC_SIZE is added to buf_size so a magic word can be
5698c2ecf20Sopenharmony_ci	 * inserted at the end to data integrity check purpose.
5708c2ecf20Sopenharmony_ci	 * Hack 3: AF module writes one paxel data more than it should, so
5718c2ecf20Sopenharmony_ci	 * the buffer allocation must consider it to avoid invalid memory
5728c2ecf20Sopenharmony_ci	 * access.
5738c2ecf20Sopenharmony_ci	 * Hack 4: H3A need to allocate extra space for the recover state.
5748c2ecf20Sopenharmony_ci	 */
5758c2ecf20Sopenharmony_ci	if (IS_H3A(stat)) {
5768c2ecf20Sopenharmony_ci		buf_size = user_cfg->buf_size * 2 + MAGIC_SIZE;
5778c2ecf20Sopenharmony_ci		if (IS_H3A_AF(stat))
5788c2ecf20Sopenharmony_ci			/*
5798c2ecf20Sopenharmony_ci			 * Adding one extra paxel data size for each recover
5808c2ecf20Sopenharmony_ci			 * buffer + 2 regular ones.
5818c2ecf20Sopenharmony_ci			 */
5828c2ecf20Sopenharmony_ci			buf_size += AF_EXTRA_DATA * (NUM_H3A_RECOVER_BUFS + 2);
5838c2ecf20Sopenharmony_ci		if (stat->recover_priv) {
5848c2ecf20Sopenharmony_ci			struct ispstat_generic_config *recover_cfg =
5858c2ecf20Sopenharmony_ci				stat->recover_priv;
5868c2ecf20Sopenharmony_ci			buf_size += recover_cfg->buf_size *
5878c2ecf20Sopenharmony_ci				    NUM_H3A_RECOVER_BUFS;
5888c2ecf20Sopenharmony_ci		}
5898c2ecf20Sopenharmony_ci		buf_size = PAGE_ALIGN(buf_size);
5908c2ecf20Sopenharmony_ci	} else { /* Histogram */
5918c2ecf20Sopenharmony_ci		buf_size = PAGE_ALIGN(user_cfg->buf_size + MAGIC_SIZE);
5928c2ecf20Sopenharmony_ci	}
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	ret = isp_stat_bufs_alloc(stat, buf_size);
5958c2ecf20Sopenharmony_ci	if (ret) {
5968c2ecf20Sopenharmony_ci		mutex_unlock(&stat->ioctl_lock);
5978c2ecf20Sopenharmony_ci		return ret;
5988c2ecf20Sopenharmony_ci	}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
6018c2ecf20Sopenharmony_ci	stat->ops->set_params(stat, new_conf);
6028c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	/*
6058c2ecf20Sopenharmony_ci	 * Returning the right future config_counter for this setup, so
6068c2ecf20Sopenharmony_ci	 * userspace can *know* when it has been applied.
6078c2ecf20Sopenharmony_ci	 */
6088c2ecf20Sopenharmony_ci	user_cfg->config_counter = stat->config_counter + stat->inc_config;
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	/* Module has a valid configuration. */
6118c2ecf20Sopenharmony_ci	stat->configured = 1;
6128c2ecf20Sopenharmony_ci	dev_dbg(stat->isp->dev,
6138c2ecf20Sopenharmony_ci		"%s: module has been successfully configured.\n",
6148c2ecf20Sopenharmony_ci		stat->subdev.name);
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	mutex_unlock(&stat->ioctl_lock);
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	return 0;
6198c2ecf20Sopenharmony_ci}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci/*
6228c2ecf20Sopenharmony_ci * isp_stat_buf_process - Process statistic buffers.
6238c2ecf20Sopenharmony_ci * @buf_state: points out if buffer is ready to be processed. It's necessary
6248c2ecf20Sopenharmony_ci *	       because histogram needs to copy the data from internal memory
6258c2ecf20Sopenharmony_ci *	       before be able to process the buffer.
6268c2ecf20Sopenharmony_ci */
6278c2ecf20Sopenharmony_cistatic int isp_stat_buf_process(struct ispstat *stat, int buf_state)
6288c2ecf20Sopenharmony_ci{
6298c2ecf20Sopenharmony_ci	int ret = STAT_NO_BUF;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	if (!atomic_add_unless(&stat->buf_err, -1, 0) &&
6328c2ecf20Sopenharmony_ci	    buf_state == STAT_BUF_DONE && stat->state == ISPSTAT_ENABLED) {
6338c2ecf20Sopenharmony_ci		ret = isp_stat_buf_queue(stat);
6348c2ecf20Sopenharmony_ci		isp_stat_buf_next(stat);
6358c2ecf20Sopenharmony_ci	}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	return ret;
6388c2ecf20Sopenharmony_ci}
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ciint omap3isp_stat_pcr_busy(struct ispstat *stat)
6418c2ecf20Sopenharmony_ci{
6428c2ecf20Sopenharmony_ci	return stat->ops->busy(stat);
6438c2ecf20Sopenharmony_ci}
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ciint omap3isp_stat_busy(struct ispstat *stat)
6468c2ecf20Sopenharmony_ci{
6478c2ecf20Sopenharmony_ci	return omap3isp_stat_pcr_busy(stat) | stat->buf_processing |
6488c2ecf20Sopenharmony_ci		(stat->state != ISPSTAT_DISABLED);
6498c2ecf20Sopenharmony_ci}
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci/*
6528c2ecf20Sopenharmony_ci * isp_stat_pcr_enable - Disables/Enables statistic engines.
6538c2ecf20Sopenharmony_ci * @pcr_enable: 0/1 - Disables/Enables the engine.
6548c2ecf20Sopenharmony_ci *
6558c2ecf20Sopenharmony_ci * Must be called from ISP driver when the module is idle and synchronized
6568c2ecf20Sopenharmony_ci * with CCDC.
6578c2ecf20Sopenharmony_ci */
6588c2ecf20Sopenharmony_cistatic void isp_stat_pcr_enable(struct ispstat *stat, u8 pcr_enable)
6598c2ecf20Sopenharmony_ci{
6608c2ecf20Sopenharmony_ci	if ((stat->state != ISPSTAT_ENABLING &&
6618c2ecf20Sopenharmony_ci	     stat->state != ISPSTAT_ENABLED) && pcr_enable)
6628c2ecf20Sopenharmony_ci		/* Userspace has disabled the module. Aborting. */
6638c2ecf20Sopenharmony_ci		return;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	stat->ops->enable(stat, pcr_enable);
6668c2ecf20Sopenharmony_ci	if (stat->state == ISPSTAT_DISABLING && !pcr_enable)
6678c2ecf20Sopenharmony_ci		stat->state = ISPSTAT_DISABLED;
6688c2ecf20Sopenharmony_ci	else if (stat->state == ISPSTAT_ENABLING && pcr_enable)
6698c2ecf20Sopenharmony_ci		stat->state = ISPSTAT_ENABLED;
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_civoid omap3isp_stat_suspend(struct ispstat *stat)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci	unsigned long flags;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	spin_lock_irqsave(&stat->isp->stat_lock, flags);
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	if (stat->state != ISPSTAT_DISABLED)
6798c2ecf20Sopenharmony_ci		stat->ops->enable(stat, 0);
6808c2ecf20Sopenharmony_ci	if (stat->state == ISPSTAT_ENABLED)
6818c2ecf20Sopenharmony_ci		stat->state = ISPSTAT_SUSPENDED;
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
6848c2ecf20Sopenharmony_ci}
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_civoid omap3isp_stat_resume(struct ispstat *stat)
6878c2ecf20Sopenharmony_ci{
6888c2ecf20Sopenharmony_ci	/* Module will be re-enabled with its pipeline */
6898c2ecf20Sopenharmony_ci	if (stat->state == ISPSTAT_SUSPENDED)
6908c2ecf20Sopenharmony_ci		stat->state = ISPSTAT_ENABLING;
6918c2ecf20Sopenharmony_ci}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_cistatic void isp_stat_try_enable(struct ispstat *stat)
6948c2ecf20Sopenharmony_ci{
6958c2ecf20Sopenharmony_ci	unsigned long irqflags;
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	if (stat->priv == NULL)
6988c2ecf20Sopenharmony_ci		/* driver wasn't initialised */
6998c2ecf20Sopenharmony_ci		return;
7008c2ecf20Sopenharmony_ci
7018c2ecf20Sopenharmony_ci	spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
7028c2ecf20Sopenharmony_ci	if (stat->state == ISPSTAT_ENABLING && !stat->buf_processing &&
7038c2ecf20Sopenharmony_ci	    stat->buf_alloc_size) {
7048c2ecf20Sopenharmony_ci		/*
7058c2ecf20Sopenharmony_ci		 * Userspace's requested to enable the engine but it wasn't yet.
7068c2ecf20Sopenharmony_ci		 * Let's do that now.
7078c2ecf20Sopenharmony_ci		 */
7088c2ecf20Sopenharmony_ci		stat->update = 1;
7098c2ecf20Sopenharmony_ci		isp_stat_buf_next(stat);
7108c2ecf20Sopenharmony_ci		stat->ops->setup_regs(stat, stat->priv);
7118c2ecf20Sopenharmony_ci		isp_stat_buf_insert_magic(stat, stat->active_buf);
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci		/*
7148c2ecf20Sopenharmony_ci		 * H3A module has some hw issues which forces the driver to
7158c2ecf20Sopenharmony_ci		 * ignore next buffers even if it was disabled in the meantime.
7168c2ecf20Sopenharmony_ci		 * On the other hand, Histogram shouldn't ignore buffers anymore
7178c2ecf20Sopenharmony_ci		 * if it's being enabled.
7188c2ecf20Sopenharmony_ci		 */
7198c2ecf20Sopenharmony_ci		if (!IS_H3A(stat))
7208c2ecf20Sopenharmony_ci			atomic_set(&stat->buf_err, 0);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci		isp_stat_pcr_enable(stat, 1);
7238c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
7248c2ecf20Sopenharmony_ci		dev_dbg(stat->isp->dev, "%s: module is enabled.\n",
7258c2ecf20Sopenharmony_ci			stat->subdev.name);
7268c2ecf20Sopenharmony_ci	} else {
7278c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
7288c2ecf20Sopenharmony_ci	}
7298c2ecf20Sopenharmony_ci}
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_civoid omap3isp_stat_isr_frame_sync(struct ispstat *stat)
7328c2ecf20Sopenharmony_ci{
7338c2ecf20Sopenharmony_ci	isp_stat_try_enable(stat);
7348c2ecf20Sopenharmony_ci}
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_civoid omap3isp_stat_sbl_overflow(struct ispstat *stat)
7378c2ecf20Sopenharmony_ci{
7388c2ecf20Sopenharmony_ci	unsigned long irqflags;
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
7418c2ecf20Sopenharmony_ci	/*
7428c2ecf20Sopenharmony_ci	 * Due to a H3A hw issue which prevents the next buffer to start from
7438c2ecf20Sopenharmony_ci	 * the correct memory address, 2 buffers must be ignored.
7448c2ecf20Sopenharmony_ci	 */
7458c2ecf20Sopenharmony_ci	atomic_set(&stat->buf_err, 2);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	/*
7488c2ecf20Sopenharmony_ci	 * If more than one SBL overflow happen in a row, H3A module may access
7498c2ecf20Sopenharmony_ci	 * invalid memory region.
7508c2ecf20Sopenharmony_ci	 * stat->sbl_ovl_recover is set to tell to the driver to temporarily use
7518c2ecf20Sopenharmony_ci	 * a soft configuration which helps to avoid consecutive overflows.
7528c2ecf20Sopenharmony_ci	 */
7538c2ecf20Sopenharmony_ci	if (stat->recover_priv)
7548c2ecf20Sopenharmony_ci		stat->sbl_ovl_recover = 1;
7558c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
7568c2ecf20Sopenharmony_ci}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci/*
7598c2ecf20Sopenharmony_ci * omap3isp_stat_enable - Disable/Enable statistic engine as soon as possible
7608c2ecf20Sopenharmony_ci * @enable: 0/1 - Disables/Enables the engine.
7618c2ecf20Sopenharmony_ci *
7628c2ecf20Sopenharmony_ci * Client should configure all the module registers before this.
7638c2ecf20Sopenharmony_ci * This function can be called from a userspace request.
7648c2ecf20Sopenharmony_ci */
7658c2ecf20Sopenharmony_ciint omap3isp_stat_enable(struct ispstat *stat, u8 enable)
7668c2ecf20Sopenharmony_ci{
7678c2ecf20Sopenharmony_ci	unsigned long irqflags;
7688c2ecf20Sopenharmony_ci
7698c2ecf20Sopenharmony_ci	dev_dbg(stat->isp->dev, "%s: user wants to %s module.\n",
7708c2ecf20Sopenharmony_ci		stat->subdev.name, enable ? "enable" : "disable");
7718c2ecf20Sopenharmony_ci
7728c2ecf20Sopenharmony_ci	/* Prevent enabling while configuring */
7738c2ecf20Sopenharmony_ci	mutex_lock(&stat->ioctl_lock);
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	if (!stat->configured && enable) {
7788c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
7798c2ecf20Sopenharmony_ci		mutex_unlock(&stat->ioctl_lock);
7808c2ecf20Sopenharmony_ci		dev_dbg(stat->isp->dev,
7818c2ecf20Sopenharmony_ci			"%s: cannot enable module as it's never been successfully configured so far.\n",
7828c2ecf20Sopenharmony_ci			stat->subdev.name);
7838c2ecf20Sopenharmony_ci		return -EINVAL;
7848c2ecf20Sopenharmony_ci	}
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	if (enable) {
7878c2ecf20Sopenharmony_ci		if (stat->state == ISPSTAT_DISABLING)
7888c2ecf20Sopenharmony_ci			/* Previous disabling request wasn't done yet */
7898c2ecf20Sopenharmony_ci			stat->state = ISPSTAT_ENABLED;
7908c2ecf20Sopenharmony_ci		else if (stat->state == ISPSTAT_DISABLED)
7918c2ecf20Sopenharmony_ci			/* Module is now being enabled */
7928c2ecf20Sopenharmony_ci			stat->state = ISPSTAT_ENABLING;
7938c2ecf20Sopenharmony_ci	} else {
7948c2ecf20Sopenharmony_ci		if (stat->state == ISPSTAT_ENABLING) {
7958c2ecf20Sopenharmony_ci			/* Previous enabling request wasn't done yet */
7968c2ecf20Sopenharmony_ci			stat->state = ISPSTAT_DISABLED;
7978c2ecf20Sopenharmony_ci		} else if (stat->state == ISPSTAT_ENABLED) {
7988c2ecf20Sopenharmony_ci			/* Module is now being disabled */
7998c2ecf20Sopenharmony_ci			stat->state = ISPSTAT_DISABLING;
8008c2ecf20Sopenharmony_ci			isp_stat_buf_clear(stat);
8018c2ecf20Sopenharmony_ci		}
8028c2ecf20Sopenharmony_ci	}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
8058c2ecf20Sopenharmony_ci	mutex_unlock(&stat->ioctl_lock);
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	return 0;
8088c2ecf20Sopenharmony_ci}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ciint omap3isp_stat_s_stream(struct v4l2_subdev *subdev, int enable)
8118c2ecf20Sopenharmony_ci{
8128c2ecf20Sopenharmony_ci	struct ispstat *stat = v4l2_get_subdevdata(subdev);
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	if (enable) {
8158c2ecf20Sopenharmony_ci		/*
8168c2ecf20Sopenharmony_ci		 * Only set enable PCR bit if the module was previously
8178c2ecf20Sopenharmony_ci		 * enabled through ioctl.
8188c2ecf20Sopenharmony_ci		 */
8198c2ecf20Sopenharmony_ci		isp_stat_try_enable(stat);
8208c2ecf20Sopenharmony_ci	} else {
8218c2ecf20Sopenharmony_ci		unsigned long flags;
8228c2ecf20Sopenharmony_ci		/* Disable PCR bit and config enable field */
8238c2ecf20Sopenharmony_ci		omap3isp_stat_enable(stat, 0);
8248c2ecf20Sopenharmony_ci		spin_lock_irqsave(&stat->isp->stat_lock, flags);
8258c2ecf20Sopenharmony_ci		stat->ops->enable(stat, 0);
8268c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
8278c2ecf20Sopenharmony_ci
8288c2ecf20Sopenharmony_ci		/*
8298c2ecf20Sopenharmony_ci		 * If module isn't busy, a new interrupt may come or not to
8308c2ecf20Sopenharmony_ci		 * set the state to DISABLED. As Histogram needs to read its
8318c2ecf20Sopenharmony_ci		 * internal memory to clear it, let interrupt handler
8328c2ecf20Sopenharmony_ci		 * responsible of changing state to DISABLED. If the last
8338c2ecf20Sopenharmony_ci		 * interrupt is coming, it's still safe as the handler will
8348c2ecf20Sopenharmony_ci		 * ignore the second time when state is already set to DISABLED.
8358c2ecf20Sopenharmony_ci		 * It's necessary to synchronize Histogram with streamoff, once
8368c2ecf20Sopenharmony_ci		 * the module may be considered idle before last SDMA transfer
8378c2ecf20Sopenharmony_ci		 * starts if we return here.
8388c2ecf20Sopenharmony_ci		 */
8398c2ecf20Sopenharmony_ci		if (!omap3isp_stat_pcr_busy(stat))
8408c2ecf20Sopenharmony_ci			omap3isp_stat_isr(stat);
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci		dev_dbg(stat->isp->dev, "%s: module is being disabled\n",
8438c2ecf20Sopenharmony_ci			stat->subdev.name);
8448c2ecf20Sopenharmony_ci	}
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	return 0;
8478c2ecf20Sopenharmony_ci}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci/*
8508c2ecf20Sopenharmony_ci * __stat_isr - Interrupt handler for statistic drivers
8518c2ecf20Sopenharmony_ci */
8528c2ecf20Sopenharmony_cistatic void __stat_isr(struct ispstat *stat, int from_dma)
8538c2ecf20Sopenharmony_ci{
8548c2ecf20Sopenharmony_ci	int ret = STAT_BUF_DONE;
8558c2ecf20Sopenharmony_ci	int buf_processing;
8568c2ecf20Sopenharmony_ci	unsigned long irqflags;
8578c2ecf20Sopenharmony_ci	struct isp_pipeline *pipe;
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	/*
8608c2ecf20Sopenharmony_ci	 * stat->buf_processing must be set before disable module. It's
8618c2ecf20Sopenharmony_ci	 * necessary to not inform too early the buffers aren't busy in case
8628c2ecf20Sopenharmony_ci	 * of SDMA is going to be used.
8638c2ecf20Sopenharmony_ci	 */
8648c2ecf20Sopenharmony_ci	spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
8658c2ecf20Sopenharmony_ci	if (stat->state == ISPSTAT_DISABLED) {
8668c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
8678c2ecf20Sopenharmony_ci		return;
8688c2ecf20Sopenharmony_ci	}
8698c2ecf20Sopenharmony_ci	buf_processing = stat->buf_processing;
8708c2ecf20Sopenharmony_ci	stat->buf_processing = 1;
8718c2ecf20Sopenharmony_ci	stat->ops->enable(stat, 0);
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_ci	if (buf_processing && !from_dma) {
8748c2ecf20Sopenharmony_ci		if (stat->state == ISPSTAT_ENABLED) {
8758c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
8768c2ecf20Sopenharmony_ci			dev_err(stat->isp->dev,
8778c2ecf20Sopenharmony_ci				"%s: interrupt occurred when module was still processing a buffer.\n",
8788c2ecf20Sopenharmony_ci				stat->subdev.name);
8798c2ecf20Sopenharmony_ci			ret = STAT_NO_BUF;
8808c2ecf20Sopenharmony_ci			goto out;
8818c2ecf20Sopenharmony_ci		} else {
8828c2ecf20Sopenharmony_ci			/*
8838c2ecf20Sopenharmony_ci			 * Interrupt handler was called from streamoff when
8848c2ecf20Sopenharmony_ci			 * the module wasn't busy anymore to ensure it is being
8858c2ecf20Sopenharmony_ci			 * disabled after process last buffer. If such buffer
8868c2ecf20Sopenharmony_ci			 * processing has already started, no need to do
8878c2ecf20Sopenharmony_ci			 * anything else.
8888c2ecf20Sopenharmony_ci			 */
8898c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
8908c2ecf20Sopenharmony_ci			return;
8918c2ecf20Sopenharmony_ci		}
8928c2ecf20Sopenharmony_ci	}
8938c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	/* If it's busy we can't process this buffer anymore */
8968c2ecf20Sopenharmony_ci	if (!omap3isp_stat_pcr_busy(stat)) {
8978c2ecf20Sopenharmony_ci		if (!from_dma && stat->ops->buf_process)
8988c2ecf20Sopenharmony_ci			/* Module still need to copy data to buffer. */
8998c2ecf20Sopenharmony_ci			ret = stat->ops->buf_process(stat);
9008c2ecf20Sopenharmony_ci		if (ret == STAT_BUF_WAITING_DMA)
9018c2ecf20Sopenharmony_ci			/* Buffer is not ready yet */
9028c2ecf20Sopenharmony_ci			return;
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci		spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci		/*
9078c2ecf20Sopenharmony_ci		 * Histogram needs to read its internal memory to clear it
9088c2ecf20Sopenharmony_ci		 * before be disabled. For that reason, common statistic layer
9098c2ecf20Sopenharmony_ci		 * can return only after call stat's buf_process() operator.
9108c2ecf20Sopenharmony_ci		 */
9118c2ecf20Sopenharmony_ci		if (stat->state == ISPSTAT_DISABLING) {
9128c2ecf20Sopenharmony_ci			stat->state = ISPSTAT_DISABLED;
9138c2ecf20Sopenharmony_ci			spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
9148c2ecf20Sopenharmony_ci			stat->buf_processing = 0;
9158c2ecf20Sopenharmony_ci			return;
9168c2ecf20Sopenharmony_ci		}
9178c2ecf20Sopenharmony_ci		pipe = to_isp_pipeline(&stat->subdev.entity);
9188c2ecf20Sopenharmony_ci		stat->frame_number = atomic_read(&pipe->frame_number);
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_ci		/*
9218c2ecf20Sopenharmony_ci		 * Before this point, 'ret' stores the buffer's status if it's
9228c2ecf20Sopenharmony_ci		 * ready to be processed. Afterwards, it holds the status if
9238c2ecf20Sopenharmony_ci		 * it was processed successfully.
9248c2ecf20Sopenharmony_ci		 */
9258c2ecf20Sopenharmony_ci		ret = isp_stat_buf_process(stat, ret);
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci		if (likely(!stat->sbl_ovl_recover)) {
9288c2ecf20Sopenharmony_ci			stat->ops->setup_regs(stat, stat->priv);
9298c2ecf20Sopenharmony_ci		} else {
9308c2ecf20Sopenharmony_ci			/*
9318c2ecf20Sopenharmony_ci			 * Using recover config to increase the chance to have
9328c2ecf20Sopenharmony_ci			 * a good buffer processing and make the H3A module to
9338c2ecf20Sopenharmony_ci			 * go back to a valid state.
9348c2ecf20Sopenharmony_ci			 */
9358c2ecf20Sopenharmony_ci			stat->update = 1;
9368c2ecf20Sopenharmony_ci			stat->ops->setup_regs(stat, stat->recover_priv);
9378c2ecf20Sopenharmony_ci			stat->sbl_ovl_recover = 0;
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci			/*
9408c2ecf20Sopenharmony_ci			 * Set 'update' in case of the module needs to use
9418c2ecf20Sopenharmony_ci			 * regular configuration after next buffer.
9428c2ecf20Sopenharmony_ci			 */
9438c2ecf20Sopenharmony_ci			stat->update = 1;
9448c2ecf20Sopenharmony_ci		}
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci		isp_stat_buf_insert_magic(stat, stat->active_buf);
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci		/*
9498c2ecf20Sopenharmony_ci		 * Hack: H3A modules may access invalid memory address or send
9508c2ecf20Sopenharmony_ci		 * corrupted data to userspace if more than 1 SBL overflow
9518c2ecf20Sopenharmony_ci		 * happens in a row without re-writing its buffer's start memory
9528c2ecf20Sopenharmony_ci		 * address in the meantime. Such situation is avoided if the
9538c2ecf20Sopenharmony_ci		 * module is not immediately re-enabled when the ISR misses the
9548c2ecf20Sopenharmony_ci		 * timing to process the buffer and to setup the registers.
9558c2ecf20Sopenharmony_ci		 * Because of that, pcr_enable(1) was moved to inside this 'if'
9568c2ecf20Sopenharmony_ci		 * block. But the next interruption will still happen as during
9578c2ecf20Sopenharmony_ci		 * pcr_enable(0) the module was busy.
9588c2ecf20Sopenharmony_ci		 */
9598c2ecf20Sopenharmony_ci		isp_stat_pcr_enable(stat, 1);
9608c2ecf20Sopenharmony_ci		spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
9618c2ecf20Sopenharmony_ci	} else {
9628c2ecf20Sopenharmony_ci		/*
9638c2ecf20Sopenharmony_ci		 * If a SBL overflow occurs and the H3A driver misses the timing
9648c2ecf20Sopenharmony_ci		 * to process the buffer, stat->buf_err is set and won't be
9658c2ecf20Sopenharmony_ci		 * cleared now. So the next buffer will be correctly ignored.
9668c2ecf20Sopenharmony_ci		 * It's necessary due to a hw issue which makes the next H3A
9678c2ecf20Sopenharmony_ci		 * buffer to start from the memory address where the previous
9688c2ecf20Sopenharmony_ci		 * one stopped, instead of start where it was configured to.
9698c2ecf20Sopenharmony_ci		 * Do not "stat->buf_err = 0" here.
9708c2ecf20Sopenharmony_ci		 */
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci		if (stat->ops->buf_process)
9738c2ecf20Sopenharmony_ci			/*
9748c2ecf20Sopenharmony_ci			 * Driver may need to erase current data prior to
9758c2ecf20Sopenharmony_ci			 * process a new buffer. If it misses the timing, the
9768c2ecf20Sopenharmony_ci			 * next buffer might be wrong. So should be ignored.
9778c2ecf20Sopenharmony_ci			 * It happens only for Histogram.
9788c2ecf20Sopenharmony_ci			 */
9798c2ecf20Sopenharmony_ci			atomic_set(&stat->buf_err, 1);
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci		ret = STAT_NO_BUF;
9828c2ecf20Sopenharmony_ci		dev_dbg(stat->isp->dev,
9838c2ecf20Sopenharmony_ci			"%s: cannot process buffer, device is busy.\n",
9848c2ecf20Sopenharmony_ci			stat->subdev.name);
9858c2ecf20Sopenharmony_ci	}
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_ciout:
9888c2ecf20Sopenharmony_ci	stat->buf_processing = 0;
9898c2ecf20Sopenharmony_ci	isp_stat_queue_event(stat, ret != STAT_BUF_DONE);
9908c2ecf20Sopenharmony_ci}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_civoid omap3isp_stat_isr(struct ispstat *stat)
9938c2ecf20Sopenharmony_ci{
9948c2ecf20Sopenharmony_ci	__stat_isr(stat, 0);
9958c2ecf20Sopenharmony_ci}
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_civoid omap3isp_stat_dma_isr(struct ispstat *stat)
9988c2ecf20Sopenharmony_ci{
9998c2ecf20Sopenharmony_ci	__stat_isr(stat, 1);
10008c2ecf20Sopenharmony_ci}
10018c2ecf20Sopenharmony_ci
10028c2ecf20Sopenharmony_ciint omap3isp_stat_subscribe_event(struct v4l2_subdev *subdev,
10038c2ecf20Sopenharmony_ci				  struct v4l2_fh *fh,
10048c2ecf20Sopenharmony_ci				  struct v4l2_event_subscription *sub)
10058c2ecf20Sopenharmony_ci{
10068c2ecf20Sopenharmony_ci	struct ispstat *stat = v4l2_get_subdevdata(subdev);
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	if (sub->type != stat->event_type)
10098c2ecf20Sopenharmony_ci		return -EINVAL;
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ci	return v4l2_event_subscribe(fh, sub, STAT_NEVENTS, NULL);
10128c2ecf20Sopenharmony_ci}
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ciint omap3isp_stat_unsubscribe_event(struct v4l2_subdev *subdev,
10158c2ecf20Sopenharmony_ci				    struct v4l2_fh *fh,
10168c2ecf20Sopenharmony_ci				    struct v4l2_event_subscription *sub)
10178c2ecf20Sopenharmony_ci{
10188c2ecf20Sopenharmony_ci	return v4l2_event_unsubscribe(fh, sub);
10198c2ecf20Sopenharmony_ci}
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_civoid omap3isp_stat_unregister_entities(struct ispstat *stat)
10228c2ecf20Sopenharmony_ci{
10238c2ecf20Sopenharmony_ci	v4l2_device_unregister_subdev(&stat->subdev);
10248c2ecf20Sopenharmony_ci}
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ciint omap3isp_stat_register_entities(struct ispstat *stat,
10278c2ecf20Sopenharmony_ci				    struct v4l2_device *vdev)
10288c2ecf20Sopenharmony_ci{
10298c2ecf20Sopenharmony_ci	stat->subdev.dev = vdev->mdev->dev;
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_ci	return v4l2_device_register_subdev(vdev, &stat->subdev);
10328c2ecf20Sopenharmony_ci}
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_cistatic int isp_stat_init_entities(struct ispstat *stat, const char *name,
10358c2ecf20Sopenharmony_ci				  const struct v4l2_subdev_ops *sd_ops)
10368c2ecf20Sopenharmony_ci{
10378c2ecf20Sopenharmony_ci	struct v4l2_subdev *subdev = &stat->subdev;
10388c2ecf20Sopenharmony_ci	struct media_entity *me = &subdev->entity;
10398c2ecf20Sopenharmony_ci
10408c2ecf20Sopenharmony_ci	v4l2_subdev_init(subdev, sd_ops);
10418c2ecf20Sopenharmony_ci	snprintf(subdev->name, V4L2_SUBDEV_NAME_SIZE, "OMAP3 ISP %s", name);
10428c2ecf20Sopenharmony_ci	subdev->grp_id = BIT(16);	/* group ID for isp subdevs */
10438c2ecf20Sopenharmony_ci	subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
10448c2ecf20Sopenharmony_ci	v4l2_set_subdevdata(subdev, stat);
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	stat->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
10478c2ecf20Sopenharmony_ci	me->ops = NULL;
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci	return media_entity_pads_init(me, 1, &stat->pad);
10508c2ecf20Sopenharmony_ci}
10518c2ecf20Sopenharmony_ci
10528c2ecf20Sopenharmony_ciint omap3isp_stat_init(struct ispstat *stat, const char *name,
10538c2ecf20Sopenharmony_ci		       const struct v4l2_subdev_ops *sd_ops)
10548c2ecf20Sopenharmony_ci{
10558c2ecf20Sopenharmony_ci	int ret;
10568c2ecf20Sopenharmony_ci
10578c2ecf20Sopenharmony_ci	stat->buf = kcalloc(STAT_MAX_BUFS, sizeof(*stat->buf), GFP_KERNEL);
10588c2ecf20Sopenharmony_ci	if (!stat->buf)
10598c2ecf20Sopenharmony_ci		return -ENOMEM;
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	isp_stat_buf_clear(stat);
10628c2ecf20Sopenharmony_ci	mutex_init(&stat->ioctl_lock);
10638c2ecf20Sopenharmony_ci	atomic_set(&stat->buf_err, 0);
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci	ret = isp_stat_init_entities(stat, name, sd_ops);
10668c2ecf20Sopenharmony_ci	if (ret < 0) {
10678c2ecf20Sopenharmony_ci		mutex_destroy(&stat->ioctl_lock);
10688c2ecf20Sopenharmony_ci		kfree(stat->buf);
10698c2ecf20Sopenharmony_ci	}
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	return ret;
10728c2ecf20Sopenharmony_ci}
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_civoid omap3isp_stat_cleanup(struct ispstat *stat)
10758c2ecf20Sopenharmony_ci{
10768c2ecf20Sopenharmony_ci	media_entity_cleanup(&stat->subdev.entity);
10778c2ecf20Sopenharmony_ci	mutex_destroy(&stat->ioctl_lock);
10788c2ecf20Sopenharmony_ci	isp_stat_bufs_free(stat);
10798c2ecf20Sopenharmony_ci	kfree(stat->buf);
10808c2ecf20Sopenharmony_ci	kfree(stat->priv);
10818c2ecf20Sopenharmony_ci	kfree(stat->recover_priv);
10828c2ecf20Sopenharmony_ci}
1083