g2d.c 19 KB

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