x86/platform/intel/iosf_mbi Rewrite locking
[linux-2.6-microblaze.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/iommu.h>
9 #include <linux/of_device.h>
10 #include <linux/of_graph.h>
11 #include <linux/platform_device.h>
12 #include <linux/dma-mapping.h>
13 #ifdef CONFIG_DEBUG_FS
14 #include <linux/debugfs.h>
15 #include <linux/seq_file.h>
16 #endif
17
18 #include <drm/drm_print.h>
19
20 #include "komeda_dev.h"
21
22 static int komeda_register_show(struct seq_file *sf, void *x)
23 {
24         struct komeda_dev *mdev = sf->private;
25         int i;
26
27         if (mdev->funcs->dump_register)
28                 mdev->funcs->dump_register(mdev, sf);
29
30         for (i = 0; i < mdev->n_pipelines; i++)
31                 komeda_pipeline_dump_register(mdev->pipelines[i], sf);
32
33         return 0;
34 }
35
36 static int komeda_register_open(struct inode *inode, struct file *filp)
37 {
38         return single_open(filp, komeda_register_show, inode->i_private);
39 }
40
41 static const struct file_operations komeda_register_fops = {
42         .owner          = THIS_MODULE,
43         .open           = komeda_register_open,
44         .read           = seq_read,
45         .llseek         = seq_lseek,
46         .release        = single_release,
47 };
48
49 #ifdef CONFIG_DEBUG_FS
50 static void komeda_debugfs_init(struct komeda_dev *mdev)
51 {
52         if (!debugfs_initialized())
53                 return;
54
55         mdev->debugfs_root = debugfs_create_dir("komeda", NULL);
56         debugfs_create_file("register", 0444, mdev->debugfs_root,
57                             mdev, &komeda_register_fops);
58 }
59 #endif
60
61 static ssize_t
62 core_id_show(struct device *dev, struct device_attribute *attr, char *buf)
63 {
64         struct komeda_dev *mdev = dev_to_mdev(dev);
65
66         return snprintf(buf, PAGE_SIZE, "0x%08x\n", mdev->chip.core_id);
67 }
68 static DEVICE_ATTR_RO(core_id);
69
70 static ssize_t
71 config_id_show(struct device *dev, struct device_attribute *attr, char *buf)
72 {
73         struct komeda_dev *mdev = dev_to_mdev(dev);
74         struct komeda_pipeline *pipe = mdev->pipelines[0];
75         union komeda_config_id config_id;
76         int i;
77
78         memset(&config_id, 0, sizeof(config_id));
79
80         config_id.max_line_sz = pipe->layers[0]->hsize_in.end;
81         config_id.n_pipelines = mdev->n_pipelines;
82         config_id.n_scalers = pipe->n_scalers;
83         config_id.n_layers = pipe->n_layers;
84         config_id.n_richs = 0;
85         for (i = 0; i < pipe->n_layers; i++) {
86                 if (pipe->layers[i]->layer_type == KOMEDA_FMT_RICH_LAYER)
87                         config_id.n_richs++;
88         }
89         return snprintf(buf, PAGE_SIZE, "0x%08x\n", config_id.value);
90 }
91 static DEVICE_ATTR_RO(config_id);
92
93 static struct attribute *komeda_sysfs_entries[] = {
94         &dev_attr_core_id.attr,
95         &dev_attr_config_id.attr,
96         NULL,
97 };
98
99 static struct attribute_group komeda_sysfs_attr_group = {
100         .attrs = komeda_sysfs_entries,
101 };
102
103 static int komeda_parse_pipe_dt(struct komeda_dev *mdev, struct device_node *np)
104 {
105         struct komeda_pipeline *pipe;
106         struct clk *clk;
107         u32 pipe_id;
108         int ret = 0;
109
110         ret = of_property_read_u32(np, "reg", &pipe_id);
111         if (ret != 0 || pipe_id >= mdev->n_pipelines)
112                 return -EINVAL;
113
114         pipe = mdev->pipelines[pipe_id];
115
116         clk = of_clk_get_by_name(np, "pxclk");
117         if (IS_ERR(clk)) {
118                 DRM_ERROR("get pxclk for pipeline %d failed!\n", pipe_id);
119                 return PTR_ERR(clk);
120         }
121         pipe->pxlclk = clk;
122
123         /* enum ports */
124         pipe->of_output_dev =
125                 of_graph_get_remote_node(np, KOMEDA_OF_PORT_OUTPUT, 0);
126         pipe->of_output_port =
127                 of_graph_get_port_by_id(np, KOMEDA_OF_PORT_OUTPUT);
128
129         pipe->of_node = np;
130
131         return 0;
132 }
133
134 static int komeda_parse_dt(struct device *dev, struct komeda_dev *mdev)
135 {
136         struct platform_device *pdev = to_platform_device(dev);
137         struct device_node *child, *np = dev->of_node;
138         int ret;
139
140         mdev->irq  = platform_get_irq(pdev, 0);
141         if (mdev->irq < 0) {
142                 DRM_ERROR("could not get IRQ number.\n");
143                 return mdev->irq;
144         }
145
146         for_each_available_child_of_node(np, child) {
147                 if (of_node_cmp(child->name, "pipeline") == 0) {
148                         ret = komeda_parse_pipe_dt(mdev, child);
149                         if (ret) {
150                                 DRM_ERROR("parse pipeline dt error!\n");
151                                 of_node_put(child);
152                                 break;
153                         }
154                 }
155         }
156
157         return ret;
158 }
159
160 struct komeda_dev *komeda_dev_create(struct device *dev)
161 {
162         struct platform_device *pdev = to_platform_device(dev);
163         const struct komeda_product_data *product;
164         struct komeda_dev *mdev;
165         struct resource *io_res;
166         int err = 0;
167
168         product = of_device_get_match_data(dev);
169         if (!product)
170                 return ERR_PTR(-ENODEV);
171
172         io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
173         if (!io_res) {
174                 DRM_ERROR("No registers defined.\n");
175                 return ERR_PTR(-ENODEV);
176         }
177
178         mdev = devm_kzalloc(dev, sizeof(*mdev), GFP_KERNEL);
179         if (!mdev)
180                 return ERR_PTR(-ENOMEM);
181
182         mutex_init(&mdev->lock);
183
184         mdev->dev = dev;
185         mdev->reg_base = devm_ioremap_resource(dev, io_res);
186         if (IS_ERR(mdev->reg_base)) {
187                 DRM_ERROR("Map register space failed.\n");
188                 err = PTR_ERR(mdev->reg_base);
189                 mdev->reg_base = NULL;
190                 goto err_cleanup;
191         }
192
193         mdev->aclk = devm_clk_get(dev, "aclk");
194         if (IS_ERR(mdev->aclk)) {
195                 DRM_ERROR("Get engine clk failed.\n");
196                 err = PTR_ERR(mdev->aclk);
197                 mdev->aclk = NULL;
198                 goto err_cleanup;
199         }
200
201         clk_prepare_enable(mdev->aclk);
202
203         mdev->funcs = product->identify(mdev->reg_base, &mdev->chip);
204         if (!komeda_product_match(mdev, product->product_id)) {
205                 DRM_ERROR("DT configured %x mismatch with real HW %x.\n",
206                           product->product_id,
207                           MALIDP_CORE_ID_PRODUCT_ID(mdev->chip.core_id));
208                 err = -ENODEV;
209                 goto err_cleanup;
210         }
211
212         DRM_INFO("Found ARM Mali-D%x version r%dp%d\n",
213                  MALIDP_CORE_ID_PRODUCT_ID(mdev->chip.core_id),
214                  MALIDP_CORE_ID_MAJOR(mdev->chip.core_id),
215                  MALIDP_CORE_ID_MINOR(mdev->chip.core_id));
216
217         mdev->funcs->init_format_table(mdev);
218
219         err = mdev->funcs->enum_resources(mdev);
220         if (err) {
221                 DRM_ERROR("enumerate display resource failed.\n");
222                 goto err_cleanup;
223         }
224
225         err = komeda_parse_dt(dev, mdev);
226         if (err) {
227                 DRM_ERROR("parse device tree failed.\n");
228                 goto err_cleanup;
229         }
230
231         err = komeda_assemble_pipelines(mdev);
232         if (err) {
233                 DRM_ERROR("assemble display pipelines failed.\n");
234                 goto err_cleanup;
235         }
236
237         dev->dma_parms = &mdev->dma_parms;
238         dma_set_max_seg_size(dev, DMA_BIT_MASK(32));
239
240         mdev->iommu = iommu_get_domain_for_dev(mdev->dev);
241         if (!mdev->iommu)
242                 DRM_INFO("continue without IOMMU support!\n");
243
244         if (mdev->iommu && mdev->funcs->connect_iommu) {
245                 err = mdev->funcs->connect_iommu(mdev);
246                 if (err) {
247                         mdev->iommu = NULL;
248                         goto err_cleanup;
249                 }
250         }
251
252         err = sysfs_create_group(&dev->kobj, &komeda_sysfs_attr_group);
253         if (err) {
254                 DRM_ERROR("create sysfs group failed.\n");
255                 goto err_cleanup;
256         }
257
258 #ifdef CONFIG_DEBUG_FS
259         komeda_debugfs_init(mdev);
260 #endif
261
262         return mdev;
263
264 err_cleanup:
265         komeda_dev_destroy(mdev);
266         return ERR_PTR(err);
267 }
268
269 void komeda_dev_destroy(struct komeda_dev *mdev)
270 {
271         struct device *dev = mdev->dev;
272         const struct komeda_dev_funcs *funcs = mdev->funcs;
273         int i;
274
275         sysfs_remove_group(&dev->kobj, &komeda_sysfs_attr_group);
276
277 #ifdef CONFIG_DEBUG_FS
278         debugfs_remove_recursive(mdev->debugfs_root);
279 #endif
280
281         if (mdev->iommu && mdev->funcs->disconnect_iommu)
282                 mdev->funcs->disconnect_iommu(mdev);
283         mdev->iommu = NULL;
284
285         for (i = 0; i < mdev->n_pipelines; i++) {
286                 komeda_pipeline_destroy(mdev, mdev->pipelines[i]);
287                 mdev->pipelines[i] = NULL;
288         }
289
290         mdev->n_pipelines = 0;
291
292         if (funcs && funcs->cleanup)
293                 funcs->cleanup(mdev);
294
295         if (mdev->reg_base) {
296                 devm_iounmap(dev, mdev->reg_base);
297                 mdev->reg_base = NULL;
298         }
299
300         if (mdev->aclk) {
301                 clk_disable_unprepare(mdev->aclk);
302                 devm_clk_put(dev, mdev->aclk);
303                 mdev->aclk = NULL;
304         }
305
306         devm_kfree(dev, mdev);
307 }