g2d.c 20 KB

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