Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (c) 2022 Rockchip Electronics Co. Ltd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <sys/ioctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "../../../../include/uapi/linux/rk-iomux.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) static int rk_iomux_ioctl_set(int fd, int bank, int pin, int mux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 	struct iomux_ioctl_data data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 		.bank = bank,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		.pin = pin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 		.mux = mux,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	if (!fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	ret = ioctl(fd, IOMUX_IOC_MUX_SET, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 		perror("fail to ioctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) static int rk_iomux_ioctl_get(int fd, int bank, int pin, int *mux)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct iomux_ioctl_data data = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 		.bank = bank,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 		.pin = pin,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	if (!fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	ret = ioctl(fd, IOMUX_IOC_MUX_GET, &data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		perror("fail to ioctl");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	*mux = data.mux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) static void usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	printf("%s:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		"set iomux:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		"iomux [bank index] [pin index] [mux value]\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		"get iomux:\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		"iomux [bank index] [pin index]\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		__func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	const char *name = "/dev/iomux";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	int fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	int bank, pin, mux;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	if ((argc != 3) && (argc != 4)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	bank = atoi(argv[1]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	pin = atoi(argv[2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	fd = open(name, O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	if (fd < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		printf("open %s failed!\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return fd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (argc == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		mux = atoi(argv[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		ret = rk_iomux_ioctl_set(fd, bank, pin, mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	} else if (argc == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 		ret = rk_iomux_ioctl_get(fd, bank, pin, &mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			goto err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 		printf("mux get (GPIO%d-%d) = %d\n", bank, pin, mux);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }