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