xref: /linux/drivers/media/platform/ti/omap3isp/ispstat.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
1ceafdaacSMauro Carvalho Chehab // SPDX-License-Identifier: GPL-2.0-only
2ceafdaacSMauro Carvalho Chehab /*
3ceafdaacSMauro Carvalho Chehab  * ispstat.c
4ceafdaacSMauro Carvalho Chehab  *
5ceafdaacSMauro Carvalho Chehab  * TI OMAP3 ISP - Statistics core
6ceafdaacSMauro Carvalho Chehab  *
7ceafdaacSMauro Carvalho Chehab  * Copyright (C) 2010 Nokia Corporation
8ceafdaacSMauro Carvalho Chehab  * Copyright (C) 2009 Texas Instruments, Inc
9ceafdaacSMauro Carvalho Chehab  *
10ceafdaacSMauro Carvalho Chehab  * Contacts: David Cohen <dacohen@gmail.com>
11ceafdaacSMauro Carvalho Chehab  *	     Laurent Pinchart <laurent.pinchart@ideasonboard.com>
12ceafdaacSMauro Carvalho Chehab  *	     Sakari Ailus <sakari.ailus@iki.fi>
13ceafdaacSMauro Carvalho Chehab  */
14ceafdaacSMauro Carvalho Chehab 
15ceafdaacSMauro Carvalho Chehab #include <linux/dma-mapping.h>
16ceafdaacSMauro Carvalho Chehab #include <linux/slab.h>
17ceafdaacSMauro Carvalho Chehab #include <linux/timekeeping.h>
18ceafdaacSMauro Carvalho Chehab #include <linux/uaccess.h>
19ceafdaacSMauro Carvalho Chehab 
20ceafdaacSMauro Carvalho Chehab #include "isp.h"
21ceafdaacSMauro Carvalho Chehab 
22ceafdaacSMauro Carvalho Chehab #define ISP_STAT_USES_DMAENGINE(stat)	((stat)->dma_ch != NULL)
23ceafdaacSMauro Carvalho Chehab 
24ceafdaacSMauro Carvalho Chehab /*
25ceafdaacSMauro Carvalho Chehab  * MAGIC_SIZE must always be the greatest common divisor of
26ceafdaacSMauro Carvalho Chehab  * AEWB_PACKET_SIZE and AF_PAXEL_SIZE.
27ceafdaacSMauro Carvalho Chehab  */
28ceafdaacSMauro Carvalho Chehab #define MAGIC_SIZE		16
29ceafdaacSMauro Carvalho Chehab #define MAGIC_NUM		0x55
30ceafdaacSMauro Carvalho Chehab 
31ceafdaacSMauro Carvalho Chehab /* HACK: AF module seems to be writing one more paxel data than it should. */
32ceafdaacSMauro Carvalho Chehab #define AF_EXTRA_DATA		OMAP3ISP_AF_PAXEL_SIZE
33ceafdaacSMauro Carvalho Chehab 
34ceafdaacSMauro Carvalho Chehab /*
35ceafdaacSMauro Carvalho Chehab  * HACK: H3A modules go to an invalid state after have a SBL overflow. It makes
36ceafdaacSMauro Carvalho Chehab  * the next buffer to start to be written in the same point where the overflow
37ceafdaacSMauro Carvalho Chehab  * occurred instead of the configured address. The only known way to make it to
38ceafdaacSMauro Carvalho Chehab  * go back to a valid state is having a valid buffer processing. Of course it
39ceafdaacSMauro Carvalho Chehab  * requires at least a doubled buffer size to avoid an access to invalid memory
40ceafdaacSMauro Carvalho Chehab  * region. But it does not fix everything. It may happen more than one
41ceafdaacSMauro Carvalho Chehab  * consecutive SBL overflows. In that case, it might be unpredictable how many
42ceafdaacSMauro Carvalho Chehab  * buffers the allocated memory should fit. For that case, a recover
43ceafdaacSMauro Carvalho Chehab  * configuration was created. It produces the minimum buffer size for each H3A
44ceafdaacSMauro Carvalho Chehab  * module and decrease the change for more SBL overflows. This recover state
45ceafdaacSMauro Carvalho Chehab  * will be enabled every time a SBL overflow occur. As the output buffer size
46ceafdaacSMauro Carvalho Chehab  * isn't big, it's possible to have an extra size able to fit many recover
47ceafdaacSMauro Carvalho Chehab  * buffers making it extreamily unlikely to have an access to invalid memory
48ceafdaacSMauro Carvalho Chehab  * region.
49ceafdaacSMauro Carvalho Chehab  */
50ceafdaacSMauro Carvalho Chehab #define NUM_H3A_RECOVER_BUFS	10
51ceafdaacSMauro Carvalho Chehab 
52ceafdaacSMauro Carvalho Chehab /*
53ceafdaacSMauro Carvalho Chehab  * HACK: Because of HW issues the generic layer sometimes need to have
54ceafdaacSMauro Carvalho Chehab  * different behaviour for different statistic modules.
55ceafdaacSMauro Carvalho Chehab  */
56ceafdaacSMauro Carvalho Chehab #define IS_H3A_AF(stat)		((stat) == &(stat)->isp->isp_af)
57ceafdaacSMauro Carvalho Chehab #define IS_H3A_AEWB(stat)	((stat) == &(stat)->isp->isp_aewb)
58ceafdaacSMauro Carvalho Chehab #define IS_H3A(stat)		(IS_H3A_AF(stat) || IS_H3A_AEWB(stat))
59ceafdaacSMauro Carvalho Chehab 
__isp_stat_buf_sync_magic(struct ispstat * stat,struct ispstat_buffer * buf,u32 buf_size,enum dma_data_direction dir,void (* dma_sync)(struct device *,dma_addr_t,unsigned long,size_t,enum dma_data_direction))60ceafdaacSMauro Carvalho Chehab static void __isp_stat_buf_sync_magic(struct ispstat *stat,
61ceafdaacSMauro Carvalho Chehab 				      struct ispstat_buffer *buf,
62ceafdaacSMauro Carvalho Chehab 				      u32 buf_size, enum dma_data_direction dir,
63ceafdaacSMauro Carvalho Chehab 				      void (*dma_sync)(struct device *,
64ceafdaacSMauro Carvalho Chehab 					dma_addr_t, unsigned long, size_t,
65ceafdaacSMauro Carvalho Chehab 					enum dma_data_direction))
66ceafdaacSMauro Carvalho Chehab {
67ceafdaacSMauro Carvalho Chehab 	/* Sync the initial and final magic words. */
68ceafdaacSMauro Carvalho Chehab 	dma_sync(stat->isp->dev, buf->dma_addr, 0, MAGIC_SIZE, dir);
69ceafdaacSMauro Carvalho Chehab 	dma_sync(stat->isp->dev, buf->dma_addr + (buf_size & PAGE_MASK),
70ceafdaacSMauro Carvalho Chehab 		 buf_size & ~PAGE_MASK, MAGIC_SIZE, dir);
71ceafdaacSMauro Carvalho Chehab }
72ceafdaacSMauro Carvalho Chehab 
isp_stat_buf_sync_magic_for_device(struct ispstat * stat,struct ispstat_buffer * buf,u32 buf_size,enum dma_data_direction dir)73ceafdaacSMauro Carvalho Chehab static void isp_stat_buf_sync_magic_for_device(struct ispstat *stat,
74ceafdaacSMauro Carvalho Chehab 					       struct ispstat_buffer *buf,
75ceafdaacSMauro Carvalho Chehab 					       u32 buf_size,
76ceafdaacSMauro Carvalho Chehab 					       enum dma_data_direction dir)
77ceafdaacSMauro Carvalho Chehab {
78ceafdaacSMauro Carvalho Chehab 	if (ISP_STAT_USES_DMAENGINE(stat))
79ceafdaacSMauro Carvalho Chehab 		return;
80ceafdaacSMauro Carvalho Chehab 
81ceafdaacSMauro Carvalho Chehab 	__isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
82ceafdaacSMauro Carvalho Chehab 				  dma_sync_single_range_for_device);
83ceafdaacSMauro Carvalho Chehab }
84ceafdaacSMauro Carvalho Chehab 
isp_stat_buf_sync_magic_for_cpu(struct ispstat * stat,struct ispstat_buffer * buf,u32 buf_size,enum dma_data_direction dir)85ceafdaacSMauro Carvalho Chehab static void isp_stat_buf_sync_magic_for_cpu(struct ispstat *stat,
86ceafdaacSMauro Carvalho Chehab 					    struct ispstat_buffer *buf,
87ceafdaacSMauro Carvalho Chehab 					    u32 buf_size,
88ceafdaacSMauro Carvalho Chehab 					    enum dma_data_direction dir)
89ceafdaacSMauro Carvalho Chehab {
90ceafdaacSMauro Carvalho Chehab 	if (ISP_STAT_USES_DMAENGINE(stat))
91ceafdaacSMauro Carvalho Chehab 		return;
92ceafdaacSMauro Carvalho Chehab 
93ceafdaacSMauro Carvalho Chehab 	__isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
94ceafdaacSMauro Carvalho Chehab 				  dma_sync_single_range_for_cpu);
95ceafdaacSMauro Carvalho Chehab }
96ceafdaacSMauro Carvalho Chehab 
isp_stat_buf_check_magic(struct ispstat * stat,struct ispstat_buffer * buf)97ceafdaacSMauro Carvalho Chehab static int isp_stat_buf_check_magic(struct ispstat *stat,
98ceafdaacSMauro Carvalho Chehab 				    struct ispstat_buffer *buf)
99ceafdaacSMauro Carvalho Chehab {
100ceafdaacSMauro Carvalho Chehab 	const u32 buf_size = IS_H3A_AF(stat) ?
101ceafdaacSMauro Carvalho Chehab 			     buf->buf_size + AF_EXTRA_DATA : buf->buf_size;
102ceafdaacSMauro Carvalho Chehab 	u8 *w;
103ceafdaacSMauro Carvalho Chehab 	u8 *end;
104ceafdaacSMauro Carvalho Chehab 	int ret = -EINVAL;
105ceafdaacSMauro Carvalho Chehab 
106ceafdaacSMauro Carvalho Chehab 	isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
107ceafdaacSMauro Carvalho Chehab 
108ceafdaacSMauro Carvalho Chehab 	/* Checking initial magic numbers. They shouldn't be here anymore. */
109ceafdaacSMauro Carvalho Chehab 	for (w = buf->virt_addr, end = w + MAGIC_SIZE; w < end; w++)
110ceafdaacSMauro Carvalho Chehab 		if (likely(*w != MAGIC_NUM))
111ceafdaacSMauro Carvalho Chehab 			ret = 0;
112ceafdaacSMauro Carvalho Chehab 
113ceafdaacSMauro Carvalho Chehab 	if (ret) {
114ceafdaacSMauro Carvalho Chehab 		dev_dbg(stat->isp->dev,
115ceafdaacSMauro Carvalho Chehab 			"%s: beginning magic check does not match.\n",
116ceafdaacSMauro Carvalho Chehab 			stat->subdev.name);
117ceafdaacSMauro Carvalho Chehab 		return ret;
118ceafdaacSMauro Carvalho Chehab 	}
119ceafdaacSMauro Carvalho Chehab 
120ceafdaacSMauro Carvalho Chehab 	/* Checking magic numbers at the end. They must be still here. */
121ceafdaacSMauro Carvalho Chehab 	for (w = buf->virt_addr + buf_size, end = w + MAGIC_SIZE;
122ceafdaacSMauro Carvalho Chehab 	     w < end; w++) {
123ceafdaacSMauro Carvalho Chehab 		if (unlikely(*w != MAGIC_NUM)) {
124ceafdaacSMauro Carvalho Chehab 			dev_dbg(stat->isp->dev,
125ceafdaacSMauro Carvalho Chehab 				"%s: ending magic check does not match.\n",
126ceafdaacSMauro Carvalho Chehab 				stat->subdev.name);
127ceafdaacSMauro Carvalho Chehab 			return -EINVAL;
128ceafdaacSMauro Carvalho Chehab 		}
129ceafdaacSMauro Carvalho Chehab 	}
130ceafdaacSMauro Carvalho Chehab 
131ceafdaacSMauro Carvalho Chehab 	isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
132ceafdaacSMauro Carvalho Chehab 					   DMA_FROM_DEVICE);
133ceafdaacSMauro Carvalho Chehab 
134ceafdaacSMauro Carvalho Chehab 	return 0;
135ceafdaacSMauro Carvalho Chehab }
136ceafdaacSMauro Carvalho Chehab 
isp_stat_buf_insert_magic(struct ispstat * stat,struct ispstat_buffer * buf)137ceafdaacSMauro Carvalho Chehab static void isp_stat_buf_insert_magic(struct ispstat *stat,
138ceafdaacSMauro Carvalho Chehab 				      struct ispstat_buffer *buf)
139ceafdaacSMauro Carvalho Chehab {
140ceafdaacSMauro Carvalho Chehab 	const u32 buf_size = IS_H3A_AF(stat) ?
141ceafdaacSMauro Carvalho Chehab 			     stat->buf_size + AF_EXTRA_DATA : stat->buf_size;
142ceafdaacSMauro Carvalho Chehab 
143ceafdaacSMauro Carvalho Chehab 	isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
144ceafdaacSMauro Carvalho Chehab 
145ceafdaacSMauro Carvalho Chehab 	/*
146ceafdaacSMauro Carvalho Chehab 	 * Inserting MAGIC_NUM at the beginning and end of the buffer.
147ceafdaacSMauro Carvalho Chehab 	 * buf->buf_size is set only after the buffer is queued. For now the
148ceafdaacSMauro Carvalho Chehab 	 * right buf_size for the current configuration is pointed by
149ceafdaacSMauro Carvalho Chehab 	 * stat->buf_size.
150ceafdaacSMauro Carvalho Chehab 	 */
151ceafdaacSMauro Carvalho Chehab 	memset(buf->virt_addr, MAGIC_NUM, MAGIC_SIZE);
152ceafdaacSMauro Carvalho Chehab 	memset(buf->virt_addr + buf_size, MAGIC_NUM, MAGIC_SIZE);
153ceafdaacSMauro Carvalho Chehab 
154ceafdaacSMauro Carvalho Chehab 	isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
155ceafdaacSMauro Carvalho Chehab 					   DMA_BIDIRECTIONAL);
156ceafdaacSMauro Carvalho Chehab }
157ceafdaacSMauro Carvalho Chehab 
isp_stat_buf_sync_for_device(struct ispstat * stat,struct ispstat_buffer * buf)158ceafdaacSMauro Carvalho Chehab static void isp_stat_buf_sync_for_device(struct ispstat *stat,
159ceafdaacSMauro Carvalho Chehab 					 struct ispstat_buffer *buf)
160ceafdaacSMauro Carvalho Chehab {
161ceafdaacSMauro Carvalho Chehab 	if (ISP_STAT_USES_DMAENGINE(stat))
162ceafdaacSMauro Carvalho Chehab 		return;
163ceafdaacSMauro Carvalho Chehab 
164ceafdaacSMauro Carvalho Chehab 	dma_sync_sg_for_device(stat->isp->dev, buf->sgt.sgl,
165ceafdaacSMauro Carvalho Chehab 			       buf->sgt.nents, DMA_FROM_DEVICE);
166ceafdaacSMauro Carvalho Chehab }
167ceafdaacSMauro Carvalho Chehab 
isp_stat_buf_sync_for_cpu(struct ispstat * stat,struct ispstat_buffer * buf)168ceafdaacSMauro Carvalho Chehab static void isp_stat_buf_sync_for_cpu(struct ispstat *stat,
169ceafdaacSMauro Carvalho Chehab 				      struct ispstat_buffer *buf)
170ceafdaacSMauro Carvalho Chehab {
171ceafdaacSMauro Carvalho Chehab 	if (ISP_STAT_USES_DMAENGINE(stat))
172ceafdaacSMauro Carvalho Chehab 		return;
173ceafdaacSMauro Carvalho Chehab 
174ceafdaacSMauro Carvalho Chehab 	dma_sync_sg_for_cpu(stat->isp->dev, buf->sgt.sgl,
175ceafdaacSMauro Carvalho Chehab 			    buf->sgt.nents, DMA_FROM_DEVICE);
176ceafdaacSMauro Carvalho Chehab }
177ceafdaacSMauro Carvalho Chehab 
isp_stat_buf_clear(struct ispstat * stat)178ceafdaacSMauro Carvalho Chehab static void isp_stat_buf_clear(struct ispstat *stat)
179ceafdaacSMauro Carvalho Chehab {
180ceafdaacSMauro Carvalho Chehab 	int i;
181ceafdaacSMauro Carvalho Chehab 
182ceafdaacSMauro Carvalho Chehab 	for (i = 0; i < STAT_MAX_BUFS; i++)
183ceafdaacSMauro Carvalho Chehab 		stat->buf[i].empty = 1;
184ceafdaacSMauro Carvalho Chehab }
185ceafdaacSMauro Carvalho Chehab 
186ceafdaacSMauro Carvalho Chehab static struct ispstat_buffer *
__isp_stat_buf_find(struct ispstat * stat,int look_empty)187ceafdaacSMauro Carvalho Chehab __isp_stat_buf_find(struct ispstat *stat, int look_empty)
188ceafdaacSMauro Carvalho Chehab {
189ceafdaacSMauro Carvalho Chehab 	struct ispstat_buffer *found = NULL;
190ceafdaacSMauro Carvalho Chehab 	int i;
191ceafdaacSMauro Carvalho Chehab 
192ceafdaacSMauro Carvalho Chehab 	for (i = 0; i < STAT_MAX_BUFS; i++) {
193ceafdaacSMauro Carvalho Chehab 		struct ispstat_buffer *curr = &stat->buf[i];
194ceafdaacSMauro Carvalho Chehab 
195ceafdaacSMauro Carvalho Chehab 		/*
196ceafdaacSMauro Carvalho Chehab 		 * Don't select the buffer which is being copied to
197ceafdaacSMauro Carvalho Chehab 		 * userspace or used by the module.
198ceafdaacSMauro Carvalho Chehab 		 */
199ceafdaacSMauro Carvalho Chehab 		if (curr == stat->locked_buf || curr == stat->active_buf)
200ceafdaacSMauro Carvalho Chehab 			continue;
201ceafdaacSMauro Carvalho Chehab 
202ceafdaacSMauro Carvalho Chehab 		/* Don't select uninitialised buffers if it's not required */
203ceafdaacSMauro Carvalho Chehab 		if (!look_empty && curr->empty)
204ceafdaacSMauro Carvalho Chehab 			continue;
205ceafdaacSMauro Carvalho Chehab 
206ceafdaacSMauro Carvalho Chehab 		/* Pick uninitialised buffer over anything else if look_empty */
207ceafdaacSMauro Carvalho Chehab 		if (curr->empty) {
208ceafdaacSMauro Carvalho Chehab 			found = curr;
209ceafdaacSMauro Carvalho Chehab 			break;
210ceafdaacSMauro Carvalho Chehab 		}
211ceafdaacSMauro Carvalho Chehab 
212ceafdaacSMauro Carvalho Chehab 		/* Choose the oldest buffer */
213ceafdaacSMauro Carvalho Chehab 		if (!found ||
214ceafdaacSMauro Carvalho Chehab 		    (s32)curr->frame_number - (s32)found->frame_number < 0)
215ceafdaacSMauro Carvalho Chehab 			found = curr;
216ceafdaacSMauro Carvalho Chehab 	}
217ceafdaacSMauro Carvalho Chehab 
218ceafdaacSMauro Carvalho Chehab 	return found;
219ceafdaacSMauro Carvalho Chehab }
220ceafdaacSMauro Carvalho Chehab 
221ceafdaacSMauro Carvalho Chehab static inline struct ispstat_buffer *
isp_stat_buf_find_oldest(struct ispstat * stat)222ceafdaacSMauro Carvalho Chehab isp_stat_buf_find_oldest(struct ispstat *stat)
223ceafdaacSMauro Carvalho Chehab {
224ceafdaacSMauro Carvalho Chehab 	return __isp_stat_buf_find(stat, 0);
225ceafdaacSMauro Carvalho Chehab }
226ceafdaacSMauro Carvalho Chehab 
227ceafdaacSMauro Carvalho Chehab static inline struct ispstat_buffer *
isp_stat_buf_find_oldest_or_empty(struct ispstat * stat)228ceafdaacSMauro Carvalho Chehab isp_stat_buf_find_oldest_or_empty(struct ispstat *stat)
229ceafdaacSMauro Carvalho Chehab {
230ceafdaacSMauro Carvalho Chehab 	return __isp_stat_buf_find(stat, 1);
231ceafdaacSMauro Carvalho Chehab }
232ceafdaacSMauro Carvalho Chehab 
isp_stat_buf_queue(struct ispstat * stat)233ceafdaacSMauro Carvalho Chehab static int isp_stat_buf_queue(struct ispstat *stat)
234ceafdaacSMauro Carvalho Chehab {
235ceafdaacSMauro Carvalho Chehab 	if (!stat->active_buf)
236ceafdaacSMauro Carvalho Chehab 		return STAT_NO_BUF;
237ceafdaacSMauro Carvalho Chehab 
238ceafdaacSMauro Carvalho Chehab 	ktime_get_ts64(&stat->active_buf->ts);
239ceafdaacSMauro Carvalho Chehab 
240ceafdaacSMauro Carvalho Chehab 	stat->active_buf->buf_size = stat->buf_size;
241ceafdaacSMauro Carvalho Chehab 	if (isp_stat_buf_check_magic(stat, stat->active_buf)) {
242ceafdaacSMauro Carvalho Chehab 		dev_dbg(stat->isp->dev, "%s: data wasn't properly written.\n",
243ceafdaacSMauro Carvalho Chehab 			stat->subdev.name);
244ceafdaacSMauro Carvalho Chehab 		return STAT_NO_BUF;
245ceafdaacSMauro Carvalho Chehab 	}
246ceafdaacSMauro Carvalho Chehab 	stat->active_buf->config_counter = stat->config_counter;
247ceafdaacSMauro Carvalho Chehab 	stat->active_buf->frame_number = stat->frame_number;
248ceafdaacSMauro Carvalho Chehab 	stat->active_buf->empty = 0;
249ceafdaacSMauro Carvalho Chehab 	stat->active_buf = NULL;
250ceafdaacSMauro Carvalho Chehab 
251ceafdaacSMauro Carvalho Chehab 	return STAT_BUF_DONE;
252ceafdaacSMauro Carvalho Chehab }
253ceafdaacSMauro Carvalho Chehab 
254ceafdaacSMauro Carvalho Chehab /* Get next free buffer to write the statistics to and mark it active. */
isp_stat_buf_next(struct ispstat * stat)255ceafdaacSMauro Carvalho Chehab static void isp_stat_buf_next(struct ispstat *stat)
256ceafdaacSMauro Carvalho Chehab {
257ceafdaacSMauro Carvalho Chehab 	if (unlikely(stat->active_buf))
258ceafdaacSMauro Carvalho Chehab 		/* Overwriting unused active buffer */
259ceafdaacSMauro Carvalho Chehab 		dev_dbg(stat->isp->dev,
260ceafdaacSMauro Carvalho Chehab 			"%s: new buffer requested without queuing active one.\n",
261ceafdaacSMauro Carvalho Chehab 			stat->subdev.name);
262ceafdaacSMauro Carvalho Chehab 	else
263ceafdaacSMauro Carvalho Chehab 		stat->active_buf = isp_stat_buf_find_oldest_or_empty(stat);
264ceafdaacSMauro Carvalho Chehab }
265ceafdaacSMauro Carvalho Chehab 
isp_stat_buf_release(struct ispstat * stat)266ceafdaacSMauro Carvalho Chehab static void isp_stat_buf_release(struct ispstat *stat)
267ceafdaacSMauro Carvalho Chehab {
268ceafdaacSMauro Carvalho Chehab 	unsigned long flags;
269ceafdaacSMauro Carvalho Chehab 
270ceafdaacSMauro Carvalho Chehab 	isp_stat_buf_sync_for_device(stat, stat->locked_buf);
271ceafdaacSMauro Carvalho Chehab 	spin_lock_irqsave(&stat->isp->stat_lock, flags);
272ceafdaacSMauro Carvalho Chehab 	stat->locked_buf = NULL;
273ceafdaacSMauro Carvalho Chehab 	spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
274ceafdaacSMauro Carvalho Chehab }
275ceafdaacSMauro Carvalho Chehab 
276ceafdaacSMauro Carvalho Chehab /* Get buffer to userspace. */
isp_stat_buf_get(struct ispstat * stat,struct omap3isp_stat_data * data)277ceafdaacSMauro Carvalho Chehab static struct ispstat_buffer *isp_stat_buf_get(struct ispstat *stat,
278ceafdaacSMauro Carvalho Chehab 					       struct omap3isp_stat_data *data)
279ceafdaacSMauro Carvalho Chehab {
280ceafdaacSMauro Carvalho Chehab 	int rval = 0;
281ceafdaacSMauro Carvalho Chehab 	unsigned long flags;
282ceafdaacSMauro Carvalho Chehab 	struct ispstat_buffer *buf;
283ceafdaacSMauro Carvalho Chehab 
284ceafdaacSMauro Carvalho Chehab 	spin_lock_irqsave(&stat->isp->stat_lock, flags);
285ceafdaacSMauro Carvalho Chehab 
286ceafdaacSMauro Carvalho Chehab 	while (1) {
287ceafdaacSMauro Carvalho Chehab 		buf = isp_stat_buf_find_oldest(stat);
288ceafdaacSMauro Carvalho Chehab 		if (!buf) {
289ceafdaacSMauro Carvalho Chehab 			spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
290ceafdaacSMauro Carvalho Chehab 			dev_dbg(stat->isp->dev, "%s: cannot find a buffer.\n",
291ceafdaacSMauro Carvalho Chehab 				stat->subdev.name);
292ceafdaacSMauro Carvalho Chehab 			return ERR_PTR(-EBUSY);
293ceafdaacSMauro Carvalho Chehab 		}
294ceafdaacSMauro Carvalho Chehab 		if (isp_stat_buf_check_magic(stat, buf)) {
295ceafdaacSMauro Carvalho Chehab 			dev_dbg(stat->isp->dev,
296ceafdaacSMauro Carvalho Chehab 				"%s: current buffer has corrupted data\n.",
297ceafdaacSMauro Carvalho Chehab 				stat->subdev.name);
298ceafdaacSMauro Carvalho Chehab 			/* Mark empty because it doesn't have valid data. */
299ceafdaacSMauro Carvalho Chehab 			buf->empty = 1;
300ceafdaacSMauro Carvalho Chehab 		} else {
301ceafdaacSMauro Carvalho Chehab 			/* Buffer isn't corrupted. */
302ceafdaacSMauro Carvalho Chehab 			break;
303ceafdaacSMauro Carvalho Chehab 		}
304ceafdaacSMauro Carvalho Chehab 	}
305ceafdaacSMauro Carvalho Chehab 
306ceafdaacSMauro Carvalho Chehab 	stat->locked_buf = buf;
307ceafdaacSMauro Carvalho Chehab 
308ceafdaacSMauro Carvalho Chehab 	spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
309ceafdaacSMauro Carvalho Chehab 
310ceafdaacSMauro Carvalho Chehab 	if (buf->buf_size > data->buf_size) {
311ceafdaacSMauro Carvalho Chehab 		dev_warn(stat->isp->dev,
312ceafdaacSMauro Carvalho Chehab 			 "%s: userspace's buffer size is not enough.\n",
313ceafdaacSMauro Carvalho Chehab 			 stat->subdev.name);
314ceafdaacSMauro Carvalho Chehab 		isp_stat_buf_release(stat);
315ceafdaacSMauro Carvalho Chehab 		return ERR_PTR(-EINVAL);
316ceafdaacSMauro Carvalho Chehab 	}
317ceafdaacSMauro Carvalho Chehab 
318ceafdaacSMauro Carvalho Chehab 	isp_stat_buf_sync_for_cpu(stat, buf);
319ceafdaacSMauro Carvalho Chehab 
320ceafdaacSMauro Carvalho Chehab 	rval = copy_to_user(data->buf,
321ceafdaacSMauro Carvalho Chehab 			    buf->virt_addr,
322ceafdaacSMauro Carvalho Chehab 			    buf->buf_size);
323ceafdaacSMauro Carvalho Chehab 
324ceafdaacSMauro Carvalho Chehab 	if (rval) {
325ceafdaacSMauro Carvalho Chehab 		dev_info(stat->isp->dev,
326ceafdaacSMauro Carvalho Chehab 			 "%s: failed copying %d bytes of stat data\n",
327ceafdaacSMauro Carvalho Chehab 			 stat->subdev.name, rval);
328ceafdaacSMauro Carvalho Chehab 		buf = ERR_PTR(-EFAULT);
329ceafdaacSMauro Carvalho Chehab 		isp_stat_buf_release(stat);
330ceafdaacSMauro Carvalho Chehab 	}
331ceafdaacSMauro Carvalho Chehab 
332ceafdaacSMauro Carvalho Chehab 	return buf;
333ceafdaacSMauro Carvalho Chehab }
334ceafdaacSMauro Carvalho Chehab 
isp_stat_bufs_free(struct ispstat * stat)335ceafdaacSMauro Carvalho Chehab static void isp_stat_bufs_free(struct ispstat *stat)
336ceafdaacSMauro Carvalho Chehab {
337ceafdaacSMauro Carvalho Chehab 	struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
338ceafdaacSMauro Carvalho Chehab 			   ? NULL : stat->isp->dev;
339ceafdaacSMauro Carvalho Chehab 	unsigned int i;
340ceafdaacSMauro Carvalho Chehab 
341ceafdaacSMauro Carvalho Chehab 	for (i = 0; i < STAT_MAX_BUFS; i++) {
342ceafdaacSMauro Carvalho Chehab 		struct ispstat_buffer *buf = &stat->buf[i];
343ceafdaacSMauro Carvalho Chehab 
344ceafdaacSMauro Carvalho Chehab 		if (!buf->virt_addr)
345ceafdaacSMauro Carvalho Chehab 			continue;
346ceafdaacSMauro Carvalho Chehab 
347ceafdaacSMauro Carvalho Chehab 		sg_free_table(&buf->sgt);
348ceafdaacSMauro Carvalho Chehab 
349ceafdaacSMauro Carvalho Chehab 		dma_free_coherent(dev, stat->buf_alloc_size, buf->virt_addr,
350ceafdaacSMauro Carvalho Chehab 				  buf->dma_addr);
351ceafdaacSMauro Carvalho Chehab 
352ceafdaacSMauro Carvalho Chehab 		buf->dma_addr = 0;
353ceafdaacSMauro Carvalho Chehab 		buf->virt_addr = NULL;
354ceafdaacSMauro Carvalho Chehab 		buf->empty = 1;
355ceafdaacSMauro Carvalho Chehab 	}
356ceafdaacSMauro Carvalho Chehab 
357ceafdaacSMauro Carvalho Chehab 	dev_dbg(stat->isp->dev, "%s: all buffers were freed.\n",
358ceafdaacSMauro Carvalho Chehab 		stat->subdev.name);
359ceafdaacSMauro Carvalho Chehab 
360ceafdaacSMauro Carvalho Chehab 	stat->buf_alloc_size = 0;
361ceafdaacSMauro Carvalho Chehab 	stat->active_buf = NULL;
362ceafdaacSMauro Carvalho Chehab }
363ceafdaacSMauro Carvalho Chehab 
isp_stat_bufs_alloc_one(struct device * dev,struct ispstat_buffer * buf,unsigned int size)364ceafdaacSMauro Carvalho Chehab static int isp_stat_bufs_alloc_one(struct device *dev,
365ceafdaacSMauro Carvalho Chehab 				   struct ispstat_buffer *buf,
366ceafdaacSMauro Carvalho Chehab 				   unsigned int size)
367ceafdaacSMauro Carvalho Chehab {
368ceafdaacSMauro Carvalho Chehab 	int ret;
369ceafdaacSMauro Carvalho Chehab 
370ceafdaacSMauro Carvalho Chehab 	buf->virt_addr = dma_alloc_coherent(dev, size, &buf->dma_addr,
371ceafdaacSMauro Carvalho Chehab 					    GFP_KERNEL);
372ceafdaacSMauro Carvalho Chehab 	if (!buf->virt_addr)
373ceafdaacSMauro Carvalho Chehab 		return -ENOMEM;
374ceafdaacSMauro Carvalho Chehab 
375ceafdaacSMauro Carvalho Chehab 	ret = dma_get_sgtable(dev, &buf->sgt, buf->virt_addr, buf->dma_addr,
376ceafdaacSMauro Carvalho Chehab 			      size);
377ceafdaacSMauro Carvalho Chehab 	if (ret < 0) {
378ceafdaacSMauro Carvalho Chehab 		dma_free_coherent(dev, size, buf->virt_addr, buf->dma_addr);
379ceafdaacSMauro Carvalho Chehab 		buf->virt_addr = NULL;
380ceafdaacSMauro Carvalho Chehab 		buf->dma_addr = 0;
381ceafdaacSMauro Carvalho Chehab 		return ret;
382ceafdaacSMauro Carvalho Chehab 	}
383ceafdaacSMauro Carvalho Chehab 
384ceafdaacSMauro Carvalho Chehab 	return 0;
385ceafdaacSMauro Carvalho Chehab }
386ceafdaacSMauro Carvalho Chehab 
387ceafdaacSMauro Carvalho Chehab /*
388ceafdaacSMauro Carvalho Chehab  * The device passed to the DMA API depends on whether the statistics block uses
389ceafdaacSMauro Carvalho Chehab  * ISP DMA, external DMA or PIO to transfer data.
390ceafdaacSMauro Carvalho Chehab  *
391ceafdaacSMauro Carvalho Chehab  * The first case (for the AEWB and AF engines) passes the ISP device, resulting
392ceafdaacSMauro Carvalho Chehab  * in the DMA buffers being mapped through the ISP IOMMU.
393ceafdaacSMauro Carvalho Chehab  *
394ceafdaacSMauro Carvalho Chehab  * The second case (for the histogram engine) should pass the DMA engine device.
395ceafdaacSMauro Carvalho Chehab  * As that device isn't accessible through the OMAP DMA engine API the driver
396ceafdaacSMauro Carvalho Chehab  * passes NULL instead, resulting in the buffers being mapped directly as
397ceafdaacSMauro Carvalho Chehab  * physical pages.
398ceafdaacSMauro Carvalho Chehab  *
399ceafdaacSMauro Carvalho Chehab  * The third case (for the histogram engine) doesn't require any mapping. The
400ceafdaacSMauro Carvalho Chehab  * buffers could be allocated with kmalloc/vmalloc, but we still use
401ceafdaacSMauro Carvalho Chehab  * dma_alloc_coherent() for consistency purpose.
402ceafdaacSMauro Carvalho Chehab  */
isp_stat_bufs_alloc(struct ispstat * stat,u32 size)403ceafdaacSMauro Carvalho Chehab static int isp_stat_bufs_alloc(struct ispstat *stat, u32 size)
404ceafdaacSMauro Carvalho Chehab {
405ceafdaacSMauro Carvalho Chehab 	struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
406ceafdaacSMauro Carvalho Chehab 			   ? NULL : stat->isp->dev;
407ceafdaacSMauro Carvalho Chehab 	unsigned long flags;
408ceafdaacSMauro Carvalho Chehab 	unsigned int i;
409ceafdaacSMauro Carvalho Chehab 
410ceafdaacSMauro Carvalho Chehab 	spin_lock_irqsave(&stat->isp->stat_lock, flags);
411ceafdaacSMauro Carvalho Chehab 
412ceafdaacSMauro Carvalho Chehab 	BUG_ON(stat->locked_buf != NULL);
413ceafdaacSMauro Carvalho Chehab 
414ceafdaacSMauro Carvalho Chehab 	/* Are the old buffers big enough? */
415ceafdaacSMauro Carvalho Chehab 	if (stat->buf_alloc_size >= size) {
416ceafdaacSMauro Carvalho Chehab 		spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
417ceafdaacSMauro Carvalho Chehab 		return 0;
418ceafdaacSMauro Carvalho Chehab 	}
419ceafdaacSMauro Carvalho Chehab 
420ceafdaacSMauro Carvalho Chehab 	if (stat->state != ISPSTAT_DISABLED || stat->buf_processing) {
421ceafdaacSMauro Carvalho Chehab 		dev_info(stat->isp->dev,
422ceafdaacSMauro Carvalho Chehab 			 "%s: trying to allocate memory when busy\n",
423ceafdaacSMauro Carvalho Chehab 			 stat->subdev.name);
424ceafdaacSMauro Carvalho Chehab 		spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
425ceafdaacSMauro Carvalho Chehab 		return -EBUSY;
426ceafdaacSMauro Carvalho Chehab 	}
427ceafdaacSMauro Carvalho Chehab 
428ceafdaacSMauro Carvalho Chehab 	spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
429ceafdaacSMauro Carvalho Chehab 
430ceafdaacSMauro Carvalho Chehab 	isp_stat_bufs_free(stat);
431ceafdaacSMauro Carvalho Chehab 
432ceafdaacSMauro Carvalho Chehab 	stat->buf_alloc_size = size;
433ceafdaacSMauro Carvalho Chehab 
434ceafdaacSMauro Carvalho Chehab 	for (i = 0; i < STAT_MAX_BUFS; i++) {
435ceafdaacSMauro Carvalho Chehab 		struct ispstat_buffer *buf = &stat->buf[i];
436ceafdaacSMauro Carvalho Chehab 		int ret;
437ceafdaacSMauro Carvalho Chehab 
438ceafdaacSMauro Carvalho Chehab 		ret = isp_stat_bufs_alloc_one(dev, buf, size);
439ceafdaacSMauro Carvalho Chehab 		if (ret < 0) {
440ceafdaacSMauro Carvalho Chehab 			dev_err(stat->isp->dev,
441ceafdaacSMauro Carvalho Chehab 				"%s: Failed to allocate DMA buffer %u\n",
442ceafdaacSMauro Carvalho Chehab 				stat->subdev.name, i);
443ceafdaacSMauro Carvalho Chehab 			isp_stat_bufs_free(stat);
444ceafdaacSMauro Carvalho Chehab 			return ret;
445ceafdaacSMauro Carvalho Chehab 		}
446ceafdaacSMauro Carvalho Chehab 
447ceafdaacSMauro Carvalho Chehab 		buf->empty = 1;
448ceafdaacSMauro Carvalho Chehab 
449ceafdaacSMauro Carvalho Chehab 		dev_dbg(stat->isp->dev,
450ceafdaacSMauro Carvalho Chehab 			"%s: buffer[%u] allocated. dma=%pad virt=%p",
451ceafdaacSMauro Carvalho Chehab 			stat->subdev.name, i, &buf->dma_addr, buf->virt_addr);
452ceafdaacSMauro Carvalho Chehab 	}
453ceafdaacSMauro Carvalho Chehab 
454ceafdaacSMauro Carvalho Chehab 	return 0;
455ceafdaacSMauro Carvalho Chehab }
456ceafdaacSMauro Carvalho Chehab 
isp_stat_queue_event(struct ispstat * stat,int err)457ceafdaacSMauro Carvalho Chehab static void isp_stat_queue_event(struct ispstat *stat, int err)
458ceafdaacSMauro Carvalho Chehab {
459ceafdaacSMauro Carvalho Chehab 	struct video_device *vdev = stat->subdev.devnode;
460ceafdaacSMauro Carvalho Chehab 	struct v4l2_event event;
461ceafdaacSMauro Carvalho Chehab 	struct omap3isp_stat_event_status *status = (void *)event.u.data;
462ceafdaacSMauro Carvalho Chehab 
463ceafdaacSMauro Carvalho Chehab 	memset(&event, 0, sizeof(event));
464ceafdaacSMauro Carvalho Chehab 	if (!err) {
465ceafdaacSMauro Carvalho Chehab 		status->frame_number = stat->frame_number;
466ceafdaacSMauro Carvalho Chehab 		status->config_counter = stat->config_counter;
467ceafdaacSMauro Carvalho Chehab 	} else {
468ceafdaacSMauro Carvalho Chehab 		status->buf_err = 1;
469ceafdaacSMauro Carvalho Chehab 	}
470ceafdaacSMauro Carvalho Chehab 	event.type = stat->event_type;
471ceafdaacSMauro Carvalho Chehab 	v4l2_event_queue(vdev, &event);
472ceafdaacSMauro Carvalho Chehab }
473ceafdaacSMauro Carvalho Chehab 
474ceafdaacSMauro Carvalho Chehab 
475ceafdaacSMauro Carvalho Chehab /*
476ceafdaacSMauro Carvalho Chehab  * omap3isp_stat_request_statistics - Request statistics.
477ceafdaacSMauro Carvalho Chehab  * @data: Pointer to return statistics data.
478ceafdaacSMauro Carvalho Chehab  *
479ceafdaacSMauro Carvalho Chehab  * Returns 0 if successful.
480ceafdaacSMauro Carvalho Chehab  */
omap3isp_stat_request_statistics(struct ispstat * stat,struct omap3isp_stat_data * data)481ceafdaacSMauro Carvalho Chehab int omap3isp_stat_request_statistics(struct ispstat *stat,
482ceafdaacSMauro Carvalho Chehab 				     struct omap3isp_stat_data *data)
483ceafdaacSMauro Carvalho Chehab {
484ceafdaacSMauro Carvalho Chehab 	struct ispstat_buffer *buf;
485ceafdaacSMauro Carvalho Chehab 
486ceafdaacSMauro Carvalho Chehab 	if (stat->state != ISPSTAT_ENABLED) {
487ceafdaacSMauro Carvalho Chehab 		dev_dbg(stat->isp->dev, "%s: engine not enabled.\n",
488ceafdaacSMauro Carvalho Chehab 			stat->subdev.name);
489ceafdaacSMauro Carvalho Chehab 		return -EINVAL;
490ceafdaacSMauro Carvalho Chehab 	}
491ceafdaacSMauro Carvalho Chehab 
492ceafdaacSMauro Carvalho Chehab 	mutex_lock(&stat->ioctl_lock);
493ceafdaacSMauro Carvalho Chehab 	buf = isp_stat_buf_get(stat, data);
494ceafdaacSMauro Carvalho Chehab 	if (IS_ERR(buf)) {
495ceafdaacSMauro Carvalho Chehab 		mutex_unlock(&stat->ioctl_lock);
496ceafdaacSMauro Carvalho Chehab 		return PTR_ERR(buf);
497ceafdaacSMauro Carvalho Chehab 	}
498ceafdaacSMauro Carvalho Chehab 
499ceafdaacSMauro Carvalho Chehab 	data->ts.tv_sec = buf->ts.tv_sec;
500ceafdaacSMauro Carvalho Chehab 	data->ts.tv_usec = buf->ts.tv_nsec / NSEC_PER_USEC;
501ceafdaacSMauro Carvalho Chehab 	data->config_counter = buf->config_counter;
502ceafdaacSMauro Carvalho Chehab 	data->frame_number = buf->frame_number;
503ceafdaacSMauro Carvalho Chehab 	data->buf_size = buf->buf_size;
504ceafdaacSMauro Carvalho Chehab 
505ceafdaacSMauro Carvalho Chehab 	buf->empty = 1;
506ceafdaacSMauro Carvalho Chehab 	isp_stat_buf_release(stat);
507ceafdaacSMauro Carvalho Chehab 	mutex_unlock(&stat->ioctl_lock);
508ceafdaacSMauro Carvalho Chehab 
509ceafdaacSMauro Carvalho Chehab 	return 0;
510ceafdaacSMauro Carvalho Chehab }
511ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_request_statistics_time32(struct ispstat * stat,struct omap3isp_stat_data_time32 * data)512ceafdaacSMauro Carvalho Chehab int omap3isp_stat_request_statistics_time32(struct ispstat *stat,
513ceafdaacSMauro Carvalho Chehab 					struct omap3isp_stat_data_time32 *data)
514ceafdaacSMauro Carvalho Chehab {
515ceafdaacSMauro Carvalho Chehab 	struct omap3isp_stat_data data64 = { };
516ceafdaacSMauro Carvalho Chehab 	int ret;
517ceafdaacSMauro Carvalho Chehab 
518ceafdaacSMauro Carvalho Chehab 	ret = omap3isp_stat_request_statistics(stat, &data64);
519ceafdaacSMauro Carvalho Chehab 	if (ret)
520ceafdaacSMauro Carvalho Chehab 		return ret;
521ceafdaacSMauro Carvalho Chehab 
522ceafdaacSMauro Carvalho Chehab 	data->ts.tv_sec = data64.ts.tv_sec;
523ceafdaacSMauro Carvalho Chehab 	data->ts.tv_usec = data64.ts.tv_usec;
524ceafdaacSMauro Carvalho Chehab 	data->buf = (uintptr_t)data64.buf;
525ceafdaacSMauro Carvalho Chehab 	memcpy(&data->frame, &data64.frame, sizeof(data->frame));
526ceafdaacSMauro Carvalho Chehab 
527ceafdaacSMauro Carvalho Chehab 	return 0;
528ceafdaacSMauro Carvalho Chehab }
529ceafdaacSMauro Carvalho Chehab 
530ceafdaacSMauro Carvalho Chehab /*
531ceafdaacSMauro Carvalho Chehab  * omap3isp_stat_config - Receives new statistic engine configuration.
532ceafdaacSMauro Carvalho Chehab  * @new_conf: Pointer to config structure.
533ceafdaacSMauro Carvalho Chehab  *
534ceafdaacSMauro Carvalho Chehab  * Returns 0 if successful, -EINVAL if new_conf pointer is NULL, -ENOMEM if
535ceafdaacSMauro Carvalho Chehab  * was unable to allocate memory for the buffer, or other errors if parameters
536ceafdaacSMauro Carvalho Chehab  * are invalid.
537ceafdaacSMauro Carvalho Chehab  */
omap3isp_stat_config(struct ispstat * stat,void * new_conf)538ceafdaacSMauro Carvalho Chehab int omap3isp_stat_config(struct ispstat *stat, void *new_conf)
539ceafdaacSMauro Carvalho Chehab {
540ceafdaacSMauro Carvalho Chehab 	int ret;
541ceafdaacSMauro Carvalho Chehab 	unsigned long irqflags;
542ceafdaacSMauro Carvalho Chehab 	struct ispstat_generic_config *user_cfg = new_conf;
543ceafdaacSMauro Carvalho Chehab 	u32 buf_size = user_cfg->buf_size;
544ceafdaacSMauro Carvalho Chehab 
545ceafdaacSMauro Carvalho Chehab 	mutex_lock(&stat->ioctl_lock);
546ceafdaacSMauro Carvalho Chehab 
547ceafdaacSMauro Carvalho Chehab 	dev_dbg(stat->isp->dev,
548ceafdaacSMauro Carvalho Chehab 		"%s: configuring module with buffer size=0x%08lx\n",
549ceafdaacSMauro Carvalho Chehab 		stat->subdev.name, (unsigned long)buf_size);
550ceafdaacSMauro Carvalho Chehab 
551ceafdaacSMauro Carvalho Chehab 	ret = stat->ops->validate_params(stat, new_conf);
552ceafdaacSMauro Carvalho Chehab 	if (ret) {
553ceafdaacSMauro Carvalho Chehab 		mutex_unlock(&stat->ioctl_lock);
554ceafdaacSMauro Carvalho Chehab 		dev_dbg(stat->isp->dev, "%s: configuration values are invalid.\n",
555ceafdaacSMauro Carvalho Chehab 			stat->subdev.name);
556ceafdaacSMauro Carvalho Chehab 		return ret;
557ceafdaacSMauro Carvalho Chehab 	}
558ceafdaacSMauro Carvalho Chehab 
559ceafdaacSMauro Carvalho Chehab 	if (buf_size != user_cfg->buf_size)
560ceafdaacSMauro Carvalho Chehab 		dev_dbg(stat->isp->dev,
561ceafdaacSMauro Carvalho Chehab 			"%s: driver has corrected buffer size request to 0x%08lx\n",
562ceafdaacSMauro Carvalho Chehab 			stat->subdev.name,
563ceafdaacSMauro Carvalho Chehab 			(unsigned long)user_cfg->buf_size);
564ceafdaacSMauro Carvalho Chehab 
565ceafdaacSMauro Carvalho Chehab 	/*
566ceafdaacSMauro Carvalho Chehab 	 * Hack: H3A modules may need a doubled buffer size to avoid access
567ceafdaacSMauro Carvalho Chehab 	 * to a invalid memory address after a SBL overflow.
568ceafdaacSMauro Carvalho Chehab 	 * The buffer size is always PAGE_ALIGNED.
569ceafdaacSMauro Carvalho Chehab 	 * Hack 2: MAGIC_SIZE is added to buf_size so a magic word can be
570ceafdaacSMauro Carvalho Chehab 	 * inserted at the end to data integrity check purpose.
571ceafdaacSMauro Carvalho Chehab 	 * Hack 3: AF module writes one paxel data more than it should, so
572ceafdaacSMauro Carvalho Chehab 	 * the buffer allocation must consider it to avoid invalid memory
573ceafdaacSMauro Carvalho Chehab 	 * access.
574ceafdaacSMauro Carvalho Chehab 	 * Hack 4: H3A need to allocate extra space for the recover state.
575ceafdaacSMauro Carvalho Chehab 	 */
576ceafdaacSMauro Carvalho Chehab 	if (IS_H3A(stat)) {
577ceafdaacSMauro Carvalho Chehab 		buf_size = user_cfg->buf_size * 2 + MAGIC_SIZE;
578ceafdaacSMauro Carvalho Chehab 		if (IS_H3A_AF(stat))
579ceafdaacSMauro Carvalho Chehab 			/*
580ceafdaacSMauro Carvalho Chehab 			 * Adding one extra paxel data size for each recover
581ceafdaacSMauro Carvalho Chehab 			 * buffer + 2 regular ones.
582ceafdaacSMauro Carvalho Chehab 			 */
583ceafdaacSMauro Carvalho Chehab 			buf_size += AF_EXTRA_DATA * (NUM_H3A_RECOVER_BUFS + 2);
584ceafdaacSMauro Carvalho Chehab 		if (stat->recover_priv) {
585ceafdaacSMauro Carvalho Chehab 			struct ispstat_generic_config *recover_cfg =
586ceafdaacSMauro Carvalho Chehab 				stat->recover_priv;
587ceafdaacSMauro Carvalho Chehab 			buf_size += recover_cfg->buf_size *
588ceafdaacSMauro Carvalho Chehab 				    NUM_H3A_RECOVER_BUFS;
589ceafdaacSMauro Carvalho Chehab 		}
590ceafdaacSMauro Carvalho Chehab 		buf_size = PAGE_ALIGN(buf_size);
591ceafdaacSMauro Carvalho Chehab 	} else { /* Histogram */
592ceafdaacSMauro Carvalho Chehab 		buf_size = PAGE_ALIGN(user_cfg->buf_size + MAGIC_SIZE);
593ceafdaacSMauro Carvalho Chehab 	}
594ceafdaacSMauro Carvalho Chehab 
595ceafdaacSMauro Carvalho Chehab 	ret = isp_stat_bufs_alloc(stat, buf_size);
596ceafdaacSMauro Carvalho Chehab 	if (ret) {
597ceafdaacSMauro Carvalho Chehab 		mutex_unlock(&stat->ioctl_lock);
598ceafdaacSMauro Carvalho Chehab 		return ret;
599ceafdaacSMauro Carvalho Chehab 	}
600ceafdaacSMauro Carvalho Chehab 
601ceafdaacSMauro Carvalho Chehab 	spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
602ceafdaacSMauro Carvalho Chehab 	stat->ops->set_params(stat, new_conf);
603ceafdaacSMauro Carvalho Chehab 	spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
604ceafdaacSMauro Carvalho Chehab 
605ceafdaacSMauro Carvalho Chehab 	/*
606ceafdaacSMauro Carvalho Chehab 	 * Returning the right future config_counter for this setup, so
607ceafdaacSMauro Carvalho Chehab 	 * userspace can *know* when it has been applied.
608ceafdaacSMauro Carvalho Chehab 	 */
609ceafdaacSMauro Carvalho Chehab 	user_cfg->config_counter = stat->config_counter + stat->inc_config;
610ceafdaacSMauro Carvalho Chehab 
611ceafdaacSMauro Carvalho Chehab 	/* Module has a valid configuration. */
612ceafdaacSMauro Carvalho Chehab 	stat->configured = 1;
613ceafdaacSMauro Carvalho Chehab 	dev_dbg(stat->isp->dev,
614ceafdaacSMauro Carvalho Chehab 		"%s: module has been successfully configured.\n",
615ceafdaacSMauro Carvalho Chehab 		stat->subdev.name);
616ceafdaacSMauro Carvalho Chehab 
617ceafdaacSMauro Carvalho Chehab 	mutex_unlock(&stat->ioctl_lock);
618ceafdaacSMauro Carvalho Chehab 
619ceafdaacSMauro Carvalho Chehab 	return 0;
620ceafdaacSMauro Carvalho Chehab }
621ceafdaacSMauro Carvalho Chehab 
622ceafdaacSMauro Carvalho Chehab /*
623ceafdaacSMauro Carvalho Chehab  * isp_stat_buf_process - Process statistic buffers.
624ceafdaacSMauro Carvalho Chehab  * @buf_state: points out if buffer is ready to be processed. It's necessary
625ceafdaacSMauro Carvalho Chehab  *	       because histogram needs to copy the data from internal memory
626ceafdaacSMauro Carvalho Chehab  *	       before be able to process the buffer.
627ceafdaacSMauro Carvalho Chehab  */
isp_stat_buf_process(struct ispstat * stat,int buf_state)628ceafdaacSMauro Carvalho Chehab static int isp_stat_buf_process(struct ispstat *stat, int buf_state)
629ceafdaacSMauro Carvalho Chehab {
630ceafdaacSMauro Carvalho Chehab 	int ret = STAT_NO_BUF;
631ceafdaacSMauro Carvalho Chehab 
632ceafdaacSMauro Carvalho Chehab 	if (!atomic_add_unless(&stat->buf_err, -1, 0) &&
633ceafdaacSMauro Carvalho Chehab 	    buf_state == STAT_BUF_DONE && stat->state == ISPSTAT_ENABLED) {
634ceafdaacSMauro Carvalho Chehab 		ret = isp_stat_buf_queue(stat);
635ceafdaacSMauro Carvalho Chehab 		isp_stat_buf_next(stat);
636ceafdaacSMauro Carvalho Chehab 	}
637ceafdaacSMauro Carvalho Chehab 
638ceafdaacSMauro Carvalho Chehab 	return ret;
639ceafdaacSMauro Carvalho Chehab }
640ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_pcr_busy(struct ispstat * stat)641ceafdaacSMauro Carvalho Chehab int omap3isp_stat_pcr_busy(struct ispstat *stat)
642ceafdaacSMauro Carvalho Chehab {
643ceafdaacSMauro Carvalho Chehab 	return stat->ops->busy(stat);
644ceafdaacSMauro Carvalho Chehab }
645ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_busy(struct ispstat * stat)646ceafdaacSMauro Carvalho Chehab int omap3isp_stat_busy(struct ispstat *stat)
647ceafdaacSMauro Carvalho Chehab {
648ceafdaacSMauro Carvalho Chehab 	return omap3isp_stat_pcr_busy(stat) | stat->buf_processing |
649ceafdaacSMauro Carvalho Chehab 		(stat->state != ISPSTAT_DISABLED);
650ceafdaacSMauro Carvalho Chehab }
651ceafdaacSMauro Carvalho Chehab 
652ceafdaacSMauro Carvalho Chehab /*
653ceafdaacSMauro Carvalho Chehab  * isp_stat_pcr_enable - Disables/Enables statistic engines.
654ceafdaacSMauro Carvalho Chehab  * @pcr_enable: 0/1 - Disables/Enables the engine.
655ceafdaacSMauro Carvalho Chehab  *
656ceafdaacSMauro Carvalho Chehab  * Must be called from ISP driver when the module is idle and synchronized
657ceafdaacSMauro Carvalho Chehab  * with CCDC.
658ceafdaacSMauro Carvalho Chehab  */
isp_stat_pcr_enable(struct ispstat * stat,u8 pcr_enable)659ceafdaacSMauro Carvalho Chehab static void isp_stat_pcr_enable(struct ispstat *stat, u8 pcr_enable)
660ceafdaacSMauro Carvalho Chehab {
661ceafdaacSMauro Carvalho Chehab 	if ((stat->state != ISPSTAT_ENABLING &&
662ceafdaacSMauro Carvalho Chehab 	     stat->state != ISPSTAT_ENABLED) && pcr_enable)
663ceafdaacSMauro Carvalho Chehab 		/* Userspace has disabled the module. Aborting. */
664ceafdaacSMauro Carvalho Chehab 		return;
665ceafdaacSMauro Carvalho Chehab 
666ceafdaacSMauro Carvalho Chehab 	stat->ops->enable(stat, pcr_enable);
667ceafdaacSMauro Carvalho Chehab 	if (stat->state == ISPSTAT_DISABLING && !pcr_enable)
668ceafdaacSMauro Carvalho Chehab 		stat->state = ISPSTAT_DISABLED;
669ceafdaacSMauro Carvalho Chehab 	else if (stat->state == ISPSTAT_ENABLING && pcr_enable)
670ceafdaacSMauro Carvalho Chehab 		stat->state = ISPSTAT_ENABLED;
671ceafdaacSMauro Carvalho Chehab }
672ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_suspend(struct ispstat * stat)673ceafdaacSMauro Carvalho Chehab void omap3isp_stat_suspend(struct ispstat *stat)
674ceafdaacSMauro Carvalho Chehab {
675ceafdaacSMauro Carvalho Chehab 	unsigned long flags;
676ceafdaacSMauro Carvalho Chehab 
677ceafdaacSMauro Carvalho Chehab 	spin_lock_irqsave(&stat->isp->stat_lock, flags);
678ceafdaacSMauro Carvalho Chehab 
679ceafdaacSMauro Carvalho Chehab 	if (stat->state != ISPSTAT_DISABLED)
680ceafdaacSMauro Carvalho Chehab 		stat->ops->enable(stat, 0);
681ceafdaacSMauro Carvalho Chehab 	if (stat->state == ISPSTAT_ENABLED)
682ceafdaacSMauro Carvalho Chehab 		stat->state = ISPSTAT_SUSPENDED;
683ceafdaacSMauro Carvalho Chehab 
684ceafdaacSMauro Carvalho Chehab 	spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
685ceafdaacSMauro Carvalho Chehab }
686ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_resume(struct ispstat * stat)687ceafdaacSMauro Carvalho Chehab void omap3isp_stat_resume(struct ispstat *stat)
688ceafdaacSMauro Carvalho Chehab {
689ceafdaacSMauro Carvalho Chehab 	/* Module will be re-enabled with its pipeline */
690ceafdaacSMauro Carvalho Chehab 	if (stat->state == ISPSTAT_SUSPENDED)
691ceafdaacSMauro Carvalho Chehab 		stat->state = ISPSTAT_ENABLING;
692ceafdaacSMauro Carvalho Chehab }
693ceafdaacSMauro Carvalho Chehab 
isp_stat_try_enable(struct ispstat * stat)694ceafdaacSMauro Carvalho Chehab static void isp_stat_try_enable(struct ispstat *stat)
695ceafdaacSMauro Carvalho Chehab {
696ceafdaacSMauro Carvalho Chehab 	unsigned long irqflags;
697ceafdaacSMauro Carvalho Chehab 
698ceafdaacSMauro Carvalho Chehab 	if (stat->priv == NULL)
699ceafdaacSMauro Carvalho Chehab 		/* driver wasn't initialised */
700ceafdaacSMauro Carvalho Chehab 		return;
701ceafdaacSMauro Carvalho Chehab 
702ceafdaacSMauro Carvalho Chehab 	spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
703ceafdaacSMauro Carvalho Chehab 	if (stat->state == ISPSTAT_ENABLING && !stat->buf_processing &&
704ceafdaacSMauro Carvalho Chehab 	    stat->buf_alloc_size) {
705ceafdaacSMauro Carvalho Chehab 		/*
706ceafdaacSMauro Carvalho Chehab 		 * Userspace's requested to enable the engine but it wasn't yet.
707ceafdaacSMauro Carvalho Chehab 		 * Let's do that now.
708ceafdaacSMauro Carvalho Chehab 		 */
709ceafdaacSMauro Carvalho Chehab 		stat->update = 1;
710ceafdaacSMauro Carvalho Chehab 		isp_stat_buf_next(stat);
711ceafdaacSMauro Carvalho Chehab 		stat->ops->setup_regs(stat, stat->priv);
712ceafdaacSMauro Carvalho Chehab 		isp_stat_buf_insert_magic(stat, stat->active_buf);
713ceafdaacSMauro Carvalho Chehab 
714ceafdaacSMauro Carvalho Chehab 		/*
715ceafdaacSMauro Carvalho Chehab 		 * H3A module has some hw issues which forces the driver to
716ceafdaacSMauro Carvalho Chehab 		 * ignore next buffers even if it was disabled in the meantime.
717ceafdaacSMauro Carvalho Chehab 		 * On the other hand, Histogram shouldn't ignore buffers anymore
718ceafdaacSMauro Carvalho Chehab 		 * if it's being enabled.
719ceafdaacSMauro Carvalho Chehab 		 */
720ceafdaacSMauro Carvalho Chehab 		if (!IS_H3A(stat))
721ceafdaacSMauro Carvalho Chehab 			atomic_set(&stat->buf_err, 0);
722ceafdaacSMauro Carvalho Chehab 
723ceafdaacSMauro Carvalho Chehab 		isp_stat_pcr_enable(stat, 1);
724ceafdaacSMauro Carvalho Chehab 		spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
725ceafdaacSMauro Carvalho Chehab 		dev_dbg(stat->isp->dev, "%s: module is enabled.\n",
726ceafdaacSMauro Carvalho Chehab 			stat->subdev.name);
727ceafdaacSMauro Carvalho Chehab 	} else {
728ceafdaacSMauro Carvalho Chehab 		spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
729ceafdaacSMauro Carvalho Chehab 	}
730ceafdaacSMauro Carvalho Chehab }
731ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_isr_frame_sync(struct ispstat * stat)732ceafdaacSMauro Carvalho Chehab void omap3isp_stat_isr_frame_sync(struct ispstat *stat)
733ceafdaacSMauro Carvalho Chehab {
734ceafdaacSMauro Carvalho Chehab 	isp_stat_try_enable(stat);
735ceafdaacSMauro Carvalho Chehab }
736ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_sbl_overflow(struct ispstat * stat)737ceafdaacSMauro Carvalho Chehab void omap3isp_stat_sbl_overflow(struct ispstat *stat)
738ceafdaacSMauro Carvalho Chehab {
739ceafdaacSMauro Carvalho Chehab 	unsigned long irqflags;
740ceafdaacSMauro Carvalho Chehab 
741ceafdaacSMauro Carvalho Chehab 	spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
742ceafdaacSMauro Carvalho Chehab 	/*
743ceafdaacSMauro Carvalho Chehab 	 * Due to a H3A hw issue which prevents the next buffer to start from
744ceafdaacSMauro Carvalho Chehab 	 * the correct memory address, 2 buffers must be ignored.
745ceafdaacSMauro Carvalho Chehab 	 */
746ceafdaacSMauro Carvalho Chehab 	atomic_set(&stat->buf_err, 2);
747ceafdaacSMauro Carvalho Chehab 
748ceafdaacSMauro Carvalho Chehab 	/*
749ceafdaacSMauro Carvalho Chehab 	 * If more than one SBL overflow happen in a row, H3A module may access
750ceafdaacSMauro Carvalho Chehab 	 * invalid memory region.
751ceafdaacSMauro Carvalho Chehab 	 * stat->sbl_ovl_recover is set to tell to the driver to temporarily use
752ceafdaacSMauro Carvalho Chehab 	 * a soft configuration which helps to avoid consecutive overflows.
753ceafdaacSMauro Carvalho Chehab 	 */
754ceafdaacSMauro Carvalho Chehab 	if (stat->recover_priv)
755ceafdaacSMauro Carvalho Chehab 		stat->sbl_ovl_recover = 1;
756ceafdaacSMauro Carvalho Chehab 	spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
757ceafdaacSMauro Carvalho Chehab }
758ceafdaacSMauro Carvalho Chehab 
759ceafdaacSMauro Carvalho Chehab /*
760ceafdaacSMauro Carvalho Chehab  * omap3isp_stat_enable - Disable/Enable statistic engine as soon as possible
761ceafdaacSMauro Carvalho Chehab  * @enable: 0/1 - Disables/Enables the engine.
762ceafdaacSMauro Carvalho Chehab  *
763ceafdaacSMauro Carvalho Chehab  * Client should configure all the module registers before this.
764ceafdaacSMauro Carvalho Chehab  * This function can be called from a userspace request.
765ceafdaacSMauro Carvalho Chehab  */
omap3isp_stat_enable(struct ispstat * stat,u8 enable)766ceafdaacSMauro Carvalho Chehab int omap3isp_stat_enable(struct ispstat *stat, u8 enable)
767ceafdaacSMauro Carvalho Chehab {
768ceafdaacSMauro Carvalho Chehab 	unsigned long irqflags;
769ceafdaacSMauro Carvalho Chehab 
770ceafdaacSMauro Carvalho Chehab 	dev_dbg(stat->isp->dev, "%s: user wants to %s module.\n",
771ceafdaacSMauro Carvalho Chehab 		stat->subdev.name, enable ? "enable" : "disable");
772ceafdaacSMauro Carvalho Chehab 
773ceafdaacSMauro Carvalho Chehab 	/* Prevent enabling while configuring */
774ceafdaacSMauro Carvalho Chehab 	mutex_lock(&stat->ioctl_lock);
775ceafdaacSMauro Carvalho Chehab 
776ceafdaacSMauro Carvalho Chehab 	spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
777ceafdaacSMauro Carvalho Chehab 
778ceafdaacSMauro Carvalho Chehab 	if (!stat->configured && enable) {
779ceafdaacSMauro Carvalho Chehab 		spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
780ceafdaacSMauro Carvalho Chehab 		mutex_unlock(&stat->ioctl_lock);
781ceafdaacSMauro Carvalho Chehab 		dev_dbg(stat->isp->dev,
782ceafdaacSMauro Carvalho Chehab 			"%s: cannot enable module as it's never been successfully configured so far.\n",
783ceafdaacSMauro Carvalho Chehab 			stat->subdev.name);
784ceafdaacSMauro Carvalho Chehab 		return -EINVAL;
785ceafdaacSMauro Carvalho Chehab 	}
786ceafdaacSMauro Carvalho Chehab 
787ceafdaacSMauro Carvalho Chehab 	if (enable) {
788ceafdaacSMauro Carvalho Chehab 		if (stat->state == ISPSTAT_DISABLING)
789ceafdaacSMauro Carvalho Chehab 			/* Previous disabling request wasn't done yet */
790ceafdaacSMauro Carvalho Chehab 			stat->state = ISPSTAT_ENABLED;
791ceafdaacSMauro Carvalho Chehab 		else if (stat->state == ISPSTAT_DISABLED)
792ceafdaacSMauro Carvalho Chehab 			/* Module is now being enabled */
793ceafdaacSMauro Carvalho Chehab 			stat->state = ISPSTAT_ENABLING;
794ceafdaacSMauro Carvalho Chehab 	} else {
795ceafdaacSMauro Carvalho Chehab 		if (stat->state == ISPSTAT_ENABLING) {
796ceafdaacSMauro Carvalho Chehab 			/* Previous enabling request wasn't done yet */
797ceafdaacSMauro Carvalho Chehab 			stat->state = ISPSTAT_DISABLED;
798ceafdaacSMauro Carvalho Chehab 		} else if (stat->state == ISPSTAT_ENABLED) {
799ceafdaacSMauro Carvalho Chehab 			/* Module is now being disabled */
800ceafdaacSMauro Carvalho Chehab 			stat->state = ISPSTAT_DISABLING;
801ceafdaacSMauro Carvalho Chehab 			isp_stat_buf_clear(stat);
802ceafdaacSMauro Carvalho Chehab 		}
803ceafdaacSMauro Carvalho Chehab 	}
804ceafdaacSMauro Carvalho Chehab 
805ceafdaacSMauro Carvalho Chehab 	spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
806ceafdaacSMauro Carvalho Chehab 	mutex_unlock(&stat->ioctl_lock);
807ceafdaacSMauro Carvalho Chehab 
808ceafdaacSMauro Carvalho Chehab 	return 0;
809ceafdaacSMauro Carvalho Chehab }
810ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_s_stream(struct v4l2_subdev * subdev,int enable)811ceafdaacSMauro Carvalho Chehab int omap3isp_stat_s_stream(struct v4l2_subdev *subdev, int enable)
812ceafdaacSMauro Carvalho Chehab {
813ceafdaacSMauro Carvalho Chehab 	struct ispstat *stat = v4l2_get_subdevdata(subdev);
814ceafdaacSMauro Carvalho Chehab 
815ceafdaacSMauro Carvalho Chehab 	if (enable) {
816ceafdaacSMauro Carvalho Chehab 		/*
817ceafdaacSMauro Carvalho Chehab 		 * Only set enable PCR bit if the module was previously
818ceafdaacSMauro Carvalho Chehab 		 * enabled through ioctl.
819ceafdaacSMauro Carvalho Chehab 		 */
820ceafdaacSMauro Carvalho Chehab 		isp_stat_try_enable(stat);
821ceafdaacSMauro Carvalho Chehab 	} else {
822ceafdaacSMauro Carvalho Chehab 		unsigned long flags;
823ceafdaacSMauro Carvalho Chehab 		/* Disable PCR bit and config enable field */
824ceafdaacSMauro Carvalho Chehab 		omap3isp_stat_enable(stat, 0);
825ceafdaacSMauro Carvalho Chehab 		spin_lock_irqsave(&stat->isp->stat_lock, flags);
826ceafdaacSMauro Carvalho Chehab 		stat->ops->enable(stat, 0);
827ceafdaacSMauro Carvalho Chehab 		spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
828ceafdaacSMauro Carvalho Chehab 
829ceafdaacSMauro Carvalho Chehab 		/*
830ceafdaacSMauro Carvalho Chehab 		 * If module isn't busy, a new interrupt may come or not to
831ceafdaacSMauro Carvalho Chehab 		 * set the state to DISABLED. As Histogram needs to read its
832ceafdaacSMauro Carvalho Chehab 		 * internal memory to clear it, let interrupt handler
833ceafdaacSMauro Carvalho Chehab 		 * responsible of changing state to DISABLED. If the last
834ceafdaacSMauro Carvalho Chehab 		 * interrupt is coming, it's still safe as the handler will
835ceafdaacSMauro Carvalho Chehab 		 * ignore the second time when state is already set to DISABLED.
836ceafdaacSMauro Carvalho Chehab 		 * It's necessary to synchronize Histogram with streamoff, once
837ceafdaacSMauro Carvalho Chehab 		 * the module may be considered idle before last SDMA transfer
838ceafdaacSMauro Carvalho Chehab 		 * starts if we return here.
839ceafdaacSMauro Carvalho Chehab 		 */
840ceafdaacSMauro Carvalho Chehab 		if (!omap3isp_stat_pcr_busy(stat))
841ceafdaacSMauro Carvalho Chehab 			omap3isp_stat_isr(stat);
842ceafdaacSMauro Carvalho Chehab 
843ceafdaacSMauro Carvalho Chehab 		dev_dbg(stat->isp->dev, "%s: module is being disabled\n",
844ceafdaacSMauro Carvalho Chehab 			stat->subdev.name);
845ceafdaacSMauro Carvalho Chehab 	}
846ceafdaacSMauro Carvalho Chehab 
847ceafdaacSMauro Carvalho Chehab 	return 0;
848ceafdaacSMauro Carvalho Chehab }
849ceafdaacSMauro Carvalho Chehab 
850ceafdaacSMauro Carvalho Chehab /*
851ceafdaacSMauro Carvalho Chehab  * __stat_isr - Interrupt handler for statistic drivers
852ceafdaacSMauro Carvalho Chehab  */
__stat_isr(struct ispstat * stat,int from_dma)853ceafdaacSMauro Carvalho Chehab static void __stat_isr(struct ispstat *stat, int from_dma)
854ceafdaacSMauro Carvalho Chehab {
855ceafdaacSMauro Carvalho Chehab 	int ret = STAT_BUF_DONE;
856ceafdaacSMauro Carvalho Chehab 	int buf_processing;
857ceafdaacSMauro Carvalho Chehab 	unsigned long irqflags;
858ceafdaacSMauro Carvalho Chehab 	struct isp_pipeline *pipe;
859ceafdaacSMauro Carvalho Chehab 
860ceafdaacSMauro Carvalho Chehab 	/*
861ceafdaacSMauro Carvalho Chehab 	 * stat->buf_processing must be set before disable module. It's
862ceafdaacSMauro Carvalho Chehab 	 * necessary to not inform too early the buffers aren't busy in case
863ceafdaacSMauro Carvalho Chehab 	 * of SDMA is going to be used.
864ceafdaacSMauro Carvalho Chehab 	 */
865ceafdaacSMauro Carvalho Chehab 	spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
866ceafdaacSMauro Carvalho Chehab 	if (stat->state == ISPSTAT_DISABLED) {
867ceafdaacSMauro Carvalho Chehab 		spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
868ceafdaacSMauro Carvalho Chehab 		return;
869ceafdaacSMauro Carvalho Chehab 	}
870ceafdaacSMauro Carvalho Chehab 	buf_processing = stat->buf_processing;
871ceafdaacSMauro Carvalho Chehab 	stat->buf_processing = 1;
872ceafdaacSMauro Carvalho Chehab 	stat->ops->enable(stat, 0);
873ceafdaacSMauro Carvalho Chehab 
874ceafdaacSMauro Carvalho Chehab 	if (buf_processing && !from_dma) {
875ceafdaacSMauro Carvalho Chehab 		if (stat->state == ISPSTAT_ENABLED) {
876ceafdaacSMauro Carvalho Chehab 			spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
877ceafdaacSMauro Carvalho Chehab 			dev_err(stat->isp->dev,
878ceafdaacSMauro Carvalho Chehab 				"%s: interrupt occurred when module was still processing a buffer.\n",
879ceafdaacSMauro Carvalho Chehab 				stat->subdev.name);
880ceafdaacSMauro Carvalho Chehab 			ret = STAT_NO_BUF;
881ceafdaacSMauro Carvalho Chehab 			goto out;
882ceafdaacSMauro Carvalho Chehab 		} else {
883ceafdaacSMauro Carvalho Chehab 			/*
884ceafdaacSMauro Carvalho Chehab 			 * Interrupt handler was called from streamoff when
885ceafdaacSMauro Carvalho Chehab 			 * the module wasn't busy anymore to ensure it is being
886ceafdaacSMauro Carvalho Chehab 			 * disabled after process last buffer. If such buffer
887ceafdaacSMauro Carvalho Chehab 			 * processing has already started, no need to do
888ceafdaacSMauro Carvalho Chehab 			 * anything else.
889ceafdaacSMauro Carvalho Chehab 			 */
890ceafdaacSMauro Carvalho Chehab 			spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
891ceafdaacSMauro Carvalho Chehab 			return;
892ceafdaacSMauro Carvalho Chehab 		}
893ceafdaacSMauro Carvalho Chehab 	}
894ceafdaacSMauro Carvalho Chehab 	spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
895ceafdaacSMauro Carvalho Chehab 
896ceafdaacSMauro Carvalho Chehab 	/* If it's busy we can't process this buffer anymore */
897ceafdaacSMauro Carvalho Chehab 	if (!omap3isp_stat_pcr_busy(stat)) {
898ceafdaacSMauro Carvalho Chehab 		if (!from_dma && stat->ops->buf_process)
899ceafdaacSMauro Carvalho Chehab 			/* Module still need to copy data to buffer. */
900ceafdaacSMauro Carvalho Chehab 			ret = stat->ops->buf_process(stat);
901ceafdaacSMauro Carvalho Chehab 		if (ret == STAT_BUF_WAITING_DMA)
902ceafdaacSMauro Carvalho Chehab 			/* Buffer is not ready yet */
903ceafdaacSMauro Carvalho Chehab 			return;
904ceafdaacSMauro Carvalho Chehab 
905ceafdaacSMauro Carvalho Chehab 		spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
906ceafdaacSMauro Carvalho Chehab 
907ceafdaacSMauro Carvalho Chehab 		/*
908ceafdaacSMauro Carvalho Chehab 		 * Histogram needs to read its internal memory to clear it
909ceafdaacSMauro Carvalho Chehab 		 * before be disabled. For that reason, common statistic layer
910ceafdaacSMauro Carvalho Chehab 		 * can return only after call stat's buf_process() operator.
911ceafdaacSMauro Carvalho Chehab 		 */
912ceafdaacSMauro Carvalho Chehab 		if (stat->state == ISPSTAT_DISABLING) {
913ceafdaacSMauro Carvalho Chehab 			stat->state = ISPSTAT_DISABLED;
914ceafdaacSMauro Carvalho Chehab 			spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
915ceafdaacSMauro Carvalho Chehab 			stat->buf_processing = 0;
916ceafdaacSMauro Carvalho Chehab 			return;
917ceafdaacSMauro Carvalho Chehab 		}
918ceafdaacSMauro Carvalho Chehab 		pipe = to_isp_pipeline(&stat->subdev.entity);
919ceafdaacSMauro Carvalho Chehab 		stat->frame_number = atomic_read(&pipe->frame_number);
920ceafdaacSMauro Carvalho Chehab 
921ceafdaacSMauro Carvalho Chehab 		/*
922ceafdaacSMauro Carvalho Chehab 		 * Before this point, 'ret' stores the buffer's status if it's
923ceafdaacSMauro Carvalho Chehab 		 * ready to be processed. Afterwards, it holds the status if
924ceafdaacSMauro Carvalho Chehab 		 * it was processed successfully.
925ceafdaacSMauro Carvalho Chehab 		 */
926ceafdaacSMauro Carvalho Chehab 		ret = isp_stat_buf_process(stat, ret);
927ceafdaacSMauro Carvalho Chehab 
928ceafdaacSMauro Carvalho Chehab 		if (likely(!stat->sbl_ovl_recover)) {
929ceafdaacSMauro Carvalho Chehab 			stat->ops->setup_regs(stat, stat->priv);
930ceafdaacSMauro Carvalho Chehab 		} else {
931ceafdaacSMauro Carvalho Chehab 			/*
932ceafdaacSMauro Carvalho Chehab 			 * Using recover config to increase the chance to have
933ceafdaacSMauro Carvalho Chehab 			 * a good buffer processing and make the H3A module to
934ceafdaacSMauro Carvalho Chehab 			 * go back to a valid state.
935ceafdaacSMauro Carvalho Chehab 			 */
936ceafdaacSMauro Carvalho Chehab 			stat->update = 1;
937ceafdaacSMauro Carvalho Chehab 			stat->ops->setup_regs(stat, stat->recover_priv);
938ceafdaacSMauro Carvalho Chehab 			stat->sbl_ovl_recover = 0;
939ceafdaacSMauro Carvalho Chehab 
940ceafdaacSMauro Carvalho Chehab 			/*
941ceafdaacSMauro Carvalho Chehab 			 * Set 'update' in case of the module needs to use
942ceafdaacSMauro Carvalho Chehab 			 * regular configuration after next buffer.
943ceafdaacSMauro Carvalho Chehab 			 */
944ceafdaacSMauro Carvalho Chehab 			stat->update = 1;
945ceafdaacSMauro Carvalho Chehab 		}
946ceafdaacSMauro Carvalho Chehab 
947ceafdaacSMauro Carvalho Chehab 		isp_stat_buf_insert_magic(stat, stat->active_buf);
948ceafdaacSMauro Carvalho Chehab 
949ceafdaacSMauro Carvalho Chehab 		/*
950ceafdaacSMauro Carvalho Chehab 		 * Hack: H3A modules may access invalid memory address or send
951ceafdaacSMauro Carvalho Chehab 		 * corrupted data to userspace if more than 1 SBL overflow
952ceafdaacSMauro Carvalho Chehab 		 * happens in a row without re-writing its buffer's start memory
953ceafdaacSMauro Carvalho Chehab 		 * address in the meantime. Such situation is avoided if the
954ceafdaacSMauro Carvalho Chehab 		 * module is not immediately re-enabled when the ISR misses the
955ceafdaacSMauro Carvalho Chehab 		 * timing to process the buffer and to setup the registers.
956ceafdaacSMauro Carvalho Chehab 		 * Because of that, pcr_enable(1) was moved to inside this 'if'
957ceafdaacSMauro Carvalho Chehab 		 * block. But the next interruption will still happen as during
958ceafdaacSMauro Carvalho Chehab 		 * pcr_enable(0) the module was busy.
959ceafdaacSMauro Carvalho Chehab 		 */
960ceafdaacSMauro Carvalho Chehab 		isp_stat_pcr_enable(stat, 1);
961ceafdaacSMauro Carvalho Chehab 		spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
962ceafdaacSMauro Carvalho Chehab 	} else {
963ceafdaacSMauro Carvalho Chehab 		/*
964ceafdaacSMauro Carvalho Chehab 		 * If a SBL overflow occurs and the H3A driver misses the timing
965ceafdaacSMauro Carvalho Chehab 		 * to process the buffer, stat->buf_err is set and won't be
966ceafdaacSMauro Carvalho Chehab 		 * cleared now. So the next buffer will be correctly ignored.
967ceafdaacSMauro Carvalho Chehab 		 * It's necessary due to a hw issue which makes the next H3A
968ceafdaacSMauro Carvalho Chehab 		 * buffer to start from the memory address where the previous
969ceafdaacSMauro Carvalho Chehab 		 * one stopped, instead of start where it was configured to.
970ceafdaacSMauro Carvalho Chehab 		 * Do not "stat->buf_err = 0" here.
971ceafdaacSMauro Carvalho Chehab 		 */
972ceafdaacSMauro Carvalho Chehab 
973ceafdaacSMauro Carvalho Chehab 		if (stat->ops->buf_process)
974ceafdaacSMauro Carvalho Chehab 			/*
975ceafdaacSMauro Carvalho Chehab 			 * Driver may need to erase current data prior to
976ceafdaacSMauro Carvalho Chehab 			 * process a new buffer. If it misses the timing, the
977ceafdaacSMauro Carvalho Chehab 			 * next buffer might be wrong. So should be ignored.
978ceafdaacSMauro Carvalho Chehab 			 * It happens only for Histogram.
979ceafdaacSMauro Carvalho Chehab 			 */
980ceafdaacSMauro Carvalho Chehab 			atomic_set(&stat->buf_err, 1);
981ceafdaacSMauro Carvalho Chehab 
982ceafdaacSMauro Carvalho Chehab 		ret = STAT_NO_BUF;
983ceafdaacSMauro Carvalho Chehab 		dev_dbg(stat->isp->dev,
984ceafdaacSMauro Carvalho Chehab 			"%s: cannot process buffer, device is busy.\n",
985ceafdaacSMauro Carvalho Chehab 			stat->subdev.name);
986ceafdaacSMauro Carvalho Chehab 	}
987ceafdaacSMauro Carvalho Chehab 
988ceafdaacSMauro Carvalho Chehab out:
989ceafdaacSMauro Carvalho Chehab 	stat->buf_processing = 0;
990ceafdaacSMauro Carvalho Chehab 	isp_stat_queue_event(stat, ret != STAT_BUF_DONE);
991ceafdaacSMauro Carvalho Chehab }
992ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_isr(struct ispstat * stat)993ceafdaacSMauro Carvalho Chehab void omap3isp_stat_isr(struct ispstat *stat)
994ceafdaacSMauro Carvalho Chehab {
995ceafdaacSMauro Carvalho Chehab 	__stat_isr(stat, 0);
996ceafdaacSMauro Carvalho Chehab }
997ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_dma_isr(struct ispstat * stat)998ceafdaacSMauro Carvalho Chehab void omap3isp_stat_dma_isr(struct ispstat *stat)
999ceafdaacSMauro Carvalho Chehab {
1000ceafdaacSMauro Carvalho Chehab 	__stat_isr(stat, 1);
1001ceafdaacSMauro Carvalho Chehab }
1002ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_subscribe_event(struct v4l2_subdev * subdev,struct v4l2_fh * fh,struct v4l2_event_subscription * sub)1003ceafdaacSMauro Carvalho Chehab int omap3isp_stat_subscribe_event(struct v4l2_subdev *subdev,
1004ceafdaacSMauro Carvalho Chehab 				  struct v4l2_fh *fh,
1005ceafdaacSMauro Carvalho Chehab 				  struct v4l2_event_subscription *sub)
1006ceafdaacSMauro Carvalho Chehab {
1007ceafdaacSMauro Carvalho Chehab 	struct ispstat *stat = v4l2_get_subdevdata(subdev);
1008ceafdaacSMauro Carvalho Chehab 
1009ceafdaacSMauro Carvalho Chehab 	if (sub->type != stat->event_type)
1010ceafdaacSMauro Carvalho Chehab 		return -EINVAL;
1011ceafdaacSMauro Carvalho Chehab 
1012ceafdaacSMauro Carvalho Chehab 	return v4l2_event_subscribe(fh, sub, STAT_NEVENTS, NULL);
1013ceafdaacSMauro Carvalho Chehab }
1014ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_unsubscribe_event(struct v4l2_subdev * subdev,struct v4l2_fh * fh,struct v4l2_event_subscription * sub)1015ceafdaacSMauro Carvalho Chehab int omap3isp_stat_unsubscribe_event(struct v4l2_subdev *subdev,
1016ceafdaacSMauro Carvalho Chehab 				    struct v4l2_fh *fh,
1017ceafdaacSMauro Carvalho Chehab 				    struct v4l2_event_subscription *sub)
1018ceafdaacSMauro Carvalho Chehab {
1019ceafdaacSMauro Carvalho Chehab 	return v4l2_event_unsubscribe(fh, sub);
1020ceafdaacSMauro Carvalho Chehab }
1021ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_unregister_entities(struct ispstat * stat)1022ceafdaacSMauro Carvalho Chehab void omap3isp_stat_unregister_entities(struct ispstat *stat)
1023ceafdaacSMauro Carvalho Chehab {
1024ceafdaacSMauro Carvalho Chehab 	v4l2_device_unregister_subdev(&stat->subdev);
1025ceafdaacSMauro Carvalho Chehab }
1026ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_register_entities(struct ispstat * stat,struct v4l2_device * vdev)1027ceafdaacSMauro Carvalho Chehab int omap3isp_stat_register_entities(struct ispstat *stat,
1028ceafdaacSMauro Carvalho Chehab 				    struct v4l2_device *vdev)
1029ceafdaacSMauro Carvalho Chehab {
1030ceafdaacSMauro Carvalho Chehab 	stat->subdev.dev = vdev->mdev->dev;
1031ceafdaacSMauro Carvalho Chehab 
1032ceafdaacSMauro Carvalho Chehab 	return v4l2_device_register_subdev(vdev, &stat->subdev);
1033ceafdaacSMauro Carvalho Chehab }
1034ceafdaacSMauro Carvalho Chehab 
isp_stat_init_entities(struct ispstat * stat,const char * name,const struct v4l2_subdev_ops * sd_ops)1035ceafdaacSMauro Carvalho Chehab static int isp_stat_init_entities(struct ispstat *stat, const char *name,
1036ceafdaacSMauro Carvalho Chehab 				  const struct v4l2_subdev_ops *sd_ops)
1037ceafdaacSMauro Carvalho Chehab {
1038ceafdaacSMauro Carvalho Chehab 	struct v4l2_subdev *subdev = &stat->subdev;
1039ceafdaacSMauro Carvalho Chehab 	struct media_entity *me = &subdev->entity;
1040ceafdaacSMauro Carvalho Chehab 
1041ceafdaacSMauro Carvalho Chehab 	v4l2_subdev_init(subdev, sd_ops);
1042*8cdd708fSHans Verkuil 	snprintf(subdev->name, sizeof(subdev->name), "OMAP3 ISP %s", name);
1043ceafdaacSMauro Carvalho Chehab 	subdev->grp_id = BIT(16);	/* group ID for isp subdevs */
1044ceafdaacSMauro Carvalho Chehab 	subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
1045ceafdaacSMauro Carvalho Chehab 	v4l2_set_subdevdata(subdev, stat);
1046ceafdaacSMauro Carvalho Chehab 
1047ceafdaacSMauro Carvalho Chehab 	stat->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
1048ceafdaacSMauro Carvalho Chehab 	me->ops = NULL;
1049ceafdaacSMauro Carvalho Chehab 
1050ceafdaacSMauro Carvalho Chehab 	return media_entity_pads_init(me, 1, &stat->pad);
1051ceafdaacSMauro Carvalho Chehab }
1052ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_init(struct ispstat * stat,const char * name,const struct v4l2_subdev_ops * sd_ops)1053ceafdaacSMauro Carvalho Chehab int omap3isp_stat_init(struct ispstat *stat, const char *name,
1054ceafdaacSMauro Carvalho Chehab 		       const struct v4l2_subdev_ops *sd_ops)
1055ceafdaacSMauro Carvalho Chehab {
1056ceafdaacSMauro Carvalho Chehab 	int ret;
1057ceafdaacSMauro Carvalho Chehab 
1058ceafdaacSMauro Carvalho Chehab 	stat->buf = kcalloc(STAT_MAX_BUFS, sizeof(*stat->buf), GFP_KERNEL);
1059ceafdaacSMauro Carvalho Chehab 	if (!stat->buf)
1060ceafdaacSMauro Carvalho Chehab 		return -ENOMEM;
1061ceafdaacSMauro Carvalho Chehab 
1062ceafdaacSMauro Carvalho Chehab 	isp_stat_buf_clear(stat);
1063ceafdaacSMauro Carvalho Chehab 	mutex_init(&stat->ioctl_lock);
1064ceafdaacSMauro Carvalho Chehab 	atomic_set(&stat->buf_err, 0);
1065ceafdaacSMauro Carvalho Chehab 
1066ceafdaacSMauro Carvalho Chehab 	ret = isp_stat_init_entities(stat, name, sd_ops);
1067ceafdaacSMauro Carvalho Chehab 	if (ret < 0) {
1068ceafdaacSMauro Carvalho Chehab 		mutex_destroy(&stat->ioctl_lock);
1069ceafdaacSMauro Carvalho Chehab 		kfree(stat->buf);
1070ceafdaacSMauro Carvalho Chehab 	}
1071ceafdaacSMauro Carvalho Chehab 
1072ceafdaacSMauro Carvalho Chehab 	return ret;
1073ceafdaacSMauro Carvalho Chehab }
1074ceafdaacSMauro Carvalho Chehab 
omap3isp_stat_cleanup(struct ispstat * stat)1075ceafdaacSMauro Carvalho Chehab void omap3isp_stat_cleanup(struct ispstat *stat)
1076ceafdaacSMauro Carvalho Chehab {
1077ceafdaacSMauro Carvalho Chehab 	media_entity_cleanup(&stat->subdev.entity);
1078ceafdaacSMauro Carvalho Chehab 	mutex_destroy(&stat->ioctl_lock);
1079ceafdaacSMauro Carvalho Chehab 	isp_stat_bufs_free(stat);
1080ceafdaacSMauro Carvalho Chehab 	kfree(stat->buf);
1081ceafdaacSMauro Carvalho Chehab 	kfree(stat->priv);
1082ceafdaacSMauro Carvalho Chehab 	kfree(stat->recover_priv);
1083ceafdaacSMauro Carvalho Chehab }
1084