gr2d.c 5.0 KB

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