g2d.c 19 KB

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