blob: aaaf276a5a6cc3ed823dae30005fa45dbc0984fc [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>
21
22#include <linux/platform_device.h>
23#include <media/v4l2-mem2mem.h>
24#include <media/v4l2-device.h>
25#include <media/v4l2-ioctl.h>
26#include <media/videobuf2-core.h>
27#include <media/videobuf2-dma-contig.h>
28
29#include "g2d.h"
30#include "g2d-regs.h"
31
32#define fh2ctx(__fh) container_of(__fh, struct g2d_ctx, fh)
33
34static struct g2d_fmt formats[] = {
35 {
36 .name = "XRGB_8888",
37 .fourcc = V4L2_PIX_FMT_RGB32,
38 .depth = 32,
39 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
40 },
41 {
42 .name = "RGB_565",
43 .fourcc = V4L2_PIX_FMT_RGB565X,
44 .depth = 16,
45 .hw = COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
46 },
47 {
48 .name = "XRGB_1555",
49 .fourcc = V4L2_PIX_FMT_RGB555X,
50 .depth = 16,
51 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
52 },
53 {
54 .name = "XRGB_4444",
55 .fourcc = V4L2_PIX_FMT_RGB444,
56 .depth = 16,
57 .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
58 },
59 {
60 .name = "PACKED_RGB_888",
61 .fourcc = V4L2_PIX_FMT_RGB24,
62 .depth = 24,
63 .hw = COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
64 },
65};
66#define NUM_FORMATS ARRAY_SIZE(formats)
67
Sachin Kamatec76afe2012-05-10 03:35:48 -030068static struct g2d_frame def_frame = {
Kamil Debski91884732011-10-06 11:32:12 -030069 .width = DEFAULT_WIDTH,
70 .height = DEFAULT_HEIGHT,
71 .c_width = DEFAULT_WIDTH,
72 .c_height = DEFAULT_HEIGHT,
73 .o_width = 0,
74 .o_height = 0,
75 .fmt = &formats[0],
76 .right = DEFAULT_WIDTH,
77 .bottom = DEFAULT_HEIGHT,
78};
79
Sachin Kamatec76afe2012-05-10 03:35:48 -030080static struct g2d_fmt *find_fmt(struct v4l2_format *f)
Kamil Debski91884732011-10-06 11:32:12 -030081{
82 unsigned int i;
83 for (i = 0; i < NUM_FORMATS; i++) {
84 if (formats[i].fourcc == f->fmt.pix.pixelformat)
85 return &formats[i];
86 }
87 return NULL;
88}
89
90
91static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
92 enum v4l2_buf_type type)
93{
94 switch (type) {
95 case V4L2_BUF_TYPE_VIDEO_OUTPUT:
96 return &ctx->in;
97 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
98 return &ctx->out;
99 default:
100 return ERR_PTR(-EINVAL);
101 }
102}
103
104static int g2d_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
105 unsigned int *nbuffers, unsigned int *nplanes,
106 unsigned int sizes[], void *alloc_ctxs[])
107{
108 struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
109 struct g2d_frame *f = get_frame(ctx, vq->type);
110
111 if (IS_ERR(f))
112 return PTR_ERR(f);
113
114 sizes[0] = f->size;
115 *nplanes = 1;
116 alloc_ctxs[0] = ctx->dev->alloc_ctx;
117
118 if (*nbuffers == 0)
119 *nbuffers = 1;
120
121 return 0;
122}
123
124static int g2d_buf_prepare(struct vb2_buffer *vb)
125{
126 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
127 struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
128
129 if (IS_ERR(f))
130 return PTR_ERR(f);
131 vb2_set_plane_payload(vb, 0, f->size);
132 return 0;
133}
134
135static void g2d_buf_queue(struct vb2_buffer *vb)
136{
137 struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
138 v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
139}
140
141
142static struct vb2_ops g2d_qops = {
143 .queue_setup = g2d_queue_setup,
144 .buf_prepare = g2d_buf_prepare,
145 .buf_queue = g2d_buf_queue,
146};
147
148static int queue_init(void *priv, struct vb2_queue *src_vq,
149 struct vb2_queue *dst_vq)
150{
151 struct g2d_ctx *ctx = priv;
152 int ret;
153
Kamil Debski91884732011-10-06 11:32:12 -0300154 src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
155 src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
156 src_vq->drv_priv = ctx;
157 src_vq->ops = &g2d_qops;
158 src_vq->mem_ops = &vb2_dma_contig_memops;
159 src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
160
161 ret = vb2_queue_init(src_vq);
162 if (ret)
163 return ret;
164
Kamil Debski91884732011-10-06 11:32:12 -0300165 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);
171
172 return vb2_queue_init(dst_vq);
173}
174
175static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
176{
177 struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
178 ctrl_handler);
Kamil Debski27dda972012-02-16 10:52:47 -0300179 unsigned long flags;
180
181 spin_lock_irqsave(&ctx->dev->ctrl_lock, flags);
Kamil Debski91884732011-10-06 11:32:12 -0300182 switch (ctrl->id) {
183 case V4L2_CID_COLORFX:
184 if (ctrl->val == V4L2_COLORFX_NEGATIVE)
185 ctx->rop = ROP4_INVERT;
186 else
187 ctx->rop = ROP4_COPY;
Kamil Debski7f6cce62012-01-02 09:19:25 -0300188 break;
Sachin Kamatd0d28322012-02-16 10:52:16 -0300189
190 case V4L2_CID_HFLIP:
191 ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
192 break;
193
Kamil Debski91884732011-10-06 11:32:12 -0300194 }
Kamil Debski27dda972012-02-16 10:52:47 -0300195 spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags);
Kamil Debski91884732011-10-06 11:32:12 -0300196 return 0;
197}
198
199static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
200 .s_ctrl = g2d_s_ctrl,
201};
202
Sachin Kamatec76afe2012-05-10 03:35:48 -0300203static int g2d_setup_ctrls(struct g2d_ctx *ctx)
Kamil Debski91884732011-10-06 11:32:12 -0300204{
205 struct g2d_dev *dev = ctx->dev;
206
Sachin Kamatd0d28322012-02-16 10:52:16 -0300207 v4l2_ctrl_handler_init(&ctx->ctrl_handler, 3);
208
209 ctx->ctrl_hflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
210 V4L2_CID_HFLIP, 0, 1, 1, 0);
211
212 ctx->ctrl_vflip = v4l2_ctrl_new_std(&ctx->ctrl_handler, &g2d_ctrl_ops,
213 V4L2_CID_VFLIP, 0, 1, 1, 0);
Kamil Debski91884732011-10-06 11:32:12 -0300214
215 v4l2_ctrl_new_std_menu(
216 &ctx->ctrl_handler,
217 &g2d_ctrl_ops,
218 V4L2_CID_COLORFX,
219 V4L2_COLORFX_NEGATIVE,
220 ~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
221 V4L2_COLORFX_NONE);
222
223 if (ctx->ctrl_handler.error) {
Sachin Kamatd0d28322012-02-16 10:52:16 -0300224 int err = ctx->ctrl_handler.error;
225 v4l2_err(&dev->v4l2_dev, "g2d_setup_ctrls failed\n");
226 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
227 return err;
Kamil Debski91884732011-10-06 11:32:12 -0300228 }
229
Sachin Kamatd0d28322012-02-16 10:52:16 -0300230 v4l2_ctrl_cluster(2, &ctx->ctrl_hflip);
231
Kamil Debski91884732011-10-06 11:32:12 -0300232 return 0;
233}
234
235static int g2d_open(struct file *file)
236{
237 struct g2d_dev *dev = video_drvdata(file);
238 struct g2d_ctx *ctx = NULL;
239 int ret = 0;
240
241 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
242 if (!ctx)
243 return -ENOMEM;
244 ctx->dev = dev;
245 /* Set default formats */
246 ctx->in = def_frame;
247 ctx->out = def_frame;
248
Hans Verkuilde40cb22012-06-24 06:58:28 -0300249 if (mutex_lock_interruptible(&dev->mutex)) {
250 kfree(ctx);
251 return -ERESTARTSYS;
252 }
Kamil Debski91884732011-10-06 11:32:12 -0300253 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
254 if (IS_ERR(ctx->m2m_ctx)) {
255 ret = PTR_ERR(ctx->m2m_ctx);
Hans Verkuilde40cb22012-06-24 06:58:28 -0300256 mutex_unlock(&dev->mutex);
Kamil Debski91884732011-10-06 11:32:12 -0300257 kfree(ctx);
258 return ret;
259 }
260 v4l2_fh_init(&ctx->fh, video_devdata(file));
261 file->private_data = &ctx->fh;
262 v4l2_fh_add(&ctx->fh);
263
264 g2d_setup_ctrls(ctx);
265
266 /* Write the default values to the ctx struct */
267 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
268
269 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
Hans Verkuilde40cb22012-06-24 06:58:28 -0300270 mutex_unlock(&dev->mutex);
Kamil Debski91884732011-10-06 11:32:12 -0300271
272 v4l2_info(&dev->v4l2_dev, "instance opened\n");
273 return 0;
274}
275
276static int g2d_release(struct file *file)
277{
278 struct g2d_dev *dev = video_drvdata(file);
279 struct g2d_ctx *ctx = fh2ctx(file->private_data);
280
281 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
282 v4l2_fh_del(&ctx->fh);
283 v4l2_fh_exit(&ctx->fh);
284 kfree(ctx);
285 v4l2_info(&dev->v4l2_dev, "instance closed\n");
286 return 0;
287}
288
289
290static int vidioc_querycap(struct file *file, void *priv,
291 struct v4l2_capability *cap)
292{
293 strncpy(cap->driver, G2D_NAME, sizeof(cap->driver) - 1);
294 strncpy(cap->card, G2D_NAME, sizeof(cap->card) - 1);
295 cap->bus_info[0] = 0;
296 cap->version = KERNEL_VERSION(1, 0, 0);
Sylwester Nawrockif0476a82012-07-26 09:30:00 -0300297 /*
298 * This is only a mem-to-mem video device. The capture and output
299 * device capability flags are left only for backward compatibility
300 * and are scheduled for removal.
301 */
302 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
303 V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING;
Kamil Debski91884732011-10-06 11:32:12 -0300304 return 0;
305}
306
307static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
308{
309 struct g2d_fmt *fmt;
310 if (f->index >= NUM_FORMATS)
311 return -EINVAL;
312 fmt = &formats[f->index];
313 f->pixelformat = fmt->fourcc;
314 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
315 return 0;
316}
317
318static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
319{
320 struct g2d_ctx *ctx = prv;
321 struct vb2_queue *vq;
322 struct g2d_frame *frm;
323
324 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
325 if (!vq)
326 return -EINVAL;
327 frm = get_frame(ctx, f->type);
328 if (IS_ERR(frm))
329 return PTR_ERR(frm);
330
331 f->fmt.pix.width = frm->width;
332 f->fmt.pix.height = frm->height;
333 f->fmt.pix.field = V4L2_FIELD_NONE;
334 f->fmt.pix.pixelformat = frm->fmt->fourcc;
335 f->fmt.pix.bytesperline = (frm->width * frm->fmt->depth) >> 3;
336 f->fmt.pix.sizeimage = frm->size;
337 return 0;
338}
339
340static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
341{
342 struct g2d_fmt *fmt;
343 enum v4l2_field *field;
344
345 fmt = find_fmt(f);
346 if (!fmt)
347 return -EINVAL;
348
349 field = &f->fmt.pix.field;
350 if (*field == V4L2_FIELD_ANY)
351 *field = V4L2_FIELD_NONE;
352 else if (*field != V4L2_FIELD_NONE)
353 return -EINVAL;
354
355 if (f->fmt.pix.width > MAX_WIDTH)
356 f->fmt.pix.width = MAX_WIDTH;
357 if (f->fmt.pix.height > MAX_HEIGHT)
358 f->fmt.pix.height = MAX_HEIGHT;
359
360 if (f->fmt.pix.width < 1)
361 f->fmt.pix.width = 1;
362 if (f->fmt.pix.height < 1)
363 f->fmt.pix.height = 1;
364
365 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
366 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
367 return 0;
368}
369
370static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
371{
372 struct g2d_ctx *ctx = prv;
373 struct g2d_dev *dev = ctx->dev;
374 struct vb2_queue *vq;
375 struct g2d_frame *frm;
376 struct g2d_fmt *fmt;
377 int ret = 0;
378
379 /* Adjust all values accordingly to the hardware capabilities
380 * and chosen format. */
381 ret = vidioc_try_fmt(file, prv, f);
382 if (ret)
383 return ret;
384 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
385 if (vb2_is_busy(vq)) {
386 v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
387 return -EBUSY;
388 }
389 frm = get_frame(ctx, f->type);
390 if (IS_ERR(frm))
391 return PTR_ERR(frm);
392 fmt = find_fmt(f);
393 if (!fmt)
394 return -EINVAL;
395 frm->width = f->fmt.pix.width;
396 frm->height = f->fmt.pix.height;
397 frm->size = f->fmt.pix.sizeimage;
398 /* Reset crop settings */
399 frm->o_width = 0;
400 frm->o_height = 0;
401 frm->c_width = frm->width;
402 frm->c_height = frm->height;
403 frm->right = frm->width;
404 frm->bottom = frm->height;
405 frm->fmt = fmt;
406 frm->stride = f->fmt.pix.bytesperline;
407 return 0;
408}
409
410static unsigned int g2d_poll(struct file *file, struct poll_table_struct *wait)
411{
412 struct g2d_ctx *ctx = fh2ctx(file->private_data);
Hans Verkuilde40cb22012-06-24 06:58:28 -0300413 struct g2d_dev *dev = ctx->dev;
414 unsigned int res;
415
416 mutex_lock(&dev->mutex);
417 res = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
418 mutex_unlock(&dev->mutex);
419 return res;
Kamil Debski91884732011-10-06 11:32:12 -0300420}
421
422static int g2d_mmap(struct file *file, struct vm_area_struct *vma)
423{
424 struct g2d_ctx *ctx = fh2ctx(file->private_data);
Hans Verkuilde40cb22012-06-24 06:58:28 -0300425 struct g2d_dev *dev = ctx->dev;
426 int ret;
427
428 if (mutex_lock_interruptible(&dev->mutex))
429 return -ERESTARTSYS;
430 ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
431 mutex_unlock(&dev->mutex);
432 return ret;
Kamil Debski91884732011-10-06 11:32:12 -0300433}
434
435static int vidioc_reqbufs(struct file *file, void *priv,
436 struct v4l2_requestbuffers *reqbufs)
437{
438 struct g2d_ctx *ctx = priv;
439 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
440}
441
442static int vidioc_querybuf(struct file *file, void *priv,
443 struct v4l2_buffer *buf)
444{
445 struct g2d_ctx *ctx = priv;
446 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
447}
448
449static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
450{
451 struct g2d_ctx *ctx = priv;
452 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
453}
454
455static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
456{
457 struct g2d_ctx *ctx = priv;
458 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
459}
460
461
462static int vidioc_streamon(struct file *file, void *priv,
463 enum v4l2_buf_type type)
464{
465 struct g2d_ctx *ctx = priv;
466 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
467}
468
469static int vidioc_streamoff(struct file *file, void *priv,
470 enum v4l2_buf_type type)
471{
472 struct g2d_ctx *ctx = priv;
473 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
474}
475
476static int vidioc_cropcap(struct file *file, void *priv,
477 struct v4l2_cropcap *cr)
478{
479 struct g2d_ctx *ctx = priv;
480 struct g2d_frame *f;
481
482 f = get_frame(ctx, cr->type);
483 if (IS_ERR(f))
484 return PTR_ERR(f);
485
486 cr->bounds.left = 0;
487 cr->bounds.top = 0;
488 cr->bounds.width = f->width;
489 cr->bounds.height = f->height;
490 cr->defrect = cr->bounds;
491 return 0;
492}
493
494static int vidioc_g_crop(struct file *file, void *prv, struct v4l2_crop *cr)
495{
496 struct g2d_ctx *ctx = prv;
497 struct g2d_frame *f;
498
499 f = get_frame(ctx, cr->type);
500 if (IS_ERR(f))
501 return PTR_ERR(f);
502
503 cr->c.left = f->o_height;
504 cr->c.top = f->o_width;
505 cr->c.width = f->c_width;
506 cr->c.height = f->c_height;
507 return 0;
508}
509
Hans Verkuil21ec9c92012-09-05 05:10:48 -0300510static int vidioc_try_crop(struct file *file, void *prv, const struct v4l2_crop *cr)
Kamil Debski91884732011-10-06 11:32:12 -0300511{
512 struct g2d_ctx *ctx = prv;
513 struct g2d_dev *dev = ctx->dev;
514 struct g2d_frame *f;
515
516 f = get_frame(ctx, cr->type);
517 if (IS_ERR(f))
518 return PTR_ERR(f);
519
520 if (cr->c.top < 0 || cr->c.left < 0) {
521 v4l2_err(&dev->v4l2_dev,
522 "doesn't support negative values for top & left\n");
523 return -EINVAL;
524 }
525
526 return 0;
527}
528
Hans Verkuil4f996592012-09-05 05:10:48 -0300529static int vidioc_s_crop(struct file *file, void *prv, const struct v4l2_crop *cr)
Kamil Debski91884732011-10-06 11:32:12 -0300530{
531 struct g2d_ctx *ctx = prv;
532 struct g2d_frame *f;
533 int ret;
534
535 ret = vidioc_try_crop(file, prv, cr);
536 if (ret)
537 return ret;
538 f = get_frame(ctx, cr->type);
539 if (IS_ERR(f))
540 return PTR_ERR(f);
541
542 f->c_width = cr->c.width;
543 f->c_height = cr->c.height;
544 f->o_width = cr->c.left;
545 f->o_height = cr->c.top;
546 f->bottom = f->o_height + f->c_height;
547 f->right = f->o_width + f->c_width;
548 return 0;
549}
550
551static void g2d_lock(void *prv)
552{
553 struct g2d_ctx *ctx = prv;
554 struct g2d_dev *dev = ctx->dev;
555 mutex_lock(&dev->mutex);
556}
557
558static void g2d_unlock(void *prv)
559{
560 struct g2d_ctx *ctx = prv;
561 struct g2d_dev *dev = ctx->dev;
562 mutex_unlock(&dev->mutex);
563}
564
565static void job_abort(void *prv)
566{
567 struct g2d_ctx *ctx = prv;
568 struct g2d_dev *dev = ctx->dev;
569 int ret;
570
Sachin Kamat23568772012-05-10 03:35:47 -0300571 if (dev->curr == NULL) /* No job currently running */
Kamil Debski91884732011-10-06 11:32:12 -0300572 return;
573
574 ret = wait_event_timeout(dev->irq_queue,
Sachin Kamat23568772012-05-10 03:35:47 -0300575 dev->curr == NULL,
Kamil Debski91884732011-10-06 11:32:12 -0300576 msecs_to_jiffies(G2D_TIMEOUT));
577}
578
579static void device_run(void *prv)
580{
581 struct g2d_ctx *ctx = prv;
582 struct g2d_dev *dev = ctx->dev;
583 struct vb2_buffer *src, *dst;
Kamil Debski27dda972012-02-16 10:52:47 -0300584 unsigned long flags;
Kamil Debski91884732011-10-06 11:32:12 -0300585 u32 cmd = 0;
586
587 dev->curr = ctx;
588
589 src = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
590 dst = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
591
592 clk_enable(dev->gate);
593 g2d_reset(dev);
594
Kamil Debski27dda972012-02-16 10:52:47 -0300595 spin_lock_irqsave(&dev->ctrl_lock, flags);
596
Kamil Debski91884732011-10-06 11:32:12 -0300597 g2d_set_src_size(dev, &ctx->in);
598 g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(src, 0));
599
600 g2d_set_dst_size(dev, &ctx->out);
601 g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(dst, 0));
602
603 g2d_set_rop4(dev, ctx->rop);
Sachin Kamatd0d28322012-02-16 10:52:16 -0300604 g2d_set_flip(dev, ctx->flip);
605
Kamil Debski91884732011-10-06 11:32:12 -0300606 if (ctx->in.c_width != ctx->out.c_width ||
Sachin Kamat62ce2722013-01-17 00:07:18 -0300607 ctx->in.c_height != ctx->out.c_height) {
608 if (dev->variant->hw_rev == TYPE_G2D_3X)
609 cmd |= CMD_V3_ENABLE_STRETCH;
610 else
611 g2d_set_v41_stretch(dev, &ctx->in, &ctx->out);
612 }
613
Kamil Debski91884732011-10-06 11:32:12 -0300614 g2d_set_cmd(dev, cmd);
615 g2d_start(dev);
Kamil Debski27dda972012-02-16 10:52:47 -0300616
617 spin_unlock_irqrestore(&dev->ctrl_lock, flags);
Kamil Debski91884732011-10-06 11:32:12 -0300618}
619
620static irqreturn_t g2d_isr(int irq, void *prv)
621{
622 struct g2d_dev *dev = prv;
623 struct g2d_ctx *ctx = dev->curr;
624 struct vb2_buffer *src, *dst;
625
626 g2d_clear_int(dev);
627 clk_disable(dev->gate);
628
Sachin Kamat23568772012-05-10 03:35:47 -0300629 BUG_ON(ctx == NULL);
Kamil Debski91884732011-10-06 11:32:12 -0300630
631 src = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
632 dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
633
Sachin Kamat23568772012-05-10 03:35:47 -0300634 BUG_ON(src == NULL);
635 BUG_ON(dst == NULL);
Kamil Debski91884732011-10-06 11:32:12 -0300636
637 v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
638 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
639 v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
640
Sachin Kamat23568772012-05-10 03:35:47 -0300641 dev->curr = NULL;
Kamil Debski91884732011-10-06 11:32:12 -0300642 wake_up(&dev->irq_queue);
643 return IRQ_HANDLED;
644}
645
646static const struct v4l2_file_operations g2d_fops = {
647 .owner = THIS_MODULE,
648 .open = g2d_open,
649 .release = g2d_release,
650 .poll = g2d_poll,
651 .unlocked_ioctl = video_ioctl2,
652 .mmap = g2d_mmap,
653};
654
655static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
656 .vidioc_querycap = vidioc_querycap,
657
658 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
659 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
660 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
661 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
662
663 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
664 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
665 .vidioc_try_fmt_vid_out = vidioc_try_fmt,
666 .vidioc_s_fmt_vid_out = vidioc_s_fmt,
667
668 .vidioc_reqbufs = vidioc_reqbufs,
669 .vidioc_querybuf = vidioc_querybuf,
670
671 .vidioc_qbuf = vidioc_qbuf,
672 .vidioc_dqbuf = vidioc_dqbuf,
673
674 .vidioc_streamon = vidioc_streamon,
675 .vidioc_streamoff = vidioc_streamoff,
676
677 .vidioc_g_crop = vidioc_g_crop,
678 .vidioc_s_crop = vidioc_s_crop,
679 .vidioc_cropcap = vidioc_cropcap,
680};
681
682static struct video_device g2d_videodev = {
683 .name = G2D_NAME,
684 .fops = &g2d_fops,
685 .ioctl_ops = &g2d_ioctl_ops,
686 .minor = -1,
687 .release = video_device_release,
Hans Verkuil954f3402012-09-05 06:05:50 -0300688 .vfl_dir = VFL_DIR_M2M,
Kamil Debski91884732011-10-06 11:32:12 -0300689};
690
691static struct v4l2_m2m_ops g2d_m2m_ops = {
692 .device_run = device_run,
693 .job_abort = job_abort,
694 .lock = g2d_lock,
695 .unlock = g2d_unlock,
696};
697
698static int g2d_probe(struct platform_device *pdev)
699{
700 struct g2d_dev *dev;
701 struct video_device *vfd;
702 struct resource *res;
703 int ret = 0;
704
Sachin Kamat32fced02012-05-14 06:31:24 -0300705 dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
Kamil Debski91884732011-10-06 11:32:12 -0300706 if (!dev)
707 return -ENOMEM;
Sachin Kamat32fced02012-05-14 06:31:24 -0300708
Kamil Debski27dda972012-02-16 10:52:47 -0300709 spin_lock_init(&dev->ctrl_lock);
Kamil Debski91884732011-10-06 11:32:12 -0300710 mutex_init(&dev->mutex);
711 atomic_set(&dev->num_inst, 0);
712 init_waitqueue_head(&dev->irq_queue);
713
714 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kamil Debski91884732011-10-06 11:32:12 -0300715
Thierry Redingf23999e2013-01-21 06:09:07 -0300716 dev->regs = devm_ioremap_resource(&pdev->dev, res);
717 if (IS_ERR(dev->regs))
718 return PTR_ERR(dev->regs);
Kamil Debski91884732011-10-06 11:32:12 -0300719
720 dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
Tony Prisk15514fb2012-12-18 05:28:41 -0300721 if (IS_ERR(dev->clk)) {
Kamil Debski91884732011-10-06 11:32:12 -0300722 dev_err(&pdev->dev, "failed to get g2d clock\n");
Sachin Kamat32fced02012-05-14 06:31:24 -0300723 return -ENXIO;
Kamil Debski91884732011-10-06 11:32:12 -0300724 }
725
Kamil Debski11a37c72012-02-16 10:51:28 -0300726 ret = clk_prepare(dev->clk);
727 if (ret) {
728 dev_err(&pdev->dev, "failed to prepare g2d clock\n");
729 goto put_clk;
730 }
731
Kamil Debski91884732011-10-06 11:32:12 -0300732 dev->gate = clk_get(&pdev->dev, "fimg2d");
Tony Prisk15514fb2012-12-18 05:28:41 -0300733 if (IS_ERR(dev->gate)) {
Kamil Debski91884732011-10-06 11:32:12 -0300734 dev_err(&pdev->dev, "failed to get g2d clock gate\n");
735 ret = -ENXIO;
Kamil Debski11a37c72012-02-16 10:51:28 -0300736 goto unprep_clk;
737 }
738
739 ret = clk_prepare(dev->gate);
740 if (ret) {
741 dev_err(&pdev->dev, "failed to prepare g2d clock gate\n");
742 goto put_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300743 }
744
745 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
746 if (!res) {
747 dev_err(&pdev->dev, "failed to find IRQ\n");
748 ret = -ENXIO;
Kamil Debski11a37c72012-02-16 10:51:28 -0300749 goto unprep_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300750 }
751
752 dev->irq = res->start;
753
Sachin Kamat32fced02012-05-14 06:31:24 -0300754 ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr,
755 0, pdev->name, dev);
Kamil Debski91884732011-10-06 11:32:12 -0300756 if (ret) {
757 dev_err(&pdev->dev, "failed to install IRQ\n");
758 goto put_clk_gate;
759 }
760
761 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
762 if (IS_ERR(dev->alloc_ctx)) {
763 ret = PTR_ERR(dev->alloc_ctx);
Sachin Kamat32fced02012-05-14 06:31:24 -0300764 goto unprep_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300765 }
766
767 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
768 if (ret)
769 goto alloc_ctx_cleanup;
770 vfd = video_device_alloc();
771 if (!vfd) {
772 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
773 ret = -ENOMEM;
774 goto unreg_v4l2_dev;
775 }
776 *vfd = g2d_videodev;
777 vfd->lock = &dev->mutex;
778 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
779 if (ret) {
780 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
781 goto rel_vdev;
782 }
783 video_set_drvdata(vfd, dev);
784 snprintf(vfd->name, sizeof(vfd->name), "%s", g2d_videodev.name);
785 dev->vfd = vfd;
786 v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
787 vfd->num);
788 platform_set_drvdata(pdev, dev);
789 dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
790 if (IS_ERR(dev->m2m_dev)) {
791 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
792 ret = PTR_ERR(dev->m2m_dev);
793 goto unreg_video_dev;
794 }
795
796 def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
Sachin Kamat62ce2722013-01-17 00:07:18 -0300797 dev->variant = g2d_get_drv_data(pdev);
Kamil Debski91884732011-10-06 11:32:12 -0300798
799 return 0;
800
801unreg_video_dev:
802 video_unregister_device(dev->vfd);
803rel_vdev:
804 video_device_release(vfd);
805unreg_v4l2_dev:
806 v4l2_device_unregister(&dev->v4l2_dev);
807alloc_ctx_cleanup:
808 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
Kamil Debski11a37c72012-02-16 10:51:28 -0300809unprep_clk_gate:
810 clk_unprepare(dev->gate);
Kamil Debski91884732011-10-06 11:32:12 -0300811put_clk_gate:
812 clk_put(dev->gate);
Kamil Debski11a37c72012-02-16 10:51:28 -0300813unprep_clk:
814 clk_unprepare(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300815put_clk:
816 clk_put(dev->clk);
Sachin Kamat32fced02012-05-14 06:31:24 -0300817
Kamil Debski91884732011-10-06 11:32:12 -0300818 return ret;
819}
820
821static int g2d_remove(struct platform_device *pdev)
822{
823 struct g2d_dev *dev = (struct g2d_dev *)platform_get_drvdata(pdev);
824
825 v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
826 v4l2_m2m_release(dev->m2m_dev);
827 video_unregister_device(dev->vfd);
828 v4l2_device_unregister(&dev->v4l2_dev);
829 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
Kamil Debski11a37c72012-02-16 10:51:28 -0300830 clk_unprepare(dev->gate);
Kamil Debski91884732011-10-06 11:32:12 -0300831 clk_put(dev->gate);
Kamil Debski11a37c72012-02-16 10:51:28 -0300832 clk_unprepare(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300833 clk_put(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300834 return 0;
835}
836
Sachin Kamat62ce2722013-01-17 00:07:18 -0300837static struct g2d_variant g2d_drvdata_v3x = {
838 .hw_rev = TYPE_G2D_3X,
839};
840
841static struct g2d_variant g2d_drvdata_v4x = {
842 .hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */
843};
844
845static struct platform_device_id g2d_driver_ids[] = {
846 {
847 .name = "s5p-g2d",
848 .driver_data = (unsigned long)&g2d_drvdata_v3x,
849 }, {
850 .name = "s5p-g2d-v4x",
851 .driver_data = (unsigned long)&g2d_drvdata_v4x,
852 },
853 {},
854};
855MODULE_DEVICE_TABLE(platform, g2d_driver_ids);
856
Kamil Debski91884732011-10-06 11:32:12 -0300857static struct platform_driver g2d_pdrv = {
858 .probe = g2d_probe,
859 .remove = g2d_remove,
Sachin Kamat62ce2722013-01-17 00:07:18 -0300860 .id_table = g2d_driver_ids,
Kamil Debski91884732011-10-06 11:32:12 -0300861 .driver = {
862 .name = G2D_NAME,
863 .owner = THIS_MODULE,
864 },
865};
866
Axel Lin1d6629b2012-01-10 03:21:49 -0300867module_platform_driver(g2d_pdrv);
Kamil Debski91884732011-10-06 11:32:12 -0300868
869MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
870MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
871MODULE_LICENSE("GPL");