gr2d.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. unsigned long flags = HOST1X_SYNCPT_HAS_BASE;
  35. struct gr2d *gr2d = to_gr2d(drm);
  36. gr2d->channel = host1x_channel_request(client->dev);
  37. if (!gr2d->channel)
  38. return -ENOMEM;
  39. client->syncpts[0] = host1x_syncpt_request(client->dev, flags);
  40. if (!client->syncpts[0]) {
  41. host1x_channel_free(gr2d->channel);
  42. return -ENOMEM;
  43. }
  44. return tegra_drm_register_client(tegra, drm);
  45. }
  46. static int gr2d_exit(struct host1x_client *client)
  47. {
  48. struct tegra_drm_client *drm = host1x_to_drm_client(client);
  49. struct tegra_drm *tegra = dev_get_drvdata(client->parent);
  50. struct gr2d *gr2d = to_gr2d(drm);
  51. int err;
  52. err = tegra_drm_unregister_client(tegra, drm);
  53. if (err < 0)
  54. return err;
  55. host1x_syncpt_free(client->syncpts[0]);
  56. host1x_channel_free(gr2d->channel);
  57. return 0;
  58. }
  59. static const struct host1x_client_ops gr2d_client_ops = {
  60. .init = gr2d_init,
  61. .exit = gr2d_exit,
  62. };
  63. static int gr2d_open_channel(struct tegra_drm_client *client,
  64. struct tegra_drm_context *context)
  65. {
  66. struct gr2d *gr2d = to_gr2d(client);
  67. context->channel = host1x_channel_get(gr2d->channel);
  68. if (!context->channel)
  69. return -ENOMEM;
  70. return 0;
  71. }
  72. static void gr2d_close_channel(struct tegra_drm_context *context)
  73. {
  74. host1x_channel_put(context->channel);
  75. }
  76. static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 offset)
  77. {
  78. struct gr2d *gr2d = dev_get_drvdata(dev);
  79. switch (class) {
  80. case HOST1X_CLASS_HOST1X:
  81. if (offset == 0x2b)
  82. return 1;
  83. break;
  84. case HOST1X_CLASS_GR2D:
  85. case HOST1X_CLASS_GR2D_SB:
  86. if (offset >= GR2D_NUM_REGS)
  87. break;
  88. if (test_bit(offset, gr2d->addr_regs))
  89. return 1;
  90. break;
  91. }
  92. return 0;
  93. }
  94. static const struct tegra_drm_client_ops gr2d_ops = {
  95. .open_channel = gr2d_open_channel,
  96. .close_channel = gr2d_close_channel,
  97. .is_addr_reg = gr2d_is_addr_reg,
  98. .submit = tegra_drm_submit,
  99. };
  100. static const struct of_device_id gr2d_match[] = {
  101. { .compatible = "nvidia,tegra30-gr2d" },
  102. { .compatible = "nvidia,tegra20-gr2d" },
  103. { },
  104. };
  105. static const u32 gr2d_addr_regs[] = {
  106. GR2D_UA_BASE_ADDR,
  107. GR2D_VA_BASE_ADDR,
  108. GR2D_PAT_BASE_ADDR,
  109. GR2D_DSTA_BASE_ADDR,
  110. GR2D_DSTB_BASE_ADDR,
  111. GR2D_DSTC_BASE_ADDR,
  112. GR2D_SRCA_BASE_ADDR,
  113. GR2D_SRCB_BASE_ADDR,
  114. GR2D_SRC_BASE_ADDR_SB,
  115. GR2D_DSTA_BASE_ADDR_SB,
  116. GR2D_DSTB_BASE_ADDR_SB,
  117. GR2D_UA_BASE_ADDR_SB,
  118. GR2D_VA_BASE_ADDR_SB,
  119. };
  120. static int gr2d_probe(struct platform_device *pdev)
  121. {
  122. struct device *dev = &pdev->dev;
  123. struct host1x_syncpt **syncpts;
  124. struct gr2d *gr2d;
  125. unsigned int i;
  126. int err;
  127. gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL);
  128. if (!gr2d)
  129. return -ENOMEM;
  130. syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL);
  131. if (!syncpts)
  132. return -ENOMEM;
  133. gr2d->clk = devm_clk_get(dev, NULL);
  134. if (IS_ERR(gr2d->clk)) {
  135. dev_err(dev, "cannot get clock\n");
  136. return PTR_ERR(gr2d->clk);
  137. }
  138. err = clk_prepare_enable(gr2d->clk);
  139. if (err) {
  140. dev_err(dev, "cannot turn on clock\n");
  141. return err;
  142. }
  143. INIT_LIST_HEAD(&gr2d->client.base.list);
  144. gr2d->client.base.ops = &gr2d_client_ops;
  145. gr2d->client.base.dev = dev;
  146. gr2d->client.base.class = HOST1X_CLASS_GR2D;
  147. gr2d->client.base.syncpts = syncpts;
  148. gr2d->client.base.num_syncpts = 1;
  149. INIT_LIST_HEAD(&gr2d->client.list);
  150. gr2d->client.ops = &gr2d_ops;
  151. err = host1x_client_register(&gr2d->client.base);
  152. if (err < 0) {
  153. dev_err(dev, "failed to register host1x client: %d\n", err);
  154. clk_disable_unprepare(gr2d->clk);
  155. return err;
  156. }
  157. /* initialize address register map */
  158. for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); i++)
  159. set_bit(gr2d_addr_regs[i], gr2d->addr_regs);
  160. platform_set_drvdata(pdev, gr2d);
  161. return 0;
  162. }
  163. static int gr2d_remove(struct platform_device *pdev)
  164. {
  165. struct gr2d *gr2d = platform_get_drvdata(pdev);
  166. int err;
  167. err = host1x_client_unregister(&gr2d->client.base);
  168. if (err < 0) {
  169. dev_err(&pdev->dev, "failed to unregister host1x client: %d\n",
  170. err);
  171. return err;
  172. }
  173. clk_disable_unprepare(gr2d->clk);
  174. return 0;
  175. }
  176. struct platform_driver tegra_gr2d_driver = {
  177. .driver = {
  178. .name = "tegra-gr2d",
  179. .of_match_table = gr2d_match,
  180. },
  181. .probe = gr2d_probe,
  182. .remove = gr2d_remove,
  183. };