blob: c8f394e1aff46ec2aef95a15adecfb9164951f7d [file] [log] [blame]
Kamil Debski91884732011-10-06 11:32:12 -03001/*
2 * Samsung S5P G2D - 2D Graphics Accelerator Driver
3 *
4 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * Kamil Debski, <k.debski@samsung.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version
11 */
12
13#include <linux/module.h>
14#include <linux/fs.h>
Kamil Debski91884732011-10-06 11:32:12 -030015#include <linux/timer.h>
16#include <linux/sched.h>
17#include <linux/slab.h>
18#include <linux/clk.h>
19#include <linux/interrupt.h>
Sachin Kamat5ce60d72013-02-06 01:29:43 -030020#include <linux/of.h>
Kamil Debski91884732011-10-06 11:32:12 -030021
22#include <linux/platform_device.h>
23#include <media/v4l2-mem2mem.h>
24#include <media/v4l2-device.h>
25#include <media/v4l2-ioctl.h>
Junghak Sungc1399902015-09-22 10:30:29 -030026#include <media/videobuf2-v4l2.h>
Kamil Debski91884732011-10-06 11:32:12 -030027#include <media/videobuf2-dma-contig.h>
28
29#include "g2d.h"
30#include "g2d-regs.h"
31
32#define fh2ctx(__fh) container_of(__fh, struct g2d_ctx, fh)
33
34static struct g2d_fmt formats[] = {
35 {
36 .name = "XRGB_8888",
37 .fourcc = V4L2_PIX_FMT_RGB32,
38 .depth = 32,
39 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
40 },
41 {
42 .name = "RGB_565",
43 .fourcc = V4L2_PIX_FMT_RGB565X,
44 .depth = 16,
45 .hw = COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
46 },
47 {
48 .name = "XRGB_1555",
49 .fourcc = V4L2_PIX_FMT_RGB555X,
50 .depth = 16,
51 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
52 },
53 {
54 .name = "XRGB_4444",
55 .fourcc = V4L2_PIX_FMT_RGB444,
56 .depth = 16,
57 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
58 },
59 {
60 .name = "PACKED_RGB_888",
61 .fourcc = V4L2_PIX_FMT_RGB24,
62 .depth = 24,
63 .hw = COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
64 },
65};
66#define NUM_FORMATS ARRAY_SIZE(formats)
67
Sachin Kamatec76afe2012-05-10 03:35:48 -030068static struct g2d_frame def_frame = {
Kamil Debski91884732011-10-06 11:32:12 -030069 .width = DEFAULT_WIDTH,
70 .height = DEFAULT_HEIGHT,
71 .c_width = DEFAULT_WIDTH,
72 .c_height = DEFAULT_HEIGHT,
73 .o_width = 0,
74 .o_height = 0,
75 .fmt = &formats[0],
76 .right = DEFAULT_WIDTH,
77 .bottom = DEFAULT_HEIGHT,
78};
79
Sachin Kamatec76afe2012-05-10 03:35:48 -030080static struct g2d_fmt *find_fmt(struct v4l2_format *f)
Kamil Debski91884732011-10-06 11:32:12 -030081{
82 unsigned int i;
83 for (i = 0; i < NUM_FORMATS; i++) {
84 if (formats[i].fourcc == f->fmt.pix.pixelformat)
85 return &formats[i];
86 }
87 return NULL;
88}
89
90
91static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
Hans Verkuilf72b9d82018-10-04 16:12:06 -040092 enum v4l2_buf_type type)
Kamil Debski91884732011-10-06 11:32:12 -030093{
94 switch (type) {
95 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
96 return &ctx->in;
97 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
98 return &ctx->out;
99 default:
100 return ERR_PTR(-EINVAL);
101 }
102}
103
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -0200104static int g2d_queue_setup(struct vb2_queue *vq,
Kamil Debski91884732011-10-06 11:32:12 -0300105 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -0300106 unsigned int sizes[], struct device *alloc_devs[])
Kamil Debski91884732011-10-06 11:32:12 -0300107{
108 struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
109 struct g2d_frame *f = get_frame(ctx, vq->type);
110
111 if (IS_ERR(f))
112 return PTR_ERR(f);
113
114 sizes[0] = f->size;
115 *nplanes = 1;
Kamil Debski91884732011-10-06 11:32:12 -0300116
117 if (*nbuffers == 0)
118 *nbuffers = 1;
119
120 return 0;
121}
122
123static int g2d_buf_prepare(struct vb2_buffer *vb)
124{
125 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
126 struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
127
128 if (IS_ERR(f))
129 return PTR_ERR(f);
130 vb2_set_plane_payload(vb, 0, f->size);
131 return 0;
132}
133
134static void g2d_buf_queue(struct vb2_buffer *vb)
135{
Junghak Sung2d700712015-09-22 10:30:30 -0300136 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Kamil Debski91884732011-10-06 11:32:12 -0300137 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300138 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Kamil Debski91884732011-10-06 11:32:12 -0300139}
140
Julia Lawallb7b361f2016-09-08 20:59:10 -0300141static const struct vb2_ops g2d_qops = {
Kamil Debski91884732011-10-06 11:32:12 -0300142 .queue_setup = g2d_queue_setup,
143 .buf_prepare = g2d_buf_prepare,
144 .buf_queue = g2d_buf_queue,
Ezequiel Garciab53e19e2018-06-15 15:07:26 -0400145 .wait_prepare = vb2_ops_wait_prepare,
146 .wait_finish = vb2_ops_wait_finish,
Kamil Debski91884732011-10-06 11:32:12 -0300147};
148
149static int queue_init(void *priv, struct vb2_queue *src_vq,
150 struct vb2_queue *dst_vq)
151{
152 struct g2d_ctx *ctx = priv;
153 int ret;
154
Kamil Debski91884732011-10-06 11:32:12 -0300155 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
156 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
157 src_vq->drv_priv = ctx;
158 src_vq->ops = &g2d_qops;
159 src_vq->mem_ops = &vb2_dma_contig_memops;
160 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
Sakari Ailusade48682014-02-25 19:12:19 -0300161 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300162 src_vq->lock = &ctx->dev->mutex;
Hans Verkuilc781e4a2016-02-15 14:25:09 -0200163 src_vq->dev = ctx->dev->v4l2_dev.dev;
Kamil Debski91884732011-10-06 11:32:12 -0300164
165 ret = vb2_queue_init(src_vq);
166 if (ret)
167 return ret;
168
Kamil Debski91884732011-10-06 11:32:12 -0300169 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
170 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
171 dst_vq->drv_priv = ctx;
172 dst_vq->ops = &g2d_qops;
173 dst_vq->mem_ops = &vb2_dma_contig_memops;
174 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
Sakari Ailusade48682014-02-25 19:12:19 -0300175 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300176 dst_vq->lock = &ctx->dev->mutex;
Hans Verkuilc781e4a2016-02-15 14:25:09 -0200177 dst_vq->dev = ctx->dev->v4l2_dev.dev;
Kamil Debski91884732011-10-06 11:32:12 -0300178
179 return vb2_queue_init(dst_vq);
180}
181
182static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
183{
184 struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
185 ctrl_handler);
Kamil Debski27dda972012-02-16 10:52:47 -0300186 unsigned long flags;
187
188 spin_lock_irqsave(&ctx->dev->ctrl_lock, flags);
Kamil Debski91884732011-10-06 11:32:12 -0300189 switch (ctrl->id) {
190 case V4L2_CID_COLORFX:
191 if (ctrl->val == V4L2_COLORFX_NEGATIVE)
192 ctx->rop = ROP4_INVERT;
193 else
194 ctx->rop = ROP4_COPY;
Kamil Debski7f6cce62012-01-02 09:19:25 -0300195 break;
Sachin Kamatd0d28322012-02-16 10:52:16 -0300196
197 case V4L2_CID_HFLIP:
198 ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
199 break;
200
Kamil Debski91884732011-10-06 11:32:12 -0300201 }
Kamil Debski27dda972012-02-16 10:52:47 -0300202 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
Kamil Debski91884732011-10-06 11:32:12 -0300203 return 0;
204}
205
206static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
207 .s_ctrl = g2d_s_ctrl,
208};
209
Sachin Kamatec76afe2012-05-10 03:35:48 -0300210static int g2d_setup_ctrls(struct g2d_ctx *ctx)
Kamil Debski91884732011-10-06 11:32:12 -0300211{
212 struct g2d_dev *dev = ctx->dev;
213
Sachin Kamatd0d28322012-02-16 10:52:16 -0300214 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
215
216 ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
217 V4L2_CID_HFLIP, 0, 1, 1, 0);
218
219 ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
220 V4L2_CID_VFLIP, 0, 1, 1, 0);
Kamil Debski91884732011-10-06 11:32:12 -0300221
222 v4l2_ctrl_new_std_menu(
223 &ctx->ctrl_handler,
224 &g2d_ctrl_ops,
225 V4L2_CID_COLORFX,
226 V4L2_COLORFX_NEGATIVE,
227 ~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
228 V4L2_COLORFX_NONE);
229
230 if (ctx->ctrl_handler.error) {
Sachin Kamatd0d28322012-02-16 10:52:16 -0300231 int err = ctx->ctrl_handler.error;
232 v4l2_err(&dev->v4l2_dev, "g2d_setup_ctrls failed\n");
233 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
234 return err;
Kamil Debski91884732011-10-06 11:32:12 -0300235 }
236
Sachin Kamatd0d28322012-02-16 10:52:16 -0300237 v4l2_ctrl_cluster(2, &ctx->ctrl_hflip);
238
Kamil Debski91884732011-10-06 11:32:12 -0300239 return 0;
240}
241
242static int g2d_open(struct file *file)
243{
244 struct g2d_dev *dev = video_drvdata(file);
245 struct g2d_ctx *ctx = NULL;
246 int ret = 0;
247
248 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
249 if (!ctx)
250 return -ENOMEM;
251 ctx->dev = dev;
252 /* Set default formats */
253 ctx->in = def_frame;
254 ctx->out = def_frame;
255
Hans Verkuilde40cb22012-06-24 06:58:28 -0300256 if (mutex_lock_interruptible(&dev->mutex)) {
257 kfree(ctx);
258 return -ERESTARTSYS;
259 }
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300260 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
261 if (IS_ERR(ctx->fh.m2m_ctx)) {
262 ret = PTR_ERR(ctx->fh.m2m_ctx);
Hans Verkuilde40cb22012-06-24 06:58:28 -0300263 mutex_unlock(&dev->mutex);
Kamil Debski91884732011-10-06 11:32:12 -0300264 kfree(ctx);
265 return ret;
266 }
267 v4l2_fh_init(&ctx->fh, video_devdata(file));
268 file->private_data = &ctx->fh;
269 v4l2_fh_add(&ctx->fh);
270
271 g2d_setup_ctrls(ctx);
272
273 /* Write the default values to the ctx struct */
274 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
275
276 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
Hans Verkuilde40cb22012-06-24 06:58:28 -0300277 mutex_unlock(&dev->mutex);
Kamil Debski91884732011-10-06 11:32:12 -0300278
279 v4l2_info(&dev->v4l2_dev, "instance opened\n");
280 return 0;
281}
282
283static int g2d_release(struct file *file)
284{
285 struct g2d_dev *dev = video_drvdata(file);
286 struct g2d_ctx *ctx = fh2ctx(file->private_data);
287
288 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
289 v4l2_fh_del(&ctx->fh);
290 v4l2_fh_exit(&ctx->fh);
291 kfree(ctx);
292 v4l2_info(&dev->v4l2_dev, "instance closed\n");
293 return 0;
294}
295
296
297static int vidioc_querycap(struct file *file, void *priv,
298 struct v4l2_capability *cap)
299{
Mauro Carvalho Chehab85709cb2018-09-10 08:19:16 -0400300 strscpy(cap->driver, G2D_NAME, sizeof(cap->driver));
301 strscpy(cap->card, G2D_NAME, sizeof(cap->card));
Kamil Debski91884732011-10-06 11:32:12 -0300302 cap->bus_info[0] = 0;
Hans Verkuil8c17e5e2014-11-24 06:37:26 -0300303 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
304 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Kamil Debski91884732011-10-06 11:32:12 -0300305 return 0;
306}
307
308static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
309{
310 struct g2d_fmt *fmt;
311 if (f->index >= NUM_FORMATS)
312 return -EINVAL;
313 fmt = &formats[f->index];
314 f->pixelformat = fmt->fourcc;
Mauro Carvalho Chehab85709cb2018-09-10 08:19:16 -0400315 strscpy(f->description, fmt->name, sizeof(f->description));
Kamil Debski91884732011-10-06 11:32:12 -0300316 return 0;
317}
318
319static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
320{
321 struct g2d_ctx *ctx = prv;
322 struct vb2_queue *vq;
323 struct g2d_frame *frm;
324
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300325 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Kamil Debski91884732011-10-06 11:32:12 -0300326 if (!vq)
327 return -EINVAL;
328 frm = get_frame(ctx, f->type);
329 if (IS_ERR(frm))
330 return PTR_ERR(frm);
331
332 f->fmt.pix.width = frm->width;
333 f->fmt.pix.height = frm->height;
334 f->fmt.pix.field = V4L2_FIELD_NONE;
335 f->fmt.pix.pixelformat = frm->fmt->fourcc;
336 f->fmt.pix.bytesperline = (frm->width * frm->fmt->depth) >> 3;
337 f->fmt.pix.sizeimage = frm->size;
338 return 0;
339}
340
341static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
342{
343 struct g2d_fmt *fmt;
344 enum v4l2_field *field;
345
346 fmt = find_fmt(f);
347 if (!fmt)
348 return -EINVAL;
349
350 field = &f->fmt.pix.field;
351 if (*field == V4L2_FIELD_ANY)
352 *field = V4L2_FIELD_NONE;
353 else if (*field != V4L2_FIELD_NONE)
354 return -EINVAL;
355
356 if (f->fmt.pix.width > MAX_WIDTH)
357 f->fmt.pix.width = MAX_WIDTH;
358 if (f->fmt.pix.height > MAX_HEIGHT)
359 f->fmt.pix.height = MAX_HEIGHT;
360
361 if (f->fmt.pix.width < 1)
362 f->fmt.pix.width = 1;
363 if (f->fmt.pix.height < 1)
364 f->fmt.pix.height = 1;
365
366 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
367 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
368 return 0;
369}
370
371static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
372{
373 struct g2d_ctx *ctx = prv;
374 struct g2d_dev *dev = ctx->dev;
375 struct vb2_queue *vq;
376 struct g2d_frame *frm;
377 struct g2d_fmt *fmt;
378 int ret = 0;
379
380 /* Adjust all values accordingly to the hardware capabilities
381 * and chosen format. */
382 ret = vidioc_try_fmt(file, prv, f);
383 if (ret)
384 return ret;
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300385 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Kamil Debski91884732011-10-06 11:32:12 -0300386 if (vb2_is_busy(vq)) {
387 v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
388 return -EBUSY;
389 }
390 frm = get_frame(ctx, f->type);
391 if (IS_ERR(frm))
392 return PTR_ERR(frm);
393 fmt = find_fmt(f);
394 if (!fmt)
395 return -EINVAL;
396 frm->width = f->fmt.pix.width;
397 frm->height = f->fmt.pix.height;
398 frm->size = f->fmt.pix.sizeimage;
399 /* Reset crop settings */
400 frm->o_width = 0;
401 frm->o_height = 0;
402 frm->c_width = frm->width;
403 frm->c_height = frm->height;
404 frm->right = frm->width;
405 frm->bottom = frm->height;
406 frm->fmt = fmt;
407 frm->stride = f->fmt.pix.bytesperline;
408 return 0;
409}
410
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400411static int vidioc_g_selection(struct file *file, void *prv,
412 struct v4l2_selection *s)
Kamil Debski91884732011-10-06 11:32:12 -0300413{
414 struct g2d_ctx *ctx = prv;
415 struct g2d_frame *f;
416
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400417 f = get_frame(ctx, s->type);
Kamil Debski91884732011-10-06 11:32:12 -0300418 if (IS_ERR(f))
419 return PTR_ERR(f);
420
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400421 switch (s->target) {
422 case V4L2_SEL_TGT_CROP:
423 case V4L2_SEL_TGT_CROP_DEFAULT:
424 case V4L2_SEL_TGT_CROP_BOUNDS:
425 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
426 return -EINVAL;
427 break;
428 case V4L2_SEL_TGT_COMPOSE:
429 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
430 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
431 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
432 return -EINVAL;
433 break;
434 default:
435 return -EINVAL;
436 }
437
438 switch (s->target) {
439 case V4L2_SEL_TGT_CROP:
440 case V4L2_SEL_TGT_COMPOSE:
441 s->r.left = f->o_height;
442 s->r.top = f->o_width;
443 s->r.width = f->c_width;
444 s->r.height = f->c_height;
445 break;
446 case V4L2_SEL_TGT_CROP_DEFAULT:
447 case V4L2_SEL_TGT_CROP_BOUNDS:
448 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
449 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
450 s->r.left = 0;
451 s->r.top = 0;
452 s->r.width = f->width;
453 s->r.height = f->height;
454 break;
455 default:
456 return -EINVAL;
457 }
Kamil Debski91884732011-10-06 11:32:12 -0300458 return 0;
459}
460
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400461static int vidioc_try_selection(struct file *file, void *prv,
462 const struct v4l2_selection *s)
Kamil Debski91884732011-10-06 11:32:12 -0300463{
464 struct g2d_ctx *ctx = prv;
465 struct g2d_dev *dev = ctx->dev;
466 struct g2d_frame *f;
467
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400468 f = get_frame(ctx, s->type);
Kamil Debski91884732011-10-06 11:32:12 -0300469 if (IS_ERR(f))
470 return PTR_ERR(f);
471
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400472 if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
473 if (s->target != V4L2_SEL_TGT_COMPOSE)
474 return -EINVAL;
475 } else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
476 if (s->target != V4L2_SEL_TGT_CROP)
477 return -EINVAL;
478 }
479
480 if (s->r.top < 0 || s->r.left < 0) {
Kamil Debski91884732011-10-06 11:32:12 -0300481 v4l2_err(&dev->v4l2_dev,
482 "doesn't support negative values for top & left\n");
483 return -EINVAL;
484 }
485
486 return 0;
487}
488
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400489static int vidioc_s_selection(struct file *file, void *prv,
490 struct v4l2_selection *s)
Kamil Debski91884732011-10-06 11:32:12 -0300491{
492 struct g2d_ctx *ctx = prv;
493 struct g2d_frame *f;
494 int ret;
495
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400496 ret = vidioc_try_selection(file, prv, s);
Kamil Debski91884732011-10-06 11:32:12 -0300497 if (ret)
498 return ret;
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400499 f = get_frame(ctx, s->type);
Kamil Debski91884732011-10-06 11:32:12 -0300500 if (IS_ERR(f))
501 return PTR_ERR(f);
502
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400503 f->c_width = s->r.width;
504 f->c_height = s->r.height;
505 f->o_width = s->r.left;
506 f->o_height = s->r.top;
Kamil Debski91884732011-10-06 11:32:12 -0300507 f->bottom = f->o_height + f->c_height;
508 f->right = f->o_width + f->c_width;
509 return 0;
510}
511
Kamil Debski91884732011-10-06 11:32:12 -0300512static void device_run(void *prv)
513{
514 struct g2d_ctx *ctx = prv;
515 struct g2d_dev *dev = ctx->dev;
Ezequiel Garcia30fa6272019-02-08 11:17:44 -0500516 struct vb2_v4l2_buffer *src, *dst;
Kamil Debski27dda972012-02-16 10:52:47 -0300517 unsigned long flags;
Kamil Debski91884732011-10-06 11:32:12 -0300518 u32 cmd = 0;
519
520 dev->curr = ctx;
521
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300522 src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
523 dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Kamil Debski91884732011-10-06 11:32:12 -0300524
525 clk_enable(dev->gate);
526 g2d_reset(dev);
527
Kamil Debski27dda972012-02-16 10:52:47 -0300528 spin_lock_irqsave(&dev->ctrl_lock, flags);
529
Kamil Debski91884732011-10-06 11:32:12 -0300530 g2d_set_src_size(dev, &ctx->in);
Ezequiel Garcia30fa6272019-02-08 11:17:44 -0500531 g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0));
Kamil Debski91884732011-10-06 11:32:12 -0300532
533 g2d_set_dst_size(dev, &ctx->out);
Ezequiel Garcia30fa6272019-02-08 11:17:44 -0500534 g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0));
Kamil Debski91884732011-10-06 11:32:12 -0300535
536 g2d_set_rop4(dev, ctx->rop);
Sachin Kamatd0d28322012-02-16 10:52:16 -0300537 g2d_set_flip(dev, ctx->flip);
538
Kamil Debski91884732011-10-06 11:32:12 -0300539 if (ctx->in.c_width != ctx->out.c_width ||
Sachin Kamat62ce2722013-01-17 00:07:18 -0300540 ctx->in.c_height != ctx->out.c_height) {
541 if (dev->variant->hw_rev == TYPE_G2D_3X)
542 cmd |= CMD_V3_ENABLE_STRETCH;
543 else
544 g2d_set_v41_stretch(dev, &ctx->in, &ctx->out);
545 }
546
Kamil Debski91884732011-10-06 11:32:12 -0300547 g2d_set_cmd(dev, cmd);
548 g2d_start(dev);
Kamil Debski27dda972012-02-16 10:52:47 -0300549
550 spin_unlock_irqrestore(&dev->ctrl_lock, flags);
Kamil Debski91884732011-10-06 11:32:12 -0300551}
552
553static irqreturn_t g2d_isr(int irq, void *prv)
554{
555 struct g2d_dev *dev = prv;
556 struct g2d_ctx *ctx = dev->curr;
Junghak Sung2d700712015-09-22 10:30:30 -0300557 struct vb2_v4l2_buffer *src, *dst;
Kamil Debski91884732011-10-06 11:32:12 -0300558
559 g2d_clear_int(dev);
560 clk_disable(dev->gate);
561
Sachin Kamat23568772012-05-10 03:35:47 -0300562 BUG_ON(ctx == NULL);
Kamil Debski91884732011-10-06 11:32:12 -0300563
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300564 src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
565 dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Kamil Debski91884732011-10-06 11:32:12 -0300566
Sachin Kamat23568772012-05-10 03:35:47 -0300567 BUG_ON(src == NULL);
568 BUG_ON(dst == NULL);
Kamil Debski91884732011-10-06 11:32:12 -0300569
Junghak Sung2d700712015-09-22 10:30:30 -0300570 dst->timecode = src->timecode;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200571 dst->vb2_buf.timestamp = src->vb2_buf.timestamp;
Junghak Sung2d700712015-09-22 10:30:30 -0300572 dst->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
573 dst->flags |=
574 src->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskidc7be2952013-04-24 09:34:03 -0300575
Kamil Debski91884732011-10-06 11:32:12 -0300576 v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
577 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300578 v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
Kamil Debski91884732011-10-06 11:32:12 -0300579
Sachin Kamat23568772012-05-10 03:35:47 -0300580 dev->curr = NULL;
Kamil Debski91884732011-10-06 11:32:12 -0300581 return IRQ_HANDLED;
582}
583
584static const struct v4l2_file_operations g2d_fops = {
585 .owner = THIS_MODULE,
586 .open = g2d_open,
587 .release = g2d_release,
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300588 .poll = v4l2_m2m_fop_poll,
Kamil Debski91884732011-10-06 11:32:12 -0300589 .unlocked_ioctl = video_ioctl2,
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300590 .mmap = v4l2_m2m_fop_mmap,
Kamil Debski91884732011-10-06 11:32:12 -0300591};
592
593static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
594 .vidioc_querycap = vidioc_querycap,
595
596 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
597 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
598 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
599 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
600
601 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
602 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
603 .vidioc_try_fmt_vid_out = vidioc_try_fmt,
604 .vidioc_s_fmt_vid_out = vidioc_s_fmt,
605
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300606 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
607 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
608 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
609 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Kamil Debski91884732011-10-06 11:32:12 -0300610
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300611 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
612 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Kamil Debski91884732011-10-06 11:32:12 -0300613
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400614 .vidioc_g_selection = vidioc_g_selection,
615 .vidioc_s_selection = vidioc_s_selection,
Kamil Debski91884732011-10-06 11:32:12 -0300616};
617
Bhumika Goyal53031352017-08-26 08:57:26 -0400618static const struct video_device g2d_videodev = {
Kamil Debski91884732011-10-06 11:32:12 -0300619 .name = G2D_NAME,
620 .fops = &g2d_fops,
621 .ioctl_ops = &g2d_ioctl_ops,
622 .minor = -1,
623 .release = video_device_release,
Hans Verkuil954f3402012-09-05 06:05:50 -0300624 .vfl_dir = VFL_DIR_M2M,
Kamil Debski91884732011-10-06 11:32:12 -0300625};
626
Julia Lawall65d371a2017-08-06 04:25:12 -0400627static const struct v4l2_m2m_ops g2d_m2m_ops = {
Kamil Debski91884732011-10-06 11:32:12 -0300628 .device_run = device_run,
Kamil Debski91884732011-10-06 11:32:12 -0300629};
630
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300631static const struct of_device_id exynos_g2d_match[];
632
Kamil Debski91884732011-10-06 11:32:12 -0300633static int g2d_probe(struct platform_device *pdev)
634{
635 struct g2d_dev *dev;
636 struct video_device *vfd;
637 struct resource *res;
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300638 const struct of_device_id *of_id;
Kamil Debski91884732011-10-06 11:32:12 -0300639 int ret = 0;
640
Sachin Kamat32fced02012-05-14 06:31:24 -0300641 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Kamil Debski91884732011-10-06 11:32:12 -0300642 if (!dev)
643 return -ENOMEM;
Sachin Kamat32fced02012-05-14 06:31:24 -0300644
Kamil Debski27dda972012-02-16 10:52:47 -0300645 spin_lock_init(&dev->ctrl_lock);
Kamil Debski91884732011-10-06 11:32:12 -0300646 mutex_init(&dev->mutex);
647 atomic_set(&dev->num_inst, 0);
Kamil Debski91884732011-10-06 11:32:12 -0300648
649 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kamil Debski91884732011-10-06 11:32:12 -0300650
Thierry Redingf23999e2013-01-21 06:09:07 -0300651 dev->regs = devm_ioremap_resource(&pdev->dev, res);
652 if (IS_ERR(dev->regs))
653 return PTR_ERR(dev->regs);
Kamil Debski91884732011-10-06 11:32:12 -0300654
655 dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
Tony Prisk15514fb2012-12-18 05:28:41 -0300656 if (IS_ERR(dev->clk)) {
Kamil Debski91884732011-10-06 11:32:12 -0300657 dev_err(&pdev->dev, "failed to get g2d clock\n");
Sachin Kamat32fced02012-05-14 06:31:24 -0300658 return -ENXIO;
Kamil Debski91884732011-10-06 11:32:12 -0300659 }
660
Kamil Debski11a37c72012-02-16 10:51:28 -0300661 ret = clk_prepare(dev->clk);
662 if (ret) {
663 dev_err(&pdev->dev, "failed to prepare g2d clock\n");
664 goto put_clk;
665 }
666
Kamil Debski91884732011-10-06 11:32:12 -0300667 dev->gate = clk_get(&pdev->dev, "fimg2d");
Tony Prisk15514fb2012-12-18 05:28:41 -0300668 if (IS_ERR(dev->gate)) {
Kamil Debski91884732011-10-06 11:32:12 -0300669 dev_err(&pdev->dev, "failed to get g2d clock gate\n");
670 ret = -ENXIO;
Kamil Debski11a37c72012-02-16 10:51:28 -0300671 goto unprep_clk;
672 }
673
674 ret = clk_prepare(dev->gate);
675 if (ret) {
676 dev_err(&pdev->dev, "failed to prepare g2d clock gate\n");
677 goto put_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300678 }
679
680 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
681 if (!res) {
682 dev_err(&pdev->dev, "failed to find IRQ\n");
683 ret = -ENXIO;
Kamil Debski11a37c72012-02-16 10:51:28 -0300684 goto unprep_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300685 }
686
687 dev->irq = res->start;
688
Sachin Kamat32fced02012-05-14 06:31:24 -0300689 ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr,
690 0, pdev->name, dev);
Kamil Debski91884732011-10-06 11:32:12 -0300691 if (ret) {
692 dev_err(&pdev->dev, "failed to install IRQ\n");
Christophe JAILLET2f65ec02017-02-19 14:30:22 -0300693 goto unprep_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300694 }
695
Marek Szyprowski712b6172016-05-24 09:16:07 +0200696 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
Kamil Debski91884732011-10-06 11:32:12 -0300697
698 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
699 if (ret)
Hans Verkuilc781e4a2016-02-15 14:25:09 -0200700 goto unprep_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300701 vfd = video_device_alloc();
702 if (!vfd) {
703 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
704 ret = -ENOMEM;
705 goto unreg_v4l2_dev;
706 }
707 *vfd = g2d_videodev;
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400708 set_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags);
Kamil Debski91884732011-10-06 11:32:12 -0300709 vfd->lock = &dev->mutex;
Sachin Kamat8a09a4c2013-07-15 02:36:23 -0300710 vfd->v4l2_dev = &dev->v4l2_dev;
Kamil Debski91884732011-10-06 11:32:12 -0300711 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
712 if (ret) {
713 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
714 goto rel_vdev;
715 }
716 video_set_drvdata(vfd, dev);
Kamil Debski91884732011-10-06 11:32:12 -0300717 dev->vfd = vfd;
718 v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
719 vfd->num);
720 platform_set_drvdata(pdev, dev);
721 dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
722 if (IS_ERR(dev->m2m_dev)) {
723 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
724 ret = PTR_ERR(dev->m2m_dev);
725 goto unreg_video_dev;
726 }
727
728 def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300729
Marek Szyprowskief3617c2015-12-09 12:00:14 -0200730 of_id = of_match_node(exynos_g2d_match, pdev->dev.of_node);
731 if (!of_id) {
732 ret = -ENODEV;
733 goto unreg_video_dev;
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300734 }
Marek Szyprowskief3617c2015-12-09 12:00:14 -0200735 dev->variant = (struct g2d_variant *)of_id->data;
Kamil Debski91884732011-10-06 11:32:12 -0300736
737 return 0;
738
739unreg_video_dev:
740 video_unregister_device(dev->vfd);
741rel_vdev:
742 video_device_release(vfd);
743unreg_v4l2_dev:
744 v4l2_device_unregister(&dev->v4l2_dev);
Kamil Debski11a37c72012-02-16 10:51:28 -0300745unprep_clk_gate:
746 clk_unprepare(dev->gate);
Kamil Debski91884732011-10-06 11:32:12 -0300747put_clk_gate:
748 clk_put(dev->gate);
Kamil Debski11a37c72012-02-16 10:51:28 -0300749unprep_clk:
750 clk_unprepare(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300751put_clk:
752 clk_put(dev->clk);
Sachin Kamat32fced02012-05-14 06:31:24 -0300753
Kamil Debski91884732011-10-06 11:32:12 -0300754 return ret;
755}
756
757static int g2d_remove(struct platform_device *pdev)
758{
Jingoo Han4e3eff62013-09-09 02:52:24 -0300759 struct g2d_dev *dev = platform_get_drvdata(pdev);
Kamil Debski91884732011-10-06 11:32:12 -0300760
761 v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
762 v4l2_m2m_release(dev->m2m_dev);
763 video_unregister_device(dev->vfd);
764 v4l2_device_unregister(&dev->v4l2_dev);
Marek Szyprowski712b6172016-05-24 09:16:07 +0200765 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
Kamil Debski11a37c72012-02-16 10:51:28 -0300766 clk_unprepare(dev->gate);
Kamil Debski91884732011-10-06 11:32:12 -0300767 clk_put(dev->gate);
Kamil Debski11a37c72012-02-16 10:51:28 -0300768 clk_unprepare(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300769 clk_put(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300770 return 0;
771}
772
Sachin Kamat62ce2722013-01-17 00:07:18 -0300773static struct g2d_variant g2d_drvdata_v3x = {
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300774 .hw_rev = TYPE_G2D_3X, /* Revision 3.0 for S5PV210 and Exynos4210 */
Sachin Kamat62ce2722013-01-17 00:07:18 -0300775};
776
777static struct g2d_variant g2d_drvdata_v4x = {
778 .hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */
779};
780
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300781static const struct of_device_id exynos_g2d_match[] = {
782 {
783 .compatible = "samsung,s5pv210-g2d",
784 .data = &g2d_drvdata_v3x,
785 }, {
786 .compatible = "samsung,exynos4212-g2d",
787 .data = &g2d_drvdata_v4x,
788 },
789 {},
790};
791MODULE_DEVICE_TABLE(of, exynos_g2d_match);
792
Kamil Debski91884732011-10-06 11:32:12 -0300793static struct platform_driver g2d_pdrv = {
794 .probe = g2d_probe,
795 .remove = g2d_remove,
796 .driver = {
797 .name = G2D_NAME,
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300798 .of_match_table = exynos_g2d_match,
Kamil Debski91884732011-10-06 11:32:12 -0300799 },
800};
801
Axel Lin1d6629b2012-01-10 03:21:49 -0300802module_platform_driver(g2d_pdrv);
Kamil Debski91884732011-10-06 11:32:12 -0300803
804MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
805MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
806MODULE_LICENSE("GPL");