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> | |
5ce60d79 | 21 | #include <linux/of.h> |
91884734 KD |
22 | |
23 | #include <linux/platform_device.h> | |
24 | #include <media/v4l2-mem2mem.h> | |
25 | #include <media/v4l2-device.h> | |
26 | #include <media/v4l2-ioctl.h> | |
27 | #include <media/videobuf2-core.h> | |
28 | #include <media/videobuf2-dma-contig.h> | |
29 | ||
30 | #include "g2d.h" | |
31 | #include "g2d-regs.h" | |
32 | ||
33 | #define fh2ctx(__fh) container_of(__fh, struct g2d_ctx, fh) | |
34 | ||
35 | static struct g2d_fmt formats[] = { | |
36 | { | |
37 | .name = "XRGB_8888", | |
38 | .fourcc = V4L2_PIX_FMT_RGB32, | |
39 | .depth = 32, | |
40 | .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888), | |
41 | }, | |
42 | { | |
43 | .name = "RGB_565", | |
44 | .fourcc = V4L2_PIX_FMT_RGB565X, | |
45 | .depth = 16, | |
46 | .hw = COLOR_MODE(ORDER_XRGB, MODE_RGB_565), | |
47 | }, | |
48 | { | |
49 | .name = "XRGB_1555", | |
50 | .fourcc = V4L2_PIX_FMT_RGB555X, | |
51 | .depth = 16, | |
52 | .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555), | |
53 | }, | |
54 | { | |
55 | .name = "XRGB_4444", | |
56 | .fourcc = V4L2_PIX_FMT_RGB444, | |
57 | .depth = 16, | |
58 | .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444), | |
59 | }, | |
60 | { | |
61 | .name = "PACKED_RGB_888", | |
62 | .fourcc = V4L2_PIX_FMT_RGB24, | |
63 | .depth = 24, | |
64 | .hw = COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888), | |
65 | }, | |
66 | }; | |
67 | #define NUM_FORMATS ARRAY_SIZE(formats) | |
68 | ||
ec76afe8 | 69 | static struct g2d_frame def_frame = { |
91884734 KD |
70 | .width = DEFAULT_WIDTH, |
71 | .height = DEFAULT_HEIGHT, | |
72 | .c_width = DEFAULT_WIDTH, | |
73 | .c_height = DEFAULT_HEIGHT, | |
74 | .o_width = 0, | |
75 | .o_height = 0, | |
76 | .fmt = &formats[0], | |
77 | .right = DEFAULT_WIDTH, | |
78 | .bottom = DEFAULT_HEIGHT, | |
79 | }; | |
80 | ||
ec76afe8 | 81 | static struct g2d_fmt *find_fmt(struct v4l2_format *f) |
91884734 KD |
82 | { |
83 | unsigned int i; | |
84 | for (i = 0; i < NUM_FORMATS; i++) { | |
85 | if (formats[i].fourcc == f->fmt.pix.pixelformat) | |
86 | return &formats[i]; | |
87 | } | |
88 | return NULL; | |
89 | } | |
90 | ||
91 | ||
92 | static struct g2d_frame *get_frame(struct g2d_ctx *ctx, | |
93 | enum v4l2_buf_type type) | |
94 | { | |
95 | switch (type) { | |
96 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: | |
97 | return &ctx->in; | |
98 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: | |
99 | return &ctx->out; | |
100 | default: | |
101 | return ERR_PTR(-EINVAL); | |
102 | } | |
103 | } | |
104 | ||
105 | static int g2d_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt, | |
106 | unsigned int *nbuffers, unsigned int *nplanes, | |
107 | unsigned int sizes[], void *alloc_ctxs[]) | |
108 | { | |
109 | struct g2d_ctx *ctx = vb2_get_drv_priv(vq); | |
110 | struct g2d_frame *f = get_frame(ctx, vq->type); | |
111 | ||
112 | if (IS_ERR(f)) | |
113 | return PTR_ERR(f); | |
114 | ||
115 | sizes[0] = f->size; | |
116 | *nplanes = 1; | |
117 | alloc_ctxs[0] = ctx->dev->alloc_ctx; | |
118 | ||
119 | if (*nbuffers == 0) | |
120 | *nbuffers = 1; | |
121 | ||
122 | return 0; | |
123 | } | |
124 | ||
125 | static int g2d_buf_prepare(struct vb2_buffer *vb) | |
126 | { | |
127 | struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); | |
128 | struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type); | |
129 | ||
130 | if (IS_ERR(f)) | |
131 | return PTR_ERR(f); | |
132 | vb2_set_plane_payload(vb, 0, f->size); | |
133 | return 0; | |
134 | } | |
135 | ||
136 | static void g2d_buf_queue(struct vb2_buffer *vb) | |
137 | { | |
138 | struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue); | |
139 | v4l2_m2m_buf_queue(ctx->m2m_ctx, vb); | |
140 | } | |
141 | ||
142 | ||
143 | static struct vb2_ops g2d_qops = { | |
144 | .queue_setup = g2d_queue_setup, | |
145 | .buf_prepare = g2d_buf_prepare, | |
146 | .buf_queue = g2d_buf_queue, | |
147 | }; | |
148 | ||
149 | static int queue_init(void *priv, struct vb2_queue *src_vq, | |
150 | struct vb2_queue *dst_vq) | |
151 | { | |
152 | struct g2d_ctx *ctx = priv; | |
153 | int ret; | |
154 | ||
91884734 KD |
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 | ||
91884734 KD |
166 | dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
167 | dst_vq->io_modes = VB2_MMAP | VB2_USERPTR; | |
168 | dst_vq->drv_priv = ctx; | |
169 | dst_vq->ops = &g2d_qops; | |
170 | dst_vq->mem_ops = &vb2_dma_contig_memops; | |
171 | dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer); | |
172 | ||
173 | return vb2_queue_init(dst_vq); | |
174 | } | |
175 | ||
176 | static int g2d_s_ctrl(struct v4l2_ctrl *ctrl) | |
177 | { | |
178 | struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx, | |
179 | ctrl_handler); | |
27dda973 KD |
180 | unsigned long flags; |
181 | ||
182 | spin_lock_irqsave(&ctx->dev->ctrl_lock, flags); | |
91884734 KD |
183 | switch (ctrl->id) { |
184 | case V4L2_CID_COLORFX: | |
185 | if (ctrl->val == V4L2_COLORFX_NEGATIVE) | |
186 | ctx->rop = ROP4_INVERT; | |
187 | else | |
188 | ctx->rop = ROP4_COPY; | |
7f6cce69 | 189 | break; |
d0d28326 SK |
190 | |
191 | case V4L2_CID_HFLIP: | |
192 | ctx->flip = ctx->ctrl_hflip->val | (ctx->ctrl_vflip->val << 1); | |
193 | break; | |
194 | ||
91884734 | 195 | } |
27dda973 | 196 | spin_unlock_irqrestore(&ctx->dev->ctrl_lock, flags); |
91884734 KD |
197 | return 0; |
198 | } | |
199 | ||
200 | static const struct v4l2_ctrl_ops g2d_ctrl_ops = { | |
201 | .s_ctrl = g2d_s_ctrl, | |
202 | }; | |
203 | ||
ec76afe8 | 204 | static int g2d_setup_ctrls(struct g2d_ctx *ctx) |
91884734 KD |
205 | { |
206 | struct g2d_dev *dev = ctx->dev; | |
207 | ||
d0d28326 SK |
208 | 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); | |
91884734 KD |
215 | |
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) { | |
d0d28326 SK |
225 | 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; | |
91884734 KD |
229 | } |
230 | ||
d0d28326 SK |
231 | v4l2_ctrl_cluster(2, &ctx->ctrl_hflip); |
232 | ||
91884734 KD |
233 | return 0; |
234 | } | |
235 | ||
236 | static 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 | ||
de40cb22 HV |
250 | if (mutex_lock_interruptible(&dev->mutex)) { |
251 | kfree(ctx); | |
252 | return -ERESTARTSYS; | |
253 | } | |
91884734 KD |
254 | ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init); |
255 | if (IS_ERR(ctx->m2m_ctx)) { | |
256 | ret = PTR_ERR(ctx->m2m_ctx); | |
de40cb22 | 257 | mutex_unlock(&dev->mutex); |
91884734 KD |
258 | kfree(ctx); |
259 | return ret; | |
260 | } | |
261 | v4l2_fh_init(&ctx->fh, video_devdata(file)); | |
262 | file->private_data = &ctx->fh; | |
263 | v4l2_fh_add(&ctx->fh); | |
264 | ||
265 | g2d_setup_ctrls(ctx); | |
266 | ||
267 | /* Write the default values to the ctx struct */ | |
268 | v4l2_ctrl_handler_setup(&ctx->ctrl_handler); | |
269 | ||
270 | ctx->fh.ctrl_handler = &ctx->ctrl_handler; | |
de40cb22 | 271 | mutex_unlock(&dev->mutex); |
91884734 KD |
272 | |
273 | v4l2_info(&dev->v4l2_dev, "instance opened\n"); | |
274 | return 0; | |
275 | } | |
276 | ||
277 | static int g2d_release(struct file *file) | |
278 | { | |
279 | struct g2d_dev *dev = video_drvdata(file); | |
280 | struct g2d_ctx *ctx = fh2ctx(file->private_data); | |
281 | ||
282 | v4l2_ctrl_handler_free(&ctx->ctrl_handler); | |
283 | v4l2_fh_del(&ctx->fh); | |
284 | v4l2_fh_exit(&ctx->fh); | |
285 | kfree(ctx); | |
286 | v4l2_info(&dev->v4l2_dev, "instance closed\n"); | |
287 | return 0; | |
288 | } | |
289 | ||
290 | ||
291 | static int vidioc_querycap(struct file *file, void *priv, | |
292 | struct v4l2_capability *cap) | |
293 | { | |
294 | strncpy(cap->driver, G2D_NAME, sizeof(cap->driver) - 1); | |
295 | strncpy(cap->card, G2D_NAME, sizeof(cap->card) - 1); | |
296 | cap->bus_info[0] = 0; | |
297 | cap->version = KERNEL_VERSION(1, 0, 0); | |
f0476a83 SN |
298 | /* |
299 | * This is only a mem-to-mem video device. The capture and output | |
300 | * device capability flags are left only for backward compatibility | |
301 | * and are scheduled for removal. | |
302 | */ | |
303 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT | | |
304 | V4L2_CAP_VIDEO_M2M | V4L2_CAP_STREAMING; | |
91884734 KD |
305 | return 0; |
306 | } | |
307 | ||
308 | static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f) | |
309 | { | |
310 | struct g2d_fmt *fmt; | |
311 | if (f->index >= NUM_FORMATS) | |
312 | return -EINVAL; | |
313 | fmt = &formats[f->index]; | |
314 | f->pixelformat = fmt->fourcc; | |
315 | strncpy(f->description, fmt->name, sizeof(f->description) - 1); | |
316 | return 0; | |
317 | } | |
318 | ||
319 | static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f) | |
320 | { | |
321 | struct g2d_ctx *ctx = prv; | |
322 | struct vb2_queue *vq; | |
323 | struct g2d_frame *frm; | |
324 | ||
325 | vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); | |
326 | if (!vq) | |
327 | return -EINVAL; | |
328 | frm = get_frame(ctx, f->type); | |
329 | if (IS_ERR(frm)) | |
330 | return PTR_ERR(frm); | |
331 | ||
332 | f->fmt.pix.width = frm->width; | |
333 | f->fmt.pix.height = frm->height; | |
334 | f->fmt.pix.field = V4L2_FIELD_NONE; | |
335 | f->fmt.pix.pixelformat = frm->fmt->fourcc; | |
336 | f->fmt.pix.bytesperline = (frm->width * frm->fmt->depth) >> 3; | |
337 | f->fmt.pix.sizeimage = frm->size; | |
338 | return 0; | |
339 | } | |
340 | ||
341 | static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f) | |
342 | { | |
343 | struct g2d_fmt *fmt; | |
344 | enum v4l2_field *field; | |
345 | ||
346 | fmt = find_fmt(f); | |
347 | if (!fmt) | |
348 | return -EINVAL; | |
349 | ||
350 | field = &f->fmt.pix.field; | |
351 | if (*field == V4L2_FIELD_ANY) | |
352 | *field = V4L2_FIELD_NONE; | |
353 | else if (*field != V4L2_FIELD_NONE) | |
354 | return -EINVAL; | |
355 | ||
356 | if (f->fmt.pix.width > MAX_WIDTH) | |
357 | f->fmt.pix.width = MAX_WIDTH; | |
358 | if (f->fmt.pix.height > MAX_HEIGHT) | |
359 | f->fmt.pix.height = MAX_HEIGHT; | |
360 | ||
361 | if (f->fmt.pix.width < 1) | |
362 | f->fmt.pix.width = 1; | |
363 | if (f->fmt.pix.height < 1) | |
364 | f->fmt.pix.height = 1; | |
365 | ||
366 | f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3; | |
367 | f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline; | |
368 | return 0; | |
369 | } | |
370 | ||
371 | static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f) | |
372 | { | |
373 | struct g2d_ctx *ctx = prv; | |
374 | struct g2d_dev *dev = ctx->dev; | |
375 | struct vb2_queue *vq; | |
376 | struct g2d_frame *frm; | |
377 | struct g2d_fmt *fmt; | |
378 | int ret = 0; | |
379 | ||
380 | /* Adjust all values accordingly to the hardware capabilities | |
381 | * and chosen format. */ | |
382 | ret = vidioc_try_fmt(file, prv, f); | |
383 | if (ret) | |
384 | return ret; | |
385 | vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type); | |
386 | if (vb2_is_busy(vq)) { | |
387 | v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type); | |
388 | return -EBUSY; | |
389 | } | |
390 | frm = get_frame(ctx, f->type); | |
391 | if (IS_ERR(frm)) | |
392 | return PTR_ERR(frm); | |
393 | fmt = find_fmt(f); | |
394 | if (!fmt) | |
395 | return -EINVAL; | |
396 | frm->width = f->fmt.pix.width; | |
397 | frm->height = f->fmt.pix.height; | |
398 | frm->size = f->fmt.pix.sizeimage; | |
399 | /* Reset crop settings */ | |
400 | frm->o_width = 0; | |
401 | frm->o_height = 0; | |
402 | frm->c_width = frm->width; | |
403 | frm->c_height = frm->height; | |
404 | frm->right = frm->width; | |
405 | frm->bottom = frm->height; | |
406 | frm->fmt = fmt; | |
407 | frm->stride = f->fmt.pix.bytesperline; | |
408 | return 0; | |
409 | } | |
410 | ||
411 | static unsigned int g2d_poll(struct file *file, struct poll_table_struct *wait) | |
412 | { | |
413 | struct g2d_ctx *ctx = fh2ctx(file->private_data); | |
de40cb22 HV |
414 | struct g2d_dev *dev = ctx->dev; |
415 | unsigned int res; | |
416 | ||
417 | mutex_lock(&dev->mutex); | |
418 | res = v4l2_m2m_poll(file, ctx->m2m_ctx, wait); | |
419 | mutex_unlock(&dev->mutex); | |
420 | return res; | |
91884734 KD |
421 | } |
422 | ||
423 | static int g2d_mmap(struct file *file, struct vm_area_struct *vma) | |
424 | { | |
425 | struct g2d_ctx *ctx = fh2ctx(file->private_data); | |
de40cb22 HV |
426 | struct g2d_dev *dev = ctx->dev; |
427 | int ret; | |
428 | ||
429 | if (mutex_lock_interruptible(&dev->mutex)) | |
430 | return -ERESTARTSYS; | |
431 | ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma); | |
432 | mutex_unlock(&dev->mutex); | |
433 | return ret; | |
91884734 KD |
434 | } |
435 | ||
436 | static int vidioc_reqbufs(struct file *file, void *priv, | |
437 | struct v4l2_requestbuffers *reqbufs) | |
438 | { | |
439 | struct g2d_ctx *ctx = priv; | |
440 | return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs); | |
441 | } | |
442 | ||
443 | static int vidioc_querybuf(struct file *file, void *priv, | |
444 | struct v4l2_buffer *buf) | |
445 | { | |
446 | struct g2d_ctx *ctx = priv; | |
447 | return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf); | |
448 | } | |
449 | ||
450 | static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf) | |
451 | { | |
452 | struct g2d_ctx *ctx = priv; | |
453 | return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf); | |
454 | } | |
455 | ||
456 | static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf) | |
457 | { | |
458 | struct g2d_ctx *ctx = priv; | |
459 | return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf); | |
460 | } | |
461 | ||
462 | ||
463 | static int vidioc_streamon(struct file *file, void *priv, | |
464 | enum v4l2_buf_type type) | |
465 | { | |
466 | struct g2d_ctx *ctx = priv; | |
467 | return v4l2_m2m_streamon(file, ctx->m2m_ctx, type); | |
468 | } | |
469 | ||
470 | static int vidioc_streamoff(struct file *file, void *priv, | |
471 | enum v4l2_buf_type type) | |
472 | { | |
473 | struct g2d_ctx *ctx = priv; | |
474 | return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type); | |
475 | } | |
476 | ||
477 | static int vidioc_cropcap(struct file *file, void *priv, | |
478 | struct v4l2_cropcap *cr) | |
479 | { | |
480 | struct g2d_ctx *ctx = priv; | |
481 | struct g2d_frame *f; | |
482 | ||
483 | f = get_frame(ctx, cr->type); | |
484 | if (IS_ERR(f)) | |
485 | return PTR_ERR(f); | |
486 | ||
487 | cr->bounds.left = 0; | |
488 | cr->bounds.top = 0; | |
489 | cr->bounds.width = f->width; | |
490 | cr->bounds.height = f->height; | |
491 | cr->defrect = cr->bounds; | |
492 | return 0; | |
493 | } | |
494 | ||
495 | static int vidioc_g_crop(struct file *file, void *prv, struct v4l2_crop *cr) | |
496 | { | |
497 | struct g2d_ctx *ctx = prv; | |
498 | struct g2d_frame *f; | |
499 | ||
500 | f = get_frame(ctx, cr->type); | |
501 | if (IS_ERR(f)) | |
502 | return PTR_ERR(f); | |
503 | ||
504 | cr->c.left = f->o_height; | |
505 | cr->c.top = f->o_width; | |
506 | cr->c.width = f->c_width; | |
507 | cr->c.height = f->c_height; | |
508 | return 0; | |
509 | } | |
510 | ||
21ec9c97 | 511 | static int vidioc_try_crop(struct file *file, void *prv, const struct v4l2_crop *cr) |
91884734 KD |
512 | { |
513 | struct g2d_ctx *ctx = prv; | |
514 | struct g2d_dev *dev = ctx->dev; | |
515 | struct g2d_frame *f; | |
516 | ||
517 | f = get_frame(ctx, cr->type); | |
518 | if (IS_ERR(f)) | |
519 | return PTR_ERR(f); | |
520 | ||
521 | if (cr->c.top < 0 || cr->c.left < 0) { | |
522 | v4l2_err(&dev->v4l2_dev, | |
523 | "doesn't support negative values for top & left\n"); | |
524 | return -EINVAL; | |
525 | } | |
526 | ||
527 | return 0; | |
528 | } | |
529 | ||
4f996594 | 530 | static int vidioc_s_crop(struct file *file, void *prv, const struct v4l2_crop *cr) |
91884734 KD |
531 | { |
532 | struct g2d_ctx *ctx = prv; | |
533 | struct g2d_frame *f; | |
534 | int ret; | |
535 | ||
536 | ret = vidioc_try_crop(file, prv, cr); | |
537 | if (ret) | |
538 | return ret; | |
539 | f = get_frame(ctx, cr->type); | |
540 | if (IS_ERR(f)) | |
541 | return PTR_ERR(f); | |
542 | ||
543 | f->c_width = cr->c.width; | |
544 | f->c_height = cr->c.height; | |
545 | f->o_width = cr->c.left; | |
546 | f->o_height = cr->c.top; | |
547 | f->bottom = f->o_height + f->c_height; | |
548 | f->right = f->o_width + f->c_width; | |
549 | return 0; | |
550 | } | |
551 | ||
552 | static void g2d_lock(void *prv) | |
553 | { | |
554 | struct g2d_ctx *ctx = prv; | |
555 | struct g2d_dev *dev = ctx->dev; | |
556 | mutex_lock(&dev->mutex); | |
557 | } | |
558 | ||
559 | static void g2d_unlock(void *prv) | |
560 | { | |
561 | struct g2d_ctx *ctx = prv; | |
562 | struct g2d_dev *dev = ctx->dev; | |
563 | mutex_unlock(&dev->mutex); | |
564 | } | |
565 | ||
566 | static void job_abort(void *prv) | |
567 | { | |
568 | struct g2d_ctx *ctx = prv; | |
569 | struct g2d_dev *dev = ctx->dev; | |
570 | int ret; | |
571 | ||
2356877c | 572 | if (dev->curr == NULL) /* No job currently running */ |
91884734 KD |
573 | return; |
574 | ||
575 | ret = wait_event_timeout(dev->irq_queue, | |
2356877c | 576 | dev->curr == NULL, |
91884734 KD |
577 | msecs_to_jiffies(G2D_TIMEOUT)); |
578 | } | |
579 | ||
580 | static void device_run(void *prv) | |
581 | { | |
582 | struct g2d_ctx *ctx = prv; | |
583 | struct g2d_dev *dev = ctx->dev; | |
584 | struct vb2_buffer *src, *dst; | |
27dda973 | 585 | unsigned long flags; |
91884734 KD |
586 | u32 cmd = 0; |
587 | ||
588 | dev->curr = ctx; | |
589 | ||
590 | src = v4l2_m2m_next_src_buf(ctx->m2m_ctx); | |
591 | dst = v4l2_m2m_next_dst_buf(ctx->m2m_ctx); | |
592 | ||
593 | clk_enable(dev->gate); | |
594 | g2d_reset(dev); | |
595 | ||
27dda973 KD |
596 | spin_lock_irqsave(&dev->ctrl_lock, flags); |
597 | ||
91884734 KD |
598 | g2d_set_src_size(dev, &ctx->in); |
599 | g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(src, 0)); | |
600 | ||
601 | g2d_set_dst_size(dev, &ctx->out); | |
602 | g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(dst, 0)); | |
603 | ||
604 | g2d_set_rop4(dev, ctx->rop); | |
d0d28326 SK |
605 | g2d_set_flip(dev, ctx->flip); |
606 | ||
91884734 | 607 | if (ctx->in.c_width != ctx->out.c_width || |
62ce272d SK |
608 | ctx->in.c_height != ctx->out.c_height) { |
609 | if (dev->variant->hw_rev == TYPE_G2D_3X) | |
610 | cmd |= CMD_V3_ENABLE_STRETCH; | |
611 | else | |
612 | g2d_set_v41_stretch(dev, &ctx->in, &ctx->out); | |
613 | } | |
614 | ||
91884734 KD |
615 | g2d_set_cmd(dev, cmd); |
616 | g2d_start(dev); | |
27dda973 KD |
617 | |
618 | spin_unlock_irqrestore(&dev->ctrl_lock, flags); | |
91884734 KD |
619 | } |
620 | ||
621 | static irqreturn_t g2d_isr(int irq, void *prv) | |
622 | { | |
623 | struct g2d_dev *dev = prv; | |
624 | struct g2d_ctx *ctx = dev->curr; | |
625 | struct vb2_buffer *src, *dst; | |
626 | ||
627 | g2d_clear_int(dev); | |
628 | clk_disable(dev->gate); | |
629 | ||
2356877c | 630 | BUG_ON(ctx == NULL); |
91884734 KD |
631 | |
632 | src = v4l2_m2m_src_buf_remove(ctx->m2m_ctx); | |
633 | dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx); | |
634 | ||
2356877c SK |
635 | BUG_ON(src == NULL); |
636 | BUG_ON(dst == NULL); | |
91884734 KD |
637 | |
638 | v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE); | |
639 | v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE); | |
640 | v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx); | |
641 | ||
2356877c | 642 | dev->curr = NULL; |
91884734 KD |
643 | wake_up(&dev->irq_queue); |
644 | return IRQ_HANDLED; | |
645 | } | |
646 | ||
647 | static const struct v4l2_file_operations g2d_fops = { | |
648 | .owner = THIS_MODULE, | |
649 | .open = g2d_open, | |
650 | .release = g2d_release, | |
651 | .poll = g2d_poll, | |
652 | .unlocked_ioctl = video_ioctl2, | |
653 | .mmap = g2d_mmap, | |
654 | }; | |
655 | ||
656 | static const struct v4l2_ioctl_ops g2d_ioctl_ops = { | |
657 | .vidioc_querycap = vidioc_querycap, | |
658 | ||
659 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt, | |
660 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt, | |
661 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt, | |
662 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt, | |
663 | ||
664 | .vidioc_enum_fmt_vid_out = vidioc_enum_fmt, | |
665 | .vidioc_g_fmt_vid_out = vidioc_g_fmt, | |
666 | .vidioc_try_fmt_vid_out = vidioc_try_fmt, | |
667 | .vidioc_s_fmt_vid_out = vidioc_s_fmt, | |
668 | ||
669 | .vidioc_reqbufs = vidioc_reqbufs, | |
670 | .vidioc_querybuf = vidioc_querybuf, | |
671 | ||
672 | .vidioc_qbuf = vidioc_qbuf, | |
673 | .vidioc_dqbuf = vidioc_dqbuf, | |
674 | ||
675 | .vidioc_streamon = vidioc_streamon, | |
676 | .vidioc_streamoff = vidioc_streamoff, | |
677 | ||
678 | .vidioc_g_crop = vidioc_g_crop, | |
679 | .vidioc_s_crop = vidioc_s_crop, | |
680 | .vidioc_cropcap = vidioc_cropcap, | |
681 | }; | |
682 | ||
683 | static struct video_device g2d_videodev = { | |
684 | .name = G2D_NAME, | |
685 | .fops = &g2d_fops, | |
686 | .ioctl_ops = &g2d_ioctl_ops, | |
687 | .minor = -1, | |
688 | .release = video_device_release, | |
954f340f | 689 | .vfl_dir = VFL_DIR_M2M, |
91884734 KD |
690 | }; |
691 | ||
692 | static struct v4l2_m2m_ops g2d_m2m_ops = { | |
693 | .device_run = device_run, | |
694 | .job_abort = job_abort, | |
695 | .lock = g2d_lock, | |
696 | .unlock = g2d_unlock, | |
697 | }; | |
698 | ||
5ce60d79 SK |
699 | static const struct of_device_id exynos_g2d_match[]; |
700 | ||
91884734 KD |
701 | static int g2d_probe(struct platform_device *pdev) |
702 | { | |
703 | struct g2d_dev *dev; | |
704 | struct video_device *vfd; | |
705 | struct resource *res; | |
5ce60d79 | 706 | const struct of_device_id *of_id; |
91884734 KD |
707 | int ret = 0; |
708 | ||
32fced05 | 709 | dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL); |
91884734 KD |
710 | if (!dev) |
711 | return -ENOMEM; | |
32fced05 | 712 | |
27dda973 | 713 | spin_lock_init(&dev->ctrl_lock); |
91884734 KD |
714 | mutex_init(&dev->mutex); |
715 | atomic_set(&dev->num_inst, 0); | |
716 | init_waitqueue_head(&dev->irq_queue); | |
717 | ||
718 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
91884734 | 719 | |
f23999ec TR |
720 | dev->regs = devm_ioremap_resource(&pdev->dev, res); |
721 | if (IS_ERR(dev->regs)) | |
722 | return PTR_ERR(dev->regs); | |
91884734 KD |
723 | |
724 | dev->clk = clk_get(&pdev->dev, "sclk_fimg2d"); | |
15514fb5 | 725 | if (IS_ERR(dev->clk)) { |
91884734 | 726 | dev_err(&pdev->dev, "failed to get g2d clock\n"); |
32fced05 | 727 | return -ENXIO; |
91884734 KD |
728 | } |
729 | ||
11a37c70 KD |
730 | ret = clk_prepare(dev->clk); |
731 | if (ret) { | |
732 | dev_err(&pdev->dev, "failed to prepare g2d clock\n"); | |
733 | goto put_clk; | |
734 | } | |
735 | ||
91884734 | 736 | dev->gate = clk_get(&pdev->dev, "fimg2d"); |
15514fb5 | 737 | if (IS_ERR(dev->gate)) { |
91884734 KD |
738 | dev_err(&pdev->dev, "failed to get g2d clock gate\n"); |
739 | ret = -ENXIO; | |
11a37c70 KD |
740 | goto unprep_clk; |
741 | } | |
742 | ||
743 | ret = clk_prepare(dev->gate); | |
744 | if (ret) { | |
745 | dev_err(&pdev->dev, "failed to prepare g2d clock gate\n"); | |
746 | goto put_clk_gate; | |
91884734 KD |
747 | } |
748 | ||
749 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | |
750 | if (!res) { | |
751 | dev_err(&pdev->dev, "failed to find IRQ\n"); | |
752 | ret = -ENXIO; | |
11a37c70 | 753 | goto unprep_clk_gate; |
91884734 KD |
754 | } |
755 | ||
756 | dev->irq = res->start; | |
757 | ||
32fced05 SK |
758 | ret = devm_request_irq(&pdev->dev, dev->irq, g2d_isr, |
759 | 0, pdev->name, dev); | |
91884734 KD |
760 | if (ret) { |
761 | dev_err(&pdev->dev, "failed to install IRQ\n"); | |
762 | goto put_clk_gate; | |
763 | } | |
764 | ||
765 | dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev); | |
766 | if (IS_ERR(dev->alloc_ctx)) { | |
767 | ret = PTR_ERR(dev->alloc_ctx); | |
32fced05 | 768 | goto unprep_clk_gate; |
91884734 KD |
769 | } |
770 | ||
771 | ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev); | |
772 | if (ret) | |
773 | goto alloc_ctx_cleanup; | |
774 | vfd = video_device_alloc(); | |
775 | if (!vfd) { | |
776 | v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n"); | |
777 | ret = -ENOMEM; | |
778 | goto unreg_v4l2_dev; | |
779 | } | |
780 | *vfd = g2d_videodev; | |
781 | vfd->lock = &dev->mutex; | |
782 | ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0); | |
783 | if (ret) { | |
784 | v4l2_err(&dev->v4l2_dev, "Failed to register video device\n"); | |
785 | goto rel_vdev; | |
786 | } | |
787 | video_set_drvdata(vfd, dev); | |
788 | snprintf(vfd->name, sizeof(vfd->name), "%s", g2d_videodev.name); | |
789 | dev->vfd = vfd; | |
790 | v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n", | |
791 | vfd->num); | |
792 | platform_set_drvdata(pdev, dev); | |
793 | dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops); | |
794 | if (IS_ERR(dev->m2m_dev)) { | |
795 | v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n"); | |
796 | ret = PTR_ERR(dev->m2m_dev); | |
797 | goto unreg_video_dev; | |
798 | } | |
799 | ||
800 | def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3; | |
5ce60d79 SK |
801 | |
802 | if (!pdev->dev.of_node) { | |
803 | dev->variant = g2d_get_drv_data(pdev); | |
804 | } else { | |
805 | of_id = of_match_node(exynos_g2d_match, pdev->dev.of_node); | |
806 | if (!of_id) { | |
807 | ret = -ENODEV; | |
808 | goto unreg_video_dev; | |
809 | } | |
810 | dev->variant = (struct g2d_variant *)of_id->data; | |
811 | } | |
91884734 KD |
812 | |
813 | return 0; | |
814 | ||
815 | unreg_video_dev: | |
816 | video_unregister_device(dev->vfd); | |
817 | rel_vdev: | |
818 | video_device_release(vfd); | |
819 | unreg_v4l2_dev: | |
820 | v4l2_device_unregister(&dev->v4l2_dev); | |
821 | alloc_ctx_cleanup: | |
822 | vb2_dma_contig_cleanup_ctx(dev->alloc_ctx); | |
11a37c70 KD |
823 | unprep_clk_gate: |
824 | clk_unprepare(dev->gate); | |
91884734 KD |
825 | put_clk_gate: |
826 | clk_put(dev->gate); | |
11a37c70 KD |
827 | unprep_clk: |
828 | clk_unprepare(dev->clk); | |
91884734 KD |
829 | put_clk: |
830 | clk_put(dev->clk); | |
32fced05 | 831 | |
91884734 KD |
832 | return ret; |
833 | } | |
834 | ||
835 | static int g2d_remove(struct platform_device *pdev) | |
836 | { | |
837 | struct g2d_dev *dev = (struct g2d_dev *)platform_get_drvdata(pdev); | |
838 | ||
839 | v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME); | |
840 | v4l2_m2m_release(dev->m2m_dev); | |
841 | video_unregister_device(dev->vfd); | |
842 | v4l2_device_unregister(&dev->v4l2_dev); | |
843 | vb2_dma_contig_cleanup_ctx(dev->alloc_ctx); | |
11a37c70 | 844 | clk_unprepare(dev->gate); |
91884734 | 845 | clk_put(dev->gate); |
11a37c70 | 846 | clk_unprepare(dev->clk); |
91884734 | 847 | clk_put(dev->clk); |
91884734 KD |
848 | return 0; |
849 | } | |
850 | ||
62ce272d | 851 | static struct g2d_variant g2d_drvdata_v3x = { |
5ce60d79 | 852 | .hw_rev = TYPE_G2D_3X, /* Revision 3.0 for S5PV210 and Exynos4210 */ |
62ce272d SK |
853 | }; |
854 | ||
855 | static struct g2d_variant g2d_drvdata_v4x = { | |
856 | .hw_rev = TYPE_G2D_4X, /* Revision 4.1 for Exynos4X12 and Exynos5 */ | |
857 | }; | |
858 | ||
5ce60d79 SK |
859 | static const struct of_device_id exynos_g2d_match[] = { |
860 | { | |
861 | .compatible = "samsung,s5pv210-g2d", | |
862 | .data = &g2d_drvdata_v3x, | |
863 | }, { | |
864 | .compatible = "samsung,exynos4212-g2d", | |
865 | .data = &g2d_drvdata_v4x, | |
866 | }, | |
867 | {}, | |
868 | }; | |
869 | MODULE_DEVICE_TABLE(of, exynos_g2d_match); | |
870 | ||
62ce272d SK |
871 | static struct platform_device_id g2d_driver_ids[] = { |
872 | { | |
873 | .name = "s5p-g2d", | |
874 | .driver_data = (unsigned long)&g2d_drvdata_v3x, | |
875 | }, { | |
876 | .name = "s5p-g2d-v4x", | |
877 | .driver_data = (unsigned long)&g2d_drvdata_v4x, | |
878 | }, | |
879 | {}, | |
880 | }; | |
881 | MODULE_DEVICE_TABLE(platform, g2d_driver_ids); | |
882 | ||
91884734 KD |
883 | static struct platform_driver g2d_pdrv = { |
884 | .probe = g2d_probe, | |
885 | .remove = g2d_remove, | |
62ce272d | 886 | .id_table = g2d_driver_ids, |
91884734 KD |
887 | .driver = { |
888 | .name = G2D_NAME, | |
889 | .owner = THIS_MODULE, | |
5ce60d79 | 890 | .of_match_table = exynos_g2d_match, |
91884734 KD |
891 | }, |
892 | }; | |
893 | ||
1d6629b1 | 894 | module_platform_driver(g2d_pdrv); |
91884734 KD |
895 | |
896 | MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>"); | |
897 | MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver"); | |
898 | MODULE_LICENSE("GPL"); |