treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 152
[linux-2.6-block.git] / drivers / media / platform / s5p-g2d / g2d.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
91884734
KD
2/*
3 * Samsung S5P G2D - 2D Graphics Accelerator Driver
4 *
5 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
6 * Kamil Debski, <k.debski@samsung.com>
91884734
KD
7 */
8
9#include <linux/module.h>
10#include <linux/fs.h>
91884734
KD
11#include <linux/timer.h>
12#include <linux/sched.h>
13#include <linux/slab.h>
14#include <linux/clk.h>
15#include <linux/interrupt.h>
5ce60d79 16#include <linux/of.h>
91884734
KD
17
18#include <linux/platform_device.h>
19#include <media/v4l2-mem2mem.h>
20#include <media/v4l2-device.h>
21#include <media/v4l2-ioctl.h>
c139990e 22#include <media/videobuf2-v4l2.h>
91884734
KD
23#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 {
32 .name = "XRGB_8888",
33 .fourcc = V4L2_PIX_FMT_RGB32,
34 .depth = 32,
35 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
36 },
37 {
38 .name = "RGB_565",
39 .fourcc = V4L2_PIX_FMT_RGB565X,
40 .depth = 16,
41 .hw = COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
42 },
43 {
44 .name = "XRGB_1555",
45 .fourcc = V4L2_PIX_FMT_RGB555X,
46 .depth = 16,
47 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
48 },
49 {
50 .name = "XRGB_4444",
51 .fourcc = V4L2_PIX_FMT_RGB444,
52 .depth = 16,
53 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
54 },
55 {
56 .name = "PACKED_RGB_888",
57 .fourcc = V4L2_PIX_FMT_RGB24,
58 .depth = 24,
59 .hw = COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
60 },
61};
62#define NUM_FORMATS ARRAY_SIZE(formats)
63
ec76afe8 64static struct g2d_frame def_frame = {
91884734
KD
65 .width = DEFAULT_WIDTH,
66 .height = DEFAULT_HEIGHT,
67 .c_width = DEFAULT_WIDTH,
68 .c_height = DEFAULT_HEIGHT,
69 .o_width = 0,
70 .o_height = 0,
71 .fmt = &formats[0],
72 .right = DEFAULT_WIDTH,
73 .bottom = DEFAULT_HEIGHT,
74};
75
ec76afe8 76static struct g2d_fmt *find_fmt(struct v4l2_format *f)
91884734
KD
77{
78 unsigned int i;
79 for (i = 0; i < NUM_FORMATS; i++) {
80 if (formats[i].fourcc == f->fmt.pix.pixelformat)
81 return &formats[i];
82 }
83 return NULL;
84}
85
86
87static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
f72b9d8c 88 enum v4l2_buf_type type)
91884734
KD
89{
90 switch (type) {
91 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
92 return &ctx->in;
93 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
94 return &ctx->out;
95 default:
96 return ERR_PTR(-EINVAL);
97 }
98}
99
df9ecb0c 100static int g2d_queue_setup(struct vb2_queue *vq,
91884734 101 unsigned int *nbuffers, unsigned int *nplanes,
36c0f8b3 102 unsigned int sizes[], struct device *alloc_devs[])
91884734
KD
103{
104 struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
105 struct g2d_frame *f = get_frame(ctx, vq->type);
106
107 if (IS_ERR(f))
108 return PTR_ERR(f);
109
110 sizes[0] = f->size;
111 *nplanes = 1;
91884734
KD
112
113 if (*nbuffers == 0)
114 *nbuffers = 1;
115
116 return 0;
117}
118
119static int g2d_buf_prepare(struct vb2_buffer *vb)
120{
121 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
122 struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
123
124 if (IS_ERR(f))
125 return PTR_ERR(f);
126 vb2_set_plane_payload(vb, 0, f->size);
127 return 0;
128}
129
130static void g2d_buf_queue(struct vb2_buffer *vb)
131{
2d700715 132 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
91884734 133 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
2d700715 134 v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
91884734
KD
135}
136
b7b361f0 137static const struct vb2_ops g2d_qops = {
91884734
KD
138 .queue_setup = g2d_queue_setup,
139 .buf_prepare = g2d_buf_prepare,
140 .buf_queue = g2d_buf_queue,
b53e19e1
EG
141 .wait_prepare = vb2_ops_wait_prepare,
142 .wait_finish = vb2_ops_wait_finish,
91884734
KD
143};
144
145static int queue_init(void *priv, struct vb2_queue *src_vq,
146 struct vb2_queue *dst_vq)
147{
148 struct g2d_ctx *ctx = priv;
149 int ret;
150
91884734
KD
151 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
152 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
153 src_vq->drv_priv = ctx;
154 src_vq->ops = &g2d_qops;
155 src_vq->mem_ops = &vb2_dma_contig_memops;
156 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
ade48681 157 src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
febeaa45 158 src_vq->lock = &ctx->dev->mutex;
c781e4a5 159 src_vq->dev = ctx->dev->v4l2_dev.dev;
91884734
KD
160
161 ret = vb2_queue_init(src_vq);
162 if (ret)
163 return ret;
164
91884734
KD
165 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
166 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
167 dst_vq->drv_priv = ctx;
168 dst_vq->ops = &g2d_qops;
169 dst_vq->mem_ops = &vb2_dma_contig_memops;
170 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
ade48681 171 dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
febeaa45 172 dst_vq->lock = &ctx->dev->mutex;
c781e4a5 173 dst_vq->dev = ctx->dev->v4l2_dev.dev;
91884734
KD
174
175 return vb2_queue_init(dst_vq);
176}
177
178static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
179{
180 struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
181 ctrl_handler);
27dda973
KD
182 unsigned long flags;
183
184 spin_lock_irqsave(&ctx->dev->ctrl_lock, flags);
91884734
KD
185 switch (ctrl->id) {
186 case V4L2_CID_COLORFX:
187 if (ctrl->val == V4L2_COLORFX_NEGATIVE)
188 ctx->rop = ROP4_INVERT;
189 else
190 ctx->rop = ROP4_COPY;
7f6cce69 191 break;
d0d28326
SK
192
193 case V4L2_CID_HFLIP:
194 ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
195 break;
196
91884734 197 }
27dda973 198 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
91884734
KD
199 return 0;
200}
201
202static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
203 .s_ctrl = g2d_s_ctrl,
204};
205
ec76afe8 206static int g2d_setup_ctrls(struct g2d_ctx *ctx)
91884734
KD
207{
208 struct g2d_dev *dev = ctx->dev;
209
d0d28326
SK
210 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
211
212 ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
213 V4L2_CID_HFLIP, 0, 1, 1, 0);
214
215 ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
216 V4L2_CID_VFLIP, 0, 1, 1, 0);
91884734
KD
217
218 v4l2_ctrl_new_std_menu(
219 &ctx->ctrl_handler,
220 &g2d_ctrl_ops,
221 V4L2_CID_COLORFX,
222 V4L2_COLORFX_NEGATIVE,
223 ~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
224 V4L2_COLORFX_NONE);
225
226 if (ctx->ctrl_handler.error) {
d0d28326
SK
227 int err = ctx->ctrl_handler.error;
228 v4l2_err(&dev->v4l2_dev, "g2d_setup_ctrls failed\n");
229 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
230 return err;
91884734
KD
231 }
232
d0d28326
SK
233 v4l2_ctrl_cluster(2, &ctx->ctrl_hflip);
234
91884734
KD
235 return 0;
236}
237
238static int g2d_open(struct file *file)
239{
240 struct g2d_dev *dev = video_drvdata(file);
241 struct g2d_ctx *ctx = NULL;
242 int ret = 0;
243
244 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
245 if (!ctx)
246 return -ENOMEM;
247 ctx->dev = dev;
248 /* Set default formats */
249 ctx->in = def_frame;
250 ctx->out = def_frame;
251
de40cb22
HV
252 if (mutex_lock_interruptible(&dev->mutex)) {
253 kfree(ctx);
254 return -ERESTARTSYS;
255 }
febeaa45
SN
256 ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
257 if (IS_ERR(ctx->fh.m2m_ctx)) {
258 ret = PTR_ERR(ctx->fh.m2m_ctx);
de40cb22 259 mutex_unlock(&dev->mutex);
91884734
KD
260 kfree(ctx);
261 return ret;
262 }
263 v4l2_fh_init(&ctx->fh, video_devdata(file));
264 file->private_data = &ctx->fh;
265 v4l2_fh_add(&ctx->fh);
266
267 g2d_setup_ctrls(ctx);
268
269 /* Write the default values to the ctx struct */
270 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
271
272 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
de40cb22 273 mutex_unlock(&dev->mutex);
91884734
KD
274
275 v4l2_info(&dev->v4l2_dev, "instance opened\n");
276 return 0;
277}
278
279static int g2d_release(struct file *file)
280{
281 struct g2d_dev *dev = video_drvdata(file);
282 struct g2d_ctx *ctx = fh2ctx(file->private_data);
283
284 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
285 v4l2_fh_del(&ctx->fh);
286 v4l2_fh_exit(&ctx->fh);
287 kfree(ctx);
288 v4l2_info(&dev->v4l2_dev, "instance closed\n");
289 return 0;
290}
291
292
293static int vidioc_querycap(struct file *file, void *priv,
294 struct v4l2_capability *cap)
295{
85709cbf
MCC
296 strscpy(cap->driver, G2D_NAME, sizeof(cap->driver));
297 strscpy(cap->card, G2D_NAME, sizeof(cap->card));
91884734 298 cap->bus_info[0] = 0;
8c17e5e3
HV
299 cap->device_caps = V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
300 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
91884734
KD
301 return 0;
302}
303
304static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
305{
306 struct g2d_fmt *fmt;
307 if (f->index >= NUM_FORMATS)
308 return -EINVAL;
309 fmt = &formats[f->index];
310 f->pixelformat = fmt->fourcc;
85709cbf 311 strscpy(f->description, fmt->name, sizeof(f->description));
91884734
KD
312 return 0;
313}
314
315static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
316{
317 struct g2d_ctx *ctx = prv;
318 struct vb2_queue *vq;
319 struct g2d_frame *frm;
320
febeaa45 321 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
91884734
KD
322 if (!vq)
323 return -EINVAL;
324 frm = get_frame(ctx, f->type);
325 if (IS_ERR(frm))
326 return PTR_ERR(frm);
327
328 f->fmt.pix.width = frm->width;
329 f->fmt.pix.height = frm->height;
330 f->fmt.pix.field = V4L2_FIELD_NONE;
331 f->fmt.pix.pixelformat = frm->fmt->fourcc;
332 f->fmt.pix.bytesperline = (frm->width * frm->fmt->depth) >> 3;
333 f->fmt.pix.sizeimage = frm->size;
334 return 0;
335}
336
337static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
338{
339 struct g2d_fmt *fmt;
340 enum v4l2_field *field;
341
342 fmt = find_fmt(f);
343 if (!fmt)
344 return -EINVAL;
345
346 field = &f->fmt.pix.field;
347 if (*field == V4L2_FIELD_ANY)
348 *field = V4L2_FIELD_NONE;
349 else if (*field != V4L2_FIELD_NONE)
350 return -EINVAL;
351
352 if (f->fmt.pix.width > MAX_WIDTH)
353 f->fmt.pix.width = MAX_WIDTH;
354 if (f->fmt.pix.height > MAX_HEIGHT)
355 f->fmt.pix.height = MAX_HEIGHT;
356
357 if (f->fmt.pix.width < 1)
358 f->fmt.pix.width = 1;
359 if (f->fmt.pix.height < 1)
360 f->fmt.pix.height = 1;
361
362 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
363 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
364 return 0;
365}
366
367static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
368{
369 struct g2d_ctx *ctx = prv;
370 struct g2d_dev *dev = ctx->dev;
371 struct vb2_queue *vq;
372 struct g2d_frame *frm;
373 struct g2d_fmt *fmt;
374 int ret = 0;
375
376 /* Adjust all values accordingly to the hardware capabilities
377 * and chosen format. */
378 ret = vidioc_try_fmt(file, prv, f);
379 if (ret)
380 return ret;
febeaa45 381 vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
91884734
KD
382 if (vb2_is_busy(vq)) {
383 v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
384 return -EBUSY;
385 }
386 frm = get_frame(ctx, f->type);
387 if (IS_ERR(frm))
388 return PTR_ERR(frm);
389 fmt = find_fmt(f);
390 if (!fmt)
391 return -EINVAL;
392 frm->width = f->fmt.pix.width;
393 frm->height = f->fmt.pix.height;
394 frm->size = f->fmt.pix.sizeimage;
395 /* Reset crop settings */
396 frm->o_width = 0;
397 frm->o_height = 0;
398 frm->c_width = frm->width;
399 frm->c_height = frm->height;
400 frm->right = frm->width;
401 frm->bottom = frm->height;
402 frm->fmt = fmt;
403 frm->stride = f->fmt.pix.bytesperline;
404 return 0;
405}
406
f72b9d8c
HV
407static int vidioc_g_selection(struct file *file, void *prv,
408 struct v4l2_selection *s)
91884734
KD
409{
410 struct g2d_ctx *ctx = prv;
411 struct g2d_frame *f;
412
f72b9d8c 413 f = get_frame(ctx, s->type);
91884734
KD
414 if (IS_ERR(f))
415 return PTR_ERR(f);
416
f72b9d8c
HV
417 switch (s->target) {
418 case V4L2_SEL_TGT_CROP:
419 case V4L2_SEL_TGT_CROP_DEFAULT:
420 case V4L2_SEL_TGT_CROP_BOUNDS:
421 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
422 return -EINVAL;
423 break;
424 case V4L2_SEL_TGT_COMPOSE:
425 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
426 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
427 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
428 return -EINVAL;
429 break;
430 default:
431 return -EINVAL;
432 }
433
434 switch (s->target) {
435 case V4L2_SEL_TGT_CROP:
436 case V4L2_SEL_TGT_COMPOSE:
437 s->r.left = f->o_height;
438 s->r.top = f->o_width;
439 s->r.width = f->c_width;
440 s->r.height = f->c_height;
441 break;
442 case V4L2_SEL_TGT_CROP_DEFAULT:
443 case V4L2_SEL_TGT_CROP_BOUNDS:
444 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
445 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
446 s->r.left = 0;
447 s->r.top = 0;
448 s->r.width = f->width;
449 s->r.height = f->height;
450 break;
451 default:
452 return -EINVAL;
453 }
91884734
KD
454 return 0;
455}
456
f72b9d8c
HV
457static int vidioc_try_selection(struct file *file, void *prv,
458 const struct v4l2_selection *s)
91884734
KD
459{
460 struct g2d_ctx *ctx = prv;
461 struct g2d_dev *dev = ctx->dev;
462 struct g2d_frame *f;
463
f72b9d8c 464 f = get_frame(ctx, s->type);
91884734
KD
465 if (IS_ERR(f))
466 return PTR_ERR(f);
467
f72b9d8c
HV
468 if (s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
469 if (s->target != V4L2_SEL_TGT_COMPOSE)
470 return -EINVAL;
471 } else if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
472 if (s->target != V4L2_SEL_TGT_CROP)
473 return -EINVAL;
474 }
475
476 if (s->r.top < 0 || s->r.left < 0) {
91884734
KD
477 v4l2_err(&dev->v4l2_dev,
478 "doesn't support negative values for top & left\n");
479 return -EINVAL;
480 }
481
482 return 0;
483}
484
f72b9d8c
HV
485static int vidioc_s_selection(struct file *file, void *prv,
486 struct v4l2_selection *s)
91884734
KD
487{
488 struct g2d_ctx *ctx = prv;
489 struct g2d_frame *f;
490 int ret;
491
f72b9d8c 492 ret = vidioc_try_selection(file, prv, s);
91884734
KD
493 if (ret)
494 return ret;
f72b9d8c 495 f = get_frame(ctx, s->type);
91884734
KD
496 if (IS_ERR(f))
497 return PTR_ERR(f);
498
f72b9d8c
HV
499 f->c_width = s->r.width;
500 f->c_height = s->r.height;
501 f->o_width = s->r.left;
502 f->o_height = s->r.top;
91884734
KD
503 f->bottom = f->o_height + f->c_height;
504 f->right = f->o_width + f->c_width;
505 return 0;
506}
507
91884734
KD
508static void device_run(void *prv)
509{
510 struct g2d_ctx *ctx = prv;
511 struct g2d_dev *dev = ctx->dev;
30fa627b 512 struct vb2_v4l2_buffer *src, *dst;
27dda973 513 unsigned long flags;
91884734
KD
514 u32 cmd = 0;
515
516 dev->curr = ctx;
517
febeaa45
SN
518 src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
519 dst = v4l2_m2m_next_dst_buf(ctx->fh.m2m_ctx);
91884734
KD
520
521 clk_enable(dev->gate);
522 g2d_reset(dev);
523
27dda973
KD
524 spin_lock_irqsave(&dev->ctrl_lock, flags);
525
91884734 526 g2d_set_src_size(dev, &ctx->in);
30fa627b 527 g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(&src->vb2_buf, 0));
91884734
KD
528
529 g2d_set_dst_size(dev, &ctx->out);
30fa627b 530 g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(&dst->vb2_buf, 0));
91884734
KD
531
532 g2d_set_rop4(dev, ctx->rop);
d0d28326
SK
533 g2d_set_flip(dev, ctx->flip);
534
91884734 535 if (ctx->in.c_width != ctx->out.c_width ||
62ce272d
SK
536 ctx->in.c_height != ctx->out.c_height) {
537 if (dev->variant->hw_rev == TYPE_G2D_3X)
538 cmd |= CMD_V3_ENABLE_STRETCH;
539 else
540 g2d_set_v41_stretch(dev, &ctx->in, &ctx->out);
541 }
542
91884734
KD
543 g2d_set_cmd(dev, cmd);
544 g2d_start(dev);
27dda973
KD
545
546 spin_unlock_irqrestore(&dev->ctrl_lock, flags);
91884734
KD
547}
548
549static irqreturn_t g2d_isr(int irq, void *prv)
550{
551 struct g2d_dev *dev = prv;
552 struct g2d_ctx *ctx = dev->curr;
2d700715 553 struct vb2_v4l2_buffer *src, *dst;
91884734
KD
554
555 g2d_clear_int(dev);
556 clk_disable(dev->gate);
557
2356877c 558 BUG_ON(ctx == NULL);
91884734 559
febeaa45
SN
560 src = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
561 dst = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
91884734 562
2356877c
SK
563 BUG_ON(src == NULL);
564 BUG_ON(dst == NULL);
91884734 565
2d700715 566 dst->timecode = src->timecode;
d6dd645e 567 dst->vb2_buf.timestamp = src->vb2_buf.timestamp;
2d700715
JS
568 dst->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
569 dst->flags |=
570 src->flags & V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
dc7be295 571
91884734
KD
572 v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
573 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
febeaa45 574 v4l2_m2m_job_finish(dev->m2m_dev, ctx->fh.m2m_ctx);
91884734 575
2356877c 576 dev->curr = NULL;
91884734
KD
577 return IRQ_HANDLED;
578}
579
580static const struct v4l2_file_operations g2d_fops = {
581 .owner = THIS_MODULE,
582 .open = g2d_open,
583 .release = g2d_release,
febeaa45 584 .poll = v4l2_m2m_fop_poll,
91884734 585 .unlocked_ioctl = video_ioctl2,
febeaa45 586 .mmap = v4l2_m2m_fop_mmap,
91884734
KD
587};
588
589static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
590 .vidioc_querycap = vidioc_querycap,
591
592 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
593 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
594 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
595 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
596
597 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
598 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
599 .vidioc_try_fmt_vid_out = vidioc_try_fmt,
600 .vidioc_s_fmt_vid_out = vidioc_s_fmt,
601
febeaa45
SN
602 .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
603 .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
604 .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
605 .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
91884734 606
febeaa45
SN
607 .vidioc_streamon = v4l2_m2m_ioctl_streamon,
608 .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
91884734 609
f72b9d8c
HV
610 .vidioc_g_selection = vidioc_g_selection,
611 .vidioc_s_selection = vidioc_s_selection,
91884734
KD
612};
613
5303135c 614static const struct video_device g2d_videodev = {
91884734
KD
615 .name = G2D_NAME,
616 .fops = &g2d_fops,
617 .ioctl_ops = &g2d_ioctl_ops,
618 .minor = -1,
619 .release = video_device_release,
954f340f 620 .vfl_dir = VFL_DIR_M2M,
91884734
KD
621};
622
65d371af 623static const struct v4l2_m2m_ops g2d_m2m_ops = {
91884734 624 .device_run = device_run,
91884734
KD
625};
626
5ce60d79
SK
627static const struct of_device_id exynos_g2d_match[];
628
91884734
KD
629static int g2d_probe(struct platform_device *pdev)
630{
631 struct g2d_dev *dev;
632 struct video_device *vfd;
633 struct resource *res;
5ce60d79 634 const struct of_device_id *of_id;
91884734
KD
635 int ret = 0;
636
32fced05 637 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
91884734
KD
638 if (!dev)
639 return -ENOMEM;
32fced05 640
27dda973 641 spin_lock_init(&dev->ctrl_lock);
91884734
KD
642 mutex_init(&dev->mutex);
643 atomic_set(&dev->num_inst, 0);
91884734
KD
644
645 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
91884734 646
f23999ec
TR
647 dev->regs = devm_ioremap_resource(&pdev->dev, res);
648 if (IS_ERR(dev->regs))
649 return PTR_ERR(dev->regs);
91884734
KD
650
651 dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
15514fb5 652 if (IS_ERR(dev->clk)) {
91884734 653 dev_err(&pdev->dev, "failed to get g2d clock\n");
32fced05 654 return -ENXIO;
91884734
KD
655 }
656
11a37c70
KD
657 ret = clk_prepare(dev->clk);
658 if (ret) {
659 dev_err(&pdev->dev, "failed to prepare g2d clock\n");
660 goto put_clk;
661 }
662
91884734 663 dev->gate = clk_get(&pdev->dev, "fimg2d");
15514fb5 664 if (IS_ERR(dev->gate)) {
91884734
KD
665 dev_err(&pdev->dev, "failed to get g2d clock gate\n");
666 ret = -ENXIO;
11a37c70
KD
667 goto unprep_clk;
668 }
669
670 ret = clk_prepare(dev->gate);
671 if (ret) {
672 dev_err(&pdev->dev, "failed to prepare g2d clock gate\n");
673 goto put_clk_gate;
91884734
KD
674 }
675
676 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
677 if (!res) {
678 dev_err(&pdev->dev, "failed to find IRQ\n");
679 ret = -ENXIO;
11a37c70 680 goto unprep_clk_gate;
91884734
KD
681 }
682
683 dev->irq = res->start;
684
32fced05
SK
685 ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr,
686 0, pdev->name, dev);
91884734
KD
687 if (ret) {
688 dev_err(&pdev->dev, "failed to install IRQ\n");
2f65ec05 689 goto unprep_clk_gate;
91884734
KD
690 }
691
712b617e 692 vb2_dma_contig_set_max_seg_size(&pdev->dev, DMA_BIT_MASK(32));
91884734
KD
693
694 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
695 if (ret)
c781e4a5 696 goto unprep_clk_gate;
91884734
KD
697 vfd = video_device_alloc();
698 if (!vfd) {
699 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
700 ret = -ENOMEM;
701 goto unreg_v4l2_dev;
702 }
703 *vfd = g2d_videodev;
f72b9d8c 704 set_bit(V4L2_FL_QUIRK_INVERTED_CROP, &vfd->flags);
91884734 705 vfd->lock = &dev->mutex;
8a09a4cc 706 vfd->v4l2_dev = &dev->v4l2_dev;
91884734
KD
707 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
708 if (ret) {
709 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
710 goto rel_vdev;
711 }
712 video_set_drvdata(vfd, dev);
91884734
KD
713 dev->vfd = vfd;
714 v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
715 vfd->num);
716 platform_set_drvdata(pdev, dev);
717 dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
718 if (IS_ERR(dev->m2m_dev)) {
719 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
720 ret = PTR_ERR(dev->m2m_dev);
721 goto unreg_video_dev;
722 }
723
724 def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
5ce60d79 725
ef3617c4
MS
726 of_id = of_match_node(exynos_g2d_match, pdev->dev.of_node);
727 if (!of_id) {
728 ret = -ENODEV;
729 goto unreg_video_dev;
5ce60d79 730 }
ef3617c4 731 dev->variant = (struct g2d_variant *)of_id->data;
91884734
KD
732
733 return 0;
734
735unreg_video_dev:
736 video_unregister_device(dev->vfd);
737rel_vdev:
738 video_device_release(vfd);
739unreg_v4l2_dev:
740 v4l2_device_unregister(&dev->v4l2_dev);
11a37c70
KD
741unprep_clk_gate:
742 clk_unprepare(dev->gate);
91884734
KD
743put_clk_gate:
744 clk_put(dev->gate);
11a37c70
KD
745unprep_clk:
746 clk_unprepare(dev->clk);
91884734
KD
747put_clk:
748 clk_put(dev->clk);
32fced05 749
91884734
KD
750 return ret;
751}
752
753static int g2d_remove(struct platform_device *pdev)
754{
4e3eff61 755 struct g2d_dev *dev = platform_get_drvdata(pdev);
91884734
KD
756
757 v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
758 v4l2_m2m_release(dev->m2m_dev);
759 video_unregister_device(dev->vfd);
760 v4l2_device_unregister(&dev->v4l2_dev);
712b617e 761 vb2_dma_contig_clear_max_seg_size(&pdev->dev);
11a37c70 762 clk_unprepare(dev->gate);
91884734 763 clk_put(dev->gate);
11a37c70 764 clk_unprepare(dev->clk);
91884734 765 clk_put(dev->clk);
91884734
KD
766 return 0;
767}
768
62ce272d 769static struct g2d_variant g2d_drvdata_v3x = {
5ce60d79 770 .hw_rev = TYPE_G2D_3X, /* Revision 3.0 for S5PV210 and Exynos4210 */
62ce272d
SK
771};
772
773static struct g2d_variant g2d_drvdata_v4x = {
774 .hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */
775};
776
5ce60d79
SK
777static const struct of_device_id exynos_g2d_match[] = {
778 {
779 .compatible = "samsung,s5pv210-g2d",
780 .data = &g2d_drvdata_v3x,
781 }, {
782 .compatible = "samsung,exynos4212-g2d",
783 .data = &g2d_drvdata_v4x,
784 },
785 {},
786};
787MODULE_DEVICE_TABLE(of, exynos_g2d_match);
788
91884734
KD
789static struct platform_driver g2d_pdrv = {
790 .probe = g2d_probe,
791 .remove = g2d_remove,
792 .driver = {
793 .name = G2D_NAME,
5ce60d79 794 .of_match_table = exynos_g2d_match,
91884734
KD
795 },
796};
797
1d6629b1 798module_platform_driver(g2d_pdrv);
91884734
KD
799
800MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
801MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
802MODULE_LICENSE("GPL");