gr2d.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2012-2013, NVIDIA Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/clk.h>
  9. #include "drm.h"
  10. #include "gem.h"
  11. #include "gr2d.h"
  12. struct gr2d {
  13. struct tegra_drm_client client;
  14. struct host1x_channel *channel;
  15. struct clk *clk;
  16. DECLARE_BITMAP(addr_regs, GR2D_NUM_REGS);
  17. };
  18. static inline struct gr2d *to_gr2d(struct tegra_drm_client *client)
  19. {
  20. return container_of(client, struct gr2d, client);
  21. }
  22. static int gr2d_init(struct host1x_client *client)
  23. {
  24. struct tegra_drm_client *drm = host1x_to_drm_client(client);
  25. struct drm_device *dev = dev_get_drvdata(client->parent);
  26. unsigned long flags = HOST1X_SYNCPT_HAS_BASE;
  27. struct gr2d *gr2d = to_gr2d(drm);
  28. gr2d->channel = host1x_channel_request(client->dev);
  29. if (!gr2d->channel)
  30. return -ENOMEM;
  31. client->syncpts[0] = host1x_syncpt_request(client->dev, flags);
  32. if (!client->syncpts[0]) {
  33. host1x_channel_free(gr2d->channel);
  34. return -ENOMEM;
  35. }
  36. return tegra_drm_register_client(dev->dev_private, drm);
  37. }
  38. static int gr2d_exit(struct host1x_client *client)
  39. {
  40. struct tegra_drm_client *drm = host1x_to_drm_client(client);
  41. struct drm_device *dev = dev_get_drvdata(client->parent);
  42. struct gr2d *gr2d = to_gr2d(drm);
  43. int err;
  44. err = tegra_drm_unregister_client(dev->dev_private, drm);
  45. if (err < 0)
  46. return err;
  47. host1x_syncpt_free(client->syncpts[0]);
  48. host1x_channel_free(gr2d->channel);
  49. return 0;
  50. }
  51. static const struct host1x_client_ops gr2d_client_ops = {
  52. .init = gr2d_init,
  53. .exit = gr2d_exit,
  54. };
  55. static int gr2d_open_channel(struct tegra_drm_client *client,
  56. struct tegra_drm_context *context)
  57. {
  58. struct gr2d *gr2d = to_gr2d(client);
  59. context->channel = host1x_channel_get(gr2d->channel);
  60. if (!context->channel)
  61. return -ENOMEM;
  62. return 0;
  63. }
  64. static void gr2d_close_channel(struct tegra_drm_context *context)
  65. {
  66. host1x_channel_put(context->channel);
  67. }
  68. static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 offset)
  69. {
  70. struct gr2d *gr2d = dev_get_drvdata(dev);
  71. switch (class) {
  72. case HOST1X_CLASS_HOST1X:
  73. if (offset == 0x2b)
  74. return 1;
  75. break;
  76. case HOST1X_CLASS_GR2D:
  77. case HOST1X_CLASS_GR2D_SB:
  78. if (offset >= GR2D_NUM_REGS)
  79. break;
  80. if (test_bit(offset, gr2d->addr_regs))
  81. return 1;
  82. break;
  83. }
  84. return 0;
  85. }
  86. static const struct tegra_drm_client_ops gr2d_ops = {
  87. .open_channel = gr2d_open_channel,
  88. .close_channel = gr2d_close_channel,
  89. .is_addr_reg = gr2d_is_addr_reg,
  90. .submit = tegra_drm_submit,
  91. };
  92. static const struct of_device_id gr2d_match[] = {
  93. { .compatible = "nvidia,tegra30-gr2d" },
  94. { .compatible = "nvidia,tegra20-gr2d" },
  95. { },
  96. };
  97. MODULE_DEVICE_TABLE(of, gr2d_match);
  98. static const u32 gr2d_addr_regs[] = {
  99. GR2D_UA_BASE_ADDR,
  100. GR2D_VA_BASE_ADDR,
  101. GR2D_PAT_BASE_ADDR,
  102. GR2D_DSTA_BASE_ADDR,
  103. GR2D_DSTB_BASE_ADDR,
  104. GR2D_DSTC_BASE_ADDR,
  105. GR2D_SRCA_BASE_ADDR,
  106. GR2D_SRCB_BASE_ADDR,
  107. GR2D_SRC_BASE_ADDR_SB,
  108. GR2D_DSTA_BASE_ADDR_SB,
  109. GR2D_DSTB_BASE_ADDR_SB,
  110. GR2D_UA_BASE_ADDR_SB,
  111. GR2D_VA_BASE_ADDR_SB,
  112. };
  113. static int gr2d_probe(struct platform_device *pdev)
  114. {
  115. struct device *dev = &pdev->dev;
  116. struct host1x_syncpt **syncpts;
  117. struct gr2d *gr2d;
  118. unsigned int i;
  119. int err;
  120. gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL);
  121. if (!gr2d)
  122. return -ENOMEM;
  123. syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
  124. if (!syncpts)
  125. return -ENOMEM;
  126. gr2d->clk = devm_clk_get(dev, NULL);
  127. if (IS_ERR(gr2d->clk)) {
  128. dev_err(dev, "cannot get clock\n");
  129. return PTR_ERR(gr2d->clk);
  130. }
  131. err = clk_prepare_enable(gr2d->clk);
  132. if (err) {
  133. dev_err(dev, "cannot turn on clock\n");
  134. return err;
  135. }
  136. INIT_LIST_HEAD(&gr2d->client.base.list);
  137. gr2d->client.base.ops = &gr2d_client_ops;
  138. gr2d->client.base.dev = dev;
  139. gr2d->client.base.class = HOST1X_CLASS_GR2D;
  140. gr2d->client.base.syncpts = syncpts;
  141. gr2d->client.base.num_syncpts = 1;
  142. INIT_LIST_HEAD(&gr2d->client.list);
  143. gr2d->client.ops = &gr2d_ops;
  144. err = host1x_client_register(&gr2d->client.base);
  145. if (err < 0) {
  146. dev_err(dev, "failed to register host1x client: %d\n", err);
  147. clk_disable_unprepare(gr2d->clk);
  148. return err;
  149. }
  150. /* initialize address register map */
  151. for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); i++)
  152. set_bit(gr2d_addr_regs[i], gr2d->addr_regs);
  153. platform_set_drvdata(pdev, gr2d);
  154. return 0;
  155. }
  156. static int gr2d_remove(struct platform_device *pdev)
  157. {
  158. struct gr2d *gr2d = platform_get_drvdata(pdev);
  159. int err;
  160. err = host1x_client_unregister(&gr2d->client.base);
  161. if (err < 0) {
  162. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  163. err);
  164. return err;
  165. }
  166. clk_disable_unprepare(gr2d->clk);
  167. return 0;
  168. }
  169. struct platform_driver tegra_gr2d_driver = {
  170. .driver = {
  171. .name = "tegra-gr2d",
  172. .of_match_table = gr2d_match,
  173. },
  174. .probe = gr2d_probe,
  175. .remove = gr2d_remove,
  176. };