Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 1 | /* |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 2 | * Copyright (c) 2012-2013, NVIDIA Corporation. |
| 3 | * |
Thierry Reding | d105a6c | 2014-02-11 15:53:33 +0100 | [diff] [blame] | 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. |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 9 | #include <linux/clk.h> |
| 10 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 11 | #include "drm.h" |
| 12 | #include "gem.h" |
Thierry Reding | 497c56a | 2013-10-07 09:55:57 +0200 | [diff] [blame] | 13 | #include "gr2d.h" |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame] | 14 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 15 | struct gr2d { |
Thierry Reding | 53fa7f7 | 2013-09-24 15:35:40 +0200 | [diff] [blame] | 16 | struct tegra_drm_client client; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 17 | struct host1x_channel *channel; |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame] | 18 | struct clk *clk; |
| 19 | |
| 20 | DECLARE_BITMAP(addr_regs, GR2D_NUM_REGS); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 21 | }; |
| 22 | |
Thierry Reding | 53fa7f7 | 2013-09-24 15:35:40 +0200 | [diff] [blame] | 23 | static inline struct gr2d *to_gr2d(struct tegra_drm_client *client) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 24 | { |
| 25 | return container_of(client, struct gr2d, client); |
| 26 | } |
| 27 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 28 | static int gr2d_init(struct host1x_client *client) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 29 | { |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 30 | struct tegra_drm_client *drm = host1x_to_drm_client(client); |
Thierry Reding | 9910f5c | 2014-05-22 09:57:15 +0200 | [diff] [blame] | 31 | struct drm_device *dev = dev_get_drvdata(client->parent); |
Arto Merilainen | 61644dc | 2013-10-14 15:21:55 +0300 | [diff] [blame] | 32 | unsigned long flags = HOST1X_SYNCPT_HAS_BASE; |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 33 | struct gr2d *gr2d = to_gr2d(drm); |
| 34 | |
| 35 | gr2d->channel = host1x_channel_request(client->dev); |
| 36 | if (!gr2d->channel) |
| 37 | return -ENOMEM; |
| 38 | |
Arto Merilainen | 61644dc | 2013-10-14 15:21:55 +0300 | [diff] [blame] | 39 | client->syncpts[0] = host1x_syncpt_request(client->dev, flags); |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 40 | if (!client->syncpts[0]) { |
| 41 | host1x_channel_free(gr2d->channel); |
| 42 | return -ENOMEM; |
| 43 | } |
| 44 | |
Thierry Reding | 9910f5c | 2014-05-22 09:57:15 +0200 | [diff] [blame] | 45 | return tegra_drm_register_client(dev->dev_private, drm); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 48 | static int gr2d_exit(struct host1x_client *client) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 49 | { |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 50 | struct tegra_drm_client *drm = host1x_to_drm_client(client); |
Thierry Reding | 9910f5c | 2014-05-22 09:57:15 +0200 | [diff] [blame] | 51 | struct drm_device *dev = dev_get_drvdata(client->parent); |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 52 | struct gr2d *gr2d = to_gr2d(drm); |
| 53 | int err; |
| 54 | |
Thierry Reding | 9910f5c | 2014-05-22 09:57:15 +0200 | [diff] [blame] | 55 | err = tegra_drm_unregister_client(dev->dev_private, drm); |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 56 | if (err < 0) |
| 57 | return err; |
| 58 | |
| 59 | host1x_syncpt_free(client->syncpts[0]); |
| 60 | host1x_channel_free(gr2d->channel); |
| 61 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
Thierry Reding | 53fa7f7 | 2013-09-24 15:35:40 +0200 | [diff] [blame] | 65 | static const struct host1x_client_ops gr2d_client_ops = { |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 66 | .init = gr2d_init, |
| 67 | .exit = gr2d_exit, |
Thierry Reding | 53fa7f7 | 2013-09-24 15:35:40 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static int gr2d_open_channel(struct tegra_drm_client *client, |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 71 | struct tegra_drm_context *context) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 72 | { |
| 73 | struct gr2d *gr2d = to_gr2d(client); |
| 74 | |
| 75 | context->channel = host1x_channel_get(gr2d->channel); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 76 | if (!context->channel) |
| 77 | return -ENOMEM; |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
Thierry Reding | c88c363 | 2013-09-26 16:08:22 +0200 | [diff] [blame] | 82 | static void gr2d_close_channel(struct tegra_drm_context *context) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 83 | { |
| 84 | host1x_channel_put(context->channel); |
| 85 | } |
| 86 | |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame] | 87 | static int gr2d_is_addr_reg(struct device *dev, u32 class, u32 offset) |
| 88 | { |
| 89 | struct gr2d *gr2d = dev_get_drvdata(dev); |
| 90 | |
| 91 | switch (class) { |
| 92 | case HOST1X_CLASS_HOST1X: |
| 93 | if (offset == 0x2b) |
| 94 | return 1; |
| 95 | |
| 96 | break; |
| 97 | |
| 98 | case HOST1X_CLASS_GR2D: |
| 99 | case HOST1X_CLASS_GR2D_SB: |
| 100 | if (offset >= GR2D_NUM_REGS) |
| 101 | break; |
| 102 | |
| 103 | if (test_bit(offset, gr2d->addr_regs)) |
| 104 | return 1; |
| 105 | |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Thierry Reding | 53fa7f7 | 2013-09-24 15:35:40 +0200 | [diff] [blame] | 112 | static const struct tegra_drm_client_ops gr2d_ops = { |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 113 | .open_channel = gr2d_open_channel, |
| 114 | .close_channel = gr2d_close_channel, |
Thierry Reding | c40f0f1 | 2013-10-10 11:00:33 +0200 | [diff] [blame] | 115 | .is_addr_reg = gr2d_is_addr_reg, |
| 116 | .submit = tegra_drm_submit, |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 117 | }; |
| 118 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 119 | static const struct of_device_id gr2d_match[] = { |
| 120 | { .compatible = "nvidia,tegra30-gr2d" }, |
| 121 | { .compatible = "nvidia,tegra20-gr2d" }, |
| 122 | { }, |
| 123 | }; |
Stephen Warren | ef70728 | 2014-06-18 16:21:55 -0600 | [diff] [blame] | 124 | MODULE_DEVICE_TABLE(of, gr2d_match); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 125 | |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame] | 126 | static const u32 gr2d_addr_regs[] = { |
Thierry Reding | 497c56a | 2013-10-07 09:55:57 +0200 | [diff] [blame] | 127 | GR2D_UA_BASE_ADDR, |
| 128 | GR2D_VA_BASE_ADDR, |
| 129 | GR2D_PAT_BASE_ADDR, |
| 130 | GR2D_DSTA_BASE_ADDR, |
| 131 | GR2D_DSTB_BASE_ADDR, |
| 132 | GR2D_DSTC_BASE_ADDR, |
| 133 | GR2D_SRCA_BASE_ADDR, |
| 134 | GR2D_SRCB_BASE_ADDR, |
| 135 | GR2D_SRC_BASE_ADDR_SB, |
| 136 | GR2D_DSTA_BASE_ADDR_SB, |
| 137 | GR2D_DSTB_BASE_ADDR_SB, |
| 138 | GR2D_UA_BASE_ADDR_SB, |
| 139 | GR2D_VA_BASE_ADDR_SB, |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame] | 140 | }; |
| 141 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 142 | static int gr2d_probe(struct platform_device *pdev) |
| 143 | { |
| 144 | struct device *dev = &pdev->dev; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 145 | struct host1x_syncpt **syncpts; |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame] | 146 | struct gr2d *gr2d; |
| 147 | unsigned int i; |
| 148 | int err; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 149 | |
| 150 | gr2d = devm_kzalloc(dev, sizeof(*gr2d), GFP_KERNEL); |
| 151 | if (!gr2d) |
| 152 | return -ENOMEM; |
| 153 | |
| 154 | syncpts = devm_kzalloc(dev, sizeof(*syncpts), GFP_KERNEL); |
| 155 | if (!syncpts) |
| 156 | return -ENOMEM; |
| 157 | |
| 158 | gr2d->clk = devm_clk_get(dev, NULL); |
| 159 | if (IS_ERR(gr2d->clk)) { |
| 160 | dev_err(dev, "cannot get clock\n"); |
| 161 | return PTR_ERR(gr2d->clk); |
| 162 | } |
| 163 | |
| 164 | err = clk_prepare_enable(gr2d->clk); |
| 165 | if (err) { |
| 166 | dev_err(dev, "cannot turn on clock\n"); |
| 167 | return err; |
| 168 | } |
| 169 | |
Thierry Reding | 53fa7f7 | 2013-09-24 15:35:40 +0200 | [diff] [blame] | 170 | INIT_LIST_HEAD(&gr2d->client.base.list); |
| 171 | gr2d->client.base.ops = &gr2d_client_ops; |
| 172 | gr2d->client.base.dev = dev; |
| 173 | gr2d->client.base.class = HOST1X_CLASS_GR2D; |
| 174 | gr2d->client.base.syncpts = syncpts; |
| 175 | gr2d->client.base.num_syncpts = 1; |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 176 | |
| 177 | INIT_LIST_HEAD(&gr2d->client.list); |
Thierry Reding | 53fa7f7 | 2013-09-24 15:35:40 +0200 | [diff] [blame] | 178 | gr2d->client.ops = &gr2d_ops; |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 179 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 180 | err = host1x_client_register(&gr2d->client.base); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 181 | if (err < 0) { |
| 182 | dev_err(dev, "failed to register host1x client: %d\n", err); |
Wei Yongjun | b008403 | 2013-10-21 13:38:34 +0800 | [diff] [blame] | 183 | clk_disable_unprepare(gr2d->clk); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 184 | return err; |
| 185 | } |
| 186 | |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame] | 187 | /* initialize address register map */ |
| 188 | for (i = 0; i < ARRAY_SIZE(gr2d_addr_regs); i++) |
| 189 | set_bit(gr2d_addr_regs[i], gr2d->addr_regs); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 190 | |
| 191 | platform_set_drvdata(pdev, gr2d); |
| 192 | |
| 193 | return 0; |
| 194 | } |
| 195 | |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame] | 196 | static int gr2d_remove(struct platform_device *pdev) |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 197 | { |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 198 | struct gr2d *gr2d = platform_get_drvdata(pdev); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 199 | int err; |
| 200 | |
Thierry Reding | 776dc38 | 2013-10-14 14:43:22 +0200 | [diff] [blame] | 201 | err = host1x_client_unregister(&gr2d->client.base); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 202 | if (err < 0) { |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame] | 203 | dev_err(&pdev->dev, "failed to unregister host1x client: %d\n", |
| 204 | err); |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 205 | return err; |
| 206 | } |
| 207 | |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 208 | clk_disable_unprepare(gr2d->clk); |
| 209 | |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | struct platform_driver tegra_gr2d_driver = { |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 214 | .driver = { |
Thierry Reding | a137ce3 | 2013-10-14 14:44:54 +0200 | [diff] [blame] | 215 | .name = "tegra-gr2d", |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 216 | .of_match_table = gr2d_match, |
Thierry Reding | c1bef81 | 2013-09-26 16:09:43 +0200 | [diff] [blame] | 217 | }, |
| 218 | .probe = gr2d_probe, |
| 219 | .remove = gr2d_remove, |
Terje Bergstrom | d43f81c | 2013-03-22 16:34:09 +0200 | [diff] [blame] | 220 | }; |