summaryrefslogtreecommitdiff
path: root/drivers/media/video/mxc/capture/ipu_still.c
blob: 22cf3f51e1cb226c768c63be8de638c1294f22b6 (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
/*
 * Copyright 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/*!
 * @file ipu_still.c
 *
 * @brief IPU Use case for still image capture
 *
 * @ingroup IPU
 */

#include <linux/ipu.h>
#include <linux/semaphore.h>
#include <linux/ipu.h>
#include "mxc_v4l2_capture.h"
#include "ipu_prp_sw.h"

static int callback_eof_flag;
#ifndef CONFIG_MXC_IPU_V1
static int buffer_num;
#endif

#ifdef CONFIG_MXC_IPU_V1
static int callback_flag;
/*
 * Function definitions
 */
/*!
 * CSI EOF callback function.
 *
 * @param irq       int irq line
 * @param dev_id    void * device id
 *
 * @return status   IRQ_HANDLED for handled
 */
static irqreturn_t prp_csi_eof_callback(int irq, void *dev_id)
{
	ipu_select_buffer(CSI_MEM, IPU_OUTPUT_BUFFER,
			  callback_flag%2 ? 1 : 0);
	if (callback_flag == 0)
		ipu_enable_channel(CSI_MEM);

	callback_flag++;
	return IRQ_HANDLED;
}
#endif

/*!
 * CSI callback function.
 *
 * @param irq       int irq line
 * @param dev_id    void * device id
 *
 * @return status   IRQ_HANDLED for handled
 */
static irqreturn_t prp_still_callback(int irq, void *dev_id)
{
	cam_data *cam = (cam_data *) dev_id;

	callback_eof_flag++;
	if (callback_eof_flag < 5) {
#ifndef CONFIG_MXC_IPU_V1
		buffer_num = (buffer_num == 0) ? 1 : 0;
		ipu_select_buffer(CSI_MEM, IPU_OUTPUT_BUFFER, buffer_num);
#endif
	} else {
		cam->still_counter++;
		wake_up_interruptible(&cam->still_queue);
	}

	return IRQ_HANDLED;
}

/*!
 * start csi->mem task
 * @param private       struct cam_data * mxc capture instance
 *
 * @return  status
 */
static int prp_still_start(void *private)
{
	cam_data *cam = (cam_data *) private;
	u32 pixel_fmt;
	int err;
	ipu_channel_params_t params;
	ipu_channel_t channel;
	int ipu_irq_csi_out_eof;

	if (cam->v2f.fmt.pix.pixelformat == V4L2_PIX_FMT_YUV420)
		pixel_fmt = IPU_PIX_FMT_YUV420P;
	else if (cam->v2f.fmt.pix.pixelformat == V4L2_PIX_FMT_NV12)
		pixel_fmt = IPU_PIX_FMT_NV12;
	else if (cam->v2f.fmt.pix.pixelformat == V4L2_PIX_FMT_YUV422P)
		pixel_fmt = IPU_PIX_FMT_YUV422P;
	else if (cam->v2f.fmt.pix.pixelformat == V4L2_PIX_FMT_UYVY)
		pixel_fmt = IPU_PIX_FMT_UYVY;
	else if (cam->v2f.fmt.pix.pixelformat == V4L2_PIX_FMT_BGR24)
		pixel_fmt = IPU_PIX_FMT_BGR24;
	else if (cam->v2f.fmt.pix.pixelformat == V4L2_PIX_FMT_RGB24)
		pixel_fmt = IPU_PIX_FMT_RGB24;
	else if (cam->v2f.fmt.pix.pixelformat == V4L2_PIX_FMT_RGB565)
		pixel_fmt = IPU_PIX_FMT_RGB565;
	else if (cam->v2f.fmt.pix.pixelformat == V4L2_PIX_FMT_BGR32)
		pixel_fmt = IPU_PIX_FMT_BGR32;
	else if (cam->v2f.fmt.pix.pixelformat == V4L2_PIX_FMT_RGB32)
		pixel_fmt = IPU_PIX_FMT_RGB32;
	else {
		printk(KERN_ERR "format not supported\n");
		return -EINVAL;
	}

	ipu_csi_enable_mclk_if(CSI_MCLK_RAW, cam->csi, true, true);

	if (cam->csi == 0) {
		channel = CSI_MEM0;
		ipu_irq_csi_out_eof = IPU_IRQ_CSI0_OUT_EOF;
	}
	else {
		channel = CSI_MEM1;
		ipu_irq_csi_out_eof = IPU_IRQ_CSI1_OUT_EOF;
	}

	memset(&params, 0, sizeof(params));
	params.csi_mem.csi = cam->csi;
	err = ipu_init_channel(channel, &params);
	if (err != 0)
		return err;

	err = ipu_init_channel_buffer(channel, IPU_OUTPUT_BUFFER,
				      pixel_fmt, cam->v2f.fmt.pix.width,
				      cam->v2f.fmt.pix.height,
				      cam->v2f.fmt.pix.width, IPU_ROTATE_NONE,
				      cam->still_buf[0], cam->still_buf[1],
				      0, 0);
	if (err != 0)
		return err;

#ifdef CONFIG_MXC_IPU_V1
	ipu_clear_irq(IPU_IRQ_SENSOR_OUT_EOF);
	err = ipu_request_irq(IPU_IRQ_SENSOR_OUT_EOF, prp_still_callback,
			      0, "Mxc Camera", cam);
	if (err != 0) {
		printk(KERN_ERR "Error registering irq.\n");
		return err;
	}
	callback_flag = 0;
	callback_eof_flag = 0;
	ipu_clear_irq(IPU_IRQ_SENSOR_EOF);
	err = ipu_request_irq(IPU_IRQ_SENSOR_EOF, prp_csi_eof_callback,
			      0, "Mxc Camera", NULL);
	if (err != 0) {
		printk(KERN_ERR "Error IPU_IRQ_SENSOR_EOF \n");
		return err;
	}
#else

	ipu_clear_irq(ipu_irq_csi_out_eof);
	err = ipu_request_irq(ipu_irq_csi_out_eof, prp_still_callback,
			      0, "Mxc Camera", cam);
	if (err != 0) {
		printk(KERN_ERR "Error registering irq.\n");
		return err;
	}

	callback_eof_flag = 0;
	ipu_select_buffer(channel, IPU_OUTPUT_BUFFER, 0);
	ipu_enable_channel(channel);

	ipu_enable_csi(cam->csi);
#endif

	return err;
}

/*!
 * stop csi->mem encoder task
 * @param private       struct cam_data * mxc capture instance
 *
 * @return  status
 */
static int prp_still_stop(void *private)
{
	cam_data *cam = (cam_data *) private;
	int err = 0;
	ipu_channel_t channel;
	int ipu_irq_csi_out_eof;

	if (cam->csi == 0) {
		channel = CSI_MEM0;
		ipu_irq_csi_out_eof = IPU_IRQ_CSI0_OUT_EOF;
	}
	else {
		channel = CSI_MEM1;
		ipu_irq_csi_out_eof = IPU_IRQ_CSI1_OUT_EOF;
	}

#ifdef CONFIG_MXC_IPU_V1
	ipu_free_irq(IPU_IRQ_SENSOR_EOF, NULL);
	ipu_free_irq(IPU_IRQ_SENSOR_OUT_EOF, cam);
#else
	ipu_free_irq(ipu_irq_csi_out_eof, cam);
#endif

	ipu_disable_csi(cam->csi);
	ipu_disable_channel(channel, true);
	ipu_uninit_channel(channel);
	ipu_csi_enable_mclk_if(CSI_MCLK_RAW, cam->csi, false, false);

	return err;
}

/*!
 * function to select CSI_MEM as the working path
 *
 * @param private       struct cam_data * mxc capture instance
 *
 * @return  status
 */
int prp_still_select(void *private)
{
	cam_data *cam = (cam_data *) private;

	if (cam) {
		cam->csi_start = prp_still_start;
		cam->csi_stop = prp_still_stop;
	}

	return 0;
}

/*!
 * function to de-select CSI_MEM as the working path
 *
 * @param private       struct cam_data * mxc capture instance
 *
 * @return  status
 */
int prp_still_deselect(void *private)
{
	cam_data *cam = (cam_data *) private;
	int err = 0;

	err = prp_still_stop(cam);

	if (cam) {
		cam->csi_start = NULL;
		cam->csi_stop = NULL;
	}

	return err;
}

/*!
 * Init the Encorder channels
 *
 * @return  Error code indicating success or failure
 */
__init int prp_still_init(void)
{
	return 0;
}

/*!
 * Deinit the Encorder channels
 *
 */
void __exit prp_still_exit(void)
{
}

module_init(prp_still_init);
module_exit(prp_still_exit);

EXPORT_SYMBOL(prp_still_select);
EXPORT_SYMBOL(prp_still_deselect);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("IPU PRP STILL IMAGE Driver");
MODULE_LICENSE("GPL");