1 // SPDX-License-Identifier: GPL-2.0
3 * (C) COPYRIGHT 2018 ARM Limited. All rights reserved.
4 * Author: James.Qian.Wang <james.qian.wang@arm.com>
8 #include <linux/of_device.h>
9 #include <linux/of_graph.h>
10 #include <linux/platform_device.h>
12 #include <drm/drm_print.h>
14 #include "komeda_dev.h"
16 static int komeda_parse_pipe_dt(struct komeda_dev *mdev, struct device_node *np)
18 struct komeda_pipeline *pipe;
23 ret = of_property_read_u32(np, "reg", &pipe_id);
24 if (ret != 0 || pipe_id >= mdev->n_pipelines)
27 pipe = mdev->pipelines[pipe_id];
29 clk = of_clk_get_by_name(np, "aclk");
31 DRM_ERROR("get aclk for pipeline %d failed!\n", pipe_id);
36 clk = of_clk_get_by_name(np, "pxclk");
38 DRM_ERROR("get pxclk for pipeline %d failed!\n", pipe_id);
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);
54 static int komeda_parse_dt(struct device *dev, struct komeda_dev *mdev)
56 struct device_node *child, *np = dev->of_node;
60 clk = devm_clk_get(dev, "mclk");
66 for_each_available_child_of_node(np, child) {
67 if (of_node_cmp(child->name, "pipeline") == 0) {
68 ret = komeda_parse_pipe_dt(mdev, child);
70 DRM_ERROR("parse pipeline dt error!\n");
80 struct komeda_dev *komeda_dev_create(struct device *dev)
82 struct platform_device *pdev = to_platform_device(dev);
83 const struct komeda_product_data *product;
84 struct komeda_dev *mdev;
85 struct resource *io_res;
88 product = of_device_get_match_data(dev);
90 return ERR_PTR(-ENODEV);
92 io_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
94 DRM_ERROR("No registers defined.\n");
95 return ERR_PTR(-ENODEV);
98 mdev = devm_kzalloc(dev, sizeof(*mdev), GFP_KERNEL);
100 return ERR_PTR(-ENOMEM);
103 mdev->reg_base = devm_ioremap_resource(dev, io_res);
104 if (IS_ERR(mdev->reg_base)) {
105 DRM_ERROR("Map register space failed.\n");
106 err = PTR_ERR(mdev->reg_base);
107 mdev->reg_base = NULL;
111 mdev->pclk = devm_clk_get(dev, "pclk");
112 if (IS_ERR(mdev->pclk)) {
113 DRM_ERROR("Get APB clk failed.\n");
114 err = PTR_ERR(mdev->pclk);
119 /* Enable APB clock to access the registers */
120 clk_prepare_enable(mdev->pclk);
122 mdev->funcs = product->identify(mdev->reg_base, &mdev->chip);
123 if (!komeda_product_match(mdev, product->product_id)) {
124 DRM_ERROR("DT configured %x mismatch with real HW %x.\n",
126 MALIDP_CORE_ID_PRODUCT_ID(mdev->chip.core_id));
131 DRM_INFO("Found ARM Mali-D%x version r%dp%d\n",
132 MALIDP_CORE_ID_PRODUCT_ID(mdev->chip.core_id),
133 MALIDP_CORE_ID_MAJOR(mdev->chip.core_id),
134 MALIDP_CORE_ID_MINOR(mdev->chip.core_id));
136 mdev->funcs->init_format_table(mdev);
138 err = mdev->funcs->enum_resources(mdev);
140 DRM_ERROR("enumerate display resource failed.\n");
144 err = komeda_parse_dt(dev, mdev);
146 DRM_ERROR("parse device tree failed.\n");
150 err = komeda_assemble_pipelines(mdev);
152 DRM_ERROR("assemble display pipelines failed.\n");
159 komeda_dev_destroy(mdev);
163 void komeda_dev_destroy(struct komeda_dev *mdev)
165 struct device *dev = mdev->dev;
166 struct komeda_dev_funcs *funcs = mdev->funcs;
169 for (i = 0; i < mdev->n_pipelines; i++) {
170 komeda_pipeline_destroy(mdev, mdev->pipelines[i]);
171 mdev->pipelines[i] = NULL;
174 mdev->n_pipelines = 0;
176 if (funcs && funcs->cleanup)
177 funcs->cleanup(mdev);
179 if (mdev->reg_base) {
180 devm_iounmap(dev, mdev->reg_base);
181 mdev->reg_base = NULL;
185 devm_clk_put(dev, mdev->mclk);
190 clk_disable_unprepare(mdev->pclk);
191 devm_clk_put(dev, mdev->pclk);
195 devm_kfree(dev, mdev);