1ff4a7481SKuninori Morimoto // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * arch/sh/drivers/dma/dma-sysfs.c
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * sysfs interface for SH DMA API
61da177e4SLinus Torvalds *
74dfc119fSPaul Mundt * Copyright (C) 2004 - 2006 Paul Mundt
81da177e4SLinus Torvalds */
91da177e4SLinus Torvalds #include <linux/kernel.h>
101da177e4SLinus Torvalds #include <linux/init.h>
110c43871bSPaul Gortmaker #include <linux/stat.h>
12dc6876a2SKay Sievers #include <linux/device.h>
130d831770SPaul Mundt #include <linux/platform_device.h>
140d831770SPaul Mundt #include <linux/err.h>
154e57b681STim Schmielau #include <linux/string.h>
161da177e4SLinus Torvalds #include <asm/dma.h>
171da177e4SLinus Torvalds
18*d32b8d7eSRicardo B. Marliere static const struct bus_type dma_subsys = {
19af5ca3f4SKay Sievers .name = "dma",
20dc6876a2SKay Sievers .dev_name = "dma",
211da177e4SLinus Torvalds };
221da177e4SLinus Torvalds
dma_show_devices(struct device * dev,struct device_attribute * attr,char * buf)23dc6876a2SKay Sievers static ssize_t dma_show_devices(struct device *dev,
24dc6876a2SKay Sievers struct device_attribute *attr, char *buf)
251da177e4SLinus Torvalds {
261da177e4SLinus Torvalds ssize_t len = 0;
271da177e4SLinus Torvalds int i;
281da177e4SLinus Torvalds
29e24cca19SPaul Mundt for (i = 0; i < 16; i++) {
301da177e4SLinus Torvalds struct dma_info *info = get_dma_info(i);
314dfc119fSPaul Mundt struct dma_channel *channel = get_dma_channel(i);
324dfc119fSPaul Mundt
334dfc119fSPaul Mundt if (unlikely(!info) || !channel)
344dfc119fSPaul Mundt continue;
351da177e4SLinus Torvalds
361da177e4SLinus Torvalds len += sprintf(buf + len, "%2d: %14s %s\n",
371da177e4SLinus Torvalds channel->chan, info->name,
381da177e4SLinus Torvalds channel->dev_id);
391da177e4SLinus Torvalds }
401da177e4SLinus Torvalds
411da177e4SLinus Torvalds return len;
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds
44dc6876a2SKay Sievers static DEVICE_ATTR(devices, S_IRUGO, dma_show_devices, NULL);
451da177e4SLinus Torvalds
dma_subsys_init(void)46dc6876a2SKay Sievers static int __init dma_subsys_init(void)
471da177e4SLinus Torvalds {
48f6d1975cSGreg Kroah-Hartman struct device *dev_root;
491da177e4SLinus Torvalds int ret;
501da177e4SLinus Torvalds
51dc6876a2SKay Sievers ret = subsys_system_register(&dma_subsys, NULL);
52711fa809SPaul Mundt if (unlikely(ret))
531da177e4SLinus Torvalds return ret;
541da177e4SLinus Torvalds
55f6d1975cSGreg Kroah-Hartman dev_root = bus_get_dev_root(&dma_subsys);
56f6d1975cSGreg Kroah-Hartman if (dev_root) {
57f6d1975cSGreg Kroah-Hartman ret = device_create_file(dev_root, &dev_attr_devices);
58f6d1975cSGreg Kroah-Hartman put_device(dev_root);
59f6d1975cSGreg Kroah-Hartman }
60f6d1975cSGreg Kroah-Hartman return ret;
61711fa809SPaul Mundt }
62dc6876a2SKay Sievers postcore_initcall(dma_subsys_init);
631da177e4SLinus Torvalds
dma_show_dev_id(struct device * dev,struct device_attribute * attr,char * buf)64dc6876a2SKay Sievers static ssize_t dma_show_dev_id(struct device *dev,
65dc6876a2SKay Sievers struct device_attribute *attr, char *buf)
661da177e4SLinus Torvalds {
671da177e4SLinus Torvalds struct dma_channel *channel = to_dma_channel(dev);
681da177e4SLinus Torvalds return sprintf(buf, "%s\n", channel->dev_id);
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds
dma_store_dev_id(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)71dc6876a2SKay Sievers static ssize_t dma_store_dev_id(struct device *dev,
72dc6876a2SKay Sievers struct device_attribute *attr,
731da177e4SLinus Torvalds const char *buf, size_t count)
741da177e4SLinus Torvalds {
751da177e4SLinus Torvalds struct dma_channel *channel = to_dma_channel(dev);
761da177e4SLinus Torvalds strcpy(channel->dev_id, buf);
771da177e4SLinus Torvalds return count;
781da177e4SLinus Torvalds }
791da177e4SLinus Torvalds
80dc6876a2SKay Sievers static DEVICE_ATTR(dev_id, S_IRUGO | S_IWUSR, dma_show_dev_id, dma_store_dev_id);
811da177e4SLinus Torvalds
dma_store_config(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)82dc6876a2SKay Sievers static ssize_t dma_store_config(struct device *dev,
83dc6876a2SKay Sievers struct device_attribute *attr,
841da177e4SLinus Torvalds const char *buf, size_t count)
851da177e4SLinus Torvalds {
861da177e4SLinus Torvalds struct dma_channel *channel = to_dma_channel(dev);
871da177e4SLinus Torvalds unsigned long config;
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds config = simple_strtoul(buf, NULL, 0);
900d831770SPaul Mundt dma_configure_channel(channel->vchan, config);
911da177e4SLinus Torvalds
921da177e4SLinus Torvalds return count;
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds
95dc6876a2SKay Sievers static DEVICE_ATTR(config, S_IWUSR, NULL, dma_store_config);
961da177e4SLinus Torvalds
dma_show_mode(struct device * dev,struct device_attribute * attr,char * buf)97dc6876a2SKay Sievers static ssize_t dma_show_mode(struct device *dev,
98dc6876a2SKay Sievers struct device_attribute *attr, char *buf)
991da177e4SLinus Torvalds {
1001da177e4SLinus Torvalds struct dma_channel *channel = to_dma_channel(dev);
1011da177e4SLinus Torvalds return sprintf(buf, "0x%08x\n", channel->mode);
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds
dma_store_mode(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)104dc6876a2SKay Sievers static ssize_t dma_store_mode(struct device *dev,
105dc6876a2SKay Sievers struct device_attribute *attr,
1061da177e4SLinus Torvalds const char *buf, size_t count)
1071da177e4SLinus Torvalds {
1081da177e4SLinus Torvalds struct dma_channel *channel = to_dma_channel(dev);
1091da177e4SLinus Torvalds channel->mode = simple_strtoul(buf, NULL, 0);
1101da177e4SLinus Torvalds return count;
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds
113dc6876a2SKay Sievers static DEVICE_ATTR(mode, S_IRUGO | S_IWUSR, dma_show_mode, dma_store_mode);
1141da177e4SLinus Torvalds
1151da177e4SLinus Torvalds #define dma_ro_attr(field, fmt) \
116dc6876a2SKay Sievers static ssize_t dma_show_##field(struct device *dev, \
117dc6876a2SKay Sievers struct device_attribute *attr, char *buf)\
1181da177e4SLinus Torvalds { \
1191da177e4SLinus Torvalds struct dma_channel *channel = to_dma_channel(dev); \
1201da177e4SLinus Torvalds return sprintf(buf, fmt, channel->field); \
1211da177e4SLinus Torvalds } \
122dc6876a2SKay Sievers static DEVICE_ATTR(field, S_IRUGO, dma_show_##field, NULL);
1231da177e4SLinus Torvalds
1241da177e4SLinus Torvalds dma_ro_attr(count, "0x%08x\n");
1251da177e4SLinus Torvalds dma_ro_attr(flags, "0x%08lx\n");
1261da177e4SLinus Torvalds
dma_create_sysfs_files(struct dma_channel * chan,struct dma_info * info)1270d831770SPaul Mundt int dma_create_sysfs_files(struct dma_channel *chan, struct dma_info *info)
1281da177e4SLinus Torvalds {
129dc6876a2SKay Sievers struct device *dev = &chan->dev;
1300d831770SPaul Mundt char name[16];
1311da177e4SLinus Torvalds int ret;
1321da177e4SLinus Torvalds
1330d831770SPaul Mundt dev->id = chan->vchan;
134dc6876a2SKay Sievers dev->bus = &dma_subsys;
1351da177e4SLinus Torvalds
136dc6876a2SKay Sievers ret = device_register(dev);
1371da177e4SLinus Torvalds if (ret)
1381da177e4SLinus Torvalds return ret;
1391da177e4SLinus Torvalds
140dc6876a2SKay Sievers ret |= device_create_file(dev, &dev_attr_dev_id);
141dc6876a2SKay Sievers ret |= device_create_file(dev, &dev_attr_count);
142dc6876a2SKay Sievers ret |= device_create_file(dev, &dev_attr_mode);
143dc6876a2SKay Sievers ret |= device_create_file(dev, &dev_attr_flags);
144dc6876a2SKay Sievers ret |= device_create_file(dev, &dev_attr_config);
1454dfc119fSPaul Mundt
1464dfc119fSPaul Mundt if (unlikely(ret)) {
1474dfc119fSPaul Mundt dev_err(&info->pdev->dev, "Failed creating attrs\n");
1484dfc119fSPaul Mundt return ret;
1494dfc119fSPaul Mundt }
1501da177e4SLinus Torvalds
1510d831770SPaul Mundt snprintf(name, sizeof(name), "dma%d", chan->chan);
1520d831770SPaul Mundt return sysfs_create_link(&info->pdev->dev.kobj, &dev->kobj, name);
1530d831770SPaul Mundt }
1540d831770SPaul Mundt
dma_remove_sysfs_files(struct dma_channel * chan,struct dma_info * info)1550d831770SPaul Mundt void dma_remove_sysfs_files(struct dma_channel *chan, struct dma_info *info)
1560d831770SPaul Mundt {
157dc6876a2SKay Sievers struct device *dev = &chan->dev;
1580d831770SPaul Mundt char name[16];
1590d831770SPaul Mundt
160dc6876a2SKay Sievers device_remove_file(dev, &dev_attr_dev_id);
161dc6876a2SKay Sievers device_remove_file(dev, &dev_attr_count);
162dc6876a2SKay Sievers device_remove_file(dev, &dev_attr_mode);
163dc6876a2SKay Sievers device_remove_file(dev, &dev_attr_flags);
164dc6876a2SKay Sievers device_remove_file(dev, &dev_attr_config);
1650d831770SPaul Mundt
1660d831770SPaul Mundt snprintf(name, sizeof(name), "dma%d", chan->chan);
1670d831770SPaul Mundt sysfs_remove_link(&info->pdev->dev.kobj, name);
1680d831770SPaul Mundt
169dc6876a2SKay Sievers device_unregister(dev);
1701da177e4SLinus Torvalds }
171