^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* linux/drivers/char/nsc_gpio.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) National Semiconductor common GPIO device-file/VFS methods.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) Allows a user space process to control the GPIO pins.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) Copyright (c) 2001,2002 Christer Weinigel <wingel@nano-system.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) Copyright (c) 2005 Jim Cromie <jim.cromie@gmail.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/nsc_gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <asm/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define NAME "nsc_gpio"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) void nsc_gpio_dump(struct nsc_gpio_ops *amp, unsigned index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* retrieve current config w/o changing it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) u32 config = amp->gpio_config(index, ~0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) /* user requested via 'v' command, so its INFO */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) dev_info(amp->dev, "io%02u: 0x%04x %s %s %s %s %s %s %s\tio:%d/%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) index, config,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) (config & 1) ? "OE" : "TS", /* output-enabled/tristate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) (config & 2) ? "PP" : "OD", /* push pull / open drain */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) (config & 4) ? "PUE" : "PUD", /* pull up enabled/disabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) (config & 8) ? "LOCKED" : "", /* locked / unlocked */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) (config & 16) ? "LEVEL" : "EDGE",/* level/edge input */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) (config & 32) ? "HI" : "LO", /* trigger on rise/fall edge */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) (config & 64) ? "DEBOUNCE" : "", /* debounce */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) amp->gpio_get(index), amp->gpio_current(index));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) ssize_t nsc_gpio_write(struct file *file, const char __user *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) size_t len, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned m = iminor(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct nsc_gpio_ops *amp = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct device *dev = amp->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) for (i = 0; i < len; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) char c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (get_user(c, data + i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) switch (c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) case '0':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) amp->gpio_set(m, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) case '1':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) amp->gpio_set(m, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) case 'O':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) dev_dbg(dev, "GPIO%d output enabled\n", m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) amp->gpio_config(m, ~1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) case 'o':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) dev_dbg(dev, "GPIO%d output disabled\n", m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) amp->gpio_config(m, ~1, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) case 'T':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) dev_dbg(dev, "GPIO%d output is push pull\n", m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) amp->gpio_config(m, ~2, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) case 't':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) dev_dbg(dev, "GPIO%d output is open drain\n", m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) amp->gpio_config(m, ~2, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) case 'P':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) dev_dbg(dev, "GPIO%d pull up enabled\n", m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) amp->gpio_config(m, ~4, 4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) case 'p':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) dev_dbg(dev, "GPIO%d pull up disabled\n", m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) amp->gpio_config(m, ~4, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) case 'v':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) /* View Current pin settings */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) amp->gpio_dump(amp, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) case '\n':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* end of settings string, do nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) dev_err(dev, "io%2d bad setting: chr<0x%2x>\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) m, (int)c);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) err++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return -EINVAL; /* full string handled, report error */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) ssize_t nsc_gpio_read(struct file *file, char __user * buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) size_t len, loff_t * ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) unsigned m = iminor(file_inode(file));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct nsc_gpio_ops *amp = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) value = amp->gpio_get(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (put_user(value ? '1' : '0', buf))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) /* common file-ops routines for both scx200_gpio and pc87360_gpio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) EXPORT_SYMBOL(nsc_gpio_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) EXPORT_SYMBOL(nsc_gpio_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) EXPORT_SYMBOL(nsc_gpio_dump);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static int __init nsc_gpio_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) printk(KERN_DEBUG NAME " initializing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) static void __exit nsc_gpio_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) printk(KERN_DEBUG NAME " cleanup\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) module_init(nsc_gpio_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) module_exit(nsc_gpio_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) MODULE_AUTHOR("Jim Cromie <jim.cromie@gmail.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) MODULE_DESCRIPTION("NatSemi GPIO Common Methods");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) MODULE_LICENSE("GPL");