xref: /OK3568_Linux_fs/kernel/drivers/soc/rockchip/iomux.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2022 Rockchip Electronics Co. Ltd.
4  */
5 
6 #include <linux/device.h>
7 #include <linux/err.h>
8 #include <linux/file.h>
9 #include <linux/fs.h>
10 #include <linux/list.h>
11 #include <linux/uaccess.h>
12 #include <linux/ioctl.h>
13 #include <linux/types.h>
14 #include <linux/miscdevice.h>
15 #include <linux/slab.h>
16 #include <linux/gpio/driver.h>
17 #include <uapi/linux/rk-iomux.h>
18 #include "../../pinctrl/pinctrl-rockchip.h"
19 
20 struct rk_iomux_device {
21 	struct miscdevice dev;
22 };
23 
rk_iomux_ioctl(struct file * file,unsigned int cmd,unsigned long arg)24 static long rk_iomux_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
25 {
26 	struct iomux_ioctl_data data;
27 	int ret = 0;
28 
29 	if (_IOC_SIZE(cmd) > sizeof(data))
30 		return -EINVAL;
31 
32 	if (copy_from_user(&data, (void __user *)arg, _IOC_SIZE(cmd)))
33 		return -EFAULT;
34 
35 	if (!(_IOC_DIR(cmd) & _IOC_WRITE))
36 		memset(&data, 0, sizeof(data));
37 
38 	switch (cmd) {
39 	case IOMUX_IOC_MUX_SET:
40 		ret = rk_iomux_set(data.bank, data.pin, data.mux);
41 		if (ret)
42 			return ret;
43 		break;
44 	case IOMUX_IOC_MUX_GET:
45 		ret = rk_iomux_get(data.bank, data.pin, &data.mux);
46 		if (ret)
47 			return ret;
48 		break;
49 	default:
50 		return -ENOTTY;
51 	}
52 
53 	if (_IOC_DIR(cmd) & _IOC_READ) {
54 		if (copy_to_user((void __user *)arg, &data, _IOC_SIZE(cmd)))
55 			return -EFAULT;
56 	}
57 
58 	return ret;
59 }
60 
61 static const struct file_operations rk_iomux_fops = {
62 	.owner          = THIS_MODULE,
63 	.unlocked_ioctl = rk_iomux_ioctl,
64 	.compat_ioctl	= compat_ptr_ioctl,
65 };
66 
rk_iomux_device_create(void)67 static __init int rk_iomux_device_create(void)
68 {
69 	struct rk_iomux_device *cdev;
70 	int ret;
71 
72 	cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
73 	if (!cdev)
74 		return -ENOMEM;
75 
76 	cdev->dev.minor = MISC_DYNAMIC_MINOR;
77 	cdev->dev.name = "iomux";
78 	cdev->dev.fops = &rk_iomux_fops;
79 	cdev->dev.parent = NULL;
80 	ret = misc_register(&cdev->dev);
81 	if (ret) {
82 		pr_err("failed to register iomux device (%d)\n", ret);
83 		return ret;
84 	}
85 
86 	return 0;
87 }
88 late_initcall(rk_iomux_device_create);
89