blob: 9b878deb1437dc794f4db1ffab0a321de4e3f597 [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 */
212 int dma_desc_nent; /* Number of mapped descriptors */
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300213};
214
Junghak Sung2d700712015-09-22 10:30:30 -0300215static inline struct mcam_vb_buffer *vb_to_mvb(struct vb2_v4l2_buffer *vb)
Jonathan Corbetb5210fd2011-06-20 16:14:36 -0300216{
217 return container_of(vb, struct mcam_vb_buffer, vb_buf);
218}
219
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300220/*
221 * Hand a completed buffer back to user space.
222 */
223static void mcam_buffer_done(struct mcam_camera *cam, int frame,
Junghak Sung2d700712015-09-22 10:30:30 -0300224 struct vb2_v4l2_buffer *vbuf)
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300225{
Junghak Sung2d700712015-09-22 10:30:30 -0300226 vbuf->vb2_buf.planes[0].bytesused = cam->pix_format.sizeimage;
227 vbuf->sequence = cam->buf_seq[frame];
228 vbuf->field = V4L2_FIELD_NONE;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200229 vbuf->vb2_buf.timestamp = ktime_get_ns();
Junghak Sung2d700712015-09-22 10:30:30 -0300230 vb2_set_plane_payload(&vbuf->vb2_buf, 0, cam->pix_format.sizeimage);
231 vb2_buffer_done(&vbuf->vb2_buf, VB2_BUF_STATE_DONE);
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300232}
233
234
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300235
236/*
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300237 * Debugging and related.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300238 */
239#define cam_err(cam, fmt, arg...) \
240 dev_err((cam)->dev, fmt, ##arg);
241#define cam_warn(cam, fmt, arg...) \
242 dev_warn((cam)->dev, fmt, ##arg);
243#define cam_dbg(cam, fmt, arg...) \
244 dev_dbg((cam)->dev, fmt, ##arg);
245
246
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300247/*
248 * Flag manipulation helpers
249 */
250static void mcam_reset_buffers(struct mcam_camera *cam)
251{
252 int i;
253
254 cam->next_buf = -1;
Libin Yang0a0b3fb2013-07-03 01:56:03 -0300255 for (i = 0; i < cam->nbufs; i++) {
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300256 clear_bit(i, &cam->flags);
Libin Yang0a0b3fb2013-07-03 01:56:03 -0300257 clear_bit(CF_FRAME_SOF0 + i, &cam->flags);
258 }
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300259}
260
261static inline int mcam_needs_config(struct mcam_camera *cam)
262{
263 return test_bit(CF_CONFIG_NEEDED, &cam->flags);
264}
265
266static void mcam_set_config_needed(struct mcam_camera *cam, int needed)
267{
268 if (needed)
269 set_bit(CF_CONFIG_NEEDED, &cam->flags);
270 else
271 clear_bit(CF_CONFIG_NEEDED, &cam->flags);
272}
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300273
274/* ------------------------------------------------------------------- */
275/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300276 * Make the controller start grabbing images. Everything must
277 * be set up before doing this.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300278 */
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300279static void mcam_ctlr_start(struct mcam_camera *cam)
280{
281 /* set_bit performs a read, so no other barrier should be
282 needed here */
283 mcam_reg_set_bit(cam, REG_CTRL0, C0_ENABLE);
284}
285
286static void mcam_ctlr_stop(struct mcam_camera *cam)
287{
288 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
289}
290
Libin Yang05fed812013-07-03 01:55:58 -0300291static void mcam_enable_mipi(struct mcam_camera *mcam)
292{
293 /* Using MIPI mode and enable MIPI */
294 cam_dbg(mcam, "camera: DPHY3=0x%x, DPHY5=0x%x, DPHY6=0x%x\n",
295 mcam->dphy[0], mcam->dphy[1], mcam->dphy[2]);
296 mcam_reg_write(mcam, REG_CSI2_DPHY3, mcam->dphy[0]);
297 mcam_reg_write(mcam, REG_CSI2_DPHY5, mcam->dphy[1]);
298 mcam_reg_write(mcam, REG_CSI2_DPHY6, mcam->dphy[2]);
299
300 if (!mcam->mipi_enabled) {
301 if (mcam->lane > 4 || mcam->lane <= 0) {
302 cam_warn(mcam, "lane number error\n");
303 mcam->lane = 1; /* set the default value */
304 }
305 /*
306 * 0x41 actives 1 lane
307 * 0x43 actives 2 lanes
308 * 0x45 actives 3 lanes (never happen)
309 * 0x47 actives 4 lanes
310 */
311 mcam_reg_write(mcam, REG_CSI2_CTRL0,
312 CSI2_C0_MIPI_EN | CSI2_C0_ACT_LANE(mcam->lane));
313 mcam_reg_write(mcam, REG_CLKCTRL,
314 (mcam->mclk_src << 29) | mcam->mclk_div);
315
316 mcam->mipi_enabled = true;
317 }
318}
319
320static void mcam_disable_mipi(struct mcam_camera *mcam)
321{
322 /* Using Parallel mode or disable MIPI */
323 mcam_reg_write(mcam, REG_CSI2_CTRL0, 0x0);
324 mcam_reg_write(mcam, REG_CSI2_DPHY3, 0x0);
325 mcam_reg_write(mcam, REG_CSI2_DPHY5, 0x0);
326 mcam_reg_write(mcam, REG_CSI2_DPHY6, 0x0);
327 mcam->mipi_enabled = false;
328}
329
Hans Verkuil6eb40d52015-03-09 16:59:59 -0300330static bool mcam_fmt_is_planar(__u32 pfmt)
331{
332 struct mcam_format_struct *f;
333
334 f = mcam_find_format(pfmt);
335 return f->planar;
336}
337
338static void mcam_write_yuv_bases(struct mcam_camera *cam,
339 unsigned frame, dma_addr_t base)
340{
341 struct v4l2_pix_format *fmt = &cam->pix_format;
342 u32 pixel_count = fmt->width * fmt->height;
343 dma_addr_t y, u = 0, v = 0;
344
345 y = base;
346
347 switch (fmt->pixelformat) {
Hans Verkuil6eb40d52015-03-09 16:59:59 -0300348 case V4L2_PIX_FMT_YUV420:
349 u = y + pixel_count;
350 v = u + pixel_count / 4;
351 break;
352 case V4L2_PIX_FMT_YVU420:
353 v = y + pixel_count;
354 u = v + pixel_count / 4;
355 break;
356 default:
357 break;
358 }
359
360 mcam_reg_write(cam, REG_Y0BAR + frame * 4, y);
361 if (mcam_fmt_is_planar(fmt->pixelformat)) {
362 mcam_reg_write(cam, REG_U0BAR + frame * 4, u);
363 mcam_reg_write(cam, REG_V0BAR + frame * 4, v);
364 }
365}
366
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300367/* ------------------------------------------------------------------- */
Jonathan Corbet74984692011-07-08 17:50:49 -0300368
369#ifdef MCAM_MODE_VMALLOC
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300370/*
371 * Code specific to the vmalloc buffer mode.
372 */
373
374/*
375 * Allocate in-kernel DMA buffers for vmalloc mode.
376 */
377static int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
378{
379 int i;
380
381 mcam_set_config_needed(cam, 1);
382 if (loadtime)
383 cam->dma_buf_size = dma_buf_size;
384 else
385 cam->dma_buf_size = cam->pix_format.sizeimage;
386 if (n_dma_bufs > 3)
387 n_dma_bufs = 3;
388
389 cam->nbufs = 0;
390 for (i = 0; i < n_dma_bufs; i++) {
391 cam->dma_bufs[i] = dma_alloc_coherent(cam->dev,
392 cam->dma_buf_size, cam->dma_handles + i,
393 GFP_KERNEL);
394 if (cam->dma_bufs[i] == NULL) {
395 cam_warn(cam, "Failed to allocate DMA buffer\n");
396 break;
397 }
398 (cam->nbufs)++;
399 }
400
401 switch (cam->nbufs) {
402 case 1:
403 dma_free_coherent(cam->dev, cam->dma_buf_size,
404 cam->dma_bufs[0], cam->dma_handles[0]);
405 cam->nbufs = 0;
406 case 0:
407 cam_err(cam, "Insufficient DMA buffers, cannot operate\n");
408 return -ENOMEM;
409
410 case 2:
411 if (n_dma_bufs > 2)
412 cam_warn(cam, "Will limp along with only 2 buffers\n");
413 break;
414 }
415 return 0;
416}
417
418static void mcam_free_dma_bufs(struct mcam_camera *cam)
419{
420 int i;
421
422 for (i = 0; i < cam->nbufs; i++) {
423 dma_free_coherent(cam->dev, cam->dma_buf_size,
424 cam->dma_bufs[i], cam->dma_handles[i]);
425 cam->dma_bufs[i] = NULL;
426 }
427 cam->nbufs = 0;
428}
429
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300430
431/*
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300432 * Set up DMA buffers when operating in vmalloc mode
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300433 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300434static void mcam_ctlr_dma_vmalloc(struct mcam_camera *cam)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300435{
436 /*
Hans Verkuil6eb40d52015-03-09 16:59:59 -0300437 * Store the first two YUV buffers. Then either
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300438 * set the third if it exists, or tell the controller
439 * to just use two.
440 */
Hans Verkuil6eb40d52015-03-09 16:59:59 -0300441 mcam_write_yuv_bases(cam, 0, cam->dma_handles[0]);
442 mcam_write_yuv_bases(cam, 1, cam->dma_handles[1]);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300443 if (cam->nbufs > 2) {
Hans Verkuil6eb40d52015-03-09 16:59:59 -0300444 mcam_write_yuv_bases(cam, 2, cam->dma_handles[2]);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300445 mcam_reg_clear_bit(cam, REG_CTRL1, C1_TWOBUFS);
446 } else
447 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
Hans Verkuil7486af12013-05-29 06:59:44 -0300448 if (cam->chip_id == MCAM_CAFE)
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300449 mcam_reg_write(cam, REG_UBAR, 0); /* 32 bits only */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300450}
451
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300452/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300453 * Copy data out to user space in the vmalloc case
454 */
455static void mcam_frame_tasklet(unsigned long data)
456{
457 struct mcam_camera *cam = (struct mcam_camera *) data;
458 int i;
459 unsigned long flags;
460 struct mcam_vb_buffer *buf;
461
462 spin_lock_irqsave(&cam->dev_lock, flags);
463 for (i = 0; i < cam->nbufs; i++) {
464 int bufno = cam->next_buf;
465
466 if (cam->state != S_STREAMING || bufno < 0)
467 break; /* I/O got stopped */
468 if (++(cam->next_buf) >= cam->nbufs)
469 cam->next_buf = 0;
470 if (!test_bit(bufno, &cam->flags))
471 continue;
472 if (list_empty(&cam->buffers)) {
Libin Yangf698957a2012-12-15 05:57:50 -0300473 cam->frame_state.singles++;
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300474 break; /* Leave it valid, hope for better later */
475 }
Libin Yangf698957a2012-12-15 05:57:50 -0300476 cam->frame_state.delivered++;
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300477 clear_bit(bufno, &cam->flags);
478 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
479 queue);
480 list_del_init(&buf->queue);
481 /*
482 * Drop the lock during the big copy. This *should* be safe...
483 */
484 spin_unlock_irqrestore(&cam->dev_lock, flags);
Junghak Sung2d700712015-09-22 10:30:30 -0300485 memcpy(vb2_plane_vaddr(&buf->vb_buf.vb2_buf, 0),
486 cam->dma_bufs[bufno],
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300487 cam->pix_format.sizeimage);
488 mcam_buffer_done(cam, bufno, &buf->vb_buf);
489 spin_lock_irqsave(&cam->dev_lock, flags);
490 }
491 spin_unlock_irqrestore(&cam->dev_lock, flags);
492}
493
494
Jonathan Corbet74984692011-07-08 17:50:49 -0300495/*
496 * Make sure our allocated buffers are up to the task.
497 */
498static int mcam_check_dma_buffers(struct mcam_camera *cam)
499{
500 if (cam->nbufs > 0 && cam->dma_buf_size < cam->pix_format.sizeimage)
501 mcam_free_dma_bufs(cam);
502 if (cam->nbufs == 0)
503 return mcam_alloc_dma_bufs(cam, 0);
504 return 0;
505}
506
507static void mcam_vmalloc_done(struct mcam_camera *cam, int frame)
508{
509 tasklet_schedule(&cam->s_tasklet);
510}
511
512#else /* MCAM_MODE_VMALLOC */
513
514static inline int mcam_alloc_dma_bufs(struct mcam_camera *cam, int loadtime)
515{
516 return 0;
517}
518
519static inline void mcam_free_dma_bufs(struct mcam_camera *cam)
520{
521 return;
522}
523
524static inline int mcam_check_dma_buffers(struct mcam_camera *cam)
525{
526 return 0;
527}
528
529
530
531#endif /* MCAM_MODE_VMALLOC */
532
533
534#ifdef MCAM_MODE_DMA_CONTIG
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300535/* ---------------------------------------------------------------------- */
536/*
537 * DMA-contiguous code.
538 */
Libin Yangad6ac452013-07-03 01:56:02 -0300539
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300540/*
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300541 * Set up a contiguous buffer for the given frame. Here also is where
542 * the underrun strategy is set: if there is no buffer available, reuse
543 * the buffer from the other BAR and set the CF_SINGLE_BUFFER flag to
544 * keep the interrupt handler from giving that buffer back to user
545 * space. In this way, we always have a buffer to DMA to and don't
546 * have to try to play games stopping and restarting the controller.
547 */
548static void mcam_set_contig_buffer(struct mcam_camera *cam, int frame)
549{
550 struct mcam_vb_buffer *buf;
Libin Yangad6ac452013-07-03 01:56:02 -0300551 dma_addr_t dma_handle;
Junghak Sung2d700712015-09-22 10:30:30 -0300552 struct vb2_v4l2_buffer *vb;
Libin Yangad6ac452013-07-03 01:56:02 -0300553
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300554 /*
555 * If there are no available buffers, go into single mode
556 */
557 if (list_empty(&cam->buffers)) {
558 buf = cam->vb_bufs[frame ^ 0x1];
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300559 set_bit(CF_SINGLE_BUFFER, &cam->flags);
Libin Yangf698957a2012-12-15 05:57:50 -0300560 cam->frame_state.singles++;
Libin Yang1d3953f2013-07-03 01:56:01 -0300561 } else {
562 /*
563 * OK, we have a buffer we can use.
564 */
565 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer,
566 queue);
567 list_del_init(&buf->queue);
568 clear_bit(CF_SINGLE_BUFFER, &cam->flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300569 }
Libin Yang1d3953f2013-07-03 01:56:01 -0300570
571 cam->vb_bufs[frame] = buf;
Libin Yangad6ac452013-07-03 01:56:02 -0300572 vb = &buf->vb_buf;
573
Junghak Sung2d700712015-09-22 10:30:30 -0300574 dma_handle = vb2_dma_contig_plane_dma_addr(&vb->vb2_buf, 0);
Hans Verkuil6eb40d52015-03-09 16:59:59 -0300575 mcam_write_yuv_bases(cam, frame, dma_handle);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300576}
577
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300578/*
579 * Initial B_DMA_contig setup.
580 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300581static void mcam_ctlr_dma_contig(struct mcam_camera *cam)
582{
583 mcam_reg_set_bit(cam, REG_CTRL1, C1_TWOBUFS);
584 cam->nbufs = 2;
585 mcam_set_contig_buffer(cam, 0);
586 mcam_set_contig_buffer(cam, 1);
587}
588
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300589/*
590 * Frame completion handling.
591 */
592static void mcam_dma_contig_done(struct mcam_camera *cam, int frame)
593{
594 struct mcam_vb_buffer *buf = cam->vb_bufs[frame];
595
596 if (!test_bit(CF_SINGLE_BUFFER, &cam->flags)) {
Libin Yangf698957a2012-12-15 05:57:50 -0300597 cam->frame_state.delivered++;
Hans Verkuilca657b22015-03-05 18:00:07 -0300598 cam->vb_bufs[frame] = NULL;
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300599 mcam_buffer_done(cam, frame, &buf->vb_buf);
600 }
601 mcam_set_contig_buffer(cam, frame);
602}
603
Jonathan Corbet74984692011-07-08 17:50:49 -0300604#endif /* MCAM_MODE_DMA_CONTIG */
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300605
Jonathan Corbet74984692011-07-08 17:50:49 -0300606#ifdef MCAM_MODE_DMA_SG
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300607/* ---------------------------------------------------------------------- */
608/*
609 * Scatter/gather-specific code.
610 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300611
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300612/*
613 * Set up the next buffer for S/G I/O; caller should be sure that
614 * the controller is stopped and a buffer is available.
615 */
616static void mcam_sg_next_buffer(struct mcam_camera *cam)
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300617{
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300618 struct mcam_vb_buffer *buf;
619
620 buf = list_first_entry(&cam->buffers, struct mcam_vb_buffer, queue);
621 list_del_init(&buf->queue);
Jonathan Corbet121bbe22012-03-16 19:14:53 -0300622 /*
623 * Very Bad Not Good Things happen if you don't clear
624 * C1_DESC_ENA before making any descriptor changes.
625 */
626 mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_ENA);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300627 mcam_reg_write(cam, REG_DMA_DESC_Y, buf->dma_desc_pa);
628 mcam_reg_write(cam, REG_DESC_LEN_Y,
629 buf->dma_desc_nent*sizeof(struct mcam_dma_desc));
630 mcam_reg_write(cam, REG_DESC_LEN_U, 0);
631 mcam_reg_write(cam, REG_DESC_LEN_V, 0);
Jonathan Corbet121bbe22012-03-16 19:14:53 -0300632 mcam_reg_set_bit(cam, REG_CTRL1, C1_DESC_ENA);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300633 cam->vb_bufs[0] = buf;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -0300634}
635
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300636/*
637 * Initial B_DMA_sg setup
638 */
639static void mcam_ctlr_dma_sg(struct mcam_camera *cam)
640{
Jonathan Corbetbb0a8962011-12-30 14:13:41 -0300641 /*
642 * The list-empty condition can hit us at resume time
643 * if the buffer list was empty when the system was suspended.
644 */
645 if (list_empty(&cam->buffers)) {
646 set_bit(CF_SG_RESTART, &cam->flags);
647 return;
648 }
649
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300650 mcam_reg_clear_bit(cam, REG_CTRL1, C1_DESC_3WORD);
651 mcam_sg_next_buffer(cam);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300652 cam->nbufs = 3;
653}
654
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300655
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300656/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300657 * Frame completion with S/G is trickier. We can't muck with
658 * a descriptor chain on the fly, since the controller buffers it
659 * internally. So we have to actually stop and restart; Marvell
660 * says this is the way to do it.
661 *
662 * Of course, stopping is easier said than done; experience shows
663 * that the controller can start a frame *after* C0_ENABLE has been
664 * cleared. So when running in S/G mode, the controller is "stopped"
665 * on receipt of the start-of-frame interrupt. That means we can
666 * safely change the DMA descriptor array here and restart things
667 * (assuming there's another buffer waiting to go).
668 */
669static void mcam_dma_sg_done(struct mcam_camera *cam, int frame)
670{
671 struct mcam_vb_buffer *buf = cam->vb_bufs[0];
672
673 /*
Jonathan Corbet49df19e2012-03-16 19:14:50 -0300674 * If we're no longer supposed to be streaming, don't do anything.
675 */
676 if (cam->state != S_STREAMING)
677 return;
678 /*
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300679 * If we have another buffer available, put it in and
680 * restart the engine.
681 */
682 if (!list_empty(&cam->buffers)) {
683 mcam_sg_next_buffer(cam);
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300684 mcam_ctlr_start(cam);
685 /*
686 * Otherwise set CF_SG_RESTART and the controller will
687 * be restarted once another buffer shows up.
688 */
689 } else {
690 set_bit(CF_SG_RESTART, &cam->flags);
Libin Yangf698957a2012-12-15 05:57:50 -0300691 cam->frame_state.singles++;
Jonathan Corbetbb0a8962011-12-30 14:13:41 -0300692 cam->vb_bufs[0] = NULL;
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300693 }
694 /*
695 * Now we can give the completed frame back to user space.
696 */
Libin Yangf698957a2012-12-15 05:57:50 -0300697 cam->frame_state.delivered++;
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300698 mcam_buffer_done(cam, frame, &buf->vb_buf);
699}
700
701
702/*
703 * Scatter/gather mode requires stopping the controller between
704 * frames so we can put in a new DMA descriptor array. If no new
705 * buffer exists at frame completion, the controller is left stopped;
706 * this function is charged with gettig things going again.
707 */
708static void mcam_sg_restart(struct mcam_camera *cam)
709{
710 mcam_ctlr_dma_sg(cam);
711 mcam_ctlr_start(cam);
712 clear_bit(CF_SG_RESTART, &cam->flags);
713}
714
Jonathan Corbet74984692011-07-08 17:50:49 -0300715#else /* MCAM_MODE_DMA_SG */
716
717static inline void mcam_sg_restart(struct mcam_camera *cam)
718{
719 return;
720}
721
722#endif /* MCAM_MODE_DMA_SG */
Jonathan Corbetd43dae72011-07-08 17:50:46 -0300723
724/* ---------------------------------------------------------------------- */
725/*
726 * Buffer-mode-independent controller code.
727 */
728
729/*
730 * Image format setup
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300731 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300732static void mcam_ctlr_image(struct mcam_camera *cam)
733{
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300734 struct v4l2_pix_format *fmt = &cam->pix_format;
Libin Yangad6ac452013-07-03 01:56:02 -0300735 u32 widthy = 0, widthuv = 0, imgsz_h, imgsz_w;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300736
Libin Yangad6ac452013-07-03 01:56:02 -0300737 cam_dbg(cam, "camera: bytesperline = %d; height = %d\n",
738 fmt->bytesperline, fmt->sizeimage / fmt->bytesperline);
739 imgsz_h = (fmt->height << IMGSZ_V_SHIFT) & IMGSZ_V_MASK;
740 imgsz_w = (fmt->width * 2) & IMGSZ_H_MASK;
741
742 switch (fmt->pixelformat) {
743 case V4L2_PIX_FMT_YUYV:
Hans Verkuil2a700d82015-04-13 11:18:51 -0300744 case V4L2_PIX_FMT_YVYU:
Libin Yangad6ac452013-07-03 01:56:02 -0300745 widthy = fmt->width * 2;
746 widthuv = 0;
747 break;
Libin Yangad6ac452013-07-03 01:56:02 -0300748 case V4L2_PIX_FMT_YUV420:
749 case V4L2_PIX_FMT_YVU420:
750 widthy = fmt->width;
751 widthuv = fmt->width / 2;
752 break;
753 default:
754 widthy = fmt->bytesperline;
755 widthuv = 0;
Hans Verkuil47ba7db2015-03-09 17:14:36 -0300756 break;
Libin Yangad6ac452013-07-03 01:56:02 -0300757 }
758
759 mcam_reg_write_mask(cam, REG_IMGPITCH, widthuv << 16 | widthy,
760 IMGP_YP_MASK | IMGP_UVP_MASK);
761 mcam_reg_write(cam, REG_IMGSIZE, imgsz_h | imgsz_w);
762 mcam_reg_write(cam, REG_IMGOFFSET, 0x0);
763
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300764 /*
765 * Tell the controller about the image format we are using.
766 */
Libin Yangad6ac452013-07-03 01:56:02 -0300767 switch (fmt->pixelformat) {
Libin Yangad6ac452013-07-03 01:56:02 -0300768 case V4L2_PIX_FMT_YUV420:
769 case V4L2_PIX_FMT_YVU420:
770 mcam_reg_write_mask(cam, REG_CTRL0,
Hans Verkuil2a700d82015-04-13 11:18:51 -0300771 C0_DF_YUV | C0_YUV_420PL | C0_YUVE_VYUY, C0_DF_MASK);
Libin Yangad6ac452013-07-03 01:56:02 -0300772 break;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300773 case V4L2_PIX_FMT_YUYV:
Libin Yangad6ac452013-07-03 01:56:02 -0300774 mcam_reg_write_mask(cam, REG_CTRL0,
Hans Verkuil2a700d82015-04-13 11:18:51 -0300775 C0_DF_YUV | C0_YUV_PACKED | C0_YUVE_NOSWAP, C0_DF_MASK);
Libin Yangad6ac452013-07-03 01:56:02 -0300776 break;
Hans Verkuil2a700d82015-04-13 11:18:51 -0300777 case V4L2_PIX_FMT_YVYU:
Libin Yangad6ac452013-07-03 01:56:02 -0300778 mcam_reg_write_mask(cam, REG_CTRL0,
Hans Verkuil2a700d82015-04-13 11:18:51 -0300779 C0_DF_YUV | C0_YUV_PACKED | C0_YUVE_SWAP24, C0_DF_MASK);
Libin Yangad6ac452013-07-03 01:56:02 -0300780 break;
Hans Verkuilccf509f2015-04-24 06:52:47 -0300781 case V4L2_PIX_FMT_XRGB444:
Libin Yangad6ac452013-07-03 01:56:02 -0300782 mcam_reg_write_mask(cam, REG_CTRL0,
Hans Verkuilccf509f2015-04-24 06:52:47 -0300783 C0_DF_RGB | C0_RGBF_444 | C0_RGB4_XBGR, C0_DF_MASK);
Libin Yangad6ac452013-07-03 01:56:02 -0300784 break;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300785 case V4L2_PIX_FMT_RGB565:
Libin Yangad6ac452013-07-03 01:56:02 -0300786 mcam_reg_write_mask(cam, REG_CTRL0,
787 C0_DF_RGB | C0_RGBF_565 | C0_RGB5_BGGR, C0_DF_MASK);
788 break;
Hans Verkuil8380b7e2015-03-14 08:47:01 -0300789 case V4L2_PIX_FMT_SBGGR8:
790 mcam_reg_write_mask(cam, REG_CTRL0,
791 C0_DF_RGB | C0_RGB5_GRBG, C0_DF_MASK);
792 break;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300793 default:
Libin Yangad6ac452013-07-03 01:56:02 -0300794 cam_err(cam, "camera: unknown format: %#x\n", fmt->pixelformat);
795 break;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300796 }
Libin Yangad6ac452013-07-03 01:56:02 -0300797
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300798 /*
799 * Make sure it knows we want to use hsync/vsync.
800 */
Libin Yangad6ac452013-07-03 01:56:02 -0300801 mcam_reg_write_mask(cam, REG_CTRL0, C0_SIF_HVSYNC, C0_SIFM_MASK);
Libin Yang05fed812013-07-03 01:55:58 -0300802 /*
803 * This field controls the generation of EOF(DVP only)
804 */
805 if (cam->bus_type != V4L2_MBUS_CSI2)
806 mcam_reg_set_bit(cam, REG_CTRL0,
807 C0_EOF_VSYNC | C0_VEDGE_CTRL);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300808}
809
810
811/*
812 * Configure the controller for operation; caller holds the
813 * device mutex.
814 */
815static int mcam_ctlr_configure(struct mcam_camera *cam)
816{
817 unsigned long flags;
818
819 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetbb0a8962011-12-30 14:13:41 -0300820 clear_bit(CF_SG_RESTART, &cam->flags);
Jonathan Corbet74984692011-07-08 17:50:49 -0300821 cam->dma_setup(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300822 mcam_ctlr_image(cam);
823 mcam_set_config_needed(cam, 0);
824 spin_unlock_irqrestore(&cam->dev_lock, flags);
825 return 0;
826}
827
828static void mcam_ctlr_irq_enable(struct mcam_camera *cam)
829{
830 /*
831 * Clear any pending interrupts, since we do not
832 * expect to have I/O active prior to enabling.
833 */
834 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS);
835 mcam_reg_set_bit(cam, REG_IRQMASK, FRAMEIRQS);
836}
837
838static void mcam_ctlr_irq_disable(struct mcam_camera *cam)
839{
840 mcam_reg_clear_bit(cam, REG_IRQMASK, FRAMEIRQS);
841}
842
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300843
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300844
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300845static void mcam_ctlr_init(struct mcam_camera *cam)
846{
847 unsigned long flags;
848
849 spin_lock_irqsave(&cam->dev_lock, flags);
850 /*
851 * Make sure it's not powered down.
852 */
853 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
854 /*
855 * Turn off the enable bit. It sure should be off anyway,
856 * but it's good to be sure.
857 */
858 mcam_reg_clear_bit(cam, REG_CTRL0, C0_ENABLE);
859 /*
860 * Clock the sensor appropriately. Controller clock should
861 * be 48MHz, sensor "typical" value is half that.
862 */
863 mcam_reg_write_mask(cam, REG_CLKCTRL, 2, CLK_DIV_MASK);
864 spin_unlock_irqrestore(&cam->dev_lock, flags);
865}
866
867
868/*
869 * Stop the controller, and don't return until we're really sure that no
870 * further DMA is going on.
871 */
872static void mcam_ctlr_stop_dma(struct mcam_camera *cam)
873{
874 unsigned long flags;
875
876 /*
877 * Theory: stop the camera controller (whether it is operating
878 * or not). Delay briefly just in case we race with the SOF
879 * interrupt, then wait until no DMA is active.
880 */
881 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300882 clear_bit(CF_SG_RESTART, &cam->flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300883 mcam_ctlr_stop(cam);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -0300884 cam->state = S_IDLE;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300885 spin_unlock_irqrestore(&cam->dev_lock, flags);
Jonathan Corbet482d35c2012-03-16 19:14:52 -0300886 /*
887 * This is a brutally long sleep, but experience shows that
888 * it can take the controller a while to get the message that
889 * it needs to stop grabbing frames. In particular, we can
890 * sometimes (on mmp) get a frame at the end WITHOUT the
891 * start-of-frame indication.
892 */
893 msleep(150);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300894 if (test_bit(CF_DMA_ACTIVE, &cam->flags))
895 cam_err(cam, "Timeout waiting for DMA to end\n");
896 /* This would be bad news - what now? */
897 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300898 mcam_ctlr_irq_disable(cam);
899 spin_unlock_irqrestore(&cam->dev_lock, flags);
900}
901
902/*
903 * Power up and down.
904 */
Libin Yang05fed812013-07-03 01:55:58 -0300905static int mcam_ctlr_power_up(struct mcam_camera *cam)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300906{
907 unsigned long flags;
Libin Yang05fed812013-07-03 01:55:58 -0300908 int ret;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300909
910 spin_lock_irqsave(&cam->dev_lock, flags);
Libin Yang05fed812013-07-03 01:55:58 -0300911 ret = cam->plat_power_up(cam);
912 if (ret) {
913 spin_unlock_irqrestore(&cam->dev_lock, flags);
914 return ret;
915 }
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300916 mcam_reg_clear_bit(cam, REG_CTRL1, C1_PWRDWN);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300917 spin_unlock_irqrestore(&cam->dev_lock, flags);
918 msleep(5); /* Just to be sure */
Libin Yang05fed812013-07-03 01:55:58 -0300919 return 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300920}
921
922static void mcam_ctlr_power_down(struct mcam_camera *cam)
923{
924 unsigned long flags;
925
926 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300927 /*
928 * School of hard knocks department: be sure we do any register
929 * twiddling on the controller *before* calling the platform
930 * power down routine.
931 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300932 mcam_reg_set_bit(cam, REG_CTRL1, C1_PWRDWN);
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -0300933 cam->plat_power_down(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300934 spin_unlock_irqrestore(&cam->dev_lock, flags);
935}
936
937/* -------------------------------------------------------------------- */
938/*
939 * Communications with the sensor.
940 */
941
942static int __mcam_cam_reset(struct mcam_camera *cam)
943{
944 return sensor_call(cam, core, reset, 0);
945}
946
947/*
948 * We have found the sensor on the i2c. Let's try to have a
949 * conversation.
950 */
951static int mcam_cam_init(struct mcam_camera *cam)
952{
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300953 int ret;
954
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300955 if (cam->state != S_NOTREADY)
956 cam_warn(cam, "Cam init with device in funky state %d",
957 cam->state);
958 ret = __mcam_cam_reset(cam);
Hans Verkuil7486af12013-05-29 06:59:44 -0300959 /* Get/set parameters? */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300960 cam->state = S_IDLE;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300961 mcam_ctlr_power_down(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300962 return ret;
963}
964
965/*
966 * Configure the sensor to match the parameters we have. Caller should
967 * hold s_mutex
968 */
969static int mcam_cam_set_flip(struct mcam_camera *cam)
970{
971 struct v4l2_control ctrl;
972
973 memset(&ctrl, 0, sizeof(ctrl));
974 ctrl.id = V4L2_CID_VFLIP;
975 ctrl.value = flip;
976 return sensor_call(cam, core, s_ctrl, &ctrl);
977}
978
979
980static int mcam_cam_configure(struct mcam_camera *cam)
981{
Hans Verkuilebf984b2015-04-09 04:05:59 -0300982 struct v4l2_subdev_format format = {
983 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
984 };
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300985 int ret;
986
Hans Verkuilebf984b2015-04-09 04:05:59 -0300987 v4l2_fill_mbus_format(&format.format, &cam->pix_format, cam->mbus_code);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300988 ret = sensor_call(cam, core, init, 0);
989 if (ret == 0)
Hans Verkuilebf984b2015-04-09 04:05:59 -0300990 ret = sensor_call(cam, pad, set_fmt, NULL, &format);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300991 /*
992 * OV7670 does weird things if flip is set *before* format...
993 */
994 ret += mcam_cam_set_flip(cam);
995 return ret;
996}
997
Jonathan Corbetabfa3df2011-06-11 14:46:43 -0300998/*
999 * Get everything ready, and start grabbing frames.
1000 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001001static int mcam_read_setup(struct mcam_camera *cam)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001002{
1003 int ret;
1004 unsigned long flags;
1005
1006 /*
1007 * Configuration. If we still don't have DMA buffers,
1008 * make one last, desperate attempt.
1009 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001010 if (cam->buffer_mode == B_vmalloc && cam->nbufs == 0 &&
1011 mcam_alloc_dma_bufs(cam, 0))
1012 return -ENOMEM;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001013
1014 if (mcam_needs_config(cam)) {
1015 mcam_cam_configure(cam);
1016 ret = mcam_ctlr_configure(cam);
1017 if (ret)
1018 return ret;
1019 }
1020
1021 /*
1022 * Turn it loose.
1023 */
1024 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbet482d35c2012-03-16 19:14:52 -03001025 clear_bit(CF_DMA_ACTIVE, &cam->flags);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001026 mcam_reset_buffers(cam);
Libin Yang05fed812013-07-03 01:55:58 -03001027 /*
1028 * Update CSI2_DPHY value
1029 */
1030 if (cam->calc_dphy)
1031 cam->calc_dphy(cam);
1032 cam_dbg(cam, "camera: DPHY sets: dphy3=0x%x, dphy5=0x%x, dphy6=0x%x\n",
1033 cam->dphy[0], cam->dphy[1], cam->dphy[2]);
1034 if (cam->bus_type == V4L2_MBUS_CSI2)
1035 mcam_enable_mipi(cam);
1036 else
1037 mcam_disable_mipi(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001038 mcam_ctlr_irq_enable(cam);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001039 cam->state = S_STREAMING;
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001040 if (!test_bit(CF_SG_RESTART, &cam->flags))
1041 mcam_ctlr_start(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001042 spin_unlock_irqrestore(&cam->dev_lock, flags);
1043 return 0;
1044}
1045
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001046/* ----------------------------------------------------------------------- */
1047/*
1048 * Videobuf2 interface code.
1049 */
1050
Guennadi Liakhovetskifc714e72011-08-24 10:30:21 -03001051static int mcam_vb_queue_setup(struct vb2_queue *vq,
Hans Verkuildf9ecb02015-10-28 00:50:37 -02001052 unsigned int *nbufs,
Marek Szyprowski035aa142011-08-24 06:43:36 -03001053 unsigned int *num_planes, unsigned int sizes[],
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001054 void *alloc_ctxs[])
1055{
1056 struct mcam_camera *cam = vb2_get_drv_priv(vq);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001057 int minbufs = (cam->buffer_mode == B_DMA_contig) ? 3 : 2;
Hans Verkuildf9ecb02015-10-28 00:50:37 -02001058 unsigned size = cam->pix_format.sizeimage;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001059
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001060 if (*nbufs < minbufs)
1061 *nbufs = minbufs;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001062 if (cam->buffer_mode == B_DMA_contig)
1063 alloc_ctxs[0] = cam->vb_alloc_ctx;
Hans Verkuil0c3a14c2014-11-18 09:51:01 -03001064 else if (cam->buffer_mode == B_DMA_sg)
1065 alloc_ctxs[0] = cam->vb_alloc_ctx_sg;
Hans Verkuildf9ecb02015-10-28 00:50:37 -02001066
1067 if (*num_planes)
1068 return sizes[0] < size ? -EINVAL : 0;
1069 sizes[0] = size;
1070 *num_planes = 1; /* Someday we have to support planar formats... */
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001071 return 0;
1072}
1073
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001074
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001075static void mcam_vb_buf_queue(struct vb2_buffer *vb)
1076{
Junghak Sung2d700712015-09-22 10:30:30 -03001077 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1078 struct mcam_vb_buffer *mvb = vb_to_mvb(vbuf);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001079 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1080 unsigned long flags;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001081 int start;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001082
1083 spin_lock_irqsave(&cam->dev_lock, flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001084 start = (cam->state == S_BUFWAIT) && !list_empty(&cam->buffers);
1085 list_add(&mvb->queue, &cam->buffers);
Jonathan Corbet49df19e2012-03-16 19:14:50 -03001086 if (cam->state == S_STREAMING && test_bit(CF_SG_RESTART, &cam->flags))
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001087 mcam_sg_restart(cam);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001088 spin_unlock_irqrestore(&cam->dev_lock, flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001089 if (start)
1090 mcam_read_setup(cam);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001091}
1092
Hans Verkuilca657b22015-03-05 18:00:07 -03001093static void mcam_vb_requeue_bufs(struct vb2_queue *vq,
1094 enum vb2_buffer_state state)
1095{
1096 struct mcam_camera *cam = vb2_get_drv_priv(vq);
1097 struct mcam_vb_buffer *buf, *node;
1098 unsigned long flags;
1099 unsigned i;
1100
1101 spin_lock_irqsave(&cam->dev_lock, flags);
1102 list_for_each_entry_safe(buf, node, &cam->buffers, queue) {
Junghak Sung2d700712015-09-22 10:30:30 -03001103 vb2_buffer_done(&buf->vb_buf.vb2_buf, state);
Hans Verkuilca657b22015-03-05 18:00:07 -03001104 list_del(&buf->queue);
1105 }
1106 for (i = 0; i < MAX_DMA_BUFS; i++) {
1107 buf = cam->vb_bufs[i];
1108
1109 if (buf) {
Junghak Sung2d700712015-09-22 10:30:30 -03001110 vb2_buffer_done(&buf->vb_buf.vb2_buf, state);
Hans Verkuilca657b22015-03-05 18:00:07 -03001111 cam->vb_bufs[i] = NULL;
1112 }
1113 }
1114 spin_unlock_irqrestore(&cam->dev_lock, flags);
1115}
1116
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001117/*
1118 * These need to be called with the mutex held from vb2
1119 */
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001120static int mcam_vb_start_streaming(struct vb2_queue *vq, unsigned int count)
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001121{
1122 struct mcam_camera *cam = vb2_get_drv_priv(vq);
Libin Yang0a0b3fb2013-07-03 01:56:03 -03001123 unsigned int frame;
Hans Verkuilca657b22015-03-05 18:00:07 -03001124 int ret;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001125
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001126 if (cam->state != S_IDLE) {
Hans Verkuilca657b22015-03-05 18:00:07 -03001127 mcam_vb_requeue_bufs(vq, VB2_BUF_STATE_QUEUED);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001128 return -EINVAL;
Marek Szyprowskibd323e22011-08-29 08:51:49 -03001129 }
Hans Verkuil949bd402015-03-05 17:33:17 -03001130 cam->frame_state.frames = 0;
1131 cam->frame_state.singles = 0;
1132 cam->frame_state.delivered = 0;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001133 cam->sequence = 0;
1134 /*
1135 * Videobuf2 sneakily hoards all the buffers and won't
1136 * give them to us until *after* streaming starts. But
1137 * we can't actually start streaming until we have a
1138 * destination. So go into a wait state and hope they
1139 * give us buffers soon.
1140 */
1141 if (cam->buffer_mode != B_vmalloc && list_empty(&cam->buffers)) {
1142 cam->state = S_BUFWAIT;
1143 return 0;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001144 }
Libin Yang0a0b3fb2013-07-03 01:56:03 -03001145
1146 /*
1147 * Ensure clear the left over frame flags
1148 * before every really start streaming
1149 */
1150 for (frame = 0; frame < cam->nbufs; frame++)
1151 clear_bit(CF_FRAME_SOF0 + frame, &cam->flags);
1152
Hans Verkuilca657b22015-03-05 18:00:07 -03001153 ret = mcam_read_setup(cam);
1154 if (ret)
1155 mcam_vb_requeue_bufs(vq, VB2_BUF_STATE_QUEUED);
1156 return ret;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001157}
1158
Hans Verkuile37559b2014-04-17 02:47:21 -03001159static void mcam_vb_stop_streaming(struct vb2_queue *vq)
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001160{
1161 struct mcam_camera *cam = vb2_get_drv_priv(vq);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001162
Hans Verkuil949bd402015-03-05 17:33:17 -03001163 cam_dbg(cam, "stop_streaming: %d frames, %d singles, %d delivered\n",
1164 cam->frame_state.frames, cam->frame_state.singles,
1165 cam->frame_state.delivered);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001166 if (cam->state == S_BUFWAIT) {
1167 /* They never gave us buffers */
1168 cam->state = S_IDLE;
Hans Verkuile37559b2014-04-17 02:47:21 -03001169 return;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001170 }
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001171 if (cam->state != S_STREAMING)
Hans Verkuile37559b2014-04-17 02:47:21 -03001172 return;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001173 mcam_ctlr_stop_dma(cam);
1174 /*
Libin Yang7c269f452013-07-03 01:56:00 -03001175 * Reset the CCIC PHY after stopping streaming,
1176 * otherwise, the CCIC may be unstable.
1177 */
1178 if (cam->ctlr_reset)
1179 cam->ctlr_reset(cam);
1180 /*
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001181 * VB2 reclaims the buffers, so we need to forget
1182 * about them.
1183 */
Hans Verkuilca657b22015-03-05 18:00:07 -03001184 mcam_vb_requeue_bufs(vq, VB2_BUF_STATE_ERROR);
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001185}
1186
1187
1188static const struct vb2_ops mcam_vb2_ops = {
1189 .queue_setup = mcam_vb_queue_setup,
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001190 .buf_queue = mcam_vb_buf_queue,
1191 .start_streaming = mcam_vb_start_streaming,
1192 .stop_streaming = mcam_vb_stop_streaming,
Prabhakar Lad519694f2014-11-26 19:42:29 -03001193 .wait_prepare = vb2_ops_wait_prepare,
1194 .wait_finish = vb2_ops_wait_finish,
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001195};
1196
Jonathan Corbet74984692011-07-08 17:50:49 -03001197
1198#ifdef MCAM_MODE_DMA_SG
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001199/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001200 * Scatter/gather mode uses all of the above functions plus a
1201 * few extras to deal with DMA mapping.
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001202 */
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001203static int mcam_vb_sg_buf_init(struct vb2_buffer *vb)
1204{
Junghak Sung2d700712015-09-22 10:30:30 -03001205 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1206 struct mcam_vb_buffer *mvb = vb_to_mvb(vbuf);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001207 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
1208 int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
1209
1210 mvb->dma_desc = dma_alloc_coherent(cam->dev,
1211 ndesc * sizeof(struct mcam_dma_desc),
1212 &mvb->dma_desc_pa, GFP_KERNEL);
1213 if (mvb->dma_desc == NULL) {
1214 cam_err(cam, "Unable to get DMA descriptor array\n");
1215 return -ENOMEM;
1216 }
1217 return 0;
1218}
1219
1220static int mcam_vb_sg_buf_prepare(struct vb2_buffer *vb)
1221{
Junghak Sung2d700712015-09-22 10:30:30 -03001222 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1223 struct mcam_vb_buffer *mvb = vb_to_mvb(vbuf);
Ricardo Ribalda22301242013-08-02 10:20:00 -03001224 struct sg_table *sg_table = vb2_dma_sg_plane_desc(vb, 0);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001225 struct mcam_dma_desc *desc = mvb->dma_desc;
1226 struct scatterlist *sg;
1227 int i;
1228
Hans Verkuild790b7e2014-11-24 08:50:31 -03001229 for_each_sg(sg_table->sgl, sg, sg_table->nents, i) {
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001230 desc->dma_addr = sg_dma_address(sg);
1231 desc->segment_len = sg_dma_len(sg);
1232 desc++;
1233 }
1234 return 0;
1235}
1236
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001237static void mcam_vb_sg_buf_cleanup(struct vb2_buffer *vb)
1238{
Junghak Sung2d700712015-09-22 10:30:30 -03001239 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001240 struct mcam_camera *cam = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -03001241 struct mcam_vb_buffer *mvb = vb_to_mvb(vbuf);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001242 int ndesc = cam->pix_format.sizeimage/PAGE_SIZE + 1;
1243
1244 dma_free_coherent(cam->dev, ndesc * sizeof(struct mcam_dma_desc),
1245 mvb->dma_desc, mvb->dma_desc_pa);
1246}
1247
1248
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001249static const struct vb2_ops mcam_vb2_sg_ops = {
1250 .queue_setup = mcam_vb_queue_setup,
1251 .buf_init = mcam_vb_sg_buf_init,
1252 .buf_prepare = mcam_vb_sg_buf_prepare,
1253 .buf_queue = mcam_vb_buf_queue,
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001254 .buf_cleanup = mcam_vb_sg_buf_cleanup,
1255 .start_streaming = mcam_vb_start_streaming,
1256 .stop_streaming = mcam_vb_stop_streaming,
Prabhakar Lad519694f2014-11-26 19:42:29 -03001257 .wait_prepare = vb2_ops_wait_prepare,
1258 .wait_finish = vb2_ops_wait_finish,
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001259};
1260
Jonathan Corbet74984692011-07-08 17:50:49 -03001261#endif /* MCAM_MODE_DMA_SG */
1262
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001263static int mcam_setup_vb2(struct mcam_camera *cam)
1264{
1265 struct vb2_queue *vq = &cam->vb_queue;
1266
1267 memset(vq, 0, sizeof(*vq));
1268 vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001269 vq->drv_priv = cam;
Prabhakar Lad519694f2014-11-26 19:42:29 -03001270 vq->lock = &cam->s_mutex;
Hans Verkuil17d36752015-03-03 15:07:27 -03001271 vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
Hans Verkuilca16a642015-03-09 18:02:23 -03001272 vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF | VB2_READ;
1273 vq->buf_struct_size = sizeof(struct mcam_vb_buffer);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001274 INIT_LIST_HEAD(&cam->buffers);
1275 switch (cam->buffer_mode) {
1276 case B_DMA_contig:
Jonathan Corbet74984692011-07-08 17:50:49 -03001277#ifdef MCAM_MODE_DMA_CONTIG
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001278 vq->ops = &mcam_vb2_ops;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001279 vq->mem_ops = &vb2_dma_contig_memops;
Jonathan Corbet74984692011-07-08 17:50:49 -03001280 cam->dma_setup = mcam_ctlr_dma_contig;
1281 cam->frame_complete = mcam_dma_contig_done;
Hans Verkuil0c3a14c2014-11-18 09:51:01 -03001282 cam->vb_alloc_ctx = vb2_dma_contig_init_ctx(cam->dev);
1283 if (IS_ERR(cam->vb_alloc_ctx))
1284 return PTR_ERR(cam->vb_alloc_ctx);
Jonathan Corbet74984692011-07-08 17:50:49 -03001285#endif
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001286 break;
1287 case B_DMA_sg:
Jonathan Corbet74984692011-07-08 17:50:49 -03001288#ifdef MCAM_MODE_DMA_SG
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001289 vq->ops = &mcam_vb2_sg_ops;
1290 vq->mem_ops = &vb2_dma_sg_memops;
Jonathan Corbet74984692011-07-08 17:50:49 -03001291 cam->dma_setup = mcam_ctlr_dma_sg;
1292 cam->frame_complete = mcam_dma_sg_done;
Hans Verkuil0c3a14c2014-11-18 09:51:01 -03001293 cam->vb_alloc_ctx_sg = vb2_dma_sg_init_ctx(cam->dev);
1294 if (IS_ERR(cam->vb_alloc_ctx_sg))
1295 return PTR_ERR(cam->vb_alloc_ctx_sg);
Jonathan Corbet74984692011-07-08 17:50:49 -03001296#endif
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001297 break;
1298 case B_vmalloc:
Jonathan Corbet74984692011-07-08 17:50:49 -03001299#ifdef MCAM_MODE_VMALLOC
1300 tasklet_init(&cam->s_tasklet, mcam_frame_tasklet,
1301 (unsigned long) cam);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001302 vq->ops = &mcam_vb2_ops;
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001303 vq->mem_ops = &vb2_vmalloc_memops;
Jonathan Corbet74984692011-07-08 17:50:49 -03001304 cam->dma_setup = mcam_ctlr_dma_vmalloc;
1305 cam->frame_complete = mcam_vmalloc_done;
1306#endif
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001307 break;
1308 }
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001309 return vb2_queue_init(vq);
1310}
1311
1312static void mcam_cleanup_vb2(struct mcam_camera *cam)
1313{
Jonathan Corbet74984692011-07-08 17:50:49 -03001314#ifdef MCAM_MODE_DMA_CONTIG
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001315 if (cam->buffer_mode == B_DMA_contig)
1316 vb2_dma_contig_cleanup_ctx(cam->vb_alloc_ctx);
Jonathan Corbet74984692011-07-08 17:50:49 -03001317#endif
Hans Verkuil0c3a14c2014-11-18 09:51:01 -03001318#ifdef MCAM_MODE_DMA_SG
1319 if (cam->buffer_mode == B_DMA_sg)
1320 vb2_dma_sg_cleanup_ctx(cam->vb_alloc_ctx_sg);
1321#endif
Jonathan Corbetb5210fd2011-06-20 16:14:36 -03001322}
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001323
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001324
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001325/* ---------------------------------------------------------------------- */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001326/*
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001327 * The long list of V4L2 ioctl() operations.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001328 */
1329
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001330static int mcam_vidioc_querycap(struct file *file, void *priv,
1331 struct v4l2_capability *cap)
1332{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001333 struct mcam_camera *cam = video_drvdata(file);
Hans Verkuilb7b68392015-03-05 04:57:32 -03001334
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001335 strcpy(cap->driver, "marvell_ccic");
1336 strcpy(cap->card, "marvell_ccic");
Hans Verkuilb7b68392015-03-05 04:57:32 -03001337 strlcpy(cap->bus_info, cam->bus_info, sizeof(cap->bus_info));
Hans Verkuila020c742014-11-24 06:37:25 -03001338 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001339 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
Hans Verkuila020c742014-11-24 06:37:25 -03001340 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001341 return 0;
1342}
1343
1344
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001345static int mcam_vidioc_enum_fmt_vid_cap(struct file *filp,
1346 void *priv, struct v4l2_fmtdesc *fmt)
1347{
1348 if (fmt->index >= N_MCAM_FMTS)
1349 return -EINVAL;
1350 strlcpy(fmt->description, mcam_formats[fmt->index].desc,
1351 sizeof(fmt->description));
1352 fmt->pixelformat = mcam_formats[fmt->index].pixelformat;
1353 return 0;
1354}
1355
1356static int mcam_vidioc_try_fmt_vid_cap(struct file *filp, void *priv,
1357 struct v4l2_format *fmt)
1358{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001359 struct mcam_camera *cam = video_drvdata(filp);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001360 struct mcam_format_struct *f;
1361 struct v4l2_pix_format *pix = &fmt->fmt.pix;
Hans Verkuil5eab4982015-04-09 04:05:35 -03001362 struct v4l2_subdev_pad_config pad_cfg;
1363 struct v4l2_subdev_format format = {
1364 .which = V4L2_SUBDEV_FORMAT_TRY,
1365 };
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001366 int ret;
1367
1368 f = mcam_find_format(pix->pixelformat);
1369 pix->pixelformat = f->pixelformat;
Hans Verkuil5eab4982015-04-09 04:05:35 -03001370 v4l2_fill_mbus_format(&format.format, pix, f->mbus_code);
1371 ret = sensor_call(cam, pad, set_fmt, &pad_cfg, &format);
1372 v4l2_fill_pix_format(pix, &format.format);
Hans Verkuil47ba7db2015-03-09 17:14:36 -03001373 pix->bytesperline = pix->width * f->bpp;
Libin Yangad6ac452013-07-03 01:56:02 -03001374 switch (f->pixelformat) {
1375 case V4L2_PIX_FMT_YUV420:
1376 case V4L2_PIX_FMT_YVU420:
Hans Verkuil47ba7db2015-03-09 17:14:36 -03001377 pix->sizeimage = pix->height * pix->bytesperline * 3 / 2;
Libin Yangad6ac452013-07-03 01:56:02 -03001378 break;
1379 default:
Hans Verkuil47ba7db2015-03-09 17:14:36 -03001380 pix->sizeimage = pix->height * pix->bytesperline;
Libin Yangad6ac452013-07-03 01:56:02 -03001381 break;
1382 }
Hans Verkuil2e6e6092015-03-05 05:19:23 -03001383 pix->colorspace = V4L2_COLORSPACE_SRGB;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001384 return ret;
1385}
1386
1387static int mcam_vidioc_s_fmt_vid_cap(struct file *filp, void *priv,
1388 struct v4l2_format *fmt)
1389{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001390 struct mcam_camera *cam = video_drvdata(filp);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001391 struct mcam_format_struct *f;
1392 int ret;
1393
1394 /*
1395 * Can't do anything if the device is not idle
1396 * Also can't if there are streaming buffers in place.
1397 */
Hans Verkuile198d0ff2015-03-05 17:37:51 -03001398 if (cam->state != S_IDLE || vb2_is_busy(&cam->vb_queue))
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001399 return -EBUSY;
1400
1401 f = mcam_find_format(fmt->fmt.pix.pixelformat);
1402
1403 /*
1404 * See if the formatting works in principle.
1405 */
1406 ret = mcam_vidioc_try_fmt_vid_cap(filp, priv, fmt);
1407 if (ret)
1408 return ret;
1409 /*
1410 * Now we start to change things for real, so let's do it
1411 * under lock.
1412 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001413 cam->pix_format = fmt->fmt.pix;
1414 cam->mbus_code = f->mbus_code;
1415
1416 /*
1417 * Make sure we have appropriate DMA buffers.
1418 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001419 if (cam->buffer_mode == B_vmalloc) {
Jonathan Corbet74984692011-07-08 17:50:49 -03001420 ret = mcam_check_dma_buffers(cam);
1421 if (ret)
1422 goto out;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001423 }
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001424 mcam_set_config_needed(cam, 1);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001425out:
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001426 return ret;
1427}
1428
1429/*
1430 * Return our stored notion of how the camera is/should be configured.
1431 * The V4l2 spec wants us to be smarter, and actually get this from
1432 * the camera (and not mess with it at open time). Someday.
1433 */
1434static int mcam_vidioc_g_fmt_vid_cap(struct file *filp, void *priv,
1435 struct v4l2_format *f)
1436{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001437 struct mcam_camera *cam = video_drvdata(filp);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001438
1439 f->fmt.pix = cam->pix_format;
1440 return 0;
1441}
1442
1443/*
1444 * We only have one input - the sensor - so minimize the nonsense here.
1445 */
1446static int mcam_vidioc_enum_input(struct file *filp, void *priv,
1447 struct v4l2_input *input)
1448{
1449 if (input->index != 0)
1450 return -EINVAL;
1451
1452 input->type = V4L2_INPUT_TYPE_CAMERA;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001453 strcpy(input->name, "Camera");
1454 return 0;
1455}
1456
1457static int mcam_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
1458{
1459 *i = 0;
1460 return 0;
1461}
1462
1463static int mcam_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
1464{
1465 if (i != 0)
1466 return -EINVAL;
1467 return 0;
1468}
1469
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001470/*
1471 * G/S_PARM. Most of this is done by the sensor, but we are
1472 * the level which controls the number of read buffers.
1473 */
1474static int mcam_vidioc_g_parm(struct file *filp, void *priv,
1475 struct v4l2_streamparm *parms)
1476{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001477 struct mcam_camera *cam = video_drvdata(filp);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001478 int ret;
1479
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001480 ret = sensor_call(cam, video, g_parm, parms);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001481 parms->parm.capture.readbuffers = n_dma_bufs;
1482 return ret;
1483}
1484
1485static int mcam_vidioc_s_parm(struct file *filp, void *priv,
1486 struct v4l2_streamparm *parms)
1487{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001488 struct mcam_camera *cam = video_drvdata(filp);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001489 int ret;
1490
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001491 ret = sensor_call(cam, video, s_parm, parms);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001492 parms->parm.capture.readbuffers = n_dma_bufs;
1493 return ret;
1494}
1495
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001496static int mcam_vidioc_enum_framesizes(struct file *filp, void *priv,
1497 struct v4l2_frmsizeenum *sizes)
1498{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001499 struct mcam_camera *cam = video_drvdata(filp);
Hans Verkuil17bef882015-03-04 01:48:00 -08001500 struct mcam_format_struct *f;
1501 struct v4l2_subdev_frame_size_enum fse = {
1502 .index = sizes->index,
1503 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1504 };
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001505 int ret;
1506
Hans Verkuil17bef882015-03-04 01:48:00 -08001507 f = mcam_find_format(sizes->pixel_format);
1508 if (f->pixelformat != sizes->pixel_format)
1509 return -EINVAL;
1510 fse.code = f->mbus_code;
Hans Verkuil17bef882015-03-04 01:48:00 -08001511 ret = sensor_call(cam, pad, enum_frame_size, NULL, &fse);
Hans Verkuil17bef882015-03-04 01:48:00 -08001512 if (ret)
1513 return ret;
1514 if (fse.min_width == fse.max_width &&
1515 fse.min_height == fse.max_height) {
1516 sizes->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1517 sizes->discrete.width = fse.min_width;
1518 sizes->discrete.height = fse.min_height;
1519 return 0;
1520 }
1521 sizes->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
1522 sizes->stepwise.min_width = fse.min_width;
1523 sizes->stepwise.max_width = fse.max_width;
1524 sizes->stepwise.min_height = fse.min_height;
1525 sizes->stepwise.max_height = fse.max_height;
1526 sizes->stepwise.step_width = 1;
1527 sizes->stepwise.step_height = 1;
1528 return 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001529}
1530
1531static int mcam_vidioc_enum_frameintervals(struct file *filp, void *priv,
1532 struct v4l2_frmivalenum *interval)
1533{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001534 struct mcam_camera *cam = video_drvdata(filp);
Hans Verkuil17bef882015-03-04 01:48:00 -08001535 struct mcam_format_struct *f;
1536 struct v4l2_subdev_frame_interval_enum fie = {
1537 .index = interval->index,
1538 .width = interval->width,
1539 .height = interval->height,
1540 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1541 };
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001542 int ret;
1543
Hans Verkuil17bef882015-03-04 01:48:00 -08001544 f = mcam_find_format(interval->pixel_format);
1545 if (f->pixelformat != interval->pixel_format)
1546 return -EINVAL;
1547 fie.code = f->mbus_code;
Hans Verkuil17bef882015-03-04 01:48:00 -08001548 ret = sensor_call(cam, pad, enum_frame_interval, NULL, &fie);
Hans Verkuil17bef882015-03-04 01:48:00 -08001549 if (ret)
1550 return ret;
1551 interval->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1552 interval->discrete = fie.interval;
1553 return 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001554}
1555
1556#ifdef CONFIG_VIDEO_ADV_DEBUG
1557static int mcam_vidioc_g_register(struct file *file, void *priv,
1558 struct v4l2_dbg_register *reg)
1559{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001560 struct mcam_camera *cam = video_drvdata(file);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001561
Hans Verkuil4e032f32013-05-29 07:00:02 -03001562 if (reg->reg > cam->regs_size - 4)
1563 return -EINVAL;
Hans Verkuil7486af12013-05-29 06:59:44 -03001564 reg->val = mcam_reg_read(cam, reg->reg);
1565 reg->size = 4;
1566 return 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001567}
1568
1569static int mcam_vidioc_s_register(struct file *file, void *priv,
Hans Verkuil977ba3b2013-03-24 08:28:46 -03001570 const struct v4l2_dbg_register *reg)
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001571{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001572 struct mcam_camera *cam = video_drvdata(file);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001573
Hans Verkuil4e032f32013-05-29 07:00:02 -03001574 if (reg->reg > cam->regs_size - 4)
1575 return -EINVAL;
Hans Verkuil7486af12013-05-29 06:59:44 -03001576 mcam_reg_write(cam, reg->reg, reg->val);
1577 return 0;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001578}
1579#endif
1580
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001581static const struct v4l2_ioctl_ops mcam_v4l_ioctl_ops = {
1582 .vidioc_querycap = mcam_vidioc_querycap,
1583 .vidioc_enum_fmt_vid_cap = mcam_vidioc_enum_fmt_vid_cap,
1584 .vidioc_try_fmt_vid_cap = mcam_vidioc_try_fmt_vid_cap,
1585 .vidioc_s_fmt_vid_cap = mcam_vidioc_s_fmt_vid_cap,
1586 .vidioc_g_fmt_vid_cap = mcam_vidioc_g_fmt_vid_cap,
1587 .vidioc_enum_input = mcam_vidioc_enum_input,
1588 .vidioc_g_input = mcam_vidioc_g_input,
1589 .vidioc_s_input = mcam_vidioc_s_input,
Hans Verkuil949bd402015-03-05 17:33:17 -03001590 .vidioc_reqbufs = vb2_ioctl_reqbufs,
Hans Verkuile198d0ff2015-03-05 17:37:51 -03001591 .vidioc_create_bufs = vb2_ioctl_create_bufs,
Hans Verkuil949bd402015-03-05 17:33:17 -03001592 .vidioc_querybuf = vb2_ioctl_querybuf,
1593 .vidioc_qbuf = vb2_ioctl_qbuf,
1594 .vidioc_dqbuf = vb2_ioctl_dqbuf,
Hans Verkuilca16a642015-03-09 18:02:23 -03001595 .vidioc_expbuf = vb2_ioctl_expbuf,
Hans Verkuil949bd402015-03-05 17:33:17 -03001596 .vidioc_streamon = vb2_ioctl_streamon,
1597 .vidioc_streamoff = vb2_ioctl_streamoff,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001598 .vidioc_g_parm = mcam_vidioc_g_parm,
1599 .vidioc_s_parm = mcam_vidioc_s_parm,
1600 .vidioc_enum_framesizes = mcam_vidioc_enum_framesizes,
1601 .vidioc_enum_frameintervals = mcam_vidioc_enum_frameintervals,
Hans Verkuil87d18432015-03-05 13:05:24 -03001602 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1603 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001604#ifdef CONFIG_VIDEO_ADV_DEBUG
1605 .vidioc_g_register = mcam_vidioc_g_register,
1606 .vidioc_s_register = mcam_vidioc_s_register,
1607#endif
1608};
1609
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001610/* ---------------------------------------------------------------------- */
1611/*
1612 * Our various file operations.
1613 */
1614static int mcam_v4l_open(struct file *filp)
1615{
1616 struct mcam_camera *cam = video_drvdata(filp);
Hans Verkuil949bd402015-03-05 17:33:17 -03001617 int ret;
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001618
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001619 mutex_lock(&cam->s_mutex);
Hans Verkuil949bd402015-03-05 17:33:17 -03001620 ret = v4l2_fh_open(filp);
1621 if (ret)
1622 goto out;
1623 if (v4l2_fh_is_singular_file(filp)) {
Libin Yang05fed812013-07-03 01:55:58 -03001624 ret = mcam_ctlr_power_up(cam);
1625 if (ret)
1626 goto out;
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001627 __mcam_cam_reset(cam);
1628 mcam_set_config_needed(cam, 1);
1629 }
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001630out:
1631 mutex_unlock(&cam->s_mutex);
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001632 if (ret)
1633 v4l2_fh_release(filp);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001634 return ret;
1635}
1636
1637
1638static int mcam_v4l_release(struct file *filp)
1639{
Hans Verkuil44fbcb12015-03-05 13:03:18 -03001640 struct mcam_camera *cam = video_drvdata(filp);
Hans Verkuil949bd402015-03-05 17:33:17 -03001641 bool last_open;
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001642
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001643 mutex_lock(&cam->s_mutex);
Hans Verkuil949bd402015-03-05 17:33:17 -03001644 last_open = v4l2_fh_is_singular_file(filp);
1645 _vb2_fop_release(filp, NULL);
1646 if (last_open) {
Libin Yang05fed812013-07-03 01:55:58 -03001647 mcam_disable_mipi(cam);
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001648 mcam_ctlr_power_down(cam);
1649 if (cam->buffer_mode == B_vmalloc && alloc_bufs_at_read)
1650 mcam_free_dma_bufs(cam);
1651 }
Libin Yang05fed812013-07-03 01:55:58 -03001652
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001653 mutex_unlock(&cam->s_mutex);
1654 return 0;
1655}
1656
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001657static const struct v4l2_file_operations mcam_v4l_fops = {
1658 .owner = THIS_MODULE,
1659 .open = mcam_v4l_open,
1660 .release = mcam_v4l_release,
Hans Verkuil949bd402015-03-05 17:33:17 -03001661 .read = vb2_fop_read,
1662 .poll = vb2_fop_poll,
1663 .mmap = vb2_fop_mmap,
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001664 .unlocked_ioctl = video_ioctl2,
1665};
1666
1667
1668/*
1669 * This template device holds all of those v4l2 methods; we
1670 * clone it for specific real devices.
1671 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001672static struct video_device mcam_v4l_template = {
1673 .name = "mcam",
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001674 .fops = &mcam_v4l_fops,
1675 .ioctl_ops = &mcam_v4l_ioctl_ops,
1676 .release = video_device_release_empty,
1677};
1678
1679/* ---------------------------------------------------------------------- */
1680/*
1681 * Interrupt handler stuff
1682 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001683static void mcam_frame_complete(struct mcam_camera *cam, int frame)
1684{
1685 /*
1686 * Basic frame housekeeping.
1687 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001688 set_bit(frame, &cam->flags);
1689 clear_bit(CF_DMA_ACTIVE, &cam->flags);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001690 cam->next_buf = frame;
Hans Verkuil53423422015-03-05 17:48:39 -03001691 cam->buf_seq[frame] = cam->sequence++;
Libin Yangf698957a2012-12-15 05:57:50 -03001692 cam->frame_state.frames++;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001693 /*
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001694 * "This should never happen"
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001695 */
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001696 if (cam->state != S_STREAMING)
1697 return;
1698 /*
1699 * Process the frame and set up the next one.
1700 */
Jonathan Corbet74984692011-07-08 17:50:49 -03001701 cam->frame_complete(cam, frame);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001702}
1703
1704
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001705/*
1706 * The interrupt handler; this needs to be called from the
1707 * platform irq handler with the lock held.
1708 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001709int mccic_irq(struct mcam_camera *cam, unsigned int irqs)
1710{
1711 unsigned int frame, handled = 0;
1712
1713 mcam_reg_write(cam, REG_IRQSTAT, FRAMEIRQS); /* Clear'em all */
1714 /*
1715 * Handle any frame completions. There really should
1716 * not be more than one of these, or we have fallen
1717 * far behind.
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001718 *
1719 * When running in S/G mode, the frame number lacks any
1720 * real meaning - there's only one descriptor array - but
1721 * the controller still picks a different one to signal
1722 * each time.
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001723 */
1724 for (frame = 0; frame < cam->nbufs; frame++)
Libin Yang0a0b3fb2013-07-03 01:56:03 -03001725 if (irqs & (IRQ_EOF0 << frame) &&
1726 test_bit(CF_FRAME_SOF0 + frame, &cam->flags)) {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001727 mcam_frame_complete(cam, frame);
1728 handled = 1;
Libin Yang0a0b3fb2013-07-03 01:56:03 -03001729 clear_bit(CF_FRAME_SOF0 + frame, &cam->flags);
Jonathan Corbetf2354dd2012-03-16 19:14:54 -03001730 if (cam->buffer_mode == B_DMA_sg)
1731 break;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001732 }
1733 /*
1734 * If a frame starts, note that we have DMA active. This
1735 * code assumes that we won't get multiple frame interrupts
1736 * at once; may want to rethink that.
1737 */
Libin Yang0a0b3fb2013-07-03 01:56:03 -03001738 for (frame = 0; frame < cam->nbufs; frame++) {
1739 if (irqs & (IRQ_SOF0 << frame)) {
1740 set_bit(CF_FRAME_SOF0 + frame, &cam->flags);
1741 handled = IRQ_HANDLED;
1742 }
1743 }
1744
1745 if (handled == IRQ_HANDLED) {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001746 set_bit(CF_DMA_ACTIVE, &cam->flags);
Jonathan Corbetcbc4f3a2011-06-30 17:05:27 -03001747 if (cam->buffer_mode == B_DMA_sg)
1748 mcam_ctlr_stop(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001749 }
1750 return handled;
1751}
1752
Jonathan Corbetd43dae72011-07-08 17:50:46 -03001753/* ---------------------------------------------------------------------- */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001754/*
1755 * Registration and such.
1756 */
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001757static struct ov7670_config sensor_cfg = {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001758 /*
1759 * Exclude QCIF mode, because it only captures a tiny portion
1760 * of the sensor FOV
1761 */
1762 .min_width = 320,
1763 .min_height = 240,
1764};
1765
1766
1767int mccic_register(struct mcam_camera *cam)
1768{
1769 struct i2c_board_info ov7670_info = {
1770 .type = "ov7670",
Jonathan Corbet1c68f882011-06-11 14:46:47 -03001771 .addr = 0x42 >> 1,
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001772 .platform_data = &sensor_cfg,
1773 };
1774 int ret;
1775
1776 /*
Jonathan Corbet74984692011-07-08 17:50:49 -03001777 * Validate the requested buffer mode.
1778 */
1779 if (buffer_mode >= 0)
1780 cam->buffer_mode = buffer_mode;
1781 if (cam->buffer_mode == B_DMA_sg &&
Hans Verkuil7486af12013-05-29 06:59:44 -03001782 cam->chip_id == MCAM_CAFE) {
Jonathan Corbet74984692011-07-08 17:50:49 -03001783 printk(KERN_ERR "marvell-cam: Cafe can't do S/G I/O, "
1784 "attempting vmalloc mode instead\n");
1785 cam->buffer_mode = B_vmalloc;
1786 }
1787 if (!mcam_buffer_mode_supported(cam->buffer_mode)) {
1788 printk(KERN_ERR "marvell-cam: buffer mode %d unsupported\n",
1789 cam->buffer_mode);
1790 return -EINVAL;
1791 }
1792 /*
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001793 * Register with V4L
1794 */
1795 ret = v4l2_device_register(cam->dev, &cam->v4l2_dev);
1796 if (ret)
1797 return ret;
1798
1799 mutex_init(&cam->s_mutex);
1800 cam->state = S_NOTREADY;
1801 mcam_set_config_needed(cam, 1);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001802 cam->pix_format = mcam_def_pix_format;
1803 cam->mbus_code = mcam_def_mbus_code;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001804 mcam_ctlr_init(cam);
1805
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001806 /*
Hans Verkuil1e4cbe62015-03-05 12:00:11 -03001807 * Get the v4l2 setup done.
1808 */
1809 ret = v4l2_ctrl_handler_init(&cam->ctrl_handler, 10);
1810 if (ret)
1811 goto out_unregister;
1812 cam->v4l2_dev.ctrl_handler = &cam->ctrl_handler;
1813
1814 /*
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001815 * Try to find the sensor.
1816 */
Jonathan Corbet2164b5a2011-06-11 14:46:44 -03001817 sensor_cfg.clock_speed = cam->clock_speed;
1818 sensor_cfg.use_smbus = cam->use_smbus;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001819 cam->sensor_addr = ov7670_info.addr;
1820 cam->sensor = v4l2_i2c_new_subdev_board(&cam->v4l2_dev,
Jonathan Corbet595a93a2011-06-11 14:46:48 -03001821 cam->i2c_adapter, &ov7670_info, NULL);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001822 if (cam->sensor == NULL) {
1823 ret = -ENODEV;
1824 goto out_unregister;
1825 }
1826
1827 ret = mcam_cam_init(cam);
1828 if (ret)
1829 goto out_unregister;
Javier Martin593403c2013-01-29 07:58:48 -03001830
Hans Verkuil949bd402015-03-05 17:33:17 -03001831 ret = mcam_setup_vb2(cam);
1832 if (ret)
1833 goto out_unregister;
1834
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001835 mutex_lock(&cam->s_mutex);
1836 cam->vdev = mcam_v4l_template;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001837 cam->vdev.v4l2_dev = &cam->v4l2_dev;
Hans Verkuil949bd402015-03-05 17:33:17 -03001838 cam->vdev.lock = &cam->s_mutex;
1839 cam->vdev.queue = &cam->vb_queue;
Javier Martin593403c2013-01-29 07:58:48 -03001840 video_set_drvdata(&cam->vdev, cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001841 ret = video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1);
Hans Verkuil949bd402015-03-05 17:33:17 -03001842 if (ret) {
1843 mutex_unlock(&cam->s_mutex);
1844 goto out_unregister;
1845 }
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001846
1847 /*
1848 * If so requested, try to get our DMA buffers now.
1849 */
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001850 if (cam->buffer_mode == B_vmalloc && !alloc_bufs_at_read) {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001851 if (mcam_alloc_dma_bufs(cam, 1))
1852 cam_warn(cam, "Unable to alloc DMA buffers at load"
1853 " will try again later.");
1854 }
1855
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001856 mutex_unlock(&cam->s_mutex);
Hans Verkuil949bd402015-03-05 17:33:17 -03001857 return 0;
1858
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001859out_unregister:
Hans Verkuil1e4cbe62015-03-05 12:00:11 -03001860 v4l2_ctrl_handler_free(&cam->ctrl_handler);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001861 v4l2_device_unregister(&cam->v4l2_dev);
1862 return ret;
1863}
1864
1865
1866void mccic_shutdown(struct mcam_camera *cam)
1867{
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -03001868 /*
1869 * If we have no users (and we really, really should have no
1870 * users) the device will already be powered down. Trying to
1871 * take it down again will wedge the machine, which is frowned
1872 * upon.
1873 */
Hans Verkuil949bd402015-03-05 17:33:17 -03001874 if (!list_empty(&cam->vdev.fh_list)) {
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001875 cam_warn(cam, "Removing a device with users!\n");
Jonathan Corbet67a8dbb2011-06-11 14:46:49 -03001876 mcam_ctlr_power_down(cam);
1877 }
Hans Verkuil949bd402015-03-05 17:33:17 -03001878 mcam_cleanup_vb2(cam);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001879 if (cam->buffer_mode == B_vmalloc)
1880 mcam_free_dma_bufs(cam);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001881 video_unregister_device(&cam->vdev);
Javier Martin593403c2013-01-29 07:58:48 -03001882 v4l2_ctrl_handler_free(&cam->ctrl_handler);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001883 v4l2_device_unregister(&cam->v4l2_dev);
1884}
1885
1886/*
1887 * Power management
1888 */
1889#ifdef CONFIG_PM
1890
1891void mccic_suspend(struct mcam_camera *cam)
1892{
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001893 mutex_lock(&cam->s_mutex);
Hans Verkuil949bd402015-03-05 17:33:17 -03001894 if (!list_empty(&cam->vdev.fh_list)) {
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001895 enum mcam_state cstate = cam->state;
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001896
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001897 mcam_ctlr_stop_dma(cam);
1898 mcam_ctlr_power_down(cam);
1899 cam->state = cstate;
1900 }
1901 mutex_unlock(&cam->s_mutex);
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001902}
1903
1904int mccic_resume(struct mcam_camera *cam)
1905{
1906 int ret = 0;
1907
1908 mutex_lock(&cam->s_mutex);
Hans Verkuil949bd402015-03-05 17:33:17 -03001909 if (!list_empty(&cam->vdev.fh_list)) {
Libin Yang05fed812013-07-03 01:55:58 -03001910 ret = mcam_ctlr_power_up(cam);
1911 if (ret) {
1912 mutex_unlock(&cam->s_mutex);
1913 return ret;
1914 }
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001915 __mcam_cam_reset(cam);
1916 } else {
1917 mcam_ctlr_power_down(cam);
1918 }
1919 mutex_unlock(&cam->s_mutex);
1920
1921 set_bit(CF_CONFIG_NEEDED, &cam->flags);
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001922 if (cam->state == S_STREAMING) {
1923 /*
1924 * If there was a buffer in the DMA engine at suspend
1925 * time, put it back on the queue or we'll forget about it.
1926 */
1927 if (cam->buffer_mode == B_DMA_sg && cam->vb_bufs[0])
1928 list_add(&cam->vb_bufs[0]->queue, &cam->buffers);
Jonathan Corbeta9b36e82011-06-20 16:14:40 -03001929 ret = mcam_read_setup(cam);
Jonathan Corbetbb0a8962011-12-30 14:13:41 -03001930 }
Jonathan Corbetabfa3df2011-06-11 14:46:43 -03001931 return ret;
1932}
1933#endif /* CONFIG_PM */