g2d.c 21 KB

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