blob: f5f05ea9f5213172e387fdf02def9be199eb4597 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Kamil Debski91884732011-10-06 11:32:12 -03002/*
3 * Samsung S5P G2D - 2D Graphics Accelerator Driver
4 *
5 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
6 * Kamil Debski, <k.debski@samsung.com>
Kamil Debski91884732011-10-06 11:32:12 -03007 */
8
9#include <linux/module.h>
10#include <linux/fs.h>
Kamil Debski91884732011-10-06 11:32:12 -030011#include <linux/timer.h>
12#include <linux/sched.h>
13#include <linux/slab.h>
14#include <linux/clk.h>
15#include <linux/interrupt.h>
Sachin Kamat5ce60d72013-02-06 01:29:43 -030016#include <linux/of.h>
Kamil Debski91884732011-10-06 11:32:12 -030017
18#include <linux/platform_device.h>
19#include <media/v4l2-mem2mem.h>
20#include <media/v4l2-device.h>
21#include <media/v4l2-ioctl.h>
Junghak Sungc1399902015-09-22 10:30:29 -030022#include <media/videobuf2-v4l2.h>
Kamil Debski91884732011-10-06 11:32:12 -030023#include <media/videobuf2-dma-contig.h>
24
25#include "g2d.h"
26#include "g2d-regs.h"
27
28#define fh2ctx(__fh) container_of(__fh, struct g2d_ctx, fh)
29
30static struct g2d_fmt formats[] = {
31 {
Kamil Debski91884732011-10-06 11:32:12 -030032 .fourcc = V4L2_PIX_FMT_RGB32,
33 .depth = 32,
34 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
35 },
36 {
Kamil Debski91884732011-10-06 11:32:12 -030037 .fourcc = V4L2_PIX_FMT_RGB565X,
38 .depth = 16,
39 .hw = COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
40 },
41 {
Kamil Debski91884732011-10-06 11:32:12 -030042 .fourcc = V4L2_PIX_FMT_RGB555X,
43 .depth = 16,
44 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
45 },
46 {
Kamil Debski91884732011-10-06 11:32:12 -030047 .fourcc = V4L2_PIX_FMT_RGB444,
48 .depth = 16,
49 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
50 },
51 {
Kamil Debski91884732011-10-06 11:32:12 -030052 .fourcc = V4L2_PIX_FMT_RGB24,
53 .depth = 24,
54 .hw = COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
55 },
56};
57#define NUM_FORMATS ARRAY_SIZE(formats)
58
Sachin Kamatec76afe2012-05-10 03:35:48 -030059static struct g2d_frame def_frame = {
Kamil Debski91884732011-10-06 11:32:12 -030060 .width = DEFAULT_WIDTH,
61 .height = DEFAULT_HEIGHT,
62 .c_width = DEFAULT_WIDTH,
63 .c_height = DEFAULT_HEIGHT,
64 .o_width = 0,
65 .o_height = 0,
66 .fmt = &formats[0],
67 .right = DEFAULT_WIDTH,
68 .bottom = DEFAULT_HEIGHT,
69};
70
Sachin Kamatec76afe2012-05-10 03:35:48 -030071static struct g2d_fmt *find_fmt(struct v4l2_format *f)
Kamil Debski91884732011-10-06 11:32:12 -030072{
73 unsigned int i;
74 for (i = 0; i < NUM_FORMATS; i++) {
75 if (formats[i].fourcc == f->fmt.pix.pixelformat)
76 return &formats[i];
77 }
78 return NULL;
79}
80
81
82static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
Hans Verkuilf72b9d82018-10-04 16:12:06 -040083 enum v4l2_buf_type type)
Kamil Debski91884732011-10-06 11:32:12 -030084{
85 switch (type) {
86 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
87 return &ctx->in;
88 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
89 return &ctx->out;
90 default:
91 return ERR_PTR(-EINVAL);
92 }
93}
94
Hans Verkuildf9ecb0c2015-10-28 00:50:37 -020095static int g2d_queue_setup(struct vb2_queue *vq,
Kamil Debski91884732011-10-06 11:32:12 -030096 unsigned int *nbuffers, unsigned int *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -030097 unsigned int sizes[], struct device *alloc_devs[])
Kamil Debski91884732011-10-06 11:32:12 -030098{
99 struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
100 struct g2d_frame *f = get_frame(ctx, vq->type);
101
102 if (IS_ERR(f))
103 return PTR_ERR(f);
104
105 sizes[0] = f->size;
106 *nplanes = 1;
Kamil Debski91884732011-10-06 11:32:12 -0300107
108 if (*nbuffers == 0)
109 *nbuffers = 1;
110
111 return 0;
112}
113
114static int g2d_buf_prepare(struct vb2_buffer *vb)
115{
116 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
117 struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
118
119 if (IS_ERR(f))
120 return PTR_ERR(f);
121 vb2_set_plane_payload(vb, 0, f->size);
122 return 0;
123}
124
125static void g2d_buf_queue(struct vb2_buffer *vb)
126{
Junghak Sung2d700712015-09-22 10:30:30 -0300127 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Kamil Debski91884732011-10-06 11:32:12 -0300128 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300129 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
Kamil Debski91884732011-10-06 11:32:12 -0300130}
131
Julia Lawallb7b361f2016-09-08 20:59:10 -0300132static const struct vb2_ops g2d_qops = {
Kamil Debski91884732011-10-06 11:32:12 -0300133 .queue_setup = g2d_queue_setup,
134 .buf_prepare = g2d_buf_prepare,
135 .buf_queue = g2d_buf_queue,
Ezequiel Garciab53e19e2018-06-15 15:07:26 -0400136 .wait_prepare = vb2_ops_wait_prepare,
137 .wait_finish = vb2_ops_wait_finish,
Kamil Debski91884732011-10-06 11:32:12 -0300138};
139
140static int queue_init(void *priv, struct vb2_queue *src_vq,
141 struct vb2_queue *dst_vq)
142{
143 struct g2d_ctx *ctx = priv;
144 int ret;
145
Kamil Debski91884732011-10-06 11:32:12 -0300146 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
147 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
148 src_vq->drv_priv = ctx;
149 src_vq->ops = &g2d_qops;
150 src_vq->mem_ops = &vb2_dma_contig_memops;
151 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
Sakari Ailusade48682014-02-25 19:12:19 -0300152 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300153 src_vq->lock = &ctx->dev->mutex;
Hans Verkuilc781e4a2016-02-15 14:25:09 -0200154 src_vq->dev = ctx->dev->v4l2_dev.dev;
Kamil Debski91884732011-10-06 11:32:12 -0300155
156 ret = vb2_queue_init(src_vq);
157 if (ret)
158 return ret;
159
Kamil Debski91884732011-10-06 11:32:12 -0300160 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
161 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
162 dst_vq->drv_priv = ctx;
163 dst_vq->ops = &g2d_qops;
164 dst_vq->mem_ops = &vb2_dma_contig_memops;
165 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
Sakari Ailusade48682014-02-25 19:12:19 -0300166 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300167 dst_vq->lock = &ctx->dev->mutex;
Hans Verkuilc781e4a2016-02-15 14:25:09 -0200168 dst_vq->dev = ctx->dev->v4l2_dev.dev;
Kamil Debski91884732011-10-06 11:32:12 -0300169
170 return vb2_queue_init(dst_vq);
171}
172
173static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
174{
175 struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
176 ctrl_handler);
Kamil Debski27dda972012-02-16 10:52:47 -0300177 unsigned long flags;
178
179 spin_lock_irqsave(&ctx->dev->ctrl_lock, flags);
Kamil Debski91884732011-10-06 11:32:12 -0300180 switch (ctrl->id) {
181 case V4L2_CID_COLORFX:
182 if (ctrl->val == V4L2_COLORFX_NEGATIVE)
183 ctx->rop = ROP4_INVERT;
184 else
185 ctx->rop = ROP4_COPY;
Kamil Debski7f6cce62012-01-02 09:19:25 -0300186 break;
Sachin Kamatd0d28322012-02-16 10:52:16 -0300187
188 case V4L2_CID_HFLIP:
189 ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
190 break;
191
Kamil Debski91884732011-10-06 11:32:12 -0300192 }
Kamil Debski27dda972012-02-16 10:52:47 -0300193 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
Kamil Debski91884732011-10-06 11:32:12 -0300194 return 0;
195}
196
197static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
198 .s_ctrl = g2d_s_ctrl,
199};
200
Sachin Kamatec76afe2012-05-10 03:35:48 -0300201static int g2d_setup_ctrls(struct g2d_ctx *ctx)
Kamil Debski91884732011-10-06 11:32:12 -0300202{
203 struct g2d_dev *dev = ctx->dev;
204
Sachin Kamatd0d28322012-02-16 10:52:16 -0300205 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
206
207 ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
208 V4L2_CID_HFLIP, 0, 1, 1, 0);
209
210 ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
211 V4L2_CID_VFLIP, 0, 1, 1, 0);
Kamil Debski91884732011-10-06 11:32:12 -0300212
213 v4l2_ctrl_new_std_menu(
214 &ctx->ctrl_handler,
215 &g2d_ctrl_ops,
216 V4L2_CID_COLORFX,
217 V4L2_COLORFX_NEGATIVE,
218 ~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
219 V4L2_COLORFX_NONE);
220
221 if (ctx->ctrl_handler.error) {
Sachin Kamatd0d28322012-02-16 10:52:16 -0300222 int err = ctx->ctrl_handler.error;
223 v4l2_err(&dev->v4l2_dev, "g2d_setup_ctrls failed\n");
224 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
225 return err;
Kamil Debski91884732011-10-06 11:32:12 -0300226 }
227
Sachin Kamatd0d28322012-02-16 10:52:16 -0300228 v4l2_ctrl_cluster(2, &ctx->ctrl_hflip);
229
Kamil Debski91884732011-10-06 11:32:12 -0300230 return 0;
231}
232
233static int g2d_open(struct file *file)
234{
235 struct g2d_dev *dev = video_drvdata(file);
236 struct g2d_ctx *ctx = NULL;
237 int ret = 0;
238
239 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
240 if (!ctx)
241 return -ENOMEM;
242 ctx->dev = dev;
243 /* Set default formats */
244 ctx->in = def_frame;
245 ctx->out = def_frame;
246
Hans Verkuilde40cb22012-06-24 06:58:28 -0300247 if (mutex_lock_interruptible(&dev->mutex)) {
248 kfree(ctx);
249 return -ERESTARTSYS;
250 }
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300251 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
252 if (IS_ERR(ctx->fh.m2m_ctx)) {
253 ret = PTR_ERR(ctx->fh.m2m_ctx);
Hans Verkuilde40cb22012-06-24 06:58:28 -0300254 mutex_unlock(&dev->mutex);
Kamil Debski91884732011-10-06 11:32:12 -0300255 kfree(ctx);
256 return ret;
257 }
258 v4l2_fh_init(&ctx->fh, video_devdata(file));
259 file->private_data = &ctx->fh;
260 v4l2_fh_add(&ctx->fh);
261
262 g2d_setup_ctrls(ctx);
263
264 /* Write the default values to the ctx struct */
265 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
266
267 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
Hans Verkuilde40cb22012-06-24 06:58:28 -0300268 mutex_unlock(&dev->mutex);
Kamil Debski91884732011-10-06 11:32:12 -0300269
270 v4l2_info(&dev->v4l2_dev, "instance opened\n");
271 return 0;
272}
273
274static int g2d_release(struct file *file)
275{
276 struct g2d_dev *dev = video_drvdata(file);
277 struct g2d_ctx *ctx = fh2ctx(file->private_data);
278
279 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
280 v4l2_fh_del(&ctx->fh);
281 v4l2_fh_exit(&ctx->fh);
282 kfree(ctx);
283 v4l2_info(&dev->v4l2_dev, "instance closed\n");
284 return 0;
285}
286
287
288static int vidioc_querycap(struct file *file, void *priv,
289 struct v4l2_capability *cap)
290{
Mauro Carvalho Chehab85709cb2018-09-10 08:19:16 -0400291 strscpy(cap->driver, G2D_NAME, sizeof(cap->driver));
292 strscpy(cap->card, G2D_NAME, sizeof(cap->card));
Kamil Debski91884732011-10-06 11:32:12 -0300293 cap->bus_info[0] = 0;
Kamil Debski91884732011-10-06 11:32:12 -0300294 return 0;
295}
296
297static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
298{
Kamil Debski91884732011-10-06 11:32:12 -0300299 if (f->index >= NUM_FORMATS)
300 return -EINVAL;
Hans Verkuil59fe9162019-06-11 10:25:15 -0400301 f->pixelformat = formats[f->index].fourcc;
Kamil Debski91884732011-10-06 11:32:12 -0300302 return 0;
303}
304
305static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
306{
307 struct g2d_ctx *ctx = prv;
308 struct vb2_queue *vq;
309 struct g2d_frame *frm;
310
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300311 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Kamil Debski91884732011-10-06 11:32:12 -0300312 if (!vq)
313 return -EINVAL;
314 frm = get_frame(ctx, f->type);
315 if (IS_ERR(frm))
316 return PTR_ERR(frm);
317
318 f->fmt.pix.width = frm->width;
319 f->fmt.pix.height = frm->height;
320 f->fmt.pix.field = V4L2_FIELD_NONE;
321 f->fmt.pix.pixelformat = frm->fmt->fourcc;
322 f->fmt.pix.bytesperline = (frm->width * frm->fmt->depth) >> 3;
323 f->fmt.pix.sizeimage = frm->size;
324 return 0;
325}
326
327static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
328{
329 struct g2d_fmt *fmt;
330 enum v4l2_field *field;
331
332 fmt = find_fmt(f);
333 if (!fmt)
334 return -EINVAL;
335
336 field = &f->fmt.pix.field;
337 if (*field == V4L2_FIELD_ANY)
338 *field = V4L2_FIELD_NONE;
339 else if (*field != V4L2_FIELD_NONE)
340 return -EINVAL;
341
342 if (f->fmt.pix.width > MAX_WIDTH)
343 f->fmt.pix.width = MAX_WIDTH;
344 if (f->fmt.pix.height > MAX_HEIGHT)
345 f->fmt.pix.height = MAX_HEIGHT;
346
347 if (f->fmt.pix.width < 1)
348 f->fmt.pix.width = 1;
349 if (f->fmt.pix.height < 1)
350 f->fmt.pix.height = 1;
351
352 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
353 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
354 return 0;
355}
356
357static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
358{
359 struct g2d_ctx *ctx = prv;
360 struct g2d_dev *dev = ctx->dev;
361 struct vb2_queue *vq;
362 struct g2d_frame *frm;
363 struct g2d_fmt *fmt;
364 int ret = 0;
365
366 /* Adjust all values accordingly to the hardware capabilities
367 * and chosen format. */
368 ret = vidioc_try_fmt(file, prv, f);
369 if (ret)
370 return ret;
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300371 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
Kamil Debski91884732011-10-06 11:32:12 -0300372 if (vb2_is_busy(vq)) {
373 v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
374 return -EBUSY;
375 }
376 frm = get_frame(ctx, f->type);
377 if (IS_ERR(frm))
378 return PTR_ERR(frm);
379 fmt = find_fmt(f);
380 if (!fmt)
381 return -EINVAL;
382 frm->width = f->fmt.pix.width;
383 frm->height = f->fmt.pix.height;
384 frm->size = f->fmt.pix.sizeimage;
385 /* Reset crop settings */
386 frm->o_width = 0;
387 frm->o_height = 0;
388 frm->c_width = frm->width;
389 frm->c_height = frm->height;
390 frm->right = frm->width;
391 frm->bottom = frm->height;
392 frm->fmt = fmt;
393 frm->stride = f->fmt.pix.bytesperline;
394 return 0;
395}
396
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400397static int vidioc_g_selection(struct file *file, void *prv,
398 struct v4l2_selection *s)
Kamil Debski91884732011-10-06 11:32:12 -0300399{
400 struct g2d_ctx *ctx = prv;
401 struct g2d_frame *f;
402
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400403 f = get_frame(ctx, s->type);
Kamil Debski91884732011-10-06 11:32:12 -0300404 if (IS_ERR(f))
405 return PTR_ERR(f);
406
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400407 switch (s->target) {
408 case V4L2_SEL_TGT_CROP:
409 case V4L2_SEL_TGT_CROP_DEFAULT:
410 case V4L2_SEL_TGT_CROP_BOUNDS:
411 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
412 return -EINVAL;
413 break;
414 case V4L2_SEL_TGT_COMPOSE:
415 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
416 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
417 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
418 return -EINVAL;
419 break;
420 default:
421 return -EINVAL;
422 }
423
424 switch (s->target) {
425 case V4L2_SEL_TGT_CROP:
426 case V4L2_SEL_TGT_COMPOSE:
427 s->r.left = f->o_height;
428 s->r.top = f->o_width;
429 s->r.width = f->c_width;
430 s->r.height = f->c_height;
431 break;
432 case V4L2_SEL_TGT_CROP_DEFAULT:
433 case V4L2_SEL_TGT_CROP_BOUNDS:
434 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
435 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
436 s->r.left = 0;
437 s->r.top = 0;
438 s->r.width = f->width;
439 s->r.height = f->height;
440 break;
441 default:
442 return -EINVAL;
443 }
Kamil Debski91884732011-10-06 11:32:12 -0300444 return 0;
445}
446
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400447static int vidioc_try_selection(struct file *file, void *prv,
448 const struct v4l2_selection *s)
Kamil Debski91884732011-10-06 11:32:12 -0300449{
450 struct g2d_ctx *ctx = prv;
451 struct g2d_dev *dev = ctx->dev;
452 struct g2d_frame *f;
453
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400454 f = get_frame(ctx, s->type);
Kamil Debski91884732011-10-06 11:32:12 -0300455 if (IS_ERR(f))
456 return PTR_ERR(f);
457
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400458 if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
459 if (s->target != V4L2_SEL_TGT_COMPOSE)
460 return -EINVAL;
461 } else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
462 if (s->target != V4L2_SEL_TGT_CROP)
463 return -EINVAL;
464 }
465
466 if (s->r.top < 0 || s->r.left < 0) {
Kamil Debski91884732011-10-06 11:32:12 -0300467 v4l2_err(&dev->v4l2_dev,
468 "doesn't support negative values for top & left\n");
469 return -EINVAL;
470 }
471
472 return 0;
473}
474
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400475static int vidioc_s_selection(struct file *file, void *prv,
476 struct v4l2_selection *s)
Kamil Debski91884732011-10-06 11:32:12 -0300477{
478 struct g2d_ctx *ctx = prv;
479 struct g2d_frame *f;
480 int ret;
481
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400482 ret = vidioc_try_selection(file, prv, s);
Kamil Debski91884732011-10-06 11:32:12 -0300483 if (ret)
484 return ret;
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400485 f = get_frame(ctx, s->type);
Kamil Debski91884732011-10-06 11:32:12 -0300486 if (IS_ERR(f))
487 return PTR_ERR(f);
488
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400489 f->c_width = s->r.width;
490 f->c_height = s->r.height;
491 f->o_width = s->r.left;
492 f->o_height = s->r.top;
Kamil Debski91884732011-10-06 11:32:12 -0300493 f->bottom = f->o_height + f->c_height;
494 f->right = f->o_width + f->c_width;
495 return 0;
496}
497
Kamil Debski91884732011-10-06 11:32:12 -0300498static void device_run(void *prv)
499{
500 struct g2d_ctx *ctx = prv;
501 struct g2d_dev *dev = ctx->dev;
Ezequiel Garcia30fa6272019-02-08 11:17:44 -0500502 struct vb2_v4l2_buffer *src, *dst;
Kamil Debski27dda972012-02-16 10:52:47 -0300503 unsigned long flags;
Kamil Debski91884732011-10-06 11:32:12 -0300504 u32 cmd = 0;
505
506 dev->curr = ctx;
507
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300508 src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
509 dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
Kamil Debski91884732011-10-06 11:32:12 -0300510
511 clk_enable(dev->gate);
512 g2d_reset(dev);
513
Kamil Debski27dda972012-02-16 10:52:47 -0300514 spin_lock_irqsave(&dev->ctrl_lock, flags);
515
Kamil Debski91884732011-10-06 11:32:12 -0300516 g2d_set_src_size(dev, &ctx->in);
Ezequiel Garcia30fa6272019-02-08 11:17:44 -0500517 g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0));
Kamil Debski91884732011-10-06 11:32:12 -0300518
519 g2d_set_dst_size(dev, &ctx->out);
Ezequiel Garcia30fa6272019-02-08 11:17:44 -0500520 g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0));
Kamil Debski91884732011-10-06 11:32:12 -0300521
522 g2d_set_rop4(dev, ctx->rop);
Sachin Kamatd0d28322012-02-16 10:52:16 -0300523 g2d_set_flip(dev, ctx->flip);
524
Kamil Debski91884732011-10-06 11:32:12 -0300525 if (ctx->in.c_width != ctx->out.c_width ||
Sachin Kamat62ce2722013-01-17 00:07:18 -0300526 ctx->in.c_height != ctx->out.c_height) {
527 if (dev->variant->hw_rev == TYPE_G2D_3X)
528 cmd |= CMD_V3_ENABLE_STRETCH;
529 else
530 g2d_set_v41_stretch(dev, &ctx->in, &ctx->out);
531 }
532
Kamil Debski91884732011-10-06 11:32:12 -0300533 g2d_set_cmd(dev, cmd);
534 g2d_start(dev);
Kamil Debski27dda972012-02-16 10:52:47 -0300535
536 spin_unlock_irqrestore(&dev->ctrl_lock, flags);
Kamil Debski91884732011-10-06 11:32:12 -0300537}
538
539static irqreturn_t g2d_isr(int irq, void *prv)
540{
541 struct g2d_dev *dev = prv;
542 struct g2d_ctx *ctx = dev->curr;
Junghak Sung2d700712015-09-22 10:30:30 -0300543 struct vb2_v4l2_buffer *src, *dst;
Kamil Debski91884732011-10-06 11:32:12 -0300544
545 g2d_clear_int(dev);
546 clk_disable(dev->gate);
547
Sachin Kamat23568772012-05-10 03:35:47 -0300548 BUG_ON(ctx == NULL);
Kamil Debski91884732011-10-06 11:32:12 -0300549
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300550 src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
551 dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
Kamil Debski91884732011-10-06 11:32:12 -0300552
Sachin Kamat23568772012-05-10 03:35:47 -0300553 BUG_ON(src == NULL);
554 BUG_ON(dst == NULL);
Kamil Debski91884732011-10-06 11:32:12 -0300555
Junghak Sung2d700712015-09-22 10:30:30 -0300556 dst->timecode = src->timecode;
Junghak Sungd6dd6452015-11-03 08:16:37 -0200557 dst->vb2_buf.timestamp = src->vb2_buf.timestamp;
Junghak Sung2d700712015-09-22 10:30:30 -0300558 dst->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
559 dst->flags |=
560 src->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
Kamil Debskidc7be2952013-04-24 09:34:03 -0300561
Kamil Debski91884732011-10-06 11:32:12 -0300562 v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
563 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300564 v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
Kamil Debski91884732011-10-06 11:32:12 -0300565
Sachin Kamat23568772012-05-10 03:35:47 -0300566 dev->curr = NULL;
Kamil Debski91884732011-10-06 11:32:12 -0300567 return IRQ_HANDLED;
568}
569
570static const struct v4l2_file_operations g2d_fops = {
571 .owner = THIS_MODULE,
572 .open = g2d_open,
573 .release = g2d_release,
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300574 .poll = v4l2_m2m_fop_poll,
Kamil Debski91884732011-10-06 11:32:12 -0300575 .unlocked_ioctl = video_ioctl2,
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300576 .mmap = v4l2_m2m_fop_mmap,
Kamil Debski91884732011-10-06 11:32:12 -0300577};
578
579static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
580 .vidioc_querycap = vidioc_querycap,
581
582 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
583 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
584 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
585 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
586
587 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
588 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
589 .vidioc_try_fmt_vid_out = vidioc_try_fmt,
590 .vidioc_s_fmt_vid_out = vidioc_s_fmt,
591
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300592 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
593 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
594 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
595 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
Kamil Debski91884732011-10-06 11:32:12 -0300596
Sylwester Nawrockifebeaa42013-08-25 18:13:17 -0300597 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
598 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
Kamil Debski91884732011-10-06 11:32:12 -0300599
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400600 .vidioc_g_selection = vidioc_g_selection,
601 .vidioc_s_selection = vidioc_s_selection,
Kamil Debski91884732011-10-06 11:32:12 -0300602};
603
Bhumika Goyal53031352017-08-26 08:57:26 -0400604static const struct video_device g2d_videodev = {
Kamil Debski91884732011-10-06 11:32:12 -0300605 .name = G2D_NAME,
606 .fops = &g2d_fops,
607 .ioctl_ops = &g2d_ioctl_ops,
608 .minor = -1,
609 .release = video_device_release,
Hans Verkuil954f3402012-09-05 06:05:50 -0300610 .vfl_dir = VFL_DIR_M2M,
Kamil Debski91884732011-10-06 11:32:12 -0300611};
612
Julia Lawall65d371a2017-08-06 04:25:12 -0400613static const struct v4l2_m2m_ops g2d_m2m_ops = {
Kamil Debski91884732011-10-06 11:32:12 -0300614 .device_run = device_run,
Kamil Debski91884732011-10-06 11:32:12 -0300615};
616
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300617static const struct of_device_id exynos_g2d_match[];
618
Kamil Debski91884732011-10-06 11:32:12 -0300619static int g2d_probe(struct platform_device *pdev)
620{
621 struct g2d_dev *dev;
622 struct video_device *vfd;
623 struct resource *res;
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300624 const struct of_device_id *of_id;
Kamil Debski91884732011-10-06 11:32:12 -0300625 int ret = 0;
626
Sachin Kamat32fced02012-05-14 06:31:24 -0300627 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Kamil Debski91884732011-10-06 11:32:12 -0300628 if (!dev)
629 return -ENOMEM;
Sachin Kamat32fced02012-05-14 06:31:24 -0300630
Kamil Debski27dda972012-02-16 10:52:47 -0300631 spin_lock_init(&dev->ctrl_lock);
Kamil Debski91884732011-10-06 11:32:12 -0300632 mutex_init(&dev->mutex);
633 atomic_set(&dev->num_inst, 0);
Kamil Debski91884732011-10-06 11:32:12 -0300634
635 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kamil Debski91884732011-10-06 11:32:12 -0300636
Thierry Redingf23999e2013-01-21 06:09:07 -0300637 dev->regs = devm_ioremap_resource(&pdev->dev, res);
638 if (IS_ERR(dev->regs))
639 return PTR_ERR(dev->regs);
Kamil Debski91884732011-10-06 11:32:12 -0300640
641 dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
Tony Prisk15514fb2012-12-18 05:28:41 -0300642 if (IS_ERR(dev->clk)) {
Kamil Debski91884732011-10-06 11:32:12 -0300643 dev_err(&pdev->dev, "failed to get g2d clock\n");
Sachin Kamat32fced02012-05-14 06:31:24 -0300644 return -ENXIO;
Kamil Debski91884732011-10-06 11:32:12 -0300645 }
646
Kamil Debski11a37c72012-02-16 10:51:28 -0300647 ret = clk_prepare(dev->clk);
648 if (ret) {
649 dev_err(&pdev->dev, "failed to prepare g2d clock\n");
650 goto put_clk;
651 }
652
Kamil Debski91884732011-10-06 11:32:12 -0300653 dev->gate = clk_get(&pdev->dev, "fimg2d");
Tony Prisk15514fb2012-12-18 05:28:41 -0300654 if (IS_ERR(dev->gate)) {
Kamil Debski91884732011-10-06 11:32:12 -0300655 dev_err(&pdev->dev, "failed to get g2d clock gate\n");
656 ret = -ENXIO;
Kamil Debski11a37c72012-02-16 10:51:28 -0300657 goto unprep_clk;
658 }
659
660 ret = clk_prepare(dev->gate);
661 if (ret) {
662 dev_err(&pdev->dev, "failed to prepare g2d clock gate\n");
663 goto put_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300664 }
665
666 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
667 if (!res) {
668 dev_err(&pdev->dev, "failed to find IRQ\n");
669 ret = -ENXIO;
Kamil Debski11a37c72012-02-16 10:51:28 -0300670 goto unprep_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300671 }
672
673 dev->irq = res->start;
674
Sachin Kamat32fced02012-05-14 06:31:24 -0300675 ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr,
676 0, pdev->name, dev);
Kamil Debski91884732011-10-06 11:32:12 -0300677 if (ret) {
678 dev_err(&pdev->dev, "failed to install IRQ\n");
Christophe JAILLET2f65ec02017-02-19 14:30:22 -0300679 goto unprep_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300680 }
681
Marek Szyprowski712b6172016-05-24 09:16:07 +0200682 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
Kamil Debski91884732011-10-06 11:32:12 -0300683
684 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
685 if (ret)
Hans Verkuilc781e4a2016-02-15 14:25:09 -0200686 goto unprep_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300687 vfd = video_device_alloc();
688 if (!vfd) {
689 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
690 ret = -ENOMEM;
691 goto unreg_v4l2_dev;
692 }
693 *vfd = g2d_videodev;
Hans Verkuilf72b9d82018-10-04 16:12:06 -0400694 set_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags);
Kamil Debski91884732011-10-06 11:32:12 -0300695 vfd->lock = &dev->mutex;
Sachin Kamat8a09a4c2013-07-15 02:36:23 -0300696 vfd->v4l2_dev = &dev->v4l2_dev;
Hans Verkuil994587c2019-06-26 02:51:41 -0400697 vfd->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Kamil Debski91884732011-10-06 11:32:12 -0300698 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
699 if (ret) {
700 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
701 goto rel_vdev;
702 }
703 video_set_drvdata(vfd, dev);
Kamil Debski91884732011-10-06 11:32:12 -0300704 dev->vfd = vfd;
705 v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
706 vfd->num);
707 platform_set_drvdata(pdev, dev);
708 dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
709 if (IS_ERR(dev->m2m_dev)) {
710 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
711 ret = PTR_ERR(dev->m2m_dev);
712 goto unreg_video_dev;
713 }
714
715 def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300716
Marek Szyprowskief3617c2015-12-09 12:00:14 -0200717 of_id = of_match_node(exynos_g2d_match, pdev->dev.of_node);
718 if (!of_id) {
719 ret = -ENODEV;
720 goto unreg_video_dev;
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300721 }
Marek Szyprowskief3617c2015-12-09 12:00:14 -0200722 dev->variant = (struct g2d_variant *)of_id->data;
Kamil Debski91884732011-10-06 11:32:12 -0300723
724 return 0;
725
726unreg_video_dev:
727 video_unregister_device(dev->vfd);
728rel_vdev:
729 video_device_release(vfd);
730unreg_v4l2_dev:
731 v4l2_device_unregister(&dev->v4l2_dev);
Kamil Debski11a37c72012-02-16 10:51:28 -0300732unprep_clk_gate:
733 clk_unprepare(dev->gate);
Kamil Debski91884732011-10-06 11:32:12 -0300734put_clk_gate:
735 clk_put(dev->gate);
Kamil Debski11a37c72012-02-16 10:51:28 -0300736unprep_clk:
737 clk_unprepare(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300738put_clk:
739 clk_put(dev->clk);
Sachin Kamat32fced02012-05-14 06:31:24 -0300740
Kamil Debski91884732011-10-06 11:32:12 -0300741 return ret;
742}
743
744static int g2d_remove(struct platform_device *pdev)
745{
Jingoo Han4e3eff62013-09-09 02:52:24 -0300746 struct g2d_dev *dev = platform_get_drvdata(pdev);
Kamil Debski91884732011-10-06 11:32:12 -0300747
748 v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
749 v4l2_m2m_release(dev->m2m_dev);
750 video_unregister_device(dev->vfd);
751 v4l2_device_unregister(&dev->v4l2_dev);
Marek Szyprowski712b6172016-05-24 09:16:07 +0200752 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
Kamil Debski11a37c72012-02-16 10:51:28 -0300753 clk_unprepare(dev->gate);
Kamil Debski91884732011-10-06 11:32:12 -0300754 clk_put(dev->gate);
Kamil Debski11a37c72012-02-16 10:51:28 -0300755 clk_unprepare(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300756 clk_put(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300757 return 0;
758}
759
Sachin Kamat62ce2722013-01-17 00:07:18 -0300760static struct g2d_variant g2d_drvdata_v3x = {
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300761 .hw_rev = TYPE_G2D_3X, /* Revision 3.0 for S5PV210 and Exynos4210 */
Sachin Kamat62ce2722013-01-17 00:07:18 -0300762};
763
764static struct g2d_variant g2d_drvdata_v4x = {
765 .hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */
766};
767
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300768static const struct of_device_id exynos_g2d_match[] = {
769 {
770 .compatible = "samsung,s5pv210-g2d",
771 .data = &g2d_drvdata_v3x,
772 }, {
773 .compatible = "samsung,exynos4212-g2d",
774 .data = &g2d_drvdata_v4x,
775 },
776 {},
777};
778MODULE_DEVICE_TABLE(of, exynos_g2d_match);
779
Kamil Debski91884732011-10-06 11:32:12 -0300780static struct platform_driver g2d_pdrv = {
781 .probe = g2d_probe,
782 .remove = g2d_remove,
783 .driver = {
784 .name = G2D_NAME,
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300785 .of_match_table = exynos_g2d_match,
Kamil Debski91884732011-10-06 11:32:12 -0300786 },
787};
788
Axel Lin1d6629b2012-01-10 03:21:49 -0300789module_platform_driver(g2d_pdrv);
Kamil Debski91884732011-10-06 11:32:12 -0300790
791MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
792MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
793MODULE_LICENSE("GPL");