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