xref: /openbmc/linux/arch/sh/drivers/dma/dma-sysfs.c (revision c4c3c32d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * arch/sh/drivers/dma/dma-sysfs.c
4  *
5  * sysfs interface for SH DMA API
6  *
7  * Copyright (C) 2004 - 2006  Paul Mundt
8  */
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/stat.h>
12 #include <linux/device.h>
13 #include <linux/platform_device.h>
14 #include <linux/err.h>
15 #include <linux/string.h>
16 #include <asm/dma.h>
17 
18 static struct bus_type dma_subsys = {
19 	.name = "dma",
20 	.dev_name = "dma",
21 };
22 
23 static ssize_t dma_show_devices(struct device *dev,
24 				struct device_attribute *attr, char *buf)
25 {
26 	ssize_t len = 0;
27 	int i;
28 
29 	for (i = 0; i < 16; i++) {
30 		struct dma_info *info = get_dma_info(i);
31 		struct dma_channel *channel = get_dma_channel(i);
32 
33 		if (unlikely(!info) || !channel)
34 			continue;
35 
36 		len += sprintf(buf + len, "%2d: %14s    %s\n",
37 			       channel->chan, info->name,
38 			       channel->dev_id);
39 	}
40 
41 	return len;
42 }
43 
44 static DEVICE_ATTR(devices, S_IRUGO, dma_show_devices, NULL);
45 
46 static int __init dma_subsys_init(void)
47 {
48 	struct device *dev_root;
49 	int ret;
50 
51 	ret = subsys_system_register(&dma_subsys, NULL);
52 	if (unlikely(ret))
53 		return ret;
54 
55 	dev_root = bus_get_dev_root(&dma_subsys);
56 	if (dev_root) {
57 		ret = device_create_file(dev_root, &dev_attr_devices);
58 		put_device(dev_root);
59 	}
60 	return ret;
61 }
62 postcore_initcall(dma_subsys_init);
63 
64 static ssize_t dma_show_dev_id(struct device *dev,
65 				struct device_attribute *attr, char *buf)
66 {
67 	struct dma_channel *channel = to_dma_channel(dev);
68 	return sprintf(buf, "%s\n", channel->dev_id);
69 }
70 
71 static ssize_t dma_store_dev_id(struct device *dev,
72 				struct device_attribute *attr,
73 				const char *buf, size_t count)
74 {
75 	struct dma_channel *channel = to_dma_channel(dev);
76 	strcpy(channel->dev_id, buf);
77 	return count;
78 }
79 
80 static DEVICE_ATTR(dev_id, S_IRUGO | S_IWUSR, dma_show_dev_id, dma_store_dev_id);
81 
82 static ssize_t dma_store_config(struct device *dev,
83 				struct device_attribute *attr,
84 				const char *buf, size_t count)
85 {
86 	struct dma_channel *channel = to_dma_channel(dev);
87 	unsigned long config;
88 
89 	config = simple_strtoul(buf, NULL, 0);
90 	dma_configure_channel(channel->vchan, config);
91 
92 	return count;
93 }
94 
95 static DEVICE_ATTR(config, S_IWUSR, NULL, dma_store_config);
96 
97 static ssize_t dma_show_mode(struct device *dev,
98 				struct device_attribute *attr, char *buf)
99 {
100 	struct dma_channel *channel = to_dma_channel(dev);
101 	return sprintf(buf, "0x%08x\n", channel->mode);
102 }
103 
104 static ssize_t dma_store_mode(struct device *dev,
105 			      struct device_attribute *attr,
106 			      const char *buf, size_t count)
107 {
108 	struct dma_channel *channel = to_dma_channel(dev);
109 	channel->mode = simple_strtoul(buf, NULL, 0);
110 	return count;
111 }
112 
113 static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, dma_show_mode, dma_store_mode);
114 
115 #define dma_ro_attr(field, fmt)						\
116 static ssize_t dma_show_##field(struct device *dev,		\
117 				struct device_attribute *attr, char *buf)\
118 {									\
119 	struct dma_channel *channel = to_dma_channel(dev);		\
120 	return sprintf(buf, fmt, channel->field);			\
121 }									\
122 static DEVICE_ATTR(field, S_IRUGO, dma_show_##field, NULL);
123 
124 dma_ro_attr(count, "0x%08x\n");
125 dma_ro_attr(flags, "0x%08lx\n");
126 
127 int dma_create_sysfs_files(struct dma_channel *chan, struct dma_info *info)
128 {
129 	struct device *dev = &chan->dev;
130 	char name[16];
131 	int ret;
132 
133 	dev->id  = chan->vchan;
134 	dev->bus = &dma_subsys;
135 
136 	ret = device_register(dev);
137 	if (ret)
138 		return ret;
139 
140 	ret |= device_create_file(dev, &dev_attr_dev_id);
141 	ret |= device_create_file(dev, &dev_attr_count);
142 	ret |= device_create_file(dev, &dev_attr_mode);
143 	ret |= device_create_file(dev, &dev_attr_flags);
144 	ret |= device_create_file(dev, &dev_attr_config);
145 
146 	if (unlikely(ret)) {
147 		dev_err(&info->pdev->dev, "Failed creating attrs\n");
148 		return ret;
149 	}
150 
151 	snprintf(name, sizeof(name), "dma%d", chan->chan);
152 	return sysfs_create_link(&info->pdev->dev.kobj, &dev->kobj, name);
153 }
154 
155 void dma_remove_sysfs_files(struct dma_channel *chan, struct dma_info *info)
156 {
157 	struct device *dev = &chan->dev;
158 	char name[16];
159 
160 	device_remove_file(dev, &dev_attr_dev_id);
161 	device_remove_file(dev, &dev_attr_count);
162 	device_remove_file(dev, &dev_attr_mode);
163 	device_remove_file(dev, &dev_attr_flags);
164 	device_remove_file(dev, &dev_attr_config);
165 
166 	snprintf(name, sizeof(name), "dma%d", chan->chan);
167 	sysfs_remove_link(&info->pdev->dev.kobj, name);
168 
169 	device_unregister(dev);
170 }
171