summaryrefslogtreecommitdiff
path: root/io-module/gpio.c
diff options
context:
space:
mode:
authorJesse Gilles <jgilles@multitech.com>2014-05-16 16:43:44 -0500
committerJesse Gilles <jgilles@multitech.com>2014-05-16 16:43:44 -0500
commit9cf9a09c665ecfb6425a08e424b01c8cacbdb660 (patch)
tree4ca4e88e9e53076086610846e863c505e0d48674 /io-module/gpio.c
parentfb0cddc68ee95d61cd2af4889b18bf3d6ddd01d2 (diff)
downloadmts-io-9cf9a09c665ecfb6425a08e424b01c8cacbdb660.tar.gz
mts-io-9cf9a09c665ecfb6425a08e424b01c8cacbdb660.tar.bz2
mts-io-9cf9a09c665ecfb6425a08e424b01c8cacbdb660.zip
Major changes to split sources up
* moved common peripheral i/o functions to separate source files * moved device-specific setup to separate source files * removed LED blink functionality (unused) * mtcdp, mt100eocg are not supported, but code is included for possible future use
Diffstat (limited to 'io-module/gpio.c')
-rw-r--r--io-module/gpio.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/io-module/gpio.c b/io-module/gpio.c
new file mode 100644
index 0000000..72c55f6
--- /dev/null
+++ b/io-module/gpio.c
@@ -0,0 +1,83 @@
+
+struct gpio_pin *gpio_pin_by_name(const char *name) {
+ struct gpio_pin *pin;
+
+ for (pin = gpio_pins; *pin->name; pin++) {
+ if (!strcmp(pin->name, name)) {
+ return pin;
+ }
+ }
+
+ log_error("pin named %s not found", name);
+
+ return NULL;
+}
+
+struct gpio_pin *gpio_pin_by_attr_name(const char *name) {
+ struct gpio_pin *pin;
+
+ for (pin = gpio_pins; *pin->name; pin++) {
+ if (!strcmp(pin->pin.label, name)) {
+ return pin;
+ }
+ }
+
+ log_error("pin with attr name %s not found", name);
+
+ return NULL;
+}
+
+static ssize_t mts_attr_show_gpio_pin(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ int value;
+ struct gpio_pin *pin = gpio_pin_by_attr_name(attr->attr.name);
+
+ if (!pin) {
+ return -ENODEV;
+ }
+
+ mutex_lock(&mts_io_mutex);
+
+ value = gpio_get_value(pin->pin.gpio);
+
+ mutex_unlock(&mts_io_mutex);
+
+ if (value < 0) {
+ return value;
+ }
+
+ if (pin->active_low) {
+ value = !value;
+ }
+
+ return sprintf(buf, "%d\n", value);
+}
+
+static ssize_t mts_attr_store_gpio_pin(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t count)
+{
+ int value;
+ struct gpio_pin *pin = gpio_pin_by_attr_name(attr->attr.name);
+
+ if (!pin) {
+ return -ENODEV;
+ }
+
+ if (sscanf(buf, "%i", &value) != 1) {
+ return -EINVAL;
+ }
+
+ if (pin->active_low) {
+ value = !value;
+ }
+
+ mutex_lock(&mts_io_mutex);
+
+ gpio_set_value(pin->pin.gpio, value);
+
+ mutex_unlock(&mts_io_mutex);
+
+ return count;
+}