blob: e41357f6865dd8903931a255d058235b02f3129f [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
68struct g2d_frame def_frame = {
69 .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
80struct g2d_fmt *find_fmt(struct v4l2_format *f)
81{
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
154 memset(src_vq, 0, sizeof(*src_vq));
155 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
166 memset(dst_vq, 0, sizeof(*dst_vq));
167 dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
168 dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
169 dst_vq->drv_priv = ctx;
170 dst_vq->ops = &g2d_qops;
171 dst_vq->mem_ops = &vb2_dma_contig_memops;
172 dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
173
174 return vb2_queue_init(dst_vq);
175}
176
177static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
178{
179 struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
180 ctrl_handler);
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;
Kamil Debski7f6cce62012-01-02 09:19:25 -0300187 break;
Sachin Kamatd0d28322012-02-16 10:52:16 -0300188
189 case V4L2_CID_HFLIP:
190 ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1);
191 break;
192
Kamil Debski91884732011-10-06 11:32:12 -0300193 default:
194 v4l2_err(&ctx->dev->v4l2_dev, "unknown control\n");
195 return -EINVAL;
196 }
197 return 0;
198}
199
200static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
201 .s_ctrl = g2d_s_ctrl,
202};
203
204int g2d_setup_ctrls(struct g2d_ctx *ctx)
205{
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
250 ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
251 if (IS_ERR(ctx->m2m_ctx)) {
252 ret = PTR_ERR(ctx->m2m_ctx);
253 kfree(ctx);
254 return ret;
255 }
256 v4l2_fh_init(&ctx->fh, video_devdata(file));
257 file->private_data = &ctx->fh;
258 v4l2_fh_add(&ctx->fh);
259
260 g2d_setup_ctrls(ctx);
261
262 /* Write the default values to the ctx struct */
263 v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
264
265 ctx->fh.ctrl_handler = &ctx->ctrl_handler;
266
267 v4l2_info(&dev->v4l2_dev, "instance opened\n");
268 return 0;
269}
270
271static int g2d_release(struct file *file)
272{
273 struct g2d_dev *dev = video_drvdata(file);
274 struct g2d_ctx *ctx = fh2ctx(file->private_data);
275
276 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
277 v4l2_fh_del(&ctx->fh);
278 v4l2_fh_exit(&ctx->fh);
279 kfree(ctx);
280 v4l2_info(&dev->v4l2_dev, "instance closed\n");
281 return 0;
282}
283
284
285static int vidioc_querycap(struct file *file, void *priv,
286 struct v4l2_capability *cap)
287{
288 strncpy(cap->driver, G2D_NAME, sizeof(cap->driver) - 1);
289 strncpy(cap->card, G2D_NAME, sizeof(cap->card) - 1);
290 cap->bus_info[0] = 0;
291 cap->version = KERNEL_VERSION(1, 0, 0);
292 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
293 | V4L2_CAP_STREAMING;
294 return 0;
295}
296
297static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
298{
299 struct g2d_fmt *fmt;
300 if (f->index >= NUM_FORMATS)
301 return -EINVAL;
302 fmt = &formats[f->index];
303 f->pixelformat = fmt->fourcc;
304 strncpy(f->description, fmt->name, sizeof(f->description) - 1);
305 return 0;
306}
307
308static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
309{
310 struct g2d_ctx *ctx = prv;
311 struct vb2_queue *vq;
312 struct g2d_frame *frm;
313
314 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
315 if (!vq)
316 return -EINVAL;
317 frm = get_frame(ctx, f->type);
318 if (IS_ERR(frm))
319 return PTR_ERR(frm);
320
321 f->fmt.pix.width = frm->width;
322 f->fmt.pix.height = frm->height;
323 f->fmt.pix.field = V4L2_FIELD_NONE;
324 f->fmt.pix.pixelformat = frm->fmt->fourcc;
325 f->fmt.pix.bytesperline = (frm->width * frm->fmt->depth) >> 3;
326 f->fmt.pix.sizeimage = frm->size;
327 return 0;
328}
329
330static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
331{
332 struct g2d_fmt *fmt;
333 enum v4l2_field *field;
334
335 fmt = find_fmt(f);
336 if (!fmt)
337 return -EINVAL;
338
339 field = &f->fmt.pix.field;
340 if (*field == V4L2_FIELD_ANY)
341 *field = V4L2_FIELD_NONE;
342 else if (*field != V4L2_FIELD_NONE)
343 return -EINVAL;
344
345 if (f->fmt.pix.width > MAX_WIDTH)
346 f->fmt.pix.width = MAX_WIDTH;
347 if (f->fmt.pix.height > MAX_HEIGHT)
348 f->fmt.pix.height = MAX_HEIGHT;
349
350 if (f->fmt.pix.width < 1)
351 f->fmt.pix.width = 1;
352 if (f->fmt.pix.height < 1)
353 f->fmt.pix.height = 1;
354
355 f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
356 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
357 return 0;
358}
359
360static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
361{
362 struct g2d_ctx *ctx = prv;
363 struct g2d_dev *dev = ctx->dev;
364 struct vb2_queue *vq;
365 struct g2d_frame *frm;
366 struct g2d_fmt *fmt;
367 int ret = 0;
368
369 /* Adjust all values accordingly to the hardware capabilities
370 * and chosen format. */
371 ret = vidioc_try_fmt(file, prv, f);
372 if (ret)
373 return ret;
374 vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
375 if (vb2_is_busy(vq)) {
376 v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
377 return -EBUSY;
378 }
379 frm = get_frame(ctx, f->type);
380 if (IS_ERR(frm))
381 return PTR_ERR(frm);
382 fmt = find_fmt(f);
383 if (!fmt)
384 return -EINVAL;
385 frm->width = f->fmt.pix.width;
386 frm->height = f->fmt.pix.height;
387 frm->size = f->fmt.pix.sizeimage;
388 /* Reset crop settings */
389 frm->o_width = 0;
390 frm->o_height = 0;
391 frm->c_width = frm->width;
392 frm->c_height = frm->height;
393 frm->right = frm->width;
394 frm->bottom = frm->height;
395 frm->fmt = fmt;
396 frm->stride = f->fmt.pix.bytesperline;
397 return 0;
398}
399
400static unsigned int g2d_poll(struct file *file, struct poll_table_struct *wait)
401{
402 struct g2d_ctx *ctx = fh2ctx(file->private_data);
403 return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
404}
405
406static int g2d_mmap(struct file *file, struct vm_area_struct *vma)
407{
408 struct g2d_ctx *ctx = fh2ctx(file->private_data);
409 return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
410}
411
412static int vidioc_reqbufs(struct file *file, void *priv,
413 struct v4l2_requestbuffers *reqbufs)
414{
415 struct g2d_ctx *ctx = priv;
416 return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
417}
418
419static int vidioc_querybuf(struct file *file, void *priv,
420 struct v4l2_buffer *buf)
421{
422 struct g2d_ctx *ctx = priv;
423 return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
424}
425
426static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
427{
428 struct g2d_ctx *ctx = priv;
429 return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
430}
431
432static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
433{
434 struct g2d_ctx *ctx = priv;
435 return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
436}
437
438
439static int vidioc_streamon(struct file *file, void *priv,
440 enum v4l2_buf_type type)
441{
442 struct g2d_ctx *ctx = priv;
443 return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
444}
445
446static int vidioc_streamoff(struct file *file, void *priv,
447 enum v4l2_buf_type type)
448{
449 struct g2d_ctx *ctx = priv;
450 return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
451}
452
453static int vidioc_cropcap(struct file *file, void *priv,
454 struct v4l2_cropcap *cr)
455{
456 struct g2d_ctx *ctx = priv;
457 struct g2d_frame *f;
458
459 f = get_frame(ctx, cr->type);
460 if (IS_ERR(f))
461 return PTR_ERR(f);
462
463 cr->bounds.left = 0;
464 cr->bounds.top = 0;
465 cr->bounds.width = f->width;
466 cr->bounds.height = f->height;
467 cr->defrect = cr->bounds;
468 return 0;
469}
470
471static int vidioc_g_crop(struct file *file, void *prv, struct v4l2_crop *cr)
472{
473 struct g2d_ctx *ctx = prv;
474 struct g2d_frame *f;
475
476 f = get_frame(ctx, cr->type);
477 if (IS_ERR(f))
478 return PTR_ERR(f);
479
480 cr->c.left = f->o_height;
481 cr->c.top = f->o_width;
482 cr->c.width = f->c_width;
483 cr->c.height = f->c_height;
484 return 0;
485}
486
487static int vidioc_try_crop(struct file *file, void *prv, struct v4l2_crop *cr)
488{
489 struct g2d_ctx *ctx = prv;
490 struct g2d_dev *dev = ctx->dev;
491 struct g2d_frame *f;
492
493 f = get_frame(ctx, cr->type);
494 if (IS_ERR(f))
495 return PTR_ERR(f);
496
497 if (cr->c.top < 0 || cr->c.left < 0) {
498 v4l2_err(&dev->v4l2_dev,
499 "doesn't support negative values for top & left\n");
500 return -EINVAL;
501 }
502
503 return 0;
504}
505
506static int vidioc_s_crop(struct file *file, void *prv, struct v4l2_crop *cr)
507{
508 struct g2d_ctx *ctx = prv;
509 struct g2d_frame *f;
510 int ret;
511
512 ret = vidioc_try_crop(file, prv, cr);
513 if (ret)
514 return ret;
515 f = get_frame(ctx, cr->type);
516 if (IS_ERR(f))
517 return PTR_ERR(f);
518
519 f->c_width = cr->c.width;
520 f->c_height = cr->c.height;
521 f->o_width = cr->c.left;
522 f->o_height = cr->c.top;
523 f->bottom = f->o_height + f->c_height;
524 f->right = f->o_width + f->c_width;
525 return 0;
526}
527
528static void g2d_lock(void *prv)
529{
530 struct g2d_ctx *ctx = prv;
531 struct g2d_dev *dev = ctx->dev;
532 mutex_lock(&dev->mutex);
533}
534
535static void g2d_unlock(void *prv)
536{
537 struct g2d_ctx *ctx = prv;
538 struct g2d_dev *dev = ctx->dev;
539 mutex_unlock(&dev->mutex);
540}
541
542static void job_abort(void *prv)
543{
544 struct g2d_ctx *ctx = prv;
545 struct g2d_dev *dev = ctx->dev;
546 int ret;
547
548 if (dev->curr == 0) /* No job currently running */
549 return;
550
551 ret = wait_event_timeout(dev->irq_queue,
552 dev->curr == 0,
553 msecs_to_jiffies(G2D_TIMEOUT));
554}
555
556static void device_run(void *prv)
557{
558 struct g2d_ctx *ctx = prv;
559 struct g2d_dev *dev = ctx->dev;
560 struct vb2_buffer *src, *dst;
561 u32 cmd = 0;
562
563 dev->curr = ctx;
564
565 src = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
566 dst = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
567
568 clk_enable(dev->gate);
569 g2d_reset(dev);
570
571 g2d_set_src_size(dev, &ctx->in);
572 g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(src, 0));
573
574 g2d_set_dst_size(dev, &ctx->out);
575 g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(dst, 0));
576
577 g2d_set_rop4(dev, ctx->rop);
Sachin Kamatd0d28322012-02-16 10:52:16 -0300578 g2d_set_flip(dev, ctx->flip);
579
Kamil Debski91884732011-10-06 11:32:12 -0300580 if (ctx->in.c_width != ctx->out.c_width ||
581 ctx->in.c_height != ctx->out.c_height)
582 cmd |= g2d_cmd_stretch(1);
583 g2d_set_cmd(dev, cmd);
584 g2d_start(dev);
585}
586
587static irqreturn_t g2d_isr(int irq, void *prv)
588{
589 struct g2d_dev *dev = prv;
590 struct g2d_ctx *ctx = dev->curr;
591 struct vb2_buffer *src, *dst;
592
593 g2d_clear_int(dev);
594 clk_disable(dev->gate);
595
596 BUG_ON(ctx == 0);
597
598 src = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
599 dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
600
601 BUG_ON(src == 0);
602 BUG_ON(dst == 0);
603
604 v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
605 v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
606 v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
607
608 dev->curr = 0;
609 wake_up(&dev->irq_queue);
610 return IRQ_HANDLED;
611}
612
613static const struct v4l2_file_operations g2d_fops = {
614 .owner = THIS_MODULE,
615 .open = g2d_open,
616 .release = g2d_release,
617 .poll = g2d_poll,
618 .unlocked_ioctl = video_ioctl2,
619 .mmap = g2d_mmap,
620};
621
622static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
623 .vidioc_querycap = vidioc_querycap,
624
625 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
626 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
627 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
628 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
629
630 .vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
631 .vidioc_g_fmt_vid_out = vidioc_g_fmt,
632 .vidioc_try_fmt_vid_out = vidioc_try_fmt,
633 .vidioc_s_fmt_vid_out = vidioc_s_fmt,
634
635 .vidioc_reqbufs = vidioc_reqbufs,
636 .vidioc_querybuf = vidioc_querybuf,
637
638 .vidioc_qbuf = vidioc_qbuf,
639 .vidioc_dqbuf = vidioc_dqbuf,
640
641 .vidioc_streamon = vidioc_streamon,
642 .vidioc_streamoff = vidioc_streamoff,
643
644 .vidioc_g_crop = vidioc_g_crop,
645 .vidioc_s_crop = vidioc_s_crop,
646 .vidioc_cropcap = vidioc_cropcap,
647};
648
649static struct video_device g2d_videodev = {
650 .name = G2D_NAME,
651 .fops = &g2d_fops,
652 .ioctl_ops = &g2d_ioctl_ops,
653 .minor = -1,
654 .release = video_device_release,
655};
656
657static struct v4l2_m2m_ops g2d_m2m_ops = {
658 .device_run = device_run,
659 .job_abort = job_abort,
660 .lock = g2d_lock,
661 .unlock = g2d_unlock,
662};
663
664static int g2d_probe(struct platform_device *pdev)
665{
666 struct g2d_dev *dev;
667 struct video_device *vfd;
668 struct resource *res;
669 int ret = 0;
670
671 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
672 if (!dev)
673 return -ENOMEM;
674 spin_lock_init(&dev->irqlock);
675 mutex_init(&dev->mutex);
676 atomic_set(&dev->num_inst, 0);
677 init_waitqueue_head(&dev->irq_queue);
678
679 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
680 if (!res) {
681 dev_err(&pdev->dev, "failed to find registers\n");
682 ret = -ENOENT;
683 goto free_dev;
684 }
685
686 dev->res_regs = request_mem_region(res->start, resource_size(res),
687 dev_name(&pdev->dev));
688
689 if (!dev->res_regs) {
690 dev_err(&pdev->dev, "failed to obtain register region\n");
691 ret = -ENOENT;
692 goto free_dev;
693 }
694
695 dev->regs = ioremap(res->start, resource_size(res));
696 if (!dev->regs) {
697 dev_err(&pdev->dev, "failed to map registers\n");
698 ret = -ENOENT;
699 goto rel_res_regs;
700 }
701
702 dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
703 if (IS_ERR_OR_NULL(dev->clk)) {
704 dev_err(&pdev->dev, "failed to get g2d clock\n");
705 ret = -ENXIO;
706 goto unmap_regs;
707 }
708
Kamil Debski11a37c72012-02-16 10:51:28 -0300709 ret = clk_prepare(dev->clk);
710 if (ret) {
711 dev_err(&pdev->dev, "failed to prepare g2d clock\n");
712 goto put_clk;
713 }
714
Kamil Debski91884732011-10-06 11:32:12 -0300715 dev->gate = clk_get(&pdev->dev, "fimg2d");
716 if (IS_ERR_OR_NULL(dev->gate)) {
717 dev_err(&pdev->dev, "failed to get g2d clock gate\n");
718 ret = -ENXIO;
Kamil Debski11a37c72012-02-16 10:51:28 -0300719 goto unprep_clk;
720 }
721
722 ret = clk_prepare(dev->gate);
723 if (ret) {
724 dev_err(&pdev->dev, "failed to prepare g2d clock gate\n");
725 goto put_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300726 }
727
728 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
729 if (!res) {
730 dev_err(&pdev->dev, "failed to find IRQ\n");
731 ret = -ENXIO;
Kamil Debski11a37c72012-02-16 10:51:28 -0300732 goto unprep_clk_gate;
Kamil Debski91884732011-10-06 11:32:12 -0300733 }
734
735 dev->irq = res->start;
736
737 ret = request_irq(dev->irq, g2d_isr, 0, pdev->name, dev);
738 if (ret) {
739 dev_err(&pdev->dev, "failed to install IRQ\n");
740 goto put_clk_gate;
741 }
742
743 dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
744 if (IS_ERR(dev->alloc_ctx)) {
745 ret = PTR_ERR(dev->alloc_ctx);
746 goto rel_irq;
747 }
748
749 ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
750 if (ret)
751 goto alloc_ctx_cleanup;
752 vfd = video_device_alloc();
753 if (!vfd) {
754 v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
755 ret = -ENOMEM;
756 goto unreg_v4l2_dev;
757 }
758 *vfd = g2d_videodev;
759 vfd->lock = &dev->mutex;
760 ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
761 if (ret) {
762 v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
763 goto rel_vdev;
764 }
765 video_set_drvdata(vfd, dev);
766 snprintf(vfd->name, sizeof(vfd->name), "%s", g2d_videodev.name);
767 dev->vfd = vfd;
768 v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
769 vfd->num);
770 platform_set_drvdata(pdev, dev);
771 dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
772 if (IS_ERR(dev->m2m_dev)) {
773 v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
774 ret = PTR_ERR(dev->m2m_dev);
775 goto unreg_video_dev;
776 }
777
778 def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
779
780 return 0;
781
782unreg_video_dev:
783 video_unregister_device(dev->vfd);
784rel_vdev:
785 video_device_release(vfd);
786unreg_v4l2_dev:
787 v4l2_device_unregister(&dev->v4l2_dev);
788alloc_ctx_cleanup:
789 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
790rel_irq:
791 free_irq(dev->irq, dev);
Kamil Debski11a37c72012-02-16 10:51:28 -0300792unprep_clk_gate:
793 clk_unprepare(dev->gate);
Kamil Debski91884732011-10-06 11:32:12 -0300794put_clk_gate:
795 clk_put(dev->gate);
Kamil Debski11a37c72012-02-16 10:51:28 -0300796unprep_clk:
797 clk_unprepare(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300798put_clk:
799 clk_put(dev->clk);
800unmap_regs:
801 iounmap(dev->regs);
802rel_res_regs:
803 release_resource(dev->res_regs);
804free_dev:
805 kfree(dev);
806 return ret;
807}
808
809static int g2d_remove(struct platform_device *pdev)
810{
811 struct g2d_dev *dev = (struct g2d_dev *)platform_get_drvdata(pdev);
812
813 v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
814 v4l2_m2m_release(dev->m2m_dev);
815 video_unregister_device(dev->vfd);
816 v4l2_device_unregister(&dev->v4l2_dev);
817 vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
818 free_irq(dev->irq, dev);
Kamil Debski11a37c72012-02-16 10:51:28 -0300819 clk_unprepare(dev->gate);
Kamil Debski91884732011-10-06 11:32:12 -0300820 clk_put(dev->gate);
Kamil Debski11a37c72012-02-16 10:51:28 -0300821 clk_unprepare(dev->clk);
Kamil Debski91884732011-10-06 11:32:12 -0300822 clk_put(dev->clk);
823 iounmap(dev->regs);
824 release_resource(dev->res_regs);
825 kfree(dev);
826 return 0;
827}
828
829static struct platform_driver g2d_pdrv = {
830 .probe = g2d_probe,
831 .remove = g2d_remove,
832 .driver = {
833 .name = G2D_NAME,
834 .owner = THIS_MODULE,
835 },
836};
837
Axel Lin1d6629b2012-01-10 03:21:49 -0300838module_platform_driver(g2d_pdrv);
Kamil Debski91884732011-10-06 11:32:12 -0300839
840MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
841MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
842MODULE_LICENSE("GPL");