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