g2d.c 18 KB

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