g2d.c 19 KB

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