blob: 14d663dacdbd21b8ad8c87d74c4bcc7d1bf12ace [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>
15#include <linux/version.h>
16#include <linux/timer.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/clk.h>
20#include <linux/interrupt.h>
Sachin Kamat5ce60d72013-02-06 01:29:43 -030021#include <linux/of.h>
Kamil Debski91884732011-10-06 11:32:12 -030022
23#include <linux/platform_device.h>
24#include <media/v4l2-mem2mem.h>
25#include <media/v4l2-device.h>
26#include <media/v4l2-ioctl.h>
27#include <media/videobuf2-core.h>
28#include <media/videobuf2-dma-contig.h>
29
30#include "g2d.h"
31#include "g2d-regs.h"
32
33#define fh2ctx(__fh) container_of(__fh, struct g2d_ctx, fh)
34
35static struct g2d_fmt formats[] = {
36 {
37 .name = "XRGB_8888",
38 .fourcc = V4L2_PIX_FMT_RGB32,
39 .depth = 32,
40 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
41 },
42 {
43 .name = "RGB_565",
44 .fourcc = V4L2_PIX_FMT_RGB565X,
45 .depth = 16,
46 .hw = COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
47 },
48 {
49 .name = "XRGB_1555",
50 .fourcc = V4L2_PIX_FMT_RGB555X,
51 .depth = 16,
52 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
53 },
54 {
55 .name = "XRGB_4444",
56 .fourcc = V4L2_PIX_FMT_RGB444,
57 .depth = 16,
58 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
59 },
60 {
61 .name = "PACKED_RGB_888",
62 .fourcc = V4L2_PIX_FMT_RGB24,
63 .depth = 24,
64 .hw = COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
65 },
66};
67#define NUM_FORMATS ARRAY_SIZE(formats)
68
Sachin Kamatec76afe2012-05-10 03:35:48 -030069static struct g2d_frame def_frame = {
Kamil Debski91884732011-10-06 11:32:12 -030070 .width = DEFAULT_WIDTH,
71 .height = DEFAULT_HEIGHT,
72 .c_width = DEFAULT_WIDTH,
73 .c_height = DEFAULT_HEIGHT,
74 .o_width = 0,
75 .o_height = 0,
76 .fmt = &formats[0],
77 .right = DEFAULT_WIDTH,
78 .bottom = DEFAULT_HEIGHT,
79};
80
Sachin Kamatec76afe2012-05-10 03:35:48 -030081static struct g2d_fmt *find_fmt(struct v4l2_format *f)
Kamil Debski91884732011-10-06 11:32:12 -030082{
83 unsigned int i;
84 for (i = 0; i < NUM_FORMATS; i++) {
85 if (formats[i].fourcc == f->fmt.pix.pixelformat)
86 return &formats[i];
87 }
88 return NULL;
89}
90
91
92static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
93 enum v4l2_buf_type type)
94{
95 switch (type) {
96 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
97 return &ctx->in;
98 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
99 return &ctx->out;
100 default:
101 return ERR_PTR(-EINVAL);
102 }
103}
104
105static int g2d_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
106 unsigned int *nbuffers, unsigned int *nplanes,
107 unsigned int sizes[], void *alloc_ctxs[])
108{
109 struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
110 struct g2d_frame *f = get_frame(ctx, vq->type);
111
112 if (IS_ERR(f))
113 return PTR_ERR(f);
114
115 sizes[0] = f->size;
116 *nplanes = 1;
117 alloc_ctxs[0] = ctx->dev->alloc_ctx;
118
119 if (*nbuffers == 0)
120 *nbuffers = 1;
121
122 return 0;
123}
124
125static int g2d_buf_prepare(struct vb2_buffer *vb)
126{
127 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
128 struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
129
130 if (IS_ERR(f))
131 return PTR_ERR(f);
132 vb2_set_plane_payload(vb, 0, f->size);
133 return 0;
134}
135
136static void g2d_buf_queue(struct vb2_buffer *vb)
137{
138 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
139 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
140}
141
142
143static struct vb2_ops g2d_qops = {
144 .queue_setup = g2d_queue_setup,
145 .buf_prepare = g2d_buf_prepare,
146 .buf_queue = g2d_buf_queue,
147};
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);
161
162 ret = vb2_queue_init(src_vq);
163 if (ret)
164 return ret;
165
Kamil Debski91884732011-10-06 11:32:12 -0300166 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
167 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
168 dst_vq->drv_priv = ctx;
169 dst_vq->ops = &g2d_qops;
170 dst_vq->mem_ops = &vb2_dma_contig_memops;
171 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
172
173 return vb2_queue_init(dst_vq);
174}
175
176static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
177{
178 struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
179 ctrl_handler);
Kamil Debski27dda972012-02-16 10:52:47 -0300180 unsigned long flags;
181
182 spin_lock_irqsave(&ctx->dev->ctrl_lock, flags);
Kamil Debski91884732011-10-06 11:32:12 -0300183 switch (ctrl->id) {
184 case V4L2_CID_COLORFX:
185 if (ctrl->val == V4L2_COLORFX_NEGATIVE)
186 ctx->rop = ROP4_INVERT;
187 else
188 ctx->rop = ROP4_COPY;
Kamil Debski7f6cce62012-01-02 09:19:25 -0300189 break;
Sachin Kamatd0d28322012-02-16 10:52:16 -0300190
191 case V4L2_CID_HFLIP:
192 ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
193 break;
194
Kamil Debski91884732011-10-06 11:32:12 -0300195 }
Kamil Debski27dda972012-02-16 10:52:47 -0300196 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
Kamil Debski91884732011-10-06 11:32:12 -0300197 return 0;
198}
199
200static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
201 .s_ctrl = g2d_s_ctrl,
202};
203
Sachin Kamatec76afe2012-05-10 03:35:48 -0300204static int g2d_setup_ctrls(struct g2d_ctx *ctx)
Kamil Debski91884732011-10-06 11:32:12 -0300205{
206 struct g2d_dev *dev = ctx->dev;
207
Sachin Kamatd0d28322012-02-16 10:52:16 -0300208 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
209
210 ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
211 V4L2_CID_HFLIP, 0, 1, 1, 0);
212
213 ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
214 V4L2_CID_VFLIP, 0, 1, 1, 0);
Kamil Debski91884732011-10-06 11:32:12 -0300215
216 v4l2_ctrl_new_std_menu(
217 &ctx->ctrl_handler,
218 &g2d_ctrl_ops,
219 V4L2_CID_COLORFX,
220 V4L2_COLORFX_NEGATIVE,
221 ~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
222 V4L2_COLORFX_NONE);
223
224 if (ctx->ctrl_handler.error) {
Sachin Kamatd0d28322012-02-16 10:52:16 -0300225 int err = ctx->ctrl_handler.error;
226 v4l2_err(&dev->v4l2_dev, "g2d_setup_ctrls failed\n");
227 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
228 return err;
Kamil Debski91884732011-10-06 11:32:12 -0300229 }
230
Sachin Kamatd0d28322012-02-16 10:52:16 -0300231 v4l2_ctrl_cluster(2, &ctx->ctrl_hflip);
232
Kamil Debski91884732011-10-06 11:32:12 -0300233 return 0;
234}
235
236static int g2d_open(struct file *file)
237{
238 struct g2d_dev *dev = video_drvdata(file);
239 struct g2d_ctx *ctx = NULL;
240 int ret = 0;
241
242 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
243 if (!ctx)
244 return -ENOMEM;
245 ctx->dev = dev;
246 /* Set default formats */
247 ctx->in = def_frame;
248 ctx->out = def_frame;
249
Hans Verkuilde40cb22012-06-24 06:58:28 -0300250 if (mutex_lock_interruptible(&dev->mutex)) {
251 kfree(ctx);
252 return -ERESTARTSYS;
253 }
Kamil Debski91884732011-10-06 11:32:12 -0300254 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
255 if (IS_ERR(ctx->m2m_ctx)) {
256 ret = PTR_ERR(ctx->m2m_ctx);
Hans Verkuilde40cb22012-06-24 06:58:28 -0300257 mutex_unlock(&dev->mutex);
Kamil Debski91884732011-10-06 11:32:12 -0300258 kfree(ctx);
259 return ret;
260 }
261 v4l2_fh_init(&ctx->fh, video_devdata(file));
262 file->private_data = &ctx->fh;
263 v4l2_fh_add(&ctx->fh);
264
265 g2d_setup_ctrls(ctx);
266
267 /* Write the default values to the ctx struct */
268 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
269
270 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
Hans Verkuilde40cb22012-06-24 06:58:28 -0300271 mutex_unlock(&dev->mutex);
Kamil Debski91884732011-10-06 11:32:12 -0300272
273 v4l2_info(&dev->v4l2_dev, "instance opened\n");
274 return 0;
275}
276
277static int g2d_release(struct file *file)
278{
279 struct g2d_dev *dev = video_drvdata(file);
280 struct g2d_ctx *ctx = fh2ctx(file->private_data);
281
282 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
283 v4l2_fh_del(&ctx->fh);
284 v4l2_fh_exit(&ctx->fh);
285 kfree(ctx);
286 v4l2_info(&dev->v4l2_dev, "instance closed\n");
287 return 0;
288}
289
290
291static int vidioc_querycap(struct file *file, void *priv,
292 struct v4l2_capability *cap)
293{
294 strncpy(cap->driver, G2D_NAME, sizeof(cap->driver) - 1);
295 strncpy(cap->card, G2D_NAME, sizeof(cap->card) - 1);
296 cap->bus_info[0] = 0;
297 cap->version = KERNEL_VERSION(1, 0, 0);
Sylwester Nawrockif0476a82012-07-26 09:30:00 -0300298 /*
299 * This is only a mem-to-mem video device. The capture and output
300 * device capability flags are left only for backward compatibility
301 * and are scheduled for removal.
302 */
303 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
304 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
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;
315 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
316 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
325 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
326 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;
385 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
386 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
411static unsigned int g2d_poll(struct file *file, struct poll_table_struct *wait)
412{
413 struct g2d_ctx *ctx = fh2ctx(file->private_data);
Hans Verkuilde40cb22012-06-24 06:58:28 -0300414 struct g2d_dev *dev = ctx->dev;
415 unsigned int res;
416
417 mutex_lock(&dev->mutex);
418 res = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
419 mutex_unlock(&dev->mutex);
420 return res;
Kamil Debski91884732011-10-06 11:32:12 -0300421}
422
423static int g2d_mmap(struct file *file, struct vm_area_struct *vma)
424{
425 struct g2d_ctx *ctx = fh2ctx(file->private_data);
Hans Verkuilde40cb22012-06-24 06:58:28 -0300426 struct g2d_dev *dev = ctx->dev;
427 int ret;
428
429 if (mutex_lock_interruptible(&dev->mutex))
430 return -ERESTARTSYS;
431 ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
432 mutex_unlock(&dev->mutex);
433 return ret;
Kamil Debski91884732011-10-06 11:32:12 -0300434}
435
436static int vidioc_reqbufs(struct file *file, void *priv,
437 struct v4l2_requestbuffers *reqbufs)
438{
439 struct g2d_ctx *ctx = priv;
440 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
441}
442
443static int vidioc_querybuf(struct file *file, void *priv,
444 struct v4l2_buffer *buf)
445{
446 struct g2d_ctx *ctx = priv;
447 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
448}
449
450static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
451{
452 struct g2d_ctx *ctx = priv;
453 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
454}
455
456static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
457{
458 struct g2d_ctx *ctx = priv;
459 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
460}
461
462
463static int vidioc_streamon(struct file *file, void *priv,
464 enum v4l2_buf_type type)
465{
466 struct g2d_ctx *ctx = priv;
467 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
468}
469
470static int vidioc_streamoff(struct file *file, void *priv,
471 enum v4l2_buf_type type)
472{
473 struct g2d_ctx *ctx = priv;
474 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
475}
476
477static int vidioc_cropcap(struct file *file, void *priv,
478 struct v4l2_cropcap *cr)
479{
480 struct g2d_ctx *ctx = priv;
481 struct g2d_frame *f;
482
483 f = get_frame(ctx, cr->type);
484 if (IS_ERR(f))
485 return PTR_ERR(f);
486
487 cr->bounds.left = 0;
488 cr->bounds.top = 0;
489 cr->bounds.width = f->width;
490 cr->bounds.height = f->height;
491 cr->defrect = cr->bounds;
492 return 0;
493}
494
495static int vidioc_g_crop(struct file *file, void *prv, struct v4l2_crop *cr)
496{
497 struct g2d_ctx *ctx = prv;
498 struct g2d_frame *f;
499
500 f = get_frame(ctx, cr->type);
501 if (IS_ERR(f))
502 return PTR_ERR(f);
503
504 cr->c.left = f->o_height;
505 cr->c.top = f->o_width;
506 cr->c.width = f->c_width;
507 cr->c.height = f->c_height;
508 return 0;
509}
510
Hans Verkuil21ec9c92012-09-05 05:10:48 -0300511static int vidioc_try_crop(struct file *file, void *prv, const struct v4l2_crop *cr)
Kamil Debski91884732011-10-06 11:32:12 -0300512{
513 struct g2d_ctx *ctx = prv;
514 struct g2d_dev *dev = ctx->dev;
515 struct g2d_frame *f;
516
517 f = get_frame(ctx, cr->type);
518 if (IS_ERR(f))
519 return PTR_ERR(f);
520
521 if (cr->c.top < 0 || cr->c.left < 0) {
522 v4l2_err(&dev->v4l2_dev,
523 "doesn't support negative values for top & left\n");
524 return -EINVAL;
525 }
526
527 return 0;
528}
529
Hans Verkuil4f996592012-09-05 05:10:48 -0300530static int vidioc_s_crop(struct file *file, void *prv, const struct v4l2_crop *cr)
Kamil Debski91884732011-10-06 11:32:12 -0300531{
532 struct g2d_ctx *ctx = prv;
533 struct g2d_frame *f;
534 int ret;
535
536 ret = vidioc_try_crop(file, prv, cr);
537 if (ret)
538 return ret;
539 f = get_frame(ctx, cr->type);
540 if (IS_ERR(f))
541 return PTR_ERR(f);
542
543 f->c_width = cr->c.width;
544 f->c_height = cr->c.height;
545 f->o_width = cr->c.left;
546 f->o_height = cr->c.top;
547 f->bottom = f->o_height + f->c_height;
548 f->right = f->o_width + f->c_width;
549 return 0;
550}
551
552static void g2d_lock(void *prv)
553{
554 struct g2d_ctx *ctx = prv;
555 struct g2d_dev *dev = ctx->dev;
556 mutex_lock(&dev->mutex);
557}
558
559static void g2d_unlock(void *prv)
560{
561 struct g2d_ctx *ctx = prv;
562 struct g2d_dev *dev = ctx->dev;
563 mutex_unlock(&dev->mutex);
564}
565
566static void job_abort(void *prv)
567{
568 struct g2d_ctx *ctx = prv;
569 struct g2d_dev *dev = ctx->dev;
570 int ret;
571
Sachin Kamat23568772012-05-10 03:35:47 -0300572 if (dev->curr == NULL) /* No job currently running */
Kamil Debski91884732011-10-06 11:32:12 -0300573 return;
574
575 ret = wait_event_timeout(dev->irq_queue,
Sachin Kamat23568772012-05-10 03:35:47 -0300576 dev->curr == NULL,
Kamil Debski91884732011-10-06 11:32:12 -0300577 msecs_to_jiffies(G2D_TIMEOUT));
578}
579
580static void device_run(void *prv)
581{
582 struct g2d_ctx *ctx = prv;
583 struct g2d_dev *dev = ctx->dev;
584 struct vb2_buffer *src, *dst;
Kamil Debski27dda972012-02-16 10:52:47 -0300585 unsigned long flags;
Kamil Debski91884732011-10-06 11:32:12 -0300586 u32 cmd = 0;
587
588 dev->curr = ctx;
589
590 src = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
591 dst = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
592
593 clk_enable(dev->gate);
594 g2d_reset(dev);
595
Kamil Debski27dda972012-02-16 10:52:47 -0300596 spin_lock_irqsave(&dev->ctrl_lock, flags);
597
Kamil Debski91884732011-10-06 11:32:12 -0300598 g2d_set_src_size(dev, &ctx->in);
599 g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(src, 0));
600
601 g2d_set_dst_size(dev, &ctx->out);
602 g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(dst, 0));
603
604 g2d_set_rop4(dev, ctx->rop);
Sachin Kamatd0d28322012-02-16 10:52:16 -0300605 g2d_set_flip(dev, ctx->flip);
606
Kamil Debski91884732011-10-06 11:32:12 -0300607 if (ctx->in.c_width != ctx->out.c_width ||
Sachin Kamat62ce2722013-01-17 00:07:18 -0300608 ctx->in.c_height != ctx->out.c_height) {
609 if (dev->variant->hw_rev == TYPE_G2D_3X)
610 cmd |= CMD_V3_ENABLE_STRETCH;
611 else
612 g2d_set_v41_stretch(dev, &ctx->in, &ctx->out);
613 }
614
Kamil Debski91884732011-10-06 11:32:12 -0300615 g2d_set_cmd(dev, cmd);
616 g2d_start(dev);
Kamil Debski27dda972012-02-16 10:52:47 -0300617
618 spin_unlock_irqrestore(&dev->ctrl_lock, flags);
Kamil Debski91884732011-10-06 11:32:12 -0300619}
620
621static irqreturn_t g2d_isr(int irq, void *prv)
622{
623 struct g2d_dev *dev = prv;
624 struct g2d_ctx *ctx = dev->curr;
625 struct vb2_buffer *src, *dst;
626
627 g2d_clear_int(dev);
628 clk_disable(dev->gate);
629
Sachin Kamat23568772012-05-10 03:35:47 -0300630 BUG_ON(ctx == NULL);
Kamil Debski91884732011-10-06 11:32:12 -0300631
632 src = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
633 dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
634
Sachin Kamat23568772012-05-10 03:35:47 -0300635 BUG_ON(src == NULL);
636 BUG_ON(dst == NULL);
Kamil Debski91884732011-10-06 11:32:12 -0300637
638 v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
639 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
640 v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
641
Sachin Kamat23568772012-05-10 03:35:47 -0300642 dev->curr = NULL;
Kamil Debski91884732011-10-06 11:32:12 -0300643 wake_up(&dev->irq_queue);
644 return IRQ_HANDLED;
645}
646
647static const struct v4l2_file_operations g2d_fops = {
648 .owner = THIS_MODULE,
649 .open = g2d_open,
650 .release = g2d_release,
651 .poll = g2d_poll,
652 .unlocked_ioctl = video_ioctl2,
653 .mmap = g2d_mmap,
654};
655
656static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
657 .vidioc_querycap = vidioc_querycap,
658
659 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
660 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
661 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
662 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
663
664 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
665 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
666 .vidioc_try_fmt_vid_out = vidioc_try_fmt,
667 .vidioc_s_fmt_vid_out = vidioc_s_fmt,
668
669 .vidioc_reqbufs = vidioc_reqbufs,
670 .vidioc_querybuf = vidioc_querybuf,
671
672 .vidioc_qbuf = vidioc_qbuf,
673 .vidioc_dqbuf = vidioc_dqbuf,
674
675 .vidioc_streamon = vidioc_streamon,
676 .vidioc_streamoff = vidioc_streamoff,
677
678 .vidioc_g_crop = vidioc_g_crop,
679 .vidioc_s_crop = vidioc_s_crop,
680 .vidioc_cropcap = vidioc_cropcap,
681};
682
683static struct video_device g2d_videodev = {
684 .name = G2D_NAME,
685 .fops = &g2d_fops,
686 .ioctl_ops = &g2d_ioctl_ops,
687 .minor = -1,
688 .release = video_device_release,
Hans Verkuil954f3402012-09-05 06:05:50 -0300689 .vfl_dir = VFL_DIR_M2M,
Kamil Debski91884732011-10-06 11:32:12 -0300690};
691
692static struct v4l2_m2m_ops g2d_m2m_ops = {
693 .device_run = device_run,
694 .job_abort = job_abort,
695 .lock = g2d_lock,
696 .unlock = g2d_unlock,
697};
698
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300699static const struct of_device_id exynos_g2d_match[];
700
Kamil Debski91884732011-10-06 11:32:12 -0300701static int g2d_probe(struct platform_device *pdev)
702{
703 struct g2d_dev *dev;
704 struct video_device *vfd;
705 struct resource *res;
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300706 const struct of_device_id *of_id;
Kamil Debski91884732011-10-06 11:32:12 -0300707 int ret = 0;
708
Sachin Kamat32fced02012-05-14 06:31:24 -0300709 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Kamil Debski91884732011-10-06 11:32:12 -0300710 if (!dev)
711 return -ENOMEM;
Sachin Kamat32fced02012-05-14 06:31:24 -0300712
Kamil Debski27dda972012-02-16 10:52:47 -0300713 spin_lock_init(&dev->ctrl_lock);
Kamil Debski91884732011-10-06 11:32:12 -0300714 mutex_init(&dev->mutex);
715 atomic_set(&dev->num_inst, 0);
716 init_waitqueue_head(&dev->irq_queue);
717
718 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kamil Debski91884732011-10-06 11:32:12 -0300719
Thierry Redingf23999e2013-01-21 06:09:07 -0300720 dev->regs = devm_ioremap_resource(&pdev->dev, res);
721 if (IS_ERR(dev->regs))
722 return PTR_ERR(dev->regs);
Kamil Debski91884732011-10-06 11:32:12 -0300723
724 dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
Tony Prisk15514fb2012-12-18 05:28:41 -0300725 if (IS_ERR(dev->clk)) {
Kamil Debski91884732011-10-06 11:32:12 -0300726 dev_err(&pdev->dev, "failed to get g2d clock\n");
Sachin Kamat32fced02012-05-14 06:31:24 -0300727 return -ENXIO;
Kamil Debski91884732011-10-06 11:32:12 -0300728 }
729
Kamil Debski11a37c72012-02-16 10:51:28 -0300730 ret = clk_prepare(dev->clk);
731 if (ret) {
732 dev_err(&pdev->dev, "failed to prepare g2d clock\n");
733 goto put_clk;
734 }
735
Kamil Debski91884732011-10-06 11:32:12 -0300736 dev->gate = clk_get(&pdev->dev, "fimg2d");
Tony Prisk15514fb2012-12-18 05:28:41 -0300737 if (IS_ERR(dev->gate)) {
Kamil Debski91884732011-10-06 11:32:12 -0300738 dev_err(&pdev->dev, "failed to get g2d clock gate\n");
739 ret = -ENXIO;
Kamil Debski11a37c72012-02-16 10:51:28 -0300740 goto unprep_clk;
741 }
742
743 ret = clk_prepare(dev->gate);
744 if (ret) {
745 dev_err(&pdev->dev, "failed to prepare g2d clock gate\n");
746 goto put_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300747 }
748
749 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
750 if (!res) {
751 dev_err(&pdev->dev, "failed to find IRQ\n");
752 ret = -ENXIO;
Kamil Debski11a37c72012-02-16 10:51:28 -0300753 goto unprep_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300754 }
755
756 dev->irq = res->start;
757
Sachin Kamat32fced02012-05-14 06:31:24 -0300758 ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr,
759 0, pdev->name, dev);
Kamil Debski91884732011-10-06 11:32:12 -0300760 if (ret) {
761 dev_err(&pdev->dev, "failed to install IRQ\n");
762 goto put_clk_gate;
763 }
764
765 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
766 if (IS_ERR(dev->alloc_ctx)) {
767 ret = PTR_ERR(dev->alloc_ctx);
Sachin Kamat32fced02012-05-14 06:31:24 -0300768 goto unprep_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300769 }
770
771 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
772 if (ret)
773 goto alloc_ctx_cleanup;
774 vfd = video_device_alloc();
775 if (!vfd) {
776 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
777 ret = -ENOMEM;
778 goto unreg_v4l2_dev;
779 }
780 *vfd = g2d_videodev;
781 vfd->lock = &dev->mutex;
782 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
783 if (ret) {
784 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
785 goto rel_vdev;
786 }
787 video_set_drvdata(vfd, dev);
788 snprintf(vfd->name, sizeof(vfd->name), "%s", g2d_videodev.name);
789 dev->vfd = vfd;
790 v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
791 vfd->num);
792 platform_set_drvdata(pdev, dev);
793 dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
794 if (IS_ERR(dev->m2m_dev)) {
795 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
796 ret = PTR_ERR(dev->m2m_dev);
797 goto unreg_video_dev;
798 }
799
800 def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300801
802 if (!pdev->dev.of_node) {
803 dev->variant = g2d_get_drv_data(pdev);
804 } else {
805 of_id = of_match_node(exynos_g2d_match, pdev->dev.of_node);
806 if (!of_id) {
807 ret = -ENODEV;
808 goto unreg_video_dev;
809 }
810 dev->variant = (struct g2d_variant *)of_id->data;
811 }
Kamil Debski91884732011-10-06 11:32:12 -0300812
813 return 0;
814
815unreg_video_dev:
816 video_unregister_device(dev->vfd);
817rel_vdev:
818 video_device_release(vfd);
819unreg_v4l2_dev:
820 v4l2_device_unregister(&dev->v4l2_dev);
821alloc_ctx_cleanup:
822 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
Kamil Debski11a37c72012-02-16 10:51:28 -0300823unprep_clk_gate:
824 clk_unprepare(dev->gate);
Kamil Debski91884732011-10-06 11:32:12 -0300825put_clk_gate:
826 clk_put(dev->gate);
Kamil Debski11a37c72012-02-16 10:51:28 -0300827unprep_clk:
828 clk_unprepare(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300829put_clk:
830 clk_put(dev->clk);
Sachin Kamat32fced02012-05-14 06:31:24 -0300831
Kamil Debski91884732011-10-06 11:32:12 -0300832 return ret;
833}
834
835static int g2d_remove(struct platform_device *pdev)
836{
837 struct g2d_dev *dev = (struct g2d_dev *)platform_get_drvdata(pdev);
838
839 v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
840 v4l2_m2m_release(dev->m2m_dev);
841 video_unregister_device(dev->vfd);
842 v4l2_device_unregister(&dev->v4l2_dev);
843 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
Kamil Debski11a37c72012-02-16 10:51:28 -0300844 clk_unprepare(dev->gate);
Kamil Debski91884732011-10-06 11:32:12 -0300845 clk_put(dev->gate);
Kamil Debski11a37c72012-02-16 10:51:28 -0300846 clk_unprepare(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300847 clk_put(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300848 return 0;
849}
850
Sachin Kamat62ce2722013-01-17 00:07:18 -0300851static struct g2d_variant g2d_drvdata_v3x = {
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300852 .hw_rev = TYPE_G2D_3X, /* Revision 3.0 for S5PV210 and Exynos4210 */
Sachin Kamat62ce2722013-01-17 00:07:18 -0300853};
854
855static struct g2d_variant g2d_drvdata_v4x = {
856 .hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */
857};
858
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300859static const struct of_device_id exynos_g2d_match[] = {
860 {
861 .compatible = "samsung,s5pv210-g2d",
862 .data = &g2d_drvdata_v3x,
863 }, {
864 .compatible = "samsung,exynos4212-g2d",
865 .data = &g2d_drvdata_v4x,
866 },
867 {},
868};
869MODULE_DEVICE_TABLE(of, exynos_g2d_match);
870
Sachin Kamat62ce2722013-01-17 00:07:18 -0300871static struct platform_device_id g2d_driver_ids[] = {
872 {
873 .name = "s5p-g2d",
874 .driver_data = (unsigned long)&g2d_drvdata_v3x,
875 }, {
876 .name = "s5p-g2d-v4x",
877 .driver_data = (unsigned long)&g2d_drvdata_v4x,
878 },
879 {},
880};
881MODULE_DEVICE_TABLE(platform, g2d_driver_ids);
882
Kamil Debski91884732011-10-06 11:32:12 -0300883static struct platform_driver g2d_pdrv = {
884 .probe = g2d_probe,
885 .remove = g2d_remove,
Sachin Kamat62ce2722013-01-17 00:07:18 -0300886 .id_table = g2d_driver_ids,
Kamil Debski91884732011-10-06 11:32:12 -0300887 .driver = {
888 .name = G2D_NAME,
889 .owner = THIS_MODULE,
Sachin Kamat5ce60d72013-02-06 01:29:43 -0300890 .of_match_table = exynos_g2d_match,
Kamil Debski91884732011-10-06 11:32:12 -0300891 },
892};
893
Axel Lin1d6629b2012-01-10 03:21:49 -0300894module_platform_driver(g2d_pdrv);
Kamil Debski91884732011-10-06 11:32:12 -0300895
896MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
897MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
898MODULE_LICENSE("GPL");