aboutsummaryrefslogtreecommitdiff
path: root/arch/arm/mach-s3c2440/camera/v4l2_api.c
blob: 13aed36d917eb0ef79abec0c51d7c40a7a4165bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
 * . 2004-01-03: SW.LEE <hitchcar@sec.samsung.com>
 *   
 * This file is subject to the terms and conditions of the GNU General Public
 * License 2. See the file COPYING in the main directory of this archive
 * for more details.
 */

#include <linux/config.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/irq.h>
#include <linux/tqueue.h>
#include <linux/locks.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/miscdevice.h>
#include <linux/wait.h>

#include <asm/io.h>
#include <asm/semaphore.h>
#include <asm/hardware.h>
#include <asm/uaccess.h>

#include <asm/arch/cpu_s3c2440.h>
#include <asm/arch/S3C2440.h>

#include "camif.h"
#include "videodev.h"

/* 
  Codec_formats/Preview_format[0] must be same to initial value of 
  preview_init_param/codec_init_param 
*/

const struct v4l2_fmtdesc codec_formats[] = {
	{
		.index     = 0,
		.type      = V4L2_BUF_TYPE_VIDEO_CAPTURE,
//		.flags     = FORMAT_FLAGS_PLANAR,
		.description = "4:2:2, planar, Y-Cb-Cr",
		.pixelformat = V4L2_PIX_FMT_YUV422P,

	},{
		.index    = 1,
		.type     = V4L2_BUF_TYPE_VIDEO_CAPTURE,
//		.flags    = FORMAT_FLAGS_PLANAR,
		.name     = "4:2:0, planar, Y-Cb-Cr",
		.fourcc   = V4L2_PIX_FMT_YUV420,
	}
};


/* Todo
   FIMC V4L2_PIX_FMT_RGB565 is not same to that of V4L2spec 
   and so we need image convert to FIMC V4l2_PIX_FMT_RGB565.
*/
const struct v4l2_fmtdesc preview_formats[] = {
	{
		.index    = 1,
		.type     = V4L2_BUF_TYPE_VIDEO_CAPTURE,
		.description = "16 bpp RGB, le",
		.fourcc   = V4L2_PIX_FMT_RGB565,
//		.flags    = FORMAT_FLAGS_PACKED,
	},
	{
		.index    = 0,
		.type     = V4L2_BUF_TYPE_VIDEO_CAPTURE,
//		.flags    = FORMAT_FLAGS_PACKED,
		.description = "32 bpp RGB, le",
		.fourcc   = V4L2_PIX_FMT_BGR32,
	}
}

#define NUM_F       ARRARY_SIZE(preview_formats)


/* 
 * This function and v4l2 structure made for V4L2 API functions 
 *     App <--> v4l2 <--> logical param <--> hardware
 */
static int camif_get_v4l2(camif_cfg_t *cfg)
{
	return 0;
}


/*
** Gives the depth of a video4linux2 fourcc aka pixel format in bits.
*/
static int pixfmt2depth(int pixfmt,int *fmtptr)
{
	int fmt, depth;    
	
	switch (pixfmt) {
	case V4L2_PIX_FMT_RGB565:
	case V4L2_PIX_FMT_RGB565X:
		fmt = CAMIF_RGB_16;
		depth = 16;
		break;
	case V4L2_PIX_FMT_BGR24: /* Not tested */
	case V4L2_PIX_FMT_RGB24:
		fmt = CAMIF_RGB_24;
		depth = 24;
		break;
	case V4L2_PIX_FMT_BGR32:
	case V4L2_PIX_FMT_RGB32:
		fmt = CAMIF_RGB_24;
		depth 32;
		break;
	case V4L2_PIX_FMT_GREY:	/* Not tested  */
		fmt = CAMIF_OUT_YCBCR420;
		depth = 8;
		break;
	case V4L2_PIX_FMT_YUYV:
	case V4L2_PIX_FMT_UYVY:
	case V4L2_PIX_FMT_YUV422P:
		fmt = CAMIF_OUT_YCBCR422;
		depth = 16;
		break;
	case V4L2_PIX_FMT_YUV420:
		fmt = CAMIF_OUT_YCBCR420;
		depth = 12;
		break;
	}
	if (fmtptr) *fmtptr = fmt;		
	return depth;
}



static int camif_s_v4l2(camif_cfg_t *cfg)
{
	int num = cfg->v2.used_fmt;

	if ( !(cfg->v2.status&CAMIF_V4L2_INIT)) {
		int depth;
		int fourcc = v2.fmtdesc[num].pixelformat;

		/* To define v4l2_fmtsdesc */
		if (cfg->dma_type == CAMIF_CODEC)
			cfg->v2->fmtdesc = codec_formats;
		else
			cfg->v2->fmtdesc = preview_formats;

		/* To define v4l2_format used currently */
		cfg->v2.fmt.width    = cfg->target_x;
		cfg->v2.fmt.height   = cfg->target_y;
		cfg->v2.fmt.field    = V4L2_FIELD_NONE;
		cfg->v2.fmt.pixelformat = fourcc;
		depth       = pixfmt2depth(fourcc,NULL);
		cfg->v2.fmt.bytesperline= cfg->v2.fmt.width*depth >> 3;
		cfg->v2.fmt.sizeimage = 
                       cfg->v2.fmt.height * cfg->v2.fmt.bytesperline;

		/* To define v4l2_input */
		cfg->v2.input.index  = 0;
		if (cfg->dma_type == CAMIF_CODEC)
			snprintf(cfg->v2.input.name, 31, "CAMIF CODEC");
		else
			snprintf(cfg->v2.input.name, 31, "CAMIF PREVIEW");
		cfg->v2.input.type = V4L2_INPUT_TYPE_CAMERA;
			
		/* Write the Status of v4l2 machine */
		cfg->v2.status |= CAMIF_V4L2_INIT;
	 }
	return 0;
}


static int camif_g_fmt(camif_cfg_t *cfg, struct v4l2_format *f)
{
	int size = sizeof(struct v4l2_pix_format);

	switch (f->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
		memset(&f->fmt.pix,0,size);
		memcpy(&f->fmt.pix,&cfg->v2.fmt,size);
		return 0;
	default:
		return -EINVAL;
	}
}


/* Copy v4l2 parameter into other element of camif_cfg_t */
static int camif_s_try(camif_cfg_t *cfg, int f)
{
	int fmt;
	cfg->target_x = cfg->v2.fmt.width;
	cfg->target_y = cfg->v2.fmt.height;
	pixfmt2depth(cfg->v2.fmt.pixelformat,&fmt);
	cfg->fmt = fmt;
	camif_dynamic_conf(cfg);
}


static int camif_s_fmt(camif_cfg_t *cfg, struct v4l2_format *f)
{
	int retval;
	
	switch (f->type) {
	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
	{
		/* update our state informations */
//		down(&fh->cap.lock);
		cfg->v2.fmt          = f->pix;
		cfg->v2.status       |= CAMIF_v4L2_DIRTY;
		camif_dynamic_conf(cfg);
		cfg->v2.status      &= ~CAMIF_v4L2_DIRTY; /* dummy ? */
//		up(&fh->cap.lock);
		
		return 0;
	}
	default:
		return -EINVAL;
	}

}

/* Refer ioctl of  videodeX.c  and bttv-driver.c */
int camif_do_ioctl
(struct inode *inode, struct file *file,unsigned int cmd, void * arg)
{
	camif_cfg_t *cfg = file->private_data;
	int ret = 0;

	switch (cmd) {
        case VIDIOC_QUERYCAP:
		{
			struct v4l2_capability *cap = arg;
                                                                                                      
			strcpy(cap->driver,"Fimc Camera");
			strlcpy(cap->card,cfg->v->name,sizeof(cap->card));
			sprintf(cap->bus_info,"FIMC 2.0 AHB Bus");
			cap->version = 0;
			cap->capabilities =
				V4L2_CAP_VIDEO_CAPTURE |V4L2_CAP_READWRITE;
			return 0;
		}
	case VIDIOC_G_FMT:
		{
			struct v4l2_format *f = arg;
			return camif_g_fmt(cfg,f);
		}
	case VIDIOC_S_FMT:
		{
			struct v4l2_format *f = arg;
			return camif_s_fmt(cfg,f);
		}

	case VIDIOC_ENUM_FMT:
		{
			struct v4l2_fmtdesc *f = arg;
			enum v4l2_buf_type type = f->type;
			int index = f->index;

			if (index >= NUM_F) 
				return -EINVAL;
			switch (f->type) {
			case V4L2_BUF_TYPE_VIDEO_CAPTURE:
				break;
			case V4L2_BUF_TYPE_VIDEO_OVERLAY:
			case V4L2_BUF_TYPE_VBI_CAPTURE:
					default:
			return -EINVAL;
			}
			memset(f,0,sizeof(*f));
			memcpy(f,cfg->v2.fmtdesc+index,sizeof(*f));
			return 0;
		}
	case VIDIOC_G_INPUT:
		{
			u32 *i = arg;
			*i = cfg->v2.input;
			return 0;
		}
	case VIDIOC_S_INPUT:
		{
			int index = *((int *)arg);
			if (index != 0) 
				return -EINVAL;
			cfg->v2.input.index = index;
			return 0;
		}
		
	default:
		return -ENOIOCTLCMD; /* errno.h */
	} /* End of Switch  */


}







/* 
 * Local variables:
 * tab-width: 8
 *  c-indent-level: 8
 *  c-basic-offset: 8
 *  c-set-style: "K&R"
 * End:
 */