blob: a74bfb9afc8d0dd438101c8e915c7992d19de697 [file] [log] [blame]
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001/*
2 * The Marvell camera core. This device appears in a number of settings,
3 * so it needs platform-specific support outside of the core.
4 *
5 * Copyright 2011 Jonathan Corbet corbet@lwn.net
6 */
7#include <linux/kernel.h>
8#include <linux/module.h>
9#include <linux/fs.h>
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030010#include <linux/mm.h>
11#include <linux/i2c.h>
12#include <linux/interrupt.h>
13#include <linux/spinlock.h>
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030014#include <linux/slab.h>
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030015#include <linux/device.h>
16#include <linux/wait.h>
17#include <linux/list.h>
18#include <linux/dma-mapping.h>
19#include <linux/delay.h>
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030020#include <linux/vmalloc.h>
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030021#include <linux/io.h>
Libin Yang05fed812013-07-03 01:55:58 -030022#include <linux/clk.h>
Jonathan Corbet362d45b2011-06-20 16:14:37 -030023#include <linux/videodev2.h>
24#include <media/v4l2-device.h>
25#include <media/v4l2-ioctl.h>
Javier Martin593403c2013-01-29 07:58:48 -030026#include <media/v4l2-ctrls.h>
Hans Verkuil87d18432015-03-05 13:05:24 -030027#include <media/v4l2-event.h>
Mauro Carvalho Chehabb5dcee22015-11-10 12:01:44 -020028#include <media/i2c/ov7670.h>
Jonathan Corbet362d45b2011-06-20 16:14:37 -030029#include <media/videobuf2-vmalloc.h>
Jonathan Corbeta9b36e82011-06-20 16:14:40 -030030#include <media/videobuf2-dma-contig.h>
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -030031#include <media/videobuf2-dma-sg.h>
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030032
33#include "mcam-core.h"
34
Jonathan Corbet74984692011-07-08 17:50:49 -030035#ifdef MCAM_MODE_VMALLOC
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030036/*
37 * Internal DMA buffer management. Since the controller cannot do S/G I/O,
38 * we must have physically contiguous buffers to bring frames into.
39 * These parameters control how many buffers we use, whether we
40 * allocate them at load time (better chance of success, but nails down
41 * memory) or when somebody tries to use the camera (riskier), and,
42 * for load-time allocation, how big they should be.
43 *
44 * The controller can cycle through three buffers. We could use
45 * more by flipping pointers around, but it probably makes little
46 * sense.
47 */
48
Rusty Russell90ab5ee2012-01-13 09:32:20 +103049static bool alloc_bufs_at_read;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030050module_param(alloc_bufs_at_read, bool, 0444);
51MODULE_PARM_DESC(alloc_bufs_at_read,
52 "Non-zero value causes DMA buffers to be allocated when the "
53 "video capture device is read, rather than at module load "
54 "time. This saves memory, but decreases the chances of "
Jonathan Corbeta9b36e82011-06-20 16:14:40 -030055 "successfully getting those buffers. This parameter is "
56 "only used in the vmalloc buffer mode");
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030057
58static int n_dma_bufs = 3;
59module_param(n_dma_bufs, uint, 0644);
60MODULE_PARM_DESC(n_dma_bufs,
61 "The number of DMA buffers to allocate. Can be either two "
62 "(saves memory, makes timing tighter) or three.");
63
64static int dma_buf_size = VGA_WIDTH * VGA_HEIGHT * 2; /* Worst case */
65module_param(dma_buf_size, uint, 0444);
66MODULE_PARM_DESC(dma_buf_size,
67 "The size of the allocated DMA buffers. If actual operating "
68 "parameters require larger buffers, an attempt to reallocate "
69 "will be made.");
Jonathan Corbet74984692011-07-08 17:50:49 -030070#else /* MCAM_MODE_VMALLOC */
Mauro Carvalho Chehaba7459a92014-09-03 15:44:54 -030071static const bool alloc_bufs_at_read;
Jonathan Corbet74984692011-07-08 17:50:49 -030072static const int n_dma_bufs = 3; /* Used by S/G_PARM */
73#endif /* MCAM_MODE_VMALLOC */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030074
Rusty Russell90ab5ee2012-01-13 09:32:20 +103075static bool flip;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030076module_param(flip, bool, 0444);
77MODULE_PARM_DESC(flip,
78 "If set, the sensor will be instructed to flip the image "
79 "vertically.");
80
Jonathan Corbeta9b36e82011-06-20 16:14:40 -030081static int buffer_mode = -1;
82module_param(buffer_mode, int, 0444);
83MODULE_PARM_DESC(buffer_mode,
84 "Set the buffer mode to be used; default is to go with what "
85 "the platform driver asks for. Set to 0 for vmalloc, 1 for "
86 "DMA contiguous.");
87
Jonathan Corbetabfa3df2011-06-11 14:46:43 -030088/*
89 * Status flags. Always manipulated with bit operations.
90 */
91#define CF_BUF0_VALID 0 /* Buffers valid - first three */
92#define CF_BUF1_VALID 1
93#define CF_BUF2_VALID 2
94#define CF_DMA_ACTIVE 3 /* A frame is incoming */
95#define CF_CONFIG_NEEDED 4 /* Must configure hardware */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -030096#define CF_SINGLE_BUFFER 5 /* Running with a single buffer */
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -030097#define CF_SG_RESTART 6 /* SG restart needed */
Libin Yang0a0b3fb2013-07-03 01:56:03 -030098#define CF_FRAME_SOF0 7 /* Frame 0 started */
99#define CF_FRAME_SOF1 8
100#define CF_FRAME_SOF2 9
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300101
102#define sensor_call(cam, o, f, args...) \
103 v4l2_subdev_call(cam->sensor, o, f, ##args)
104
105static struct mcam_format_struct {
106 __u8 *desc;
107 __u32 pixelformat;
108 int bpp; /* Bytes per pixel */
Libin Yangad6ac452013-07-03 01:56:02 -0300109 bool planar;
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300110 u32 mbus_code;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300111} mcam_formats[] = {
112 {
113 .desc = "YUYV 4:2:2",
114 .pixelformat = V4L2_PIX_FMT_YUYV,
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300115 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300116 .bpp = 2,
Libin Yangad6ac452013-07-03 01:56:02 -0300117 .planar = false,
118 },
119 {
Hans Verkuil2a700d82015-04-13 11:18:51 -0300120 .desc = "YVYU 4:2:2",
121 .pixelformat = V4L2_PIX_FMT_YVYU,
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300122 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
Libin Yangad6ac452013-07-03 01:56:02 -0300123 .bpp = 2,
124 .planar = false,
125 },
126 {
Libin Yangad6ac452013-07-03 01:56:02 -0300127 .desc = "YUV 4:2:0 PLANAR",
128 .pixelformat = V4L2_PIX_FMT_YUV420,
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300129 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
Hans Verkuil47ba7db2015-03-09 17:14:36 -0300130 .bpp = 1,
Libin Yangad6ac452013-07-03 01:56:02 -0300131 .planar = true,
132 },
133 {
134 .desc = "YVU 4:2:0 PLANAR",
135 .pixelformat = V4L2_PIX_FMT_YVU420,
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300136 .mbus_code = MEDIA_BUS_FMT_YUYV8_2X8,
Hans Verkuil47ba7db2015-03-09 17:14:36 -0300137 .bpp = 1,
Libin Yangad6ac452013-07-03 01:56:02 -0300138 .planar = true,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300139 },
140 {
Hans Verkuilccf509f2015-04-24 06:52:47 -0300141 .desc = "XRGB 444",
142 .pixelformat = V4L2_PIX_FMT_XRGB444,
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300143 .mbus_code = MEDIA_BUS_FMT_RGB444_2X8_PADHI_LE,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300144 .bpp = 2,
Libin Yangad6ac452013-07-03 01:56:02 -0300145 .planar = false,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300146 },
147 {
148 .desc = "RGB 565",
149 .pixelformat = V4L2_PIX_FMT_RGB565,
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300150 .mbus_code = MEDIA_BUS_FMT_RGB565_2X8_LE,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300151 .bpp = 2,
Libin Yangad6ac452013-07-03 01:56:02 -0300152 .planar = false,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300153 },
154 {
155 .desc = "Raw RGB Bayer",
156 .pixelformat = V4L2_PIX_FMT_SBGGR8,
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300157 .mbus_code = MEDIA_BUS_FMT_SBGGR8_1X8,
Libin Yangad6ac452013-07-03 01:56:02 -0300158 .bpp = 1,
159 .planar = false,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300160 },
161};
162#define N_MCAM_FMTS ARRAY_SIZE(mcam_formats)
163
164static struct mcam_format_struct *mcam_find_format(u32 pixelformat)
165{
166 unsigned i;
167
168 for (i = 0; i < N_MCAM_FMTS; i++)
169 if (mcam_formats[i].pixelformat == pixelformat)
170 return mcam_formats + i;
171 /* Not found? Then return the first format. */
172 return mcam_formats;
173}
174
175/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300176 * The default format we use until somebody says otherwise.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300177 */
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300178static const struct v4l2_pix_format mcam_def_pix_format = {
179 .width = VGA_WIDTH,
180 .height = VGA_HEIGHT,
181 .pixelformat = V4L2_PIX_FMT_YUYV,
182 .field = V4L2_FIELD_NONE,
183 .bytesperline = VGA_WIDTH*2,
184 .sizeimage = VGA_WIDTH*VGA_HEIGHT*2,
Hans Verkuil2e6e6092015-03-05 05:19:23 -0300185 .colorspace = V4L2_COLORSPACE_SRGB,
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300186};
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300187
Boris BREZILLON27ffaeb2014-11-10 14:28:31 -0300188static const u32 mcam_def_mbus_code = MEDIA_BUS_FMT_YUYV8_2X8;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300189
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300190
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300191/*
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300192 * The two-word DMA descriptor format used by the Armada 610 and like. There
193 * Is a three-word format as well (set C1_DESC_3WORD) where the third
194 * word is a pointer to the next descriptor, but we don't use it. Two-word
195 * descriptors have to be contiguous in memory.
196 */
197struct mcam_dma_desc {
198 u32 dma_addr;
199 u32 segment_len;
200};
201
202/*
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300203 * Our buffer type for working with videobuf2. Note that the vb2
Junghak Sung2d700712015-09-22 10:30:30 -0300204 * developers have decreed that struct vb2_v4l2_buffer must be at the
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300205 * beginning of this structure.
206 */
207struct mcam_vb_buffer {
Junghak Sung2d700712015-09-22 10:30:30 -0300208 struct vb2_v4l2_buffer vb_buf;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300209 struct list_head queue;
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300210 struct mcam_dma_desc *dma_desc; /* Descriptor virtual address */
211 dma_addr_t dma_desc_pa; /* Descriptor physical address */
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300212};
213
Junghak Sung2d700712015-09-22 10:30:30 -0300214static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_v4l2_buffer *vb)
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300215{
216 return container_of(vb, struct mcam_vb_buffer, vb_buf);
217}
218
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300219/*
220 * Hand a completed buffer back to user space.
221 */
222static void mcam_buffer_done(struct mcam_camera *cam, int frame,
Junghak Sung2d700712015-09-22 10:30:30 -0300223 struct vb2_v4l2_buffer *vbuf)
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300224{
Junghak Sung2d700712015-09-22 10:30:30 -0300225 vbuf->vb2_buf.planes[0].bytesused = cam->pix_format.sizeimage;
226 vbuf->sequence = cam->buf_seq[frame];
227 vbuf->field = V4L2_FIELD_NONE;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200228 vbuf->vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300229 vb2_set_plane_payload(&vbuf->vb2_buf, 0, cam->pix_format.sizeimage);
230 vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE);
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300231}
232
233
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300234
235/*
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300236 * Debugging and related.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300237 */
238#define cam_err(cam, fmt, arg...) \
239 dev_err((cam)->dev, fmt, ##arg);
240#define cam_warn(cam, fmt, arg...) \
241 dev_warn((cam)->dev, fmt, ##arg);
242#define cam_dbg(cam, fmt, arg...) \
243 dev_dbg((cam)->dev, fmt, ##arg);
244
245
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300246/*
247 * Flag manipulation helpers
248 */
249static void mcam_reset_buffers(struct mcam_camera *cam)
250{
251 int i;
252
253 cam->next_buf = -1;
Libin Yang0a0b3fb2013-07-03 01:56:03 -0300254 for (i = 0; i < cam->nbufs; i++) {
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300255 clear_bit(i, &cam->flags);
Libin Yang0a0b3fb2013-07-03 01:56:03 -0300256 clear_bit(CF_FRAME_SOF0 + i, &cam->flags);
257 }
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300258}
259
260static inline int mcam_needs_config(struct mcam_camera *cam)
261{
262 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
263}
264
265static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
266{
267 if (needed)
268 set_bit(CF_CONFIG_NEEDED, &cam->flags);
269 else
270 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
271}
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300272
273/* ------------------------------------------------------------------- */
274/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300275 * Make the controller start grabbing images. Everything must
276 * be set up before doing this.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300277 */
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300278static void mcam_ctlr_start(struct mcam_camera *cam)
279{
280 /* set_bit performs a read, so no other barrier should be
281 needed here */
282 mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
283}
284
285static void mcam_ctlr_stop(struct mcam_camera *cam)
286{
287 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
288}
289
Libin Yang05fed812013-07-03 01:55:58 -0300290static void mcam_enable_mipi(struct mcam_camera *mcam)
291{
292 /* Using MIPI mode and enable MIPI */
293 cam_dbg(mcam, "camera: DPHY3=0x%x, DPHY5=0x%x, DPHY6=0x%x\n",
294 mcam->dphy[0], mcam->dphy[1], mcam->dphy[2]);
295 mcam_reg_write(mcam, REG_CSI2_DPHY3, mcam->dphy[0]);
296 mcam_reg_write(mcam, REG_CSI2_DPHY5, mcam->dphy[1]);
297 mcam_reg_write(mcam, REG_CSI2_DPHY6, mcam->dphy[2]);
298
299 if (!mcam->mipi_enabled) {
300 if (mcam->lane > 4 || mcam->lane <= 0) {
301 cam_warn(mcam, "lane number error\n");
302 mcam->lane = 1; /* set the default value */
303 }
304 /*
305 * 0x41 actives 1 lane
306 * 0x43 actives 2 lanes
307 * 0x45 actives 3 lanes (never happen)
308 * 0x47 actives 4 lanes
309 */
310 mcam_reg_write(mcam, REG_CSI2_CTRL0,
311 CSI2_C0_MIPI_EN | CSI2_C0_ACT_LANE(mcam->lane));
312 mcam_reg_write(mcam, REG_CLKCTRL,
313 (mcam->mclk_src << 29) | mcam->mclk_div);
314
315 mcam->mipi_enabled = true;
316 }
317}
318
319static void mcam_disable_mipi(struct mcam_camera *mcam)
320{
321 /* Using Parallel mode or disable MIPI */
322 mcam_reg_write(mcam, REG_CSI2_CTRL0, 0x0);
323 mcam_reg_write(mcam, REG_CSI2_DPHY3, 0x0);
324 mcam_reg_write(mcam, REG_CSI2_DPHY5, 0x0);
325 mcam_reg_write(mcam, REG_CSI2_DPHY6, 0x0);
326 mcam->mipi_enabled = false;
327}
328
Hans Verkuil6eb40d52015-03-09 16:59:59 -0300329static bool mcam_fmt_is_planar(__u32 pfmt)
330{
331 struct mcam_format_struct *f;
332
333 f = mcam_find_format(pfmt);
334 return f->planar;
335}
336
337static void mcam_write_yuv_bases(struct mcam_camera *cam,
338 unsigned frame, dma_addr_t base)
339{
340 struct v4l2_pix_format *fmt = &cam->pix_format;
341 u32 pixel_count = fmt->width * fmt->height;
342 dma_addr_t y, u = 0, v = 0;
343
344 y = base;
345
346 switch (fmt->pixelformat) {
Hans Verkuil6eb40d52015-03-09 16:59:59 -0300347 case V4L2_PIX_FMT_YUV420:
348 u = y + pixel_count;
349 v = u + pixel_count / 4;
350 break;
351 case V4L2_PIX_FMT_YVU420:
352 v = y + pixel_count;
353 u = v + pixel_count / 4;
354 break;
355 default:
356 break;
357 }
358
359 mcam_reg_write(cam, REG_Y0BAR + frame * 4, y);
360 if (mcam_fmt_is_planar(fmt->pixelformat)) {
361 mcam_reg_write(cam, REG_U0BAR + frame * 4, u);
362 mcam_reg_write(cam, REG_V0BAR + frame * 4, v);
363 }
364}
365
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300366/* ------------------------------------------------------------------- */
Jonathan Corbet74984692011-07-08 17:50:49 -0300367
368#ifdef MCAM_MODE_VMALLOC
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300369/*
370 * Code specific to the vmalloc buffer mode.
371 */
372
373/*
374 * Allocate in-kernel DMA buffers for vmalloc mode.
375 */
376static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
377{
378 int i;
379
380 mcam_set_config_needed(cam, 1);
381 if (loadtime)
382 cam->dma_buf_size = dma_buf_size;
383 else
384 cam->dma_buf_size = cam->pix_format.sizeimage;
385 if (n_dma_bufs > 3)
386 n_dma_bufs = 3;
387
388 cam->nbufs = 0;
389 for (i = 0; i < n_dma_bufs; i++) {
390 cam->dma_bufs[i] = dma_alloc_coherent(cam->dev,
391 cam->dma_buf_size, cam->dma_handles + i,
392 GFP_KERNEL);
393 if (cam->dma_bufs[i] == NULL) {
394 cam_warn(cam, "Failed to allocate DMA buffer\n");
395 break;
396 }
397 (cam->nbufs)++;
398 }
399
400 switch (cam->nbufs) {
401 case 1:
402 dma_free_coherent(cam->dev, cam->dma_buf_size,
403 cam->dma_bufs[0], cam->dma_handles[0]);
404 cam->nbufs = 0;
405 case 0:
406 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
407 return -ENOMEM;
408
409 case 2:
410 if (n_dma_bufs > 2)
411 cam_warn(cam, "Will limp along with only 2 buffers\n");
412 break;
413 }
414 return 0;
415}
416
417static void mcam_free_dma_bufs(struct mcam_camera *cam)
418{
419 int i;
420
421 for (i = 0; i < cam->nbufs; i++) {
422 dma_free_coherent(cam->dev, cam->dma_buf_size,
423 cam->dma_bufs[i], cam->dma_handles[i]);
424 cam->dma_bufs[i] = NULL;
425 }
426 cam->nbufs = 0;
427}
428
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300429
430/*
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300431 * Set up DMA buffers when operating in vmalloc mode
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300432 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300433static void mcam_ctlr_dma_vmalloc(struct mcam_camera *cam)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300434{
435 /*
Hans Verkuil6eb40d52015-03-09 16:59:59 -0300436 * Store the first two YUV buffers. Then either
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300437 * set the third if it exists, or tell the controller
438 * to just use two.
439 */
Hans Verkuil6eb40d52015-03-09 16:59:59 -0300440 mcam_write_yuv_bases(cam, 0, cam->dma_handles[0]);
441 mcam_write_yuv_bases(cam, 1, cam->dma_handles[1]);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300442 if (cam->nbufs > 2) {
Hans Verkuil6eb40d52015-03-09 16:59:59 -0300443 mcam_write_yuv_bases(cam, 2, cam->dma_handles[2]);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300444 mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
445 } else
446 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
Hans Verkuil7486af12013-05-29 06:59:44 -0300447 if (cam->chip_id == MCAM_CAFE)
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300448 mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300449}
450
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300451/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300452 * Copy data out to user space in the vmalloc case
453 */
454static void mcam_frame_tasklet(unsigned long data)
455{
456 struct mcam_camera *cam = (struct mcam_camera *) data;
457 int i;
458 unsigned long flags;
459 struct mcam_vb_buffer *buf;
460
461 spin_lock_irqsave(&cam->dev_lock, flags);
462 for (i = 0; i < cam->nbufs; i++) {
463 int bufno = cam->next_buf;
464
465 if (cam->state != S_STREAMING || bufno < 0)
466 break; /* I/O got stopped */
467 if (++(cam->next_buf) >= cam->nbufs)
468 cam->next_buf = 0;
469 if (!test_bit(bufno, &cam->flags))
470 continue;
471 if (list_empty(&cam->buffers)) {
Libin Yangf698957a2012-12-15 05:57:50 -0300472 cam->frame_state.singles++;
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300473 break; /* Leave it valid, hope for better later */
474 }
Libin Yangf698957a2012-12-15 05:57:50 -0300475 cam->frame_state.delivered++;
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300476 clear_bit(bufno, &cam->flags);
477 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
478 queue);
479 list_del_init(&buf->queue);
480 /*
481 * Drop the lock during the big copy. This *should* be safe...
482 */
483 spin_unlock_irqrestore(&cam->dev_lock, flags);
Junghak Sung2d700712015-09-22 10:30:30 -0300484 memcpy(vb2_plane_vaddr(&buf->vb_buf.vb2_buf, 0),
485 cam->dma_bufs[bufno],
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300486 cam->pix_format.sizeimage);
487 mcam_buffer_done(cam, bufno, &buf->vb_buf);
488 spin_lock_irqsave(&cam->dev_lock, flags);
489 }
490 spin_unlock_irqrestore(&cam->dev_lock, flags);
491}
492
493
Jonathan Corbet74984692011-07-08 17:50:49 -0300494/*
495 * Make sure our allocated buffers are up to the task.
496 */
497static int mcam_check_dma_buffers(struct mcam_camera *cam)
498{
499 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
500 mcam_free_dma_bufs(cam);
501 if (cam->nbufs == 0)
502 return mcam_alloc_dma_bufs(cam, 0);
503 return 0;
504}
505
506static void mcam_vmalloc_done(struct mcam_camera *cam, int frame)
507{
508 tasklet_schedule(&cam->s_tasklet);
509}
510
511#else /* MCAM_MODE_VMALLOC */
512
513static inline int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
514{
515 return 0;
516}
517
518static inline void mcam_free_dma_bufs(struct mcam_camera *cam)
519{
520 return;
521}
522
523static inline int mcam_check_dma_buffers(struct mcam_camera *cam)
524{
525 return 0;
526}
527
528
529
530#endif /* MCAM_MODE_VMALLOC */
531
532
533#ifdef MCAM_MODE_DMA_CONTIG
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300534/* ---------------------------------------------------------------------- */
535/*
536 * DMA-contiguous code.
537 */
Libin Yangad6ac452013-07-03 01:56:02 -0300538
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300539/*
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300540 * Set up a contiguous buffer for the given frame. Here also is where
541 * the underrun strategy is set: if there is no buffer available, reuse
542 * the buffer from the other BAR and set the CF_SINGLE_BUFFER flag to
543 * keep the interrupt handler from giving that buffer back to user
544 * space. In this way, we always have a buffer to DMA to and don't
545 * have to try to play games stopping and restarting the controller.
546 */
547static void mcam_set_contig_buffer(struct mcam_camera *cam, int frame)
548{
549 struct mcam_vb_buffer *buf;
Libin Yangad6ac452013-07-03 01:56:02 -0300550 dma_addr_t dma_handle;
Junghak Sung2d700712015-09-22 10:30:30 -0300551 struct vb2_v4l2_buffer *vb;
Libin Yangad6ac452013-07-03 01:56:02 -0300552
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300553 /*
554 * If there are no available buffers, go into single mode
555 */
556 if (list_empty(&cam->buffers)) {
557 buf = cam->vb_bufs[frame ^ 0x1];
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300558 set_bit(CF_SINGLE_BUFFER, &cam->flags);
Libin Yangf698957a2012-12-15 05:57:50 -0300559 cam->frame_state.singles++;
Libin Yang1d3953f2013-07-03 01:56:01 -0300560 } else {
561 /*
562 * OK, we have a buffer we can use.
563 */
564 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
565 queue);
566 list_del_init(&buf->queue);
567 clear_bit(CF_SINGLE_BUFFER, &cam->flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300568 }
Libin Yang1d3953f2013-07-03 01:56:01 -0300569
570 cam->vb_bufs[frame] = buf;
Libin Yangad6ac452013-07-03 01:56:02 -0300571 vb = &buf->vb_buf;
572
Junghak Sung2d700712015-09-22 10:30:30 -0300573 dma_handle = vb2_dma_contig_plane_dma_addr(&vb->vb2_buf, 0);
Hans Verkuil6eb40d52015-03-09 16:59:59 -0300574 mcam_write_yuv_bases(cam, frame, dma_handle);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300575}
576
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300577/*
578 * Initial B_DMA_contig setup.
579 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300580static void mcam_ctlr_dma_contig(struct mcam_camera *cam)
581{
582 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
583 cam->nbufs = 2;
584 mcam_set_contig_buffer(cam, 0);
585 mcam_set_contig_buffer(cam, 1);
586}
587
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300588/*
589 * Frame completion handling.
590 */
591static void mcam_dma_contig_done(struct mcam_camera *cam, int frame)
592{
593 struct mcam_vb_buffer *buf = cam->vb_bufs[frame];
594
595 if (!test_bit(CF_SINGLE_BUFFER, &cam->flags)) {
Libin Yangf698957a2012-12-15 05:57:50 -0300596 cam->frame_state.delivered++;
Hans Verkuilca657b22015-03-05 18:00:07 -0300597 cam->vb_bufs[frame] = NULL;
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300598 mcam_buffer_done(cam, frame, &buf->vb_buf);
599 }
600 mcam_set_contig_buffer(cam, frame);
601}
602
Jonathan Corbet74984692011-07-08 17:50:49 -0300603#endif /* MCAM_MODE_DMA_CONTIG */
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300604
Jonathan Corbet74984692011-07-08 17:50:49 -0300605#ifdef MCAM_MODE_DMA_SG
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300606/* ---------------------------------------------------------------------- */
607/*
608 * Scatter/gather-specific code.
609 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300610
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300611/*
612 * Set up the next buffer for S/G I/O; caller should be sure that
613 * the controller is stopped and a buffer is available.
614 */
615static void mcam_sg_next_buffer(struct mcam_camera *cam)
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300616{
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300617 struct mcam_vb_buffer *buf;
Lubomir Rintelb91c7b42019-05-05 10:00:23 -0400618 struct sg_table *sg_table;
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300619
620 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue);
621 list_del_init(&buf->queue);
Lubomir Rintelb91c7b42019-05-05 10:00:23 -0400622 sg_table = vb2_dma_sg_plane_desc(&buf->vb_buf.vb2_buf, 0);
Jonathan Corbet121bbe22012-03-16 19:14:53 -0300623 /*
624 * Very Bad Not Good Things happen if you don't clear
625 * C1_DESC_ENA before making any descriptor changes.
626 */
627 mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_ENA);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300628 mcam_reg_write(cam, REG_DMA_DESC_Y, buf->dma_desc_pa);
629 mcam_reg_write(cam, REG_DESC_LEN_Y,
Lubomir Rintelb91c7b42019-05-05 10:00:23 -0400630 sg_table->nents * sizeof(struct mcam_dma_desc));
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300631 mcam_reg_write(cam, REG_DESC_LEN_U, 0);
632 mcam_reg_write(cam, REG_DESC_LEN_V, 0);
Jonathan Corbet121bbe22012-03-16 19:14:53 -0300633 mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300634 cam->vb_bufs[0] = buf;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300635}
636
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300637/*
638 * Initial B_DMA_sg setup
639 */
640static void mcam_ctlr_dma_sg(struct mcam_camera *cam)
641{
Jonathan Corbetbb0a8962011-12-30 14:13:41 -0300642 /*
643 * The list-empty condition can hit us at resume time
644 * if the buffer list was empty when the system was suspended.
645 */
646 if (list_empty(&cam->buffers)) {
647 set_bit(CF_SG_RESTART, &cam->flags);
648 return;
649 }
650
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300651 mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_3WORD);
652 mcam_sg_next_buffer(cam);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300653 cam->nbufs = 3;
654}
655
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300656
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300657/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300658 * Frame completion with S/G is trickier. We can't muck with
659 * a descriptor chain on the fly, since the controller buffers it
660 * internally. So we have to actually stop and restart; Marvell
661 * says this is the way to do it.
662 *
663 * Of course, stopping is easier said than done; experience shows
664 * that the controller can start a frame *after* C0_ENABLE has been
665 * cleared. So when running in S/G mode, the controller is "stopped"
666 * on receipt of the start-of-frame interrupt. That means we can
667 * safely change the DMA descriptor array here and restart things
668 * (assuming there's another buffer waiting to go).
669 */
670static void mcam_dma_sg_done(struct mcam_camera *cam, int frame)
671{
672 struct mcam_vb_buffer *buf = cam->vb_bufs[0];
673
674 /*
Jonathan Corbet49df19e2012-03-16 19:14:50 -0300675 * If we're no longer supposed to be streaming, don't do anything.
676 */
677 if (cam->state != S_STREAMING)
678 return;
679 /*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300680 * If we have another buffer available, put it in and
681 * restart the engine.
682 */
683 if (!list_empty(&cam->buffers)) {
684 mcam_sg_next_buffer(cam);
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300685 mcam_ctlr_start(cam);
686 /*
687 * Otherwise set CF_SG_RESTART and the controller will
688 * be restarted once another buffer shows up.
689 */
690 } else {
691 set_bit(CF_SG_RESTART, &cam->flags);
Libin Yangf698957a2012-12-15 05:57:50 -0300692 cam->frame_state.singles++;
Jonathan Corbetbb0a8962011-12-30 14:13:41 -0300693 cam->vb_bufs[0] = NULL;
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300694 }
695 /*
696 * Now we can give the completed frame back to user space.
697 */
Libin Yangf698957a2012-12-15 05:57:50 -0300698 cam->frame_state.delivered++;
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300699 mcam_buffer_done(cam, frame, &buf->vb_buf);
700}
701
702
703/*
704 * Scatter/gather mode requires stopping the controller between
705 * frames so we can put in a new DMA descriptor array. If no new
706 * buffer exists at frame completion, the controller is left stopped;
707 * this function is charged with gettig things going again.
708 */
709static void mcam_sg_restart(struct mcam_camera *cam)
710{
711 mcam_ctlr_dma_sg(cam);
712 mcam_ctlr_start(cam);
713 clear_bit(CF_SG_RESTART, &cam->flags);
714}
715
Jonathan Corbet74984692011-07-08 17:50:49 -0300716#else /* MCAM_MODE_DMA_SG */
717
718static inline void mcam_sg_restart(struct mcam_camera *cam)
719{
720 return;
721}
722
723#endif /* MCAM_MODE_DMA_SG */
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300724
725/* ---------------------------------------------------------------------- */
726/*
727 * Buffer-mode-independent controller code.
728 */
729
730/*
731 * Image format setup
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300732 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300733static void mcam_ctlr_image(struct mcam_camera *cam)
734{
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300735 struct v4l2_pix_format *fmt = &cam->pix_format;
Libin Yangad6ac452013-07-03 01:56:02 -0300736 u32 widthy = 0, widthuv = 0, imgsz_h, imgsz_w;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300737
Libin Yangad6ac452013-07-03 01:56:02 -0300738 cam_dbg(cam, "camera: bytesperline = %d; height = %d\n",
739 fmt->bytesperline, fmt->sizeimage / fmt->bytesperline);
740 imgsz_h = (fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK;
741 imgsz_w = (fmt->width * 2) & IMGSZ_H_MASK;
742
743 switch (fmt->pixelformat) {
744 case V4L2_PIX_FMT_YUYV:
Hans Verkuil2a700d82015-04-13 11:18:51 -0300745 case V4L2_PIX_FMT_YVYU:
Libin Yangad6ac452013-07-03 01:56:02 -0300746 widthy = fmt->width * 2;
747 widthuv = 0;
748 break;
Libin Yangad6ac452013-07-03 01:56:02 -0300749 case V4L2_PIX_FMT_YUV420:
750 case V4L2_PIX_FMT_YVU420:
751 widthy = fmt->width;
752 widthuv = fmt->width / 2;
753 break;
754 default:
755 widthy = fmt->bytesperline;
756 widthuv = 0;
Hans Verkuil47ba7db2015-03-09 17:14:36 -0300757 break;
Libin Yangad6ac452013-07-03 01:56:02 -0300758 }
759
760 mcam_reg_write_mask(cam, REG_IMGPITCH, widthuv << 16 | widthy,
761 IMGP_YP_MASK | IMGP_UVP_MASK);
762 mcam_reg_write(cam, REG_IMGSIZE, imgsz_h | imgsz_w);
763 mcam_reg_write(cam, REG_IMGOFFSET, 0x0);
764
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300765 /*
766 * Tell the controller about the image format we are using.
767 */
Libin Yangad6ac452013-07-03 01:56:02 -0300768 switch (fmt->pixelformat) {
Libin Yangad6ac452013-07-03 01:56:02 -0300769 case V4L2_PIX_FMT_YUV420:
770 case V4L2_PIX_FMT_YVU420:
771 mcam_reg_write_mask(cam, REG_CTRL0,
Hans Verkuil2a700d82015-04-13 11:18:51 -0300772 C0_DF_YUV | C0_YUV_420PL | C0_YUVE_VYUY, C0_DF_MASK);
Libin Yangad6ac452013-07-03 01:56:02 -0300773 break;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300774 case V4L2_PIX_FMT_YUYV:
Libin Yangad6ac452013-07-03 01:56:02 -0300775 mcam_reg_write_mask(cam, REG_CTRL0,
Hans Verkuil2a700d82015-04-13 11:18:51 -0300776 C0_DF_YUV | C0_YUV_PACKED | C0_YUVE_NOSWAP, C0_DF_MASK);
Libin Yangad6ac452013-07-03 01:56:02 -0300777 break;
Hans Verkuil2a700d82015-04-13 11:18:51 -0300778 case V4L2_PIX_FMT_YVYU:
Libin Yangad6ac452013-07-03 01:56:02 -0300779 mcam_reg_write_mask(cam, REG_CTRL0,
Hans Verkuil2a700d82015-04-13 11:18:51 -0300780 C0_DF_YUV | C0_YUV_PACKED | C0_YUVE_SWAP24, C0_DF_MASK);
Libin Yangad6ac452013-07-03 01:56:02 -0300781 break;
Hans Verkuilccf509f2015-04-24 06:52:47 -0300782 case V4L2_PIX_FMT_XRGB444:
Libin Yangad6ac452013-07-03 01:56:02 -0300783 mcam_reg_write_mask(cam, REG_CTRL0,
Hans Verkuilccf509f2015-04-24 06:52:47 -0300784 C0_DF_RGB | C0_RGBF_444 | C0_RGB4_XBGR, C0_DF_MASK);
Libin Yangad6ac452013-07-03 01:56:02 -0300785 break;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300786 case V4L2_PIX_FMT_RGB565:
Libin Yangad6ac452013-07-03 01:56:02 -0300787 mcam_reg_write_mask(cam, REG_CTRL0,
788 C0_DF_RGB | C0_RGBF_565 | C0_RGB5_BGGR, C0_DF_MASK);
789 break;
Hans Verkuil8380b7e2015-03-14 08:47:01 -0300790 case V4L2_PIX_FMT_SBGGR8:
791 mcam_reg_write_mask(cam, REG_CTRL0,
792 C0_DF_RGB | C0_RGB5_GRBG, C0_DF_MASK);
793 break;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300794 default:
Libin Yangad6ac452013-07-03 01:56:02 -0300795 cam_err(cam, "camera: unknown format: %#x\n", fmt->pixelformat);
796 break;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300797 }
Libin Yangad6ac452013-07-03 01:56:02 -0300798
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300799 /*
800 * Make sure it knows we want to use hsync/vsync.
801 */
Libin Yangad6ac452013-07-03 01:56:02 -0300802 mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC, C0_SIFM_MASK);
Libin Yang05fed812013-07-03 01:55:58 -0300803 /*
804 * This field controls the generation of EOF(DVP only)
805 */
806 if (cam->bus_type != V4L2_MBUS_CSI2)
807 mcam_reg_set_bit(cam, REG_CTRL0,
808 C0_EOF_VSYNC | C0_VEDGE_CTRL);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300809}
810
811
812/*
813 * Configure the controller for operation; caller holds the
814 * device mutex.
815 */
816static int mcam_ctlr_configure(struct mcam_camera *cam)
817{
818 unsigned long flags;
819
820 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetbb0a8962011-12-30 14:13:41 -0300821 clear_bit(CF_SG_RESTART, &cam->flags);
Jonathan Corbet74984692011-07-08 17:50:49 -0300822 cam->dma_setup(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300823 mcam_ctlr_image(cam);
824 mcam_set_config_needed(cam, 0);
825 spin_unlock_irqrestore(&cam->dev_lock, flags);
826 return 0;
827}
828
829static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
830{
831 /*
832 * Clear any pending interrupts, since we do not
833 * expect to have I/O active prior to enabling.
834 */
835 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
836 mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
837}
838
839static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
840{
841 mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
842}
843
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300844
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300845
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300846static void mcam_ctlr_init(struct mcam_camera *cam)
847{
848 unsigned long flags;
849
850 spin_lock_irqsave(&cam->dev_lock, flags);
851 /*
852 * Make sure it's not powered down.
853 */
854 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
855 /*
856 * Turn off the enable bit. It sure should be off anyway,
857 * but it's good to be sure.
858 */
859 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
860 /*
861 * Clock the sensor appropriately. Controller clock should
862 * be 48MHz, sensor "typical" value is half that.
863 */
864 mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
865 spin_unlock_irqrestore(&cam->dev_lock, flags);
866}
867
868
869/*
870 * Stop the controller, and don't return until we're really sure that no
871 * further DMA is going on.
872 */
873static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
874{
875 unsigned long flags;
876
877 /*
878 * Theory: stop the camera controller (whether it is operating
879 * or not). Delay briefly just in case we race with the SOF
880 * interrupt, then wait until no DMA is active.
881 */
882 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300883 clear_bit(CF_SG_RESTART, &cam->flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300884 mcam_ctlr_stop(cam);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300885 cam->state = S_IDLE;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300886 spin_unlock_irqrestore(&cam->dev_lock, flags);
Jonathan Corbet482d35c2012-03-16 19:14:52 -0300887 /*
888 * This is a brutally long sleep, but experience shows that
889 * it can take the controller a while to get the message that
890 * it needs to stop grabbing frames. In particular, we can
891 * sometimes (on mmp) get a frame at the end WITHOUT the
892 * start-of-frame indication.
893 */
894 msleep(150);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300895 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
896 cam_err(cam, "Timeout waiting for DMA to end\n");
897 /* This would be bad news - what now? */
898 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300899 mcam_ctlr_irq_disable(cam);
900 spin_unlock_irqrestore(&cam->dev_lock, flags);
901}
902
903/*
904 * Power up and down.
905 */
Libin Yang05fed812013-07-03 01:55:58 -0300906static int mcam_ctlr_power_up(struct mcam_camera *cam)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300907{
908 unsigned long flags;
Libin Yang05fed812013-07-03 01:55:58 -0300909 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300910
911 spin_lock_irqsave(&cam->dev_lock, flags);
Libin Yang05fed812013-07-03 01:55:58 -0300912 ret = cam->plat_power_up(cam);
913 if (ret) {
914 spin_unlock_irqrestore(&cam->dev_lock, flags);
915 return ret;
916 }
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300917 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300918 spin_unlock_irqrestore(&cam->dev_lock, flags);
919 msleep(5); /* Just to be sure */
Libin Yang05fed812013-07-03 01:55:58 -0300920 return 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300921}
922
923static void mcam_ctlr_power_down(struct mcam_camera *cam)
924{
925 unsigned long flags;
926
927 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300928 /*
929 * School of hard knocks department: be sure we do any register
930 * twiddling on the controller *before* calling the platform
931 * power down routine.
932 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300933 mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300934 cam->plat_power_down(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300935 spin_unlock_irqrestore(&cam->dev_lock, flags);
936}
937
938/* -------------------------------------------------------------------- */
939/*
940 * Communications with the sensor.
941 */
942
943static int __mcam_cam_reset(struct mcam_camera *cam)
944{
945 return sensor_call(cam, core, reset, 0);
946}
947
948/*
949 * We have found the sensor on the i2c. Let's try to have a
950 * conversation.
951 */
952static int mcam_cam_init(struct mcam_camera *cam)
953{
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300954 int ret;
955
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300956 if (cam->state != S_NOTREADY)
957 cam_warn(cam, "Cam init with device in funky state %d",
958 cam->state);
959 ret = __mcam_cam_reset(cam);
Hans Verkuil7486af12013-05-29 06:59:44 -0300960 /* Get/set parameters? */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300961 cam->state = S_IDLE;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300962 mcam_ctlr_power_down(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300963 return ret;
964}
965
966/*
967 * Configure the sensor to match the parameters we have. Caller should
968 * hold s_mutex
969 */
970static int mcam_cam_set_flip(struct mcam_camera *cam)
971{
972 struct v4l2_control ctrl;
973
974 memset(&ctrl, 0, sizeof(ctrl));
975 ctrl.id = V4L2_CID_VFLIP;
976 ctrl.value = flip;
Hans Verkuil5f3cc482016-07-03 08:58:55 -0300977 return v4l2_s_ctrl(NULL, cam->sensor->ctrl_handler, &ctrl);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300978}
979
980
981static int mcam_cam_configure(struct mcam_camera *cam)
982{
Hans Verkuilebf984b2015-04-09 04:05:59 -0300983 struct v4l2_subdev_format format = {
984 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
985 };
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300986 int ret;
987
Hans Verkuilebf984b2015-04-09 04:05:59 -0300988 v4l2_fill_mbus_format(&format.format, &cam->pix_format, cam->mbus_code);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300989 ret = sensor_call(cam, core, init, 0);
990 if (ret == 0)
Hans Verkuilebf984b2015-04-09 04:05:59 -0300991 ret = sensor_call(cam, pad, set_fmt, NULL, &format);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300992 /*
993 * OV7670 does weird things if flip is set *before* format...
994 */
995 ret += mcam_cam_set_flip(cam);
996 return ret;
997}
998
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300999/*
1000 * Get everything ready, and start grabbing frames.
1001 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001002static int mcam_read_setup(struct mcam_camera *cam)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001003{
1004 int ret;
1005 unsigned long flags;
1006
1007 /*
1008 * Configuration. If we still don't have DMA buffers,
1009 * make one last, desperate attempt.
1010 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001011 if (cam->buffer_mode == B_vmalloc && cam->nbufs == 0 &&
1012 mcam_alloc_dma_bufs(cam, 0))
1013 return -ENOMEM;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001014
1015 if (mcam_needs_config(cam)) {
1016 mcam_cam_configure(cam);
1017 ret = mcam_ctlr_configure(cam);
1018 if (ret)
1019 return ret;
1020 }
1021
1022 /*
1023 * Turn it loose.
1024 */
1025 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbet482d35c2012-03-16 19:14:52 -03001026 clear_bit(CF_DMA_ACTIVE, &cam->flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001027 mcam_reset_buffers(cam);
Libin Yang05fed812013-07-03 01:55:58 -03001028 /*
1029 * Update CSI2_DPHY value
1030 */
1031 if (cam->calc_dphy)
1032 cam->calc_dphy(cam);
1033 cam_dbg(cam, "camera: DPHY sets: dphy3=0x%x, dphy5=0x%x, dphy6=0x%x\n",
1034 cam->dphy[0], cam->dphy[1], cam->dphy[2]);
1035 if (cam->bus_type == V4L2_MBUS_CSI2)
1036 mcam_enable_mipi(cam);
1037 else
1038 mcam_disable_mipi(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001039 mcam_ctlr_irq_enable(cam);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001040 cam->state = S_STREAMING;
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001041 if (!test_bit(CF_SG_RESTART, &cam->flags))
1042 mcam_ctlr_start(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001043 spin_unlock_irqrestore(&cam->dev_lock, flags);
1044 return 0;
1045}
1046
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001047/* ----------------------------------------------------------------------- */
1048/*
1049 * Videobuf2 interface code.
1050 */
1051
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -03001052static int mcam_vb_queue_setup(struct vb2_queue *vq,
Hans Verkuildf9ecb02015-10-28 00:50:37 -02001053 unsigned int *nbufs,
Marek Szyprowski035aa142011-08-24 06:43:36 -03001054 unsigned int *num_planes, unsigned int sizes[],
Hans Verkuil36c0f8b2016-04-15 09:15:05 -03001055 struct device *alloc_devs[])
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001056{
1057 struct mcam_camera *cam = vb2_get_drv_priv(vq);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001058 int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2;
Hans Verkuildf9ecb02015-10-28 00:50:37 -02001059 unsigned size = cam->pix_format.sizeimage;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001060
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001061 if (*nbufs < minbufs)
1062 *nbufs = minbufs;
Hans Verkuildf9ecb02015-10-28 00:50:37 -02001063
1064 if (*num_planes)
1065 return sizes[0] < size ? -EINVAL : 0;
1066 sizes[0] = size;
1067 *num_planes = 1; /* Someday we have to support planar formats... */
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001068 return 0;
1069}
1070
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001071
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001072static void mcam_vb_buf_queue(struct vb2_buffer *vb)
1073{
Junghak Sung2d700712015-09-22 10:30:30 -03001074 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1075 struct mcam_vb_buffer *mvb = vb_to_mvb(vbuf);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001076 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1077 unsigned long flags;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001078 int start;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001079
1080 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001081 start = (cam->state == S_BUFWAIT) && !list_empty(&cam->buffers);
1082 list_add(&mvb->queue, &cam->buffers);
Jonathan Corbet49df19e2012-03-16 19:14:50 -03001083 if (cam->state == S_STREAMING && test_bit(CF_SG_RESTART, &cam->flags))
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001084 mcam_sg_restart(cam);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001085 spin_unlock_irqrestore(&cam->dev_lock, flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001086 if (start)
1087 mcam_read_setup(cam);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001088}
1089
Hans Verkuilca657b22015-03-05 18:00:07 -03001090static void mcam_vb_requeue_bufs(struct vb2_queue *vq,
1091 enum vb2_buffer_state state)
1092{
1093 struct mcam_camera *cam = vb2_get_drv_priv(vq);
1094 struct mcam_vb_buffer *buf, *node;
1095 unsigned long flags;
1096 unsigned i;
1097
1098 spin_lock_irqsave(&cam->dev_lock, flags);
1099 list_for_each_entry_safe(buf, node, &cam->buffers, queue) {
Junghak Sung2d700712015-09-22 10:30:30 -03001100 vb2_buffer_done(&buf->vb_buf.vb2_buf, state);
Hans Verkuilca657b22015-03-05 18:00:07 -03001101 list_del(&buf->queue);
1102 }
1103 for (i = 0; i < MAX_DMA_BUFS; i++) {
1104 buf = cam->vb_bufs[i];
1105
1106 if (buf) {
Junghak Sung2d700712015-09-22 10:30:30 -03001107 vb2_buffer_done(&buf->vb_buf.vb2_buf, state);
Hans Verkuilca657b22015-03-05 18:00:07 -03001108 cam->vb_bufs[i] = NULL;
1109 }
1110 }
1111 spin_unlock_irqrestore(&cam->dev_lock, flags);
1112}
1113
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001114/*
1115 * These need to be called with the mutex held from vb2
1116 */
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001117static int mcam_vb_start_streaming(struct vb2_queue *vq, unsigned int count)
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001118{
1119 struct mcam_camera *cam = vb2_get_drv_priv(vq);
Libin Yang0a0b3fb2013-07-03 01:56:03 -03001120 unsigned int frame;
Hans Verkuilca657b22015-03-05 18:00:07 -03001121 int ret;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001122
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001123 if (cam->state != S_IDLE) {
Hans Verkuilca657b22015-03-05 18:00:07 -03001124 mcam_vb_requeue_bufs(vq, VB2_BUF_STATE_QUEUED);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001125 return -EINVAL;
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001126 }
Hans Verkuil949bd402015-03-05 17:33:17 -03001127 cam->frame_state.frames = 0;
1128 cam->frame_state.singles = 0;
1129 cam->frame_state.delivered = 0;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001130 cam->sequence = 0;
1131 /*
1132 * Videobuf2 sneakily hoards all the buffers and won't
1133 * give them to us until *after* streaming starts. But
1134 * we can't actually start streaming until we have a
1135 * destination. So go into a wait state and hope they
1136 * give us buffers soon.
1137 */
1138 if (cam->buffer_mode != B_vmalloc && list_empty(&cam->buffers)) {
1139 cam->state = S_BUFWAIT;
1140 return 0;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001141 }
Libin Yang0a0b3fb2013-07-03 01:56:03 -03001142
1143 /*
1144 * Ensure clear the left over frame flags
1145 * before every really start streaming
1146 */
1147 for (frame = 0; frame < cam->nbufs; frame++)
1148 clear_bit(CF_FRAME_SOF0 + frame, &cam->flags);
1149
Hans Verkuilca657b22015-03-05 18:00:07 -03001150 ret = mcam_read_setup(cam);
1151 if (ret)
1152 mcam_vb_requeue_bufs(vq, VB2_BUF_STATE_QUEUED);
1153 return ret;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001154}
1155
Hans Verkuile37559b2014-04-17 02:47:21 -03001156static void mcam_vb_stop_streaming(struct vb2_queue *vq)
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001157{
1158 struct mcam_camera *cam = vb2_get_drv_priv(vq);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001159
Hans Verkuil949bd402015-03-05 17:33:17 -03001160 cam_dbg(cam, "stop_streaming: %d frames, %d singles, %d delivered\n",
1161 cam->frame_state.frames, cam->frame_state.singles,
1162 cam->frame_state.delivered);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001163 if (cam->state == S_BUFWAIT) {
1164 /* They never gave us buffers */
1165 cam->state = S_IDLE;
Hans Verkuile37559b2014-04-17 02:47:21 -03001166 return;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001167 }
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001168 if (cam->state != S_STREAMING)
Hans Verkuile37559b2014-04-17 02:47:21 -03001169 return;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001170 mcam_ctlr_stop_dma(cam);
1171 /*
Libin Yang7c269f452013-07-03 01:56:00 -03001172 * Reset the CCIC PHY after stopping streaming,
1173 * otherwise, the CCIC may be unstable.
1174 */
1175 if (cam->ctlr_reset)
1176 cam->ctlr_reset(cam);
1177 /*
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001178 * VB2 reclaims the buffers, so we need to forget
1179 * about them.
1180 */
Hans Verkuilca657b22015-03-05 18:00:07 -03001181 mcam_vb_requeue_bufs(vq, VB2_BUF_STATE_ERROR);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001182}
1183
1184
1185static const struct vb2_ops mcam_vb2_ops = {
1186 .queue_setup = mcam_vb_queue_setup,
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001187 .buf_queue = mcam_vb_buf_queue,
1188 .start_streaming = mcam_vb_start_streaming,
1189 .stop_streaming = mcam_vb_stop_streaming,
Prabhakar Lad519694f2014-11-26 19:42:29 -03001190 .wait_prepare = vb2_ops_wait_prepare,
1191 .wait_finish = vb2_ops_wait_finish,
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001192};
1193
Jonathan Corbet74984692011-07-08 17:50:49 -03001194
1195#ifdef MCAM_MODE_DMA_SG
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001196/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001197 * Scatter/gather mode uses all of the above functions plus a
1198 * few extras to deal with DMA mapping.
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001199 */
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001200static int mcam_vb_sg_buf_init(struct vb2_buffer *vb)
1201{
Junghak Sung2d700712015-09-22 10:30:30 -03001202 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1203 struct mcam_vb_buffer *mvb = vb_to_mvb(vbuf);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001204 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1205 int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
1206
1207 mvb->dma_desc = dma_alloc_coherent(cam->dev,
1208 ndesc * sizeof(struct mcam_dma_desc),
1209 &mvb->dma_desc_pa, GFP_KERNEL);
1210 if (mvb->dma_desc == NULL) {
1211 cam_err(cam, "Unable to get DMA descriptor array\n");
1212 return -ENOMEM;
1213 }
1214 return 0;
1215}
1216
1217static int mcam_vb_sg_buf_prepare(struct vb2_buffer *vb)
1218{
Junghak Sung2d700712015-09-22 10:30:30 -03001219 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1220 struct mcam_vb_buffer *mvb = vb_to_mvb(vbuf);
Ricardo Ribalda22301242013-08-02 10:20:00 -03001221 struct sg_table *sg_table = vb2_dma_sg_plane_desc(vb, 0);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001222 struct mcam_dma_desc *desc = mvb->dma_desc;
1223 struct scatterlist *sg;
1224 int i;
1225
Hans Verkuild790b7e2014-11-24 08:50:31 -03001226 for_each_sg(sg_table->sgl, sg, sg_table->nents, i) {
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001227 desc->dma_addr = sg_dma_address(sg);
1228 desc->segment_len = sg_dma_len(sg);
1229 desc++;
1230 }
1231 return 0;
1232}
1233
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001234static void mcam_vb_sg_buf_cleanup(struct vb2_buffer *vb)
1235{
Junghak Sung2d700712015-09-22 10:30:30 -03001236 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001237 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -03001238 struct mcam_vb_buffer *mvb = vb_to_mvb(vbuf);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001239 int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
1240
1241 dma_free_coherent(cam->dev, ndesc * sizeof(struct mcam_dma_desc),
1242 mvb->dma_desc, mvb->dma_desc_pa);
1243}
1244
1245
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001246static const struct vb2_ops mcam_vb2_sg_ops = {
1247 .queue_setup = mcam_vb_queue_setup,
1248 .buf_init = mcam_vb_sg_buf_init,
1249 .buf_prepare = mcam_vb_sg_buf_prepare,
1250 .buf_queue = mcam_vb_buf_queue,
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001251 .buf_cleanup = mcam_vb_sg_buf_cleanup,
1252 .start_streaming = mcam_vb_start_streaming,
1253 .stop_streaming = mcam_vb_stop_streaming,
Prabhakar Lad519694f2014-11-26 19:42:29 -03001254 .wait_prepare = vb2_ops_wait_prepare,
1255 .wait_finish = vb2_ops_wait_finish,
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001256};
1257
Jonathan Corbet74984692011-07-08 17:50:49 -03001258#endif /* MCAM_MODE_DMA_SG */
1259
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001260static int mcam_setup_vb2(struct mcam_camera *cam)
1261{
1262 struct vb2_queue *vq = &cam->vb_queue;
1263
1264 memset(vq, 0, sizeof(*vq));
1265 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001266 vq->drv_priv = cam;
Prabhakar Lad519694f2014-11-26 19:42:29 -03001267 vq->lock = &cam->s_mutex;
Hans Verkuil17d36752015-03-03 15:07:27 -03001268 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Hans Verkuilca16a642015-03-09 18:02:23 -03001269 vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1270 vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
Hans Verkuil1ad70ce2016-02-15 13:41:51 -02001271 vq->dev = cam->dev;
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001272 INIT_LIST_HEAD(&cam->buffers);
1273 switch (cam->buffer_mode) {
1274 case B_DMA_contig:
Jonathan Corbet74984692011-07-08 17:50:49 -03001275#ifdef MCAM_MODE_DMA_CONTIG
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001276 vq->ops = &mcam_vb2_ops;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001277 vq->mem_ops = &vb2_dma_contig_memops;
Jonathan Corbet74984692011-07-08 17:50:49 -03001278 cam->dma_setup = mcam_ctlr_dma_contig;
1279 cam->frame_complete = mcam_dma_contig_done;
1280#endif
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001281 break;
1282 case B_DMA_sg:
Jonathan Corbet74984692011-07-08 17:50:49 -03001283#ifdef MCAM_MODE_DMA_SG
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001284 vq->ops = &mcam_vb2_sg_ops;
1285 vq->mem_ops = &vb2_dma_sg_memops;
Jonathan Corbet74984692011-07-08 17:50:49 -03001286 cam->dma_setup = mcam_ctlr_dma_sg;
1287 cam->frame_complete = mcam_dma_sg_done;
1288#endif
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001289 break;
1290 case B_vmalloc:
Jonathan Corbet74984692011-07-08 17:50:49 -03001291#ifdef MCAM_MODE_VMALLOC
1292 tasklet_init(&cam->s_tasklet, mcam_frame_tasklet,
1293 (unsigned long) cam);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001294 vq->ops = &mcam_vb2_ops;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001295 vq->mem_ops = &vb2_vmalloc_memops;
Jonathan Corbet74984692011-07-08 17:50:49 -03001296 cam->dma_setup = mcam_ctlr_dma_vmalloc;
1297 cam->frame_complete = mcam_vmalloc_done;
1298#endif
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001299 break;
1300 }
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001301 return vb2_queue_init(vq);
1302}
1303
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001304
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001305/* ---------------------------------------------------------------------- */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001306/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001307 * The long list of V4L2 ioctl() operations.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001308 */
1309
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001310static int mcam_vidioc_querycap(struct file *file, void *priv,
1311 struct v4l2_capability *cap)
1312{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001313 struct mcam_camera *cam = video_drvdata(file);
Hans Verkuilb7b68392015-03-05 04:57:32 -03001314
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001315 strcpy(cap->driver, "marvell_ccic");
1316 strcpy(cap->card, "marvell_ccic");
Hans Verkuilb7b68392015-03-05 04:57:32 -03001317 strlcpy(cap->bus_info, cam->bus_info, sizeof(cap->bus_info));
Hans Verkuila020c742014-11-24 06:37:25 -03001318 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001319 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
Hans Verkuila020c742014-11-24 06:37:25 -03001320 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001321 return 0;
1322}
1323
1324
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001325static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
1326 void *priv, struct v4l2_fmtdesc *fmt)
1327{
1328 if (fmt->index >= N_MCAM_FMTS)
1329 return -EINVAL;
1330 strlcpy(fmt->description, mcam_formats[fmt->index].desc,
1331 sizeof(fmt->description));
1332 fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
1333 return 0;
1334}
1335
1336static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1337 struct v4l2_format *fmt)
1338{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001339 struct mcam_camera *cam = video_drvdata(filp);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001340 struct mcam_format_struct *f;
1341 struct v4l2_pix_format *pix = &fmt->fmt.pix;
Hans Verkuil5eab4982015-04-09 04:05:35 -03001342 struct v4l2_subdev_pad_config pad_cfg;
1343 struct v4l2_subdev_format format = {
1344 .which = V4L2_SUBDEV_FORMAT_TRY,
1345 };
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001346 int ret;
1347
1348 f = mcam_find_format(pix->pixelformat);
1349 pix->pixelformat = f->pixelformat;
Hans Verkuil5eab4982015-04-09 04:05:35 -03001350 v4l2_fill_mbus_format(&format.format, pix, f->mbus_code);
1351 ret = sensor_call(cam, pad, set_fmt, &pad_cfg, &format);
1352 v4l2_fill_pix_format(pix, &format.format);
Hans Verkuil47ba7db2015-03-09 17:14:36 -03001353 pix->bytesperline = pix->width * f->bpp;
Libin Yangad6ac452013-07-03 01:56:02 -03001354 switch (f->pixelformat) {
1355 case V4L2_PIX_FMT_YUV420:
1356 case V4L2_PIX_FMT_YVU420:
Hans Verkuil47ba7db2015-03-09 17:14:36 -03001357 pix->sizeimage = pix->height * pix->bytesperline * 3 / 2;
Libin Yangad6ac452013-07-03 01:56:02 -03001358 break;
1359 default:
Hans Verkuil47ba7db2015-03-09 17:14:36 -03001360 pix->sizeimage = pix->height * pix->bytesperline;
Libin Yangad6ac452013-07-03 01:56:02 -03001361 break;
1362 }
Hans Verkuil2e6e6092015-03-05 05:19:23 -03001363 pix->colorspace = V4L2_COLORSPACE_SRGB;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001364 return ret;
1365}
1366
1367static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1368 struct v4l2_format *fmt)
1369{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001370 struct mcam_camera *cam = video_drvdata(filp);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001371 struct mcam_format_struct *f;
1372 int ret;
1373
1374 /*
1375 * Can't do anything if the device is not idle
1376 * Also can't if there are streaming buffers in place.
1377 */
Hans Verkuile198d0ff2015-03-05 17:37:51 -03001378 if (cam->state != S_IDLE || vb2_is_busy(&cam->vb_queue))
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001379 return -EBUSY;
1380
1381 f = mcam_find_format(fmt->fmt.pix.pixelformat);
1382
1383 /*
1384 * See if the formatting works in principle.
1385 */
1386 ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1387 if (ret)
1388 return ret;
1389 /*
1390 * Now we start to change things for real, so let's do it
1391 * under lock.
1392 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001393 cam->pix_format = fmt->fmt.pix;
1394 cam->mbus_code = f->mbus_code;
1395
1396 /*
1397 * Make sure we have appropriate DMA buffers.
1398 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001399 if (cam->buffer_mode == B_vmalloc) {
Jonathan Corbet74984692011-07-08 17:50:49 -03001400 ret = mcam_check_dma_buffers(cam);
1401 if (ret)
1402 goto out;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001403 }
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001404 mcam_set_config_needed(cam, 1);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001405out:
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001406 return ret;
1407}
1408
1409/*
1410 * Return our stored notion of how the camera is/should be configured.
1411 * The V4l2 spec wants us to be smarter, and actually get this from
1412 * the camera (and not mess with it at open time). Someday.
1413 */
1414static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1415 struct v4l2_format *f)
1416{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001417 struct mcam_camera *cam = video_drvdata(filp);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001418
1419 f->fmt.pix = cam->pix_format;
1420 return 0;
1421}
1422
1423/*
1424 * We only have one input - the sensor - so minimize the nonsense here.
1425 */
1426static int mcam_vidioc_enum_input(struct file *filp, void *priv,
1427 struct v4l2_input *input)
1428{
1429 if (input->index != 0)
1430 return -EINVAL;
1431
1432 input->type = V4L2_INPUT_TYPE_CAMERA;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001433 strcpy(input->name, "Camera");
1434 return 0;
1435}
1436
1437static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1438{
1439 *i = 0;
1440 return 0;
1441}
1442
1443static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1444{
1445 if (i != 0)
1446 return -EINVAL;
1447 return 0;
1448}
1449
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001450/*
1451 * G/S_PARM. Most of this is done by the sensor, but we are
1452 * the level which controls the number of read buffers.
1453 */
1454static int mcam_vidioc_g_parm(struct file *filp, void *priv,
1455 struct v4l2_streamparm *parms)
1456{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001457 struct mcam_camera *cam = video_drvdata(filp);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001458 int ret;
1459
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001460 ret = sensor_call(cam, video, g_parm, parms);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001461 parms->parm.capture.readbuffers = n_dma_bufs;
1462 return ret;
1463}
1464
1465static int mcam_vidioc_s_parm(struct file *filp, void *priv,
1466 struct v4l2_streamparm *parms)
1467{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001468 struct mcam_camera *cam = video_drvdata(filp);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001469 int ret;
1470
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001471 ret = sensor_call(cam, video, s_parm, parms);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001472 parms->parm.capture.readbuffers = n_dma_bufs;
1473 return ret;
1474}
1475
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001476static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
1477 struct v4l2_frmsizeenum *sizes)
1478{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001479 struct mcam_camera *cam = video_drvdata(filp);
Hans Verkuil17bef882015-03-04 01:48:00 -08001480 struct mcam_format_struct *f;
1481 struct v4l2_subdev_frame_size_enum fse = {
1482 .index = sizes->index,
1483 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1484 };
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001485 int ret;
1486
Hans Verkuil17bef882015-03-04 01:48:00 -08001487 f = mcam_find_format(sizes->pixel_format);
1488 if (f->pixelformat != sizes->pixel_format)
1489 return -EINVAL;
1490 fse.code = f->mbus_code;
Hans Verkuil17bef882015-03-04 01:48:00 -08001491 ret = sensor_call(cam, pad, enum_frame_size, NULL, &fse);
Hans Verkuil17bef882015-03-04 01:48:00 -08001492 if (ret)
1493 return ret;
1494 if (fse.min_width == fse.max_width &&
1495 fse.min_height == fse.max_height) {
1496 sizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1497 sizes->discrete.width = fse.min_width;
1498 sizes->discrete.height = fse.min_height;
1499 return 0;
1500 }
1501 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1502 sizes->stepwise.min_width = fse.min_width;
1503 sizes->stepwise.max_width = fse.max_width;
1504 sizes->stepwise.min_height = fse.min_height;
1505 sizes->stepwise.max_height = fse.max_height;
1506 sizes->stepwise.step_width = 1;
1507 sizes->stepwise.step_height = 1;
1508 return 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001509}
1510
1511static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
1512 struct v4l2_frmivalenum *interval)
1513{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001514 struct mcam_camera *cam = video_drvdata(filp);
Hans Verkuil17bef882015-03-04 01:48:00 -08001515 struct mcam_format_struct *f;
1516 struct v4l2_subdev_frame_interval_enum fie = {
1517 .index = interval->index,
1518 .width = interval->width,
1519 .height = interval->height,
1520 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1521 };
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001522 int ret;
1523
Hans Verkuil17bef882015-03-04 01:48:00 -08001524 f = mcam_find_format(interval->pixel_format);
1525 if (f->pixelformat != interval->pixel_format)
1526 return -EINVAL;
1527 fie.code = f->mbus_code;
Hans Verkuil17bef882015-03-04 01:48:00 -08001528 ret = sensor_call(cam, pad, enum_frame_interval, NULL, &fie);
Hans Verkuil17bef882015-03-04 01:48:00 -08001529 if (ret)
1530 return ret;
1531 interval->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1532 interval->discrete = fie.interval;
1533 return 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001534}
1535
1536#ifdef CONFIG_VIDEO_ADV_DEBUG
1537static int mcam_vidioc_g_register(struct file *file, void *priv,
1538 struct v4l2_dbg_register *reg)
1539{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001540 struct mcam_camera *cam = video_drvdata(file);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001541
Hans Verkuil4e032f32013-05-29 07:00:02 -03001542 if (reg->reg > cam->regs_size - 4)
1543 return -EINVAL;
Hans Verkuil7486af12013-05-29 06:59:44 -03001544 reg->val = mcam_reg_read(cam, reg->reg);
1545 reg->size = 4;
1546 return 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001547}
1548
1549static int mcam_vidioc_s_register(struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001550 const struct v4l2_dbg_register *reg)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001551{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001552 struct mcam_camera *cam = video_drvdata(file);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001553
Hans Verkuil4e032f32013-05-29 07:00:02 -03001554 if (reg->reg > cam->regs_size - 4)
1555 return -EINVAL;
Hans Verkuil7486af12013-05-29 06:59:44 -03001556 mcam_reg_write(cam, reg->reg, reg->val);
1557 return 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001558}
1559#endif
1560
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001561static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
1562 .vidioc_querycap = mcam_vidioc_querycap,
1563 .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
1564 .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
1565 .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap,
1566 .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap,
1567 .vidioc_enum_input = mcam_vidioc_enum_input,
1568 .vidioc_g_input = mcam_vidioc_g_input,
1569 .vidioc_s_input = mcam_vidioc_s_input,
Hans Verkuil949bd402015-03-05 17:33:17 -03001570 .vidioc_reqbufs = vb2_ioctl_reqbufs,
Hans Verkuile198d0ff2015-03-05 17:37:51 -03001571 .vidioc_create_bufs = vb2_ioctl_create_bufs,
Hans Verkuil949bd402015-03-05 17:33:17 -03001572 .vidioc_querybuf = vb2_ioctl_querybuf,
1573 .vidioc_qbuf = vb2_ioctl_qbuf,
1574 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuilca16a642015-03-09 18:02:23 -03001575 .vidioc_expbuf = vb2_ioctl_expbuf,
Hans Verkuil949bd402015-03-05 17:33:17 -03001576 .vidioc_streamon = vb2_ioctl_streamon,
1577 .vidioc_streamoff = vb2_ioctl_streamoff,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001578 .vidioc_g_parm = mcam_vidioc_g_parm,
1579 .vidioc_s_parm = mcam_vidioc_s_parm,
1580 .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
1581 .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
Hans Verkuil87d18432015-03-05 13:05:24 -03001582 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1583 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001584#ifdef CONFIG_VIDEO_ADV_DEBUG
1585 .vidioc_g_register = mcam_vidioc_g_register,
1586 .vidioc_s_register = mcam_vidioc_s_register,
1587#endif
1588};
1589
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001590/* ---------------------------------------------------------------------- */
1591/*
1592 * Our various file operations.
1593 */
1594static int mcam_v4l_open(struct file *filp)
1595{
1596 struct mcam_camera *cam = video_drvdata(filp);
Hans Verkuil949bd402015-03-05 17:33:17 -03001597 int ret;
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001598
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001599 mutex_lock(&cam->s_mutex);
Hans Verkuil949bd402015-03-05 17:33:17 -03001600 ret = v4l2_fh_open(filp);
1601 if (ret)
1602 goto out;
1603 if (v4l2_fh_is_singular_file(filp)) {
Libin Yang05fed812013-07-03 01:55:58 -03001604 ret = mcam_ctlr_power_up(cam);
1605 if (ret)
1606 goto out;
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001607 __mcam_cam_reset(cam);
1608 mcam_set_config_needed(cam, 1);
1609 }
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001610out:
1611 mutex_unlock(&cam->s_mutex);
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001612 if (ret)
1613 v4l2_fh_release(filp);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001614 return ret;
1615}
1616
1617
1618static int mcam_v4l_release(struct file *filp)
1619{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001620 struct mcam_camera *cam = video_drvdata(filp);
Hans Verkuil949bd402015-03-05 17:33:17 -03001621 bool last_open;
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001622
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001623 mutex_lock(&cam->s_mutex);
Hans Verkuil949bd402015-03-05 17:33:17 -03001624 last_open = v4l2_fh_is_singular_file(filp);
1625 _vb2_fop_release(filp, NULL);
1626 if (last_open) {
Libin Yang05fed812013-07-03 01:55:58 -03001627 mcam_disable_mipi(cam);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001628 mcam_ctlr_power_down(cam);
1629 if (cam->buffer_mode == B_vmalloc && alloc_bufs_at_read)
1630 mcam_free_dma_bufs(cam);
1631 }
Libin Yang05fed812013-07-03 01:55:58 -03001632
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001633 mutex_unlock(&cam->s_mutex);
1634 return 0;
1635}
1636
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001637static const struct v4l2_file_operations mcam_v4l_fops = {
1638 .owner = THIS_MODULE,
1639 .open = mcam_v4l_open,
1640 .release = mcam_v4l_release,
Hans Verkuil949bd402015-03-05 17:33:17 -03001641 .read = vb2_fop_read,
1642 .poll = vb2_fop_poll,
1643 .mmap = vb2_fop_mmap,
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001644 .unlocked_ioctl = video_ioctl2,
1645};
1646
1647
1648/*
1649 * This template device holds all of those v4l2 methods; we
1650 * clone it for specific real devices.
1651 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001652static struct video_device mcam_v4l_template = {
1653 .name = "mcam",
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001654 .fops = &mcam_v4l_fops,
1655 .ioctl_ops = &mcam_v4l_ioctl_ops,
1656 .release = video_device_release_empty,
1657};
1658
1659/* ---------------------------------------------------------------------- */
1660/*
1661 * Interrupt handler stuff
1662 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001663static void mcam_frame_complete(struct mcam_camera *cam, int frame)
1664{
1665 /*
1666 * Basic frame housekeeping.
1667 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001668 set_bit(frame, &cam->flags);
1669 clear_bit(CF_DMA_ACTIVE, &cam->flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001670 cam->next_buf = frame;
Hans Verkuil53423422015-03-05 17:48:39 -03001671 cam->buf_seq[frame] = cam->sequence++;
Libin Yangf698957a2012-12-15 05:57:50 -03001672 cam->frame_state.frames++;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001673 /*
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001674 * "This should never happen"
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001675 */
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001676 if (cam->state != S_STREAMING)
1677 return;
1678 /*
1679 * Process the frame and set up the next one.
1680 */
Jonathan Corbet74984692011-07-08 17:50:49 -03001681 cam->frame_complete(cam, frame);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001682}
1683
1684
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001685/*
1686 * The interrupt handler; this needs to be called from the
1687 * platform irq handler with the lock held.
1688 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001689int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
1690{
1691 unsigned int frame, handled = 0;
1692
1693 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1694 /*
1695 * Handle any frame completions. There really should
1696 * not be more than one of these, or we have fallen
1697 * far behind.
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001698 *
1699 * When running in S/G mode, the frame number lacks any
1700 * real meaning - there's only one descriptor array - but
1701 * the controller still picks a different one to signal
1702 * each time.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001703 */
1704 for (frame = 0; frame < cam->nbufs; frame++)
Libin Yang0a0b3fb2013-07-03 01:56:03 -03001705 if (irqs & (IRQ_EOF0 << frame) &&
1706 test_bit(CF_FRAME_SOF0 + frame, &cam->flags)) {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001707 mcam_frame_complete(cam, frame);
1708 handled = 1;
Libin Yang0a0b3fb2013-07-03 01:56:03 -03001709 clear_bit(CF_FRAME_SOF0 + frame, &cam->flags);
Jonathan Corbetf2354dd2012-03-16 19:14:54 -03001710 if (cam->buffer_mode == B_DMA_sg)
1711 break;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001712 }
1713 /*
1714 * If a frame starts, note that we have DMA active. This
1715 * code assumes that we won't get multiple frame interrupts
1716 * at once; may want to rethink that.
1717 */
Libin Yang0a0b3fb2013-07-03 01:56:03 -03001718 for (frame = 0; frame < cam->nbufs; frame++) {
1719 if (irqs & (IRQ_SOF0 << frame)) {
1720 set_bit(CF_FRAME_SOF0 + frame, &cam->flags);
1721 handled = IRQ_HANDLED;
1722 }
1723 }
1724
1725 if (handled == IRQ_HANDLED) {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001726 set_bit(CF_DMA_ACTIVE, &cam->flags);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001727 if (cam->buffer_mode == B_DMA_sg)
1728 mcam_ctlr_stop(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001729 }
1730 return handled;
1731}
1732
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001733/* ---------------------------------------------------------------------- */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001734/*
1735 * Registration and such.
1736 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001737static struct ov7670_config sensor_cfg = {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001738 /*
1739 * Exclude QCIF mode, because it only captures a tiny portion
1740 * of the sensor FOV
1741 */
1742 .min_width = 320,
1743 .min_height = 240,
1744};
1745
1746
1747int mccic_register(struct mcam_camera *cam)
1748{
1749 struct i2c_board_info ov7670_info = {
1750 .type = "ov7670",
Jonathan Corbet1c68f882011-06-11 14:46:47 -03001751 .addr = 0x42 >> 1,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001752 .platform_data = &sensor_cfg,
1753 };
1754 int ret;
1755
1756 /*
Jonathan Corbet74984692011-07-08 17:50:49 -03001757 * Validate the requested buffer mode.
1758 */
1759 if (buffer_mode >= 0)
1760 cam->buffer_mode = buffer_mode;
1761 if (cam->buffer_mode == B_DMA_sg &&
Hans Verkuil7486af12013-05-29 06:59:44 -03001762 cam->chip_id == MCAM_CAFE) {
Jonathan Corbet74984692011-07-08 17:50:49 -03001763 printk(KERN_ERR "marvell-cam: Cafe can't do S/G I/O, "
1764 "attempting vmalloc mode instead\n");
1765 cam->buffer_mode = B_vmalloc;
1766 }
1767 if (!mcam_buffer_mode_supported(cam->buffer_mode)) {
1768 printk(KERN_ERR "marvell-cam: buffer mode %d unsupported\n",
1769 cam->buffer_mode);
1770 return -EINVAL;
1771 }
1772 /*
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001773 * Register with V4L
1774 */
1775 ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
1776 if (ret)
1777 return ret;
1778
1779 mutex_init(&cam->s_mutex);
1780 cam->state = S_NOTREADY;
1781 mcam_set_config_needed(cam, 1);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001782 cam->pix_format = mcam_def_pix_format;
1783 cam->mbus_code = mcam_def_mbus_code;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001784 mcam_ctlr_init(cam);
1785
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001786 /*
Hans Verkuil1e4cbe62015-03-05 12:00:11 -03001787 * Get the v4l2 setup done.
1788 */
1789 ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10);
1790 if (ret)
1791 goto out_unregister;
1792 cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler;
1793
1794 /*
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001795 * Try to find the sensor.
1796 */
Jonathan Corbet2164b5a2011-06-11 14:46:44 -03001797 sensor_cfg.clock_speed = cam->clock_speed;
1798 sensor_cfg.use_smbus = cam->use_smbus;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001799 cam->sensor_addr = ov7670_info.addr;
1800 cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
Jonathan Corbet595a93a2011-06-11 14:46:48 -03001801 cam->i2c_adapter, &ov7670_info, NULL);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001802 if (cam->sensor == NULL) {
1803 ret = -ENODEV;
1804 goto out_unregister;
1805 }
1806
1807 ret = mcam_cam_init(cam);
1808 if (ret)
1809 goto out_unregister;
Javier Martin593403c2013-01-29 07:58:48 -03001810
Hans Verkuil949bd402015-03-05 17:33:17 -03001811 ret = mcam_setup_vb2(cam);
1812 if (ret)
1813 goto out_unregister;
1814
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001815 mutex_lock(&cam->s_mutex);
1816 cam->vdev = mcam_v4l_template;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001817 cam->vdev.v4l2_dev = &cam->v4l2_dev;
Hans Verkuil949bd402015-03-05 17:33:17 -03001818 cam->vdev.lock = &cam->s_mutex;
1819 cam->vdev.queue = &cam->vb_queue;
Javier Martin593403c2013-01-29 07:58:48 -03001820 video_set_drvdata(&cam->vdev, cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001821 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
Hans Verkuil949bd402015-03-05 17:33:17 -03001822 if (ret) {
1823 mutex_unlock(&cam->s_mutex);
1824 goto out_unregister;
1825 }
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001826
1827 /*
1828 * If so requested, try to get our DMA buffers now.
1829 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001830 if (cam->buffer_mode == B_vmalloc && !alloc_bufs_at_read) {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001831 if (mcam_alloc_dma_bufs(cam, 1))
1832 cam_warn(cam, "Unable to alloc DMA buffers at load"
1833 " will try again later.");
1834 }
1835
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001836 mutex_unlock(&cam->s_mutex);
Hans Verkuil949bd402015-03-05 17:33:17 -03001837 return 0;
1838
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001839out_unregister:
Hans Verkuil1e4cbe62015-03-05 12:00:11 -03001840 v4l2_ctrl_handler_free(&cam->ctrl_handler);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001841 v4l2_device_unregister(&cam->v4l2_dev);
1842 return ret;
1843}
1844
1845
1846void mccic_shutdown(struct mcam_camera *cam)
1847{
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -03001848 /*
1849 * If we have no users (and we really, really should have no
1850 * users) the device will already be powered down. Trying to
1851 * take it down again will wedge the machine, which is frowned
1852 * upon.
1853 */
Hans Verkuil949bd402015-03-05 17:33:17 -03001854 if (!list_empty(&cam->vdev.fh_list)) {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001855 cam_warn(cam, "Removing a device with users!\n");
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -03001856 mcam_ctlr_power_down(cam);
1857 }
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001858 if (cam->buffer_mode == B_vmalloc)
1859 mcam_free_dma_bufs(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001860 video_unregister_device(&cam->vdev);
Javier Martin593403c2013-01-29 07:58:48 -03001861 v4l2_ctrl_handler_free(&cam->ctrl_handler);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001862 v4l2_device_unregister(&cam->v4l2_dev);
1863}
1864
1865/*
1866 * Power management
1867 */
1868#ifdef CONFIG_PM
1869
1870void mccic_suspend(struct mcam_camera *cam)
1871{
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001872 mutex_lock(&cam->s_mutex);
Hans Verkuil949bd402015-03-05 17:33:17 -03001873 if (!list_empty(&cam->vdev.fh_list)) {
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001874 enum mcam_state cstate = cam->state;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001875
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001876 mcam_ctlr_stop_dma(cam);
1877 mcam_ctlr_power_down(cam);
1878 cam->state = cstate;
1879 }
1880 mutex_unlock(&cam->s_mutex);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001881}
1882
1883int mccic_resume(struct mcam_camera *cam)
1884{
1885 int ret = 0;
1886
1887 mutex_lock(&cam->s_mutex);
Hans Verkuil949bd402015-03-05 17:33:17 -03001888 if (!list_empty(&cam->vdev.fh_list)) {
Libin Yang05fed812013-07-03 01:55:58 -03001889 ret = mcam_ctlr_power_up(cam);
1890 if (ret) {
1891 mutex_unlock(&cam->s_mutex);
1892 return ret;
1893 }
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001894 __mcam_cam_reset(cam);
1895 } else {
1896 mcam_ctlr_power_down(cam);
1897 }
1898 mutex_unlock(&cam->s_mutex);
1899
1900 set_bit(CF_CONFIG_NEEDED, &cam->flags);
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001901 if (cam->state == S_STREAMING) {
1902 /*
1903 * If there was a buffer in the DMA engine at suspend
1904 * time, put it back on the queue or we'll forget about it.
1905 */
1906 if (cam->buffer_mode == B_DMA_sg && cam->vb_bufs[0])
1907 list_add(&cam->vb_bufs[0]->queue, &cam->buffers);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001908 ret = mcam_read_setup(cam);
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001909 }
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001910 return ret;
1911}
1912#endif /* CONFIG_PM */