g2d.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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. #include <linux/module.h>
  13. #include <linux/fs.h>
  14. #include <linux/version.h>
  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>
  20. #include <linux/platform_device.h>
  21. #include <media/v4l2-mem2mem.h>
  22. #include <media/v4l2-device.h>
  23. #include <media/v4l2-ioctl.h>
  24. #include <media/videobuf2-core.h>
  25. #include <media/videobuf2-dma-contig.h>
  26. #include "g2d.h"
  27. #include "g2d-regs.h"
  28. #define fh2ctx(__fh) container_of(__fh, struct g2d_ctx, fh)
  29. static struct g2d_fmt formats[] = {
  30. {
  31. .name = "XRGB_8888",
  32. .fourcc = V4L2_PIX_FMT_RGB32,
  33. .depth = 32,
  34. .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_8888),
  35. },
  36. {
  37. .name = "RGB_565",
  38. .fourcc = V4L2_PIX_FMT_RGB565X,
  39. .depth = 16,
  40. .hw = COLOR_MODE(ORDER_XRGB, MODE_RGB_565),
  41. },
  42. {
  43. .name = "XRGB_1555",
  44. .fourcc = V4L2_PIX_FMT_RGB555X,
  45. .depth = 16,
  46. .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_1555),
  47. },
  48. {
  49. .name = "XRGB_4444",
  50. .fourcc = V4L2_PIX_FMT_RGB444,
  51. .depth = 16,
  52. .hw = COLOR_MODE(ORDER_XRGB, MODE_XRGB_4444),
  53. },
  54. {
  55. .name = "PACKED_RGB_888",
  56. .fourcc = V4L2_PIX_FMT_RGB24,
  57. .depth = 24,
  58. .hw = COLOR_MODE(ORDER_XRGB, MODE_PACKED_RGB_888),
  59. },
  60. };
  61. #define NUM_FORMATS ARRAY_SIZE(formats)
  62. struct g2d_frame def_frame = {
  63. .width = DEFAULT_WIDTH,
  64. .height = DEFAULT_HEIGHT,
  65. .c_width = DEFAULT_WIDTH,
  66. .c_height = DEFAULT_HEIGHT,
  67. .o_width = 0,
  68. .o_height = 0,
  69. .fmt = &formats[0],
  70. .right = DEFAULT_WIDTH,
  71. .bottom = DEFAULT_HEIGHT,
  72. };
  73. struct g2d_fmt *find_fmt(struct v4l2_format *f)
  74. {
  75. unsigned int i;
  76. for (i = 0; i < NUM_FORMATS; i++) {
  77. if (formats[i].fourcc == f->fmt.pix.pixelformat)
  78. return &formats[i];
  79. }
  80. return NULL;
  81. }
  82. static struct g2d_frame *get_frame(struct g2d_ctx *ctx,
  83. enum v4l2_buf_type type)
  84. {
  85. switch (type) {
  86. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  87. return &ctx->in;
  88. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  89. return &ctx->out;
  90. default:
  91. return ERR_PTR(-EINVAL);
  92. }
  93. }
  94. static int g2d_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  95. unsigned int *nbuffers, unsigned int *nplanes,
  96. unsigned int sizes[], void *alloc_ctxs[])
  97. {
  98. struct g2d_ctx *ctx = vb2_get_drv_priv(vq);
  99. struct g2d_frame *f = get_frame(ctx, vq->type);
  100. if (IS_ERR(f))
  101. return PTR_ERR(f);
  102. sizes[0] = f->size;
  103. *nplanes = 1;
  104. alloc_ctxs[0] = ctx->dev->alloc_ctx;
  105. if (*nbuffers == 0)
  106. *nbuffers = 1;
  107. return 0;
  108. }
  109. static int g2d_buf_prepare(struct vb2_buffer *vb)
  110. {
  111. struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  112. struct g2d_frame *f = get_frame(ctx, vb->vb2_queue->type);
  113. if (IS_ERR(f))
  114. return PTR_ERR(f);
  115. vb2_set_plane_payload(vb, 0, f->size);
  116. return 0;
  117. }
  118. static void g2d_buf_queue(struct vb2_buffer *vb)
  119. {
  120. struct g2d_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
  121. v4l2_m2m_buf_queue(ctx->m2m_ctx, vb);
  122. }
  123. static struct vb2_ops g2d_qops = {
  124. .queue_setup = g2d_queue_setup,
  125. .buf_prepare = g2d_buf_prepare,
  126. .buf_queue = g2d_buf_queue,
  127. };
  128. static int queue_init(void *priv, struct vb2_queue *src_vq,
  129. struct vb2_queue *dst_vq)
  130. {
  131. struct g2d_ctx *ctx = priv;
  132. int ret;
  133. memset(src_vq, 0, sizeof(*src_vq));
  134. src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  135. src_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  136. src_vq->drv_priv = ctx;
  137. src_vq->ops = &g2d_qops;
  138. src_vq->mem_ops = &vb2_dma_contig_memops;
  139. src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  140. ret = vb2_queue_init(src_vq);
  141. if (ret)
  142. return ret;
  143. memset(dst_vq, 0, sizeof(*dst_vq));
  144. dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  145. dst_vq->io_modes = VB2_MMAP | VB2_USERPTR;
  146. dst_vq->drv_priv = ctx;
  147. dst_vq->ops = &g2d_qops;
  148. dst_vq->mem_ops = &vb2_dma_contig_memops;
  149. dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
  150. return vb2_queue_init(dst_vq);
  151. }
  152. static int g2d_s_ctrl(struct v4l2_ctrl *ctrl)
  153. {
  154. struct g2d_ctx *ctx = container_of(ctrl->handler, struct g2d_ctx,
  155. ctrl_handler);
  156. switch (ctrl->id) {
  157. case V4L2_CID_COLORFX:
  158. if (ctrl->val == V4L2_COLORFX_NEGATIVE)
  159. ctx->rop = ROP4_INVERT;
  160. else
  161. ctx->rop = ROP4_COPY;
  162. break;
  163. default:
  164. v4l2_err(&ctx->dev->v4l2_dev, "unknown control\n");
  165. return -EINVAL;
  166. }
  167. return 0;
  168. }
  169. static const struct v4l2_ctrl_ops g2d_ctrl_ops = {
  170. .s_ctrl = g2d_s_ctrl,
  171. };
  172. int g2d_setup_ctrls(struct g2d_ctx *ctx)
  173. {
  174. struct g2d_dev *dev = ctx->dev;
  175. v4l2_ctrl_handler_init(&ctx->ctrl_handler, 1);
  176. if (ctx->ctrl_handler.error) {
  177. v4l2_err(&dev->v4l2_dev, "v4l2_ctrl_handler_init failed\n");
  178. return ctx->ctrl_handler.error;
  179. }
  180. v4l2_ctrl_new_std_menu(
  181. &ctx->ctrl_handler,
  182. &g2d_ctrl_ops,
  183. V4L2_CID_COLORFX,
  184. V4L2_COLORFX_NEGATIVE,
  185. ~((1 << V4L2_COLORFX_NONE) | (1 << V4L2_COLORFX_NEGATIVE)),
  186. V4L2_COLORFX_NONE);
  187. if (ctx->ctrl_handler.error) {
  188. v4l2_err(&dev->v4l2_dev, "v4l2_ctrl_handler_init failed\n");
  189. return ctx->ctrl_handler.error;
  190. }
  191. return 0;
  192. }
  193. static int g2d_open(struct file *file)
  194. {
  195. struct g2d_dev *dev = video_drvdata(file);
  196. struct g2d_ctx *ctx = NULL;
  197. int ret = 0;
  198. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  199. if (!ctx)
  200. return -ENOMEM;
  201. ctx->dev = dev;
  202. /* Set default formats */
  203. ctx->in = def_frame;
  204. ctx->out = def_frame;
  205. ctx->m2m_ctx = v4l2_m2m_ctx_init(dev->m2m_dev, ctx, &queue_init);
  206. if (IS_ERR(ctx->m2m_ctx)) {
  207. ret = PTR_ERR(ctx->m2m_ctx);
  208. kfree(ctx);
  209. return ret;
  210. }
  211. v4l2_fh_init(&ctx->fh, video_devdata(file));
  212. file->private_data = &ctx->fh;
  213. v4l2_fh_add(&ctx->fh);
  214. g2d_setup_ctrls(ctx);
  215. /* Write the default values to the ctx struct */
  216. v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
  217. ctx->fh.ctrl_handler = &ctx->ctrl_handler;
  218. v4l2_info(&dev->v4l2_dev, "instance opened\n");
  219. return 0;
  220. }
  221. static int g2d_release(struct file *file)
  222. {
  223. struct g2d_dev *dev = video_drvdata(file);
  224. struct g2d_ctx *ctx = fh2ctx(file->private_data);
  225. v4l2_ctrl_handler_free(&ctx->ctrl_handler);
  226. v4l2_fh_del(&ctx->fh);
  227. v4l2_fh_exit(&ctx->fh);
  228. kfree(ctx);
  229. v4l2_info(&dev->v4l2_dev, "instance closed\n");
  230. return 0;
  231. }
  232. static int vidioc_querycap(struct file *file, void *priv,
  233. struct v4l2_capability *cap)
  234. {
  235. strncpy(cap->driver, G2D_NAME, sizeof(cap->driver) - 1);
  236. strncpy(cap->card, G2D_NAME, sizeof(cap->card) - 1);
  237. cap->bus_info[0] = 0;
  238. cap->version = KERNEL_VERSION(1, 0, 0);
  239. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT
  240. | V4L2_CAP_STREAMING;
  241. return 0;
  242. }
  243. static int vidioc_enum_fmt(struct file *file, void *prv, struct v4l2_fmtdesc *f)
  244. {
  245. struct g2d_fmt *fmt;
  246. if (f->index >= NUM_FORMATS)
  247. return -EINVAL;
  248. fmt = &formats[f->index];
  249. f->pixelformat = fmt->fourcc;
  250. strncpy(f->description, fmt->name, sizeof(f->description) - 1);
  251. return 0;
  252. }
  253. static int vidioc_g_fmt(struct file *file, void *prv, struct v4l2_format *f)
  254. {
  255. struct g2d_ctx *ctx = prv;
  256. struct vb2_queue *vq;
  257. struct g2d_frame *frm;
  258. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  259. if (!vq)
  260. return -EINVAL;
  261. frm = get_frame(ctx, f->type);
  262. if (IS_ERR(frm))
  263. return PTR_ERR(frm);
  264. f->fmt.pix.width = frm->width;
  265. f->fmt.pix.height = frm->height;
  266. f->fmt.pix.field = V4L2_FIELD_NONE;
  267. f->fmt.pix.pixelformat = frm->fmt->fourcc;
  268. f->fmt.pix.bytesperline = (frm->width * frm->fmt->depth) >> 3;
  269. f->fmt.pix.sizeimage = frm->size;
  270. return 0;
  271. }
  272. static int vidioc_try_fmt(struct file *file, void *prv, struct v4l2_format *f)
  273. {
  274. struct g2d_fmt *fmt;
  275. enum v4l2_field *field;
  276. fmt = find_fmt(f);
  277. if (!fmt)
  278. return -EINVAL;
  279. field = &f->fmt.pix.field;
  280. if (*field == V4L2_FIELD_ANY)
  281. *field = V4L2_FIELD_NONE;
  282. else if (*field != V4L2_FIELD_NONE)
  283. return -EINVAL;
  284. if (f->fmt.pix.width > MAX_WIDTH)
  285. f->fmt.pix.width = MAX_WIDTH;
  286. if (f->fmt.pix.height > MAX_HEIGHT)
  287. f->fmt.pix.height = MAX_HEIGHT;
  288. if (f->fmt.pix.width < 1)
  289. f->fmt.pix.width = 1;
  290. if (f->fmt.pix.height < 1)
  291. f->fmt.pix.height = 1;
  292. f->fmt.pix.bytesperline = (f->fmt.pix.width * fmt->depth) >> 3;
  293. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  294. return 0;
  295. }
  296. static int vidioc_s_fmt(struct file *file, void *prv, struct v4l2_format *f)
  297. {
  298. struct g2d_ctx *ctx = prv;
  299. struct g2d_dev *dev = ctx->dev;
  300. struct vb2_queue *vq;
  301. struct g2d_frame *frm;
  302. struct g2d_fmt *fmt;
  303. int ret = 0;
  304. /* Adjust all values accordingly to the hardware capabilities
  305. * and chosen format. */
  306. ret = vidioc_try_fmt(file, prv, f);
  307. if (ret)
  308. return ret;
  309. vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
  310. if (vb2_is_busy(vq)) {
  311. v4l2_err(&dev->v4l2_dev, "queue (%d) bust\n", f->type);
  312. return -EBUSY;
  313. }
  314. frm = get_frame(ctx, f->type);
  315. if (IS_ERR(frm))
  316. return PTR_ERR(frm);
  317. fmt = find_fmt(f);
  318. if (!fmt)
  319. return -EINVAL;
  320. frm->width = f->fmt.pix.width;
  321. frm->height = f->fmt.pix.height;
  322. frm->size = f->fmt.pix.sizeimage;
  323. /* Reset crop settings */
  324. frm->o_width = 0;
  325. frm->o_height = 0;
  326. frm->c_width = frm->width;
  327. frm->c_height = frm->height;
  328. frm->right = frm->width;
  329. frm->bottom = frm->height;
  330. frm->fmt = fmt;
  331. frm->stride = f->fmt.pix.bytesperline;
  332. return 0;
  333. }
  334. static unsigned int g2d_poll(struct file *file, struct poll_table_struct *wait)
  335. {
  336. struct g2d_ctx *ctx = fh2ctx(file->private_data);
  337. return v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
  338. }
  339. static int g2d_mmap(struct file *file, struct vm_area_struct *vma)
  340. {
  341. struct g2d_ctx *ctx = fh2ctx(file->private_data);
  342. return v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
  343. }
  344. static int vidioc_reqbufs(struct file *file, void *priv,
  345. struct v4l2_requestbuffers *reqbufs)
  346. {
  347. struct g2d_ctx *ctx = priv;
  348. return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
  349. }
  350. static int vidioc_querybuf(struct file *file, void *priv,
  351. struct v4l2_buffer *buf)
  352. {
  353. struct g2d_ctx *ctx = priv;
  354. return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
  355. }
  356. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  357. {
  358. struct g2d_ctx *ctx = priv;
  359. return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
  360. }
  361. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
  362. {
  363. struct g2d_ctx *ctx = priv;
  364. return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
  365. }
  366. static int vidioc_streamon(struct file *file, void *priv,
  367. enum v4l2_buf_type type)
  368. {
  369. struct g2d_ctx *ctx = priv;
  370. return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
  371. }
  372. static int vidioc_streamoff(struct file *file, void *priv,
  373. enum v4l2_buf_type type)
  374. {
  375. struct g2d_ctx *ctx = priv;
  376. return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
  377. }
  378. static int vidioc_cropcap(struct file *file, void *priv,
  379. struct v4l2_cropcap *cr)
  380. {
  381. struct g2d_ctx *ctx = priv;
  382. struct g2d_frame *f;
  383. f = get_frame(ctx, cr->type);
  384. if (IS_ERR(f))
  385. return PTR_ERR(f);
  386. cr->bounds.left = 0;
  387. cr->bounds.top = 0;
  388. cr->bounds.width = f->width;
  389. cr->bounds.height = f->height;
  390. cr->defrect = cr->bounds;
  391. return 0;
  392. }
  393. static int vidioc_g_crop(struct file *file, void *prv, struct v4l2_crop *cr)
  394. {
  395. struct g2d_ctx *ctx = prv;
  396. struct g2d_frame *f;
  397. f = get_frame(ctx, cr->type);
  398. if (IS_ERR(f))
  399. return PTR_ERR(f);
  400. cr->c.left = f->o_height;
  401. cr->c.top = f->o_width;
  402. cr->c.width = f->c_width;
  403. cr->c.height = f->c_height;
  404. return 0;
  405. }
  406. static int vidioc_try_crop(struct file *file, void *prv, struct v4l2_crop *cr)
  407. {
  408. struct g2d_ctx *ctx = prv;
  409. struct g2d_dev *dev = ctx->dev;
  410. struct g2d_frame *f;
  411. f = get_frame(ctx, cr->type);
  412. if (IS_ERR(f))
  413. return PTR_ERR(f);
  414. if (cr->c.top < 0 || cr->c.left < 0) {
  415. v4l2_err(&dev->v4l2_dev,
  416. "doesn't support negative values for top & left\n");
  417. return -EINVAL;
  418. }
  419. return 0;
  420. }
  421. static int vidioc_s_crop(struct file *file, void *prv, struct v4l2_crop *cr)
  422. {
  423. struct g2d_ctx *ctx = prv;
  424. struct g2d_frame *f;
  425. int ret;
  426. ret = vidioc_try_crop(file, prv, cr);
  427. if (ret)
  428. return ret;
  429. f = get_frame(ctx, cr->type);
  430. if (IS_ERR(f))
  431. return PTR_ERR(f);
  432. f->c_width = cr->c.width;
  433. f->c_height = cr->c.height;
  434. f->o_width = cr->c.left;
  435. f->o_height = cr->c.top;
  436. f->bottom = f->o_height + f->c_height;
  437. f->right = f->o_width + f->c_width;
  438. return 0;
  439. }
  440. static void g2d_lock(void *prv)
  441. {
  442. struct g2d_ctx *ctx = prv;
  443. struct g2d_dev *dev = ctx->dev;
  444. mutex_lock(&dev->mutex);
  445. }
  446. static void g2d_unlock(void *prv)
  447. {
  448. struct g2d_ctx *ctx = prv;
  449. struct g2d_dev *dev = ctx->dev;
  450. mutex_unlock(&dev->mutex);
  451. }
  452. static void job_abort(void *prv)
  453. {
  454. struct g2d_ctx *ctx = prv;
  455. struct g2d_dev *dev = ctx->dev;
  456. int ret;
  457. if (dev->curr == 0) /* No job currently running */
  458. return;
  459. ret = wait_event_timeout(dev->irq_queue,
  460. dev->curr == 0,
  461. msecs_to_jiffies(G2D_TIMEOUT));
  462. }
  463. static void device_run(void *prv)
  464. {
  465. struct g2d_ctx *ctx = prv;
  466. struct g2d_dev *dev = ctx->dev;
  467. struct vb2_buffer *src, *dst;
  468. u32 cmd = 0;
  469. dev->curr = ctx;
  470. src = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
  471. dst = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
  472. clk_enable(dev->gate);
  473. g2d_reset(dev);
  474. g2d_set_src_size(dev, &ctx->in);
  475. g2d_set_src_addr(dev, vb2_dma_contig_plane_dma_addr(src, 0));
  476. g2d_set_dst_size(dev, &ctx->out);
  477. g2d_set_dst_addr(dev, vb2_dma_contig_plane_dma_addr(dst, 0));
  478. g2d_set_rop4(dev, ctx->rop);
  479. if (ctx->in.c_width != ctx->out.c_width ||
  480. ctx->in.c_height != ctx->out.c_height)
  481. cmd |= g2d_cmd_stretch(1);
  482. g2d_set_cmd(dev, cmd);
  483. g2d_start(dev);
  484. }
  485. static irqreturn_t g2d_isr(int irq, void *prv)
  486. {
  487. struct g2d_dev *dev = prv;
  488. struct g2d_ctx *ctx = dev->curr;
  489. struct vb2_buffer *src, *dst;
  490. g2d_clear_int(dev);
  491. clk_disable(dev->gate);
  492. BUG_ON(ctx == 0);
  493. src = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
  494. dst = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
  495. BUG_ON(src == 0);
  496. BUG_ON(dst == 0);
  497. v4l2_m2m_buf_done(src, VB2_BUF_STATE_DONE);
  498. v4l2_m2m_buf_done(dst, VB2_BUF_STATE_DONE);
  499. v4l2_m2m_job_finish(dev->m2m_dev, ctx->m2m_ctx);
  500. dev->curr = 0;
  501. wake_up(&dev->irq_queue);
  502. return IRQ_HANDLED;
  503. }
  504. static const struct v4l2_file_operations g2d_fops = {
  505. .owner = THIS_MODULE,
  506. .open = g2d_open,
  507. .release = g2d_release,
  508. .poll = g2d_poll,
  509. .unlocked_ioctl = video_ioctl2,
  510. .mmap = g2d_mmap,
  511. };
  512. static const struct v4l2_ioctl_ops g2d_ioctl_ops = {
  513. .vidioc_querycap = vidioc_querycap,
  514. .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
  515. .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
  516. .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
  517. .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
  518. .vidioc_enum_fmt_vid_out = vidioc_enum_fmt,
  519. .vidioc_g_fmt_vid_out = vidioc_g_fmt,
  520. .vidioc_try_fmt_vid_out = vidioc_try_fmt,
  521. .vidioc_s_fmt_vid_out = vidioc_s_fmt,
  522. .vidioc_reqbufs = vidioc_reqbufs,
  523. .vidioc_querybuf = vidioc_querybuf,
  524. .vidioc_qbuf = vidioc_qbuf,
  525. .vidioc_dqbuf = vidioc_dqbuf,
  526. .vidioc_streamon = vidioc_streamon,
  527. .vidioc_streamoff = vidioc_streamoff,
  528. .vidioc_g_crop = vidioc_g_crop,
  529. .vidioc_s_crop = vidioc_s_crop,
  530. .vidioc_cropcap = vidioc_cropcap,
  531. };
  532. static struct video_device g2d_videodev = {
  533. .name = G2D_NAME,
  534. .fops = &g2d_fops,
  535. .ioctl_ops = &g2d_ioctl_ops,
  536. .minor = -1,
  537. .release = video_device_release,
  538. };
  539. static struct v4l2_m2m_ops g2d_m2m_ops = {
  540. .device_run = device_run,
  541. .job_abort = job_abort,
  542. .lock = g2d_lock,
  543. .unlock = g2d_unlock,
  544. };
  545. static int g2d_probe(struct platform_device *pdev)
  546. {
  547. struct g2d_dev *dev;
  548. struct video_device *vfd;
  549. struct resource *res;
  550. int ret = 0;
  551. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  552. if (!dev)
  553. return -ENOMEM;
  554. spin_lock_init(&dev->irqlock);
  555. mutex_init(&dev->mutex);
  556. atomic_set(&dev->num_inst, 0);
  557. init_waitqueue_head(&dev->irq_queue);
  558. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  559. if (!res) {
  560. dev_err(&pdev->dev, "failed to find registers\n");
  561. ret = -ENOENT;
  562. goto free_dev;
  563. }
  564. dev->res_regs = request_mem_region(res->start, resource_size(res),
  565. dev_name(&pdev->dev));
  566. if (!dev->res_regs) {
  567. dev_err(&pdev->dev, "failed to obtain register region\n");
  568. ret = -ENOENT;
  569. goto free_dev;
  570. }
  571. dev->regs = ioremap(res->start, resource_size(res));
  572. if (!dev->regs) {
  573. dev_err(&pdev->dev, "failed to map registers\n");
  574. ret = -ENOENT;
  575. goto rel_res_regs;
  576. }
  577. dev->clk = clk_get(&pdev->dev, "sclk_fimg2d");
  578. if (IS_ERR_OR_NULL(dev->clk)) {
  579. dev_err(&pdev->dev, "failed to get g2d clock\n");
  580. ret = -ENXIO;
  581. goto unmap_regs;
  582. }
  583. dev->gate = clk_get(&pdev->dev, "fimg2d");
  584. if (IS_ERR_OR_NULL(dev->gate)) {
  585. dev_err(&pdev->dev, "failed to get g2d clock gate\n");
  586. ret = -ENXIO;
  587. goto put_clk;
  588. }
  589. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  590. if (!res) {
  591. dev_err(&pdev->dev, "failed to find IRQ\n");
  592. ret = -ENXIO;
  593. goto put_clk_gate;
  594. }
  595. dev->irq = res->start;
  596. ret = request_irq(dev->irq, g2d_isr, 0, pdev->name, dev);
  597. if (ret) {
  598. dev_err(&pdev->dev, "failed to install IRQ\n");
  599. goto put_clk_gate;
  600. }
  601. dev->alloc_ctx = vb2_dma_contig_init_ctx(&pdev->dev);
  602. if (IS_ERR(dev->alloc_ctx)) {
  603. ret = PTR_ERR(dev->alloc_ctx);
  604. goto rel_irq;
  605. }
  606. ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
  607. if (ret)
  608. goto alloc_ctx_cleanup;
  609. vfd = video_device_alloc();
  610. if (!vfd) {
  611. v4l2_err(&dev->v4l2_dev, "Failed to allocate video device\n");
  612. ret = -ENOMEM;
  613. goto unreg_v4l2_dev;
  614. }
  615. *vfd = g2d_videodev;
  616. vfd->lock = &dev->mutex;
  617. ret = video_register_device(vfd, VFL_TYPE_GRABBER, 0);
  618. if (ret) {
  619. v4l2_err(&dev->v4l2_dev, "Failed to register video device\n");
  620. goto rel_vdev;
  621. }
  622. video_set_drvdata(vfd, dev);
  623. snprintf(vfd->name, sizeof(vfd->name), "%s", g2d_videodev.name);
  624. dev->vfd = vfd;
  625. v4l2_info(&dev->v4l2_dev, "device registered as /dev/video%d\n",
  626. vfd->num);
  627. platform_set_drvdata(pdev, dev);
  628. dev->m2m_dev = v4l2_m2m_init(&g2d_m2m_ops);
  629. if (IS_ERR(dev->m2m_dev)) {
  630. v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem device\n");
  631. ret = PTR_ERR(dev->m2m_dev);
  632. goto unreg_video_dev;
  633. }
  634. def_frame.stride = (def_frame.width * def_frame.fmt->depth) >> 3;
  635. return 0;
  636. unreg_video_dev:
  637. video_unregister_device(dev->vfd);
  638. rel_vdev:
  639. video_device_release(vfd);
  640. unreg_v4l2_dev:
  641. v4l2_device_unregister(&dev->v4l2_dev);
  642. alloc_ctx_cleanup:
  643. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
  644. rel_irq:
  645. free_irq(dev->irq, dev);
  646. put_clk_gate:
  647. clk_put(dev->gate);
  648. put_clk:
  649. clk_put(dev->clk);
  650. unmap_regs:
  651. iounmap(dev->regs);
  652. rel_res_regs:
  653. release_resource(dev->res_regs);
  654. free_dev:
  655. kfree(dev);
  656. return ret;
  657. }
  658. static int g2d_remove(struct platform_device *pdev)
  659. {
  660. struct g2d_dev *dev = (struct g2d_dev *)platform_get_drvdata(pdev);
  661. v4l2_info(&dev->v4l2_dev, "Removing " G2D_NAME);
  662. v4l2_m2m_release(dev->m2m_dev);
  663. video_unregister_device(dev->vfd);
  664. v4l2_device_unregister(&dev->v4l2_dev);
  665. vb2_dma_contig_cleanup_ctx(dev->alloc_ctx);
  666. free_irq(dev->irq, dev);
  667. clk_put(dev->gate);
  668. clk_put(dev->clk);
  669. iounmap(dev->regs);
  670. release_resource(dev->res_regs);
  671. kfree(dev);
  672. return 0;
  673. }
  674. static struct platform_driver g2d_pdrv = {
  675. .probe = g2d_probe,
  676. .remove = g2d_remove,
  677. .driver = {
  678. .name = G2D_NAME,
  679. .owner = THIS_MODULE,
  680. },
  681. };
  682. module_platform_driver(g2d_pdrv);
  683. MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
  684. MODULE_DESCRIPTION("S5P G2D 2d graphics accelerator driver");
  685. MODULE_LICENSE("GPL");