Merge tag 'devicetree-fixes-for-5.11-1' of git://git.kernel.org/pub/scm/linux/kernel...
[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/of_reserved_mem.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/dma-mapping.h>
15 #ifdef CONFIG_DEBUG_FS
16 #include <linux/debugfs.h>
17 #include <linux/seq_file.h>
18 #endif
19
20 #include <drm/drm_print.h>
21
22 #include "komeda_dev.h"
23
24 static int komeda_register_show(struct seq_file *sf, void *x)
25 {
26         struct komeda_dev *mdev = sf->private;
27         int i;
28
29         seq_puts(sf, "\n====== Komeda register dump =========\n");
30
31         pm_runtime_get_sync(mdev->dev);
32
33         if (mdev->funcs->dump_register)
34                 mdev->funcs->dump_register(mdev, sf);
35
36         for (i = 0; i < mdev->n_pipelines; i++)
37                 komeda_pipeline_dump_register(mdev->pipelines[i], sf);
38
39         pm_runtime_put(mdev->dev);
40
41         return 0;
42 }
43
44 DEFINE_SHOW_ATTRIBUTE(komeda_register);
45
46 #ifdef CONFIG_DEBUG_FS
47 static void komeda_debugfs_init(struct komeda_dev *mdev)
48 {
49         if (!debugfs_initialized())
50                 return;
51
52         mdev->debugfs_root = debugfs_create_dir("komeda", NULL);
53         debugfs_create_file("register", 0444, mdev->debugfs_root,
54                             mdev, &komeda_register_fops);
55         debugfs_create_x16("err_verbosity", 0664, mdev->debugfs_root,
56                            &mdev->err_verbosity);
57 }
58 #endif
59
60 static ssize_t
61 core_id_show(struct device *dev, struct device_attribute *attr, char *buf)
62 {
63         struct komeda_dev *mdev = dev_to_mdev(dev);
64
65         return snprintf(buf, PAGE_SIZE, "0x%08x\n", mdev->chip.core_id);
66 }
67 static DEVICE_ATTR_RO(core_id);
68
69 static ssize_t
70 config_id_show(struct device *dev, struct device_attribute *attr, char *buf)
71 {
72         struct komeda_dev *mdev = dev_to_mdev(dev);
73         struct komeda_pipeline *pipe = mdev->pipelines[0];
74         union komeda_config_id config_id;
75         int i;
76
77         memset(&config_id, 0, sizeof(config_id));
78
79         config_id.max_line_sz = pipe->layers[0]->hsize_in.end;
80         config_id.n_pipelines = mdev->n_pipelines;
81         config_id.n_scalers = pipe->n_scalers;
82         config_id.n_layers = pipe->n_layers;
83         config_id.n_richs = 0;
84         for (i = 0; i < pipe->n_layers; i++) {
85                 if (pipe->layers[i]->layer_type == KOMEDA_FMT_RICH_LAYER)
86                         config_id.n_richs++;
87         }
88         return snprintf(buf, PAGE_SIZE, "0x%08x\n", config_id.value);
89 }
90 static DEVICE_ATTR_RO(config_id);
91
92 static ssize_t
93 aclk_hz_show(struct device *dev, struct device_attribute *attr, char *buf)
94 {
95         struct komeda_dev *mdev = dev_to_mdev(dev);
96
97         return snprintf(buf, PAGE_SIZE, "%lu\n", clk_get_rate(mdev->aclk));
98 }
99 static DEVICE_ATTR_RO(aclk_hz);
100
101 static struct attribute *komeda_sysfs_entries[] = {
102         &dev_attr_core_id.attr,
103         &dev_attr_config_id.attr,
104         &dev_attr_aclk_hz.attr,
105         NULL,
106 };
107
108 static struct attribute_group komeda_sysfs_attr_group = {
109         .attrs = komeda_sysfs_entries,
110 };
111
112 static int komeda_parse_pipe_dt(struct komeda_pipeline *pipe)
113 {
114         struct device_node *np = pipe->of_node;
115         struct clk *clk;
116
117         clk = of_clk_get_by_name(np, "pxclk");
118         if (IS_ERR(clk)) {
119                 DRM_ERROR("get pxclk for pipeline %d failed!\n", pipe->id);
120                 return PTR_ERR(clk);
121         }
122         pipe->pxlclk = clk;
123
124         /* enum ports */
125         pipe->of_output_links[0] =
126                 of_graph_get_remote_node(np, KOMEDA_OF_PORT_OUTPUT, 0);
127         pipe->of_output_links[1] =
128                 of_graph_get_remote_node(np, KOMEDA_OF_PORT_OUTPUT, 1);
129         pipe->of_output_port =
130                 of_graph_get_port_by_id(np, KOMEDA_OF_PORT_OUTPUT);
131
132         pipe->dual_link = pipe->of_output_links[0] && pipe->of_output_links[1];
133
134         return 0;
135 }
136
137 static int komeda_parse_dt(struct device *dev, struct komeda_dev *mdev)
138 {
139         struct platform_device *pdev = to_platform_device(dev);
140         struct device_node *child, *np = dev->of_node;
141         struct komeda_pipeline *pipe;
142         u32 pipe_id = U32_MAX;
143         int ret = -1;
144
145         mdev->irq  = platform_get_irq(pdev, 0);
146         if (mdev->irq < 0) {
147                 DRM_ERROR("could not get IRQ number.\n");
148                 return mdev->irq;
149         }
150
151         /* Get the optional framebuffer memory resource */
152         ret = of_reserved_mem_device_init(dev);
153         if (ret && ret != -ENODEV)
154                 return ret;
155         ret = 0;
156
157         for_each_available_child_of_node(np, child) {
158                 if (of_node_name_eq(child, "pipeline")) {
159                         of_property_read_u32(child, "reg", &pipe_id);
160                         if (pipe_id >= mdev->n_pipelines) {
161                                 DRM_WARN("Skip the redundant DT node: pipeline-%u.\n",
162                                          pipe_id);
163                                 continue;
164                         }
165                         mdev->pipelines[pipe_id]->of_node = of_node_get(child);
166                 }
167         }
168
169         for (pipe_id = 0; pipe_id < mdev->n_pipelines; pipe_id++) {
170                 pipe = mdev->pipelines[pipe_id];
171
172                 if (!pipe->of_node) {
173                         DRM_ERROR("Pipeline-%d doesn't have a DT node.\n",
174                                   pipe->id);
175                         return -EINVAL;
176                 }
177                 ret = komeda_parse_pipe_dt(pipe);
178                 if (ret)
179                         return ret;
180         }
181
182         return 0;
183 }
184
185 struct komeda_dev *komeda_dev_create(struct device *dev)
186 {
187         struct platform_device *pdev = to_platform_device(dev);
188         komeda_identify_func komeda_identify;
189         struct komeda_dev *mdev;
190         int err = 0;
191
192         komeda_identify = of_device_get_match_data(dev);
193         if (!komeda_identify)
194                 return ERR_PTR(-ENODEV);
195
196         mdev = devm_kzalloc(dev, sizeof(*mdev), GFP_KERNEL);
197         if (!mdev)
198                 return ERR_PTR(-ENOMEM);
199
200         mutex_init(&mdev->lock);
201
202         mdev->dev = dev;
203         mdev->reg_base = devm_platform_ioremap_resource(pdev, 0);
204         if (IS_ERR(mdev->reg_base)) {
205                 DRM_ERROR("Map register space failed.\n");
206                 err = PTR_ERR(mdev->reg_base);
207                 mdev->reg_base = NULL;
208                 goto err_cleanup;
209         }
210
211         mdev->aclk = devm_clk_get(dev, "aclk");
212         if (IS_ERR(mdev->aclk)) {
213                 DRM_ERROR("Get engine clk failed.\n");
214                 err = PTR_ERR(mdev->aclk);
215                 mdev->aclk = NULL;
216                 goto err_cleanup;
217         }
218
219         clk_prepare_enable(mdev->aclk);
220
221         mdev->funcs = komeda_identify(mdev->reg_base, &mdev->chip);
222         if (!mdev->funcs) {
223                 DRM_ERROR("Failed to identify the HW.\n");
224                 err = -ENODEV;
225                 goto disable_clk;
226         }
227
228         DRM_INFO("Found ARM Mali-D%x version r%dp%d\n",
229                  MALIDP_CORE_ID_PRODUCT_ID(mdev->chip.core_id),
230                  MALIDP_CORE_ID_MAJOR(mdev->chip.core_id),
231                  MALIDP_CORE_ID_MINOR(mdev->chip.core_id));
232
233         mdev->funcs->init_format_table(mdev);
234
235         err = mdev->funcs->enum_resources(mdev);
236         if (err) {
237                 DRM_ERROR("enumerate display resource failed.\n");
238                 goto disable_clk;
239         }
240
241         err = komeda_parse_dt(dev, mdev);
242         if (err) {
243                 DRM_ERROR("parse device tree failed.\n");
244                 goto disable_clk;
245         }
246
247         err = komeda_assemble_pipelines(mdev);
248         if (err) {
249                 DRM_ERROR("assemble display pipelines failed.\n");
250                 goto disable_clk;
251         }
252
253         dma_set_max_seg_size(dev, U32_MAX);
254
255         mdev->iommu = iommu_get_domain_for_dev(mdev->dev);
256         if (!mdev->iommu)
257                 DRM_INFO("continue without IOMMU support!\n");
258
259         clk_disable_unprepare(mdev->aclk);
260
261         err = sysfs_create_group(&dev->kobj, &komeda_sysfs_attr_group);
262         if (err) {
263                 DRM_ERROR("create sysfs group failed.\n");
264                 goto err_cleanup;
265         }
266
267         mdev->err_verbosity = KOMEDA_DEV_PRINT_ERR_EVENTS;
268
269 #ifdef CONFIG_DEBUG_FS
270         komeda_debugfs_init(mdev);
271 #endif
272
273         return mdev;
274
275 disable_clk:
276         clk_disable_unprepare(mdev->aclk);
277 err_cleanup:
278         komeda_dev_destroy(mdev);
279         return ERR_PTR(err);
280 }
281
282 void komeda_dev_destroy(struct komeda_dev *mdev)
283 {
284         struct device *dev = mdev->dev;
285         const struct komeda_dev_funcs *funcs = mdev->funcs;
286         int i;
287
288         sysfs_remove_group(&dev->kobj, &komeda_sysfs_attr_group);
289
290 #ifdef CONFIG_DEBUG_FS
291         debugfs_remove_recursive(mdev->debugfs_root);
292 #endif
293
294         if (mdev->aclk)
295                 clk_prepare_enable(mdev->aclk);
296
297         for (i = 0; i < mdev->n_pipelines; i++) {
298                 komeda_pipeline_destroy(mdev, mdev->pipelines[i]);
299                 mdev->pipelines[i] = NULL;
300         }
301
302         mdev->n_pipelines = 0;
303
304         of_reserved_mem_device_release(dev);
305
306         if (funcs && funcs->cleanup)
307                 funcs->cleanup(mdev);
308
309         if (mdev->reg_base) {
310                 devm_iounmap(dev, mdev->reg_base);
311                 mdev->reg_base = NULL;
312         }
313
314         if (mdev->aclk) {
315                 clk_disable_unprepare(mdev->aclk);
316                 devm_clk_put(dev, mdev->aclk);
317                 mdev->aclk = NULL;
318         }
319
320         devm_kfree(dev, mdev);
321 }
322
323 int komeda_dev_resume(struct komeda_dev *mdev)
324 {
325         clk_prepare_enable(mdev->aclk);
326
327         mdev->funcs->enable_irq(mdev);
328
329         if (mdev->iommu && mdev->funcs->connect_iommu)
330                 if (mdev->funcs->connect_iommu(mdev))
331                         DRM_ERROR("connect iommu failed.\n");
332
333         return 0;
334 }
335
336 int komeda_dev_suspend(struct komeda_dev *mdev)
337 {
338         if (mdev->iommu && mdev->funcs->disconnect_iommu)
339                 if (mdev->funcs->disconnect_iommu(mdev))
340                         DRM_ERROR("disconnect iommu failed.\n");
341
342         mdev->funcs->disable_irq(mdev);
343
344         clk_disable_unprepare(mdev->aclk);
345
346         return 0;
347 }