]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/arm/display/komeda/komeda_dev.c
drm/komeda: Add irq handling
[linux.git] / drivers / gpu / drm / arm / display / komeda / komeda_dev.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) COPYRIGHT 2018 ARM Limited. All rights reserved.
4  * Author: James.Qian.Wang <james.qian.wang@arm.com>
5  *
6  */
7 #include <linux/io.h>
8 #include <linux/of_device.h>
9 #include <linux/of_graph.h>
10 #include <linux/platform_device.h>
11
12 #include <drm/drm_print.h>
13
14 #include "komeda_dev.h"
15
16 static int komeda_parse_pipe_dt(struct komeda_dev *mdev, struct device_node *np)
17 {
18         struct komeda_pipeline *pipe;
19         struct clk *clk;
20         u32 pipe_id;
21         int ret = 0;
22
23         ret = of_property_read_u32(np, "reg", &pipe_id);
24         if (ret != 0 || pipe_id >= mdev->n_pipelines)
25                 return -EINVAL;
26
27         pipe = mdev->pipelines[pipe_id];
28
29         clk = of_clk_get_by_name(np, "aclk");
30         if (IS_ERR(clk)) {
31                 DRM_ERROR("get aclk for pipeline %d failed!\n", pipe_id);
32                 return PTR_ERR(clk);
33         }
34         pipe->aclk = clk;
35
36         clk = of_clk_get_by_name(np, "pxclk");
37         if (IS_ERR(clk)) {
38                 DRM_ERROR("get pxclk for pipeline %d failed!\n", pipe_id);
39                 return PTR_ERR(clk);
40         }
41         pipe->pxlclk = clk;
42
43         /* enum ports */
44         pipe->of_output_dev =
45                 of_graph_get_remote_node(np, KOMEDA_OF_PORT_OUTPUT, 0);
46         pipe->of_output_port =
47                 of_graph_get_port_by_id(np, KOMEDA_OF_PORT_OUTPUT);
48
49         pipe->of_node = np;
50
51         return 0;
52 }
53
54 static int komeda_parse_dt(struct device *dev, struct komeda_dev *mdev)
55 {
56         struct platform_device *pdev = to_platform_device(dev);
57         struct device_node *child, *np = dev->of_node;
58         struct clk *clk;
59         int ret;
60
61         clk = devm_clk_get(dev, "mclk");
62         if (IS_ERR(clk))
63                 return PTR_ERR(clk);
64
65         mdev->mclk = clk;
66         mdev->irq  = platform_get_irq(pdev, 0);
67         if (mdev->irq < 0) {
68                 DRM_ERROR("could not get IRQ number.\n");
69                 return mdev->irq;
70         }
71
72         for_each_available_child_of_node(np, child) {
73                 if (of_node_cmp(child->name, "pipeline") == 0) {
74                         ret = komeda_parse_pipe_dt(mdev, child);
75                         if (ret) {
76                                 DRM_ERROR("parse pipeline dt error!\n");
77                                 of_node_put(child);
78                                 break;
79                         }
80                 }
81         }
82
83         return ret;
84 }
85
86 struct komeda_dev *komeda_dev_create(struct device *dev)
87 {
88         struct platform_device *pdev = to_platform_device(dev);
89         const struct komeda_product_data *product;
90         struct komeda_dev *mdev;
91         struct resource *io_res;
92         int err = 0;
93
94         product = of_device_get_match_data(dev);
95         if (!product)
96                 return ERR_PTR(-ENODEV);
97
98         io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
99         if (!io_res) {
100                 DRM_ERROR("No registers defined.\n");
101                 return ERR_PTR(-ENODEV);
102         }
103
104         mdev = devm_kzalloc(dev, sizeof(*mdev), GFP_KERNEL);
105         if (!mdev)
106                 return ERR_PTR(-ENOMEM);
107
108         mdev->dev = dev;
109         mdev->reg_base = devm_ioremap_resource(dev, io_res);
110         if (IS_ERR(mdev->reg_base)) {
111                 DRM_ERROR("Map register space failed.\n");
112                 err = PTR_ERR(mdev->reg_base);
113                 mdev->reg_base = NULL;
114                 goto err_cleanup;
115         }
116
117         mdev->pclk = devm_clk_get(dev, "pclk");
118         if (IS_ERR(mdev->pclk)) {
119                 DRM_ERROR("Get APB clk failed.\n");
120                 err = PTR_ERR(mdev->pclk);
121                 mdev->pclk = NULL;
122                 goto err_cleanup;
123         }
124
125         /* Enable APB clock to access the registers */
126         clk_prepare_enable(mdev->pclk);
127
128         mdev->funcs = product->identify(mdev->reg_base, &mdev->chip);
129         if (!komeda_product_match(mdev, product->product_id)) {
130                 DRM_ERROR("DT configured %x mismatch with real HW %x.\n",
131                           product->product_id,
132                           MALIDP_CORE_ID_PRODUCT_ID(mdev->chip.core_id));
133                 err = -ENODEV;
134                 goto err_cleanup;
135         }
136
137         DRM_INFO("Found ARM Mali-D%x version r%dp%d\n",
138                  MALIDP_CORE_ID_PRODUCT_ID(mdev->chip.core_id),
139                  MALIDP_CORE_ID_MAJOR(mdev->chip.core_id),
140                  MALIDP_CORE_ID_MINOR(mdev->chip.core_id));
141
142         mdev->funcs->init_format_table(mdev);
143
144         err = mdev->funcs->enum_resources(mdev);
145         if (err) {
146                 DRM_ERROR("enumerate display resource failed.\n");
147                 goto err_cleanup;
148         }
149
150         err = komeda_parse_dt(dev, mdev);
151         if (err) {
152                 DRM_ERROR("parse device tree failed.\n");
153                 goto err_cleanup;
154         }
155
156         err = komeda_assemble_pipelines(mdev);
157         if (err) {
158                 DRM_ERROR("assemble display pipelines failed.\n");
159                 goto err_cleanup;
160         }
161
162         return mdev;
163
164 err_cleanup:
165         komeda_dev_destroy(mdev);
166         return ERR_PTR(err);
167 }
168
169 void komeda_dev_destroy(struct komeda_dev *mdev)
170 {
171         struct device *dev = mdev->dev;
172         struct komeda_dev_funcs *funcs = mdev->funcs;
173         int i;
174
175         for (i = 0; i < mdev->n_pipelines; i++) {
176                 komeda_pipeline_destroy(mdev, mdev->pipelines[i]);
177                 mdev->pipelines[i] = NULL;
178         }
179
180         mdev->n_pipelines = 0;
181
182         if (funcs && funcs->cleanup)
183                 funcs->cleanup(mdev);
184
185         if (mdev->reg_base) {
186                 devm_iounmap(dev, mdev->reg_base);
187                 mdev->reg_base = NULL;
188         }
189
190         if (mdev->mclk) {
191                 devm_clk_put(dev, mdev->mclk);
192                 mdev->mclk = NULL;
193         }
194
195         if (mdev->pclk) {
196                 clk_disable_unprepare(mdev->pclk);
197                 devm_clk_put(dev, mdev->pclk);
198                 mdev->pclk = NULL;
199         }
200
201         devm_kfree(dev, mdev);
202 }