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