Linux Audio

Check our new training course

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