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