summaryrefslogtreecommitdiff
path: root/io-module/mtac_gpiob.c
diff options
context:
space:
mode:
authorMike Fiore <mfiore@multitech.com>2014-09-10 11:26:03 -0500
committerMike Fiore <mfiore@multitech.com>2014-09-10 11:26:03 -0500
commit64d8ebafbf55fa4a4547184bfbdc73ad43eb6a4c (patch)
treef36bc4934c3b6ff128bb7d8d31aded5a0f7fe344 /io-module/mtac_gpiob.c
parent5503d316c26d664117ff899c0dece6efc5ea7077 (diff)
downloadmts-io-64d8ebafbf55fa4a4547184bfbdc73ad43eb6a4c.tar.gz
mts-io-64d8ebafbf55fa4a4547184bfbdc73ad43eb6a4c.tar.bz2
mts-io-64d8ebafbf55fa4a4547184bfbdc73ad43eb6a4c.zip
rename mtdc_gpiob.c to mtac_gpiob.c
Diffstat (limited to 'io-module/mtac_gpiob.c')
-rw-r--r--io-module/mtac_gpiob.c409
1 files changed, 409 insertions, 0 deletions
diff --git a/io-module/mtac_gpiob.c b/io-module/mtac_gpiob.c
new file mode 100644
index 0000000..5a592b7
--- /dev/null
+++ b/io-module/mtac_gpiob.c
@@ -0,0 +1,409 @@
+struct gpio_pin *ap_gpio_pin_by_attr_name(const char *name) {
+ struct gpio_pin *pin;
+ char *pin_attr_name;
+
+ if (!strcmp(name, "ap1-led1")) {
+ pin_attr_name = "ap1-gpio3";
+ } else if (!strcmp(name, "ap1-led2")) {
+ pin_attr_name = "ap1-gpio4";
+ } else if (!strcmp(name, "ap1-dout-enable")) {
+ pin_attr_name = "ap1-gpio1";
+ } else if (!strcmp(name, "ap1-reset")) {
+ pin_attr_name = "ap1-reset";
+ } else if (!strcmp(name, "ap2-led1")) {
+ pin_attr_name = "ap2-gpio3";
+ } else if (!strcmp(name, "ap2-led2")) {
+ pin_attr_name = "ap2-gpio4";
+ } else if (!strcmp(name, "ap2-dout-enable")) {
+ pin_attr_name = "ap2-gpio1";
+ } else if (!strcmp(name, "ap2-reset")) {
+ pin_attr_name = "ap2-reset";
+ } else {
+ log_error("accessory card attribute %s not available", name);
+ return NULL;
+ }
+
+ for (pin = gpio_pins; *pin->name; pin++) {
+ if (!strcmp(pin->pin.label, pin_attr_name)) {
+ return pin;
+ }
+ }
+
+ log_error("pin with attr name %s not found", name);
+
+ return NULL;
+}
+
+
+static ssize_t mts_attr_show_ap_gpio_pin(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ int value;
+ struct gpio_pin *pin = ap_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_ap_gpio_pin(struct device *dev,
+ struct device_attribute *attr, const char *buf, size_t count)
+{
+ int value;
+ struct gpio_pin *pin = ap_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;
+}
+
+static ssize_t mts_attr_show_ap_din(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ int tmp;
+ u8 bit;
+ u8 byte;
+ struct spi_device* spi_dev;
+
+ if (strstr(attr->attr.name, ":0")) {
+ if (!spi_ap1_din_dev) {
+ log_error("accessory card 1 din device not present");
+ return -ENODEV;
+ }
+
+ spi_dev = spi_ap1_din_dev;
+ } else if (strstr(attr->attr.name, ":1")) {
+ if (!spi_ap2_din_dev) {
+ log_error("accessory card 2 din device not present");
+ return -ENODEV;
+ }
+
+ spi_dev = spi_ap2_din_dev;
+ } else {
+ log_error("unknown din device %s", attr->attr.name);
+ return -ENODEV;
+ }
+
+ if (strstr(attr->attr.name, "din0")) {
+ bit = BIT(0);
+ } else if (strstr(attr->attr.name, "din1")) {
+ bit = BIT(1);
+ } else if (strstr(attr->attr.name, "din2")) {
+ bit = BIT(2);
+ } else if (strstr(attr->attr.name, "din3")) {
+ bit = BIT(3);
+ } else {
+ log_error("accessory card din attr does not exist");
+ return -ENOENT;
+ }
+
+ tmp = spi_readn(spi_dev, &byte, 1);
+ if (tmp) {
+ log_error("spi_read failed %d", tmp);
+ return tmp;
+ }
+
+ tmp = byte & bit ? 1 : 0;
+
+ return sprintf(buf, "%d\n", tmp);
+}
+
+static ssize_t mts_attr_store_ap_dout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
+{
+ int value;
+ u8 bit;
+ struct spi_device* spi_dev;
+
+ if (strstr(attr->attr.name, ":0")) {
+ if (!spi_ap1_dout_dev) {
+ log_error("accessory card 1 dout device not present");
+ return -ENODEV;
+ }
+
+ spi_dev = spi_ap1_dout_dev;
+ } else if (strstr(attr->attr.name, ":1")) {
+ if (!spi_ap2_dout_dev) {
+ log_error("accessory card 2 dout device not present");
+ return -ENODEV;
+ }
+
+ spi_dev = spi_ap2_dout_dev;
+ } else {
+ log_error("unknown dout device %s", attr->attr.name);
+ return -ENODEV;
+ }
+
+ if (strstr(attr->attr.name, "dout0")) {
+ bit = BIT(0);
+ } else if (strstr(attr->attr.name, "dout1")) {
+ bit = BIT(1);
+ } else if (strstr(attr->attr.name, "dout2")) {
+ bit = BIT(2);
+ } else if (strstr(attr->attr.name, "dout3")) {
+ bit = BIT(3);
+ } else {
+ log_error("accessory card dout attr does not exist");
+ return -ENOENT;
+ }
+
+ if (sscanf(buf, "%i", &value) != 1) {
+ log_error("accessory card dout attr invalid argument");
+ return -EINVAL;
+ }
+
+ mutex_lock(&spi_ap_dout_mutex);
+
+ if (value) {
+ spi_ap_dout_value &= ~bit;
+ } else {
+ spi_ap_dout_value |= bit;
+ }
+
+ spi_writen(spi_dev, &spi_ap_dout_value, 1);
+
+ mutex_unlock(&spi_ap_dout_mutex);
+
+ return count;
+}
+
+static ssize_t mts_attr_show_ap_dout(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ int value;
+ u8 bit;
+ struct spi_device* spi_dev;
+
+ if (strstr(attr->attr.name, ":0")) {
+ if (!spi_ap1_dout_dev) {
+ log_error("accessory card 1 dout device not present");
+ return -ENODEV;
+ }
+
+ spi_dev = spi_ap1_dout_dev;
+ } else if (strstr(attr->attr.name, ":1")) {
+ if (!spi_ap2_dout_dev) {
+ log_error("accessory card 2 dout device not present");
+ return -ENODEV;
+ }
+
+ spi_dev = spi_ap2_dout_dev;
+ } else {
+ log_error("unknown dout device %s", attr->attr.name);
+ return -ENODEV;
+ }
+
+ if (strstr(attr->attr.name, "dout0")) {
+ bit = BIT(0);
+ } else if (strstr(attr->attr.name, "dout1")) {
+ bit = BIT(1);
+ } else if (strstr(attr->attr.name, "dout2")) {
+ bit = BIT(2);
+ } else if (strstr(attr->attr.name, "dout3")) {
+ bit = BIT(3);
+ } else {
+ log_error("accessory card dout attr does not exist");
+ return -ENOENT;
+ }
+
+ mutex_lock(&spi_ap_dout_mutex);
+
+ value = spi_ap_dout_value & bit ? 0 : 1;
+
+ mutex_unlock(&spi_ap_dout_mutex);
+
+ return sprintf(buf, "%d\n", value);
+}
+
+static ssize_t mts_attr_show_ap_adc(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ int tmp;
+ int tx_data;
+ int rx_data;
+ int channel;
+ int channel_mask = 0x0180; /* 0b 0000 0001 1000 0000 */
+ int manual_mode = 0x1840; /* 0b 0001 1000 0100 0000 */
+ uint8_t tx[2];
+ uint8_t rx[2];
+ struct spi_device* spi_dev;
+
+ memset(tx, 0, sizeof(tx));
+ memset(rx, 0, sizeof(rx));
+
+ if (strstr(attr->attr.name, ":0")) {
+ if (!spi_ap1_adc_dev) {
+ log_error("accessory card 1 adc device not present");
+ return -ENODEV;
+ }
+
+ spi_dev = spi_ap1_adc_dev;
+ } else if (strstr(attr->attr.name, ":1")) {
+ if (!spi_ap2_adc_dev) {
+ log_error("accessory card 2 adc device not present");
+ return -ENODEV;
+ }
+
+ spi_dev = spi_ap2_adc_dev;
+ } else {
+ log_error("unknown adc device %s", attr->attr.name);
+ return -ENODEV;
+ }
+
+ if (strstr(attr->attr.name, "adc0")) {
+ channel = 0;
+ } else if (strstr(attr->attr.name, "adc1")) {
+ channel = 1;
+ } else if (strstr(attr->attr.name, "adc2")) {
+ channel = 2;
+ } else {
+ log_error("accessory card adc attr does not exist");
+ return -ENOENT;
+ }
+
+ /* 1st transfer to set up (5V reference, channel to read from) */
+ tx_data = manual_mode | ((channel << 7) & channel_mask);
+ tx[0] = tx_data >> 8;
+ tx[1] = tx_data & 0xFF;
+ tmp = spi_writen(spi_dev, tx, 2);
+ if (tmp) {
+ log_error("spi_write failed %d", tmp);
+ return tmp;
+ }
+
+ /* 2nd transfer to clock chip for ADC conversion
+ * this can be a throw-away read or an empty write,
+ * the ADC just needs the clock running so it can convert */
+ tx[0] = 0;
+ tx[1] = 0;
+ tmp = spi_writen(spi_dev, tx, 2);
+ if (tmp) {
+ log_error("2nd spi_write failed %d", tmp);
+ return tmp;
+ }
+
+ /* 3rd transfer to read data */
+ tmp = spi_readn(spi_dev, rx, 2);
+ if (tmp) {
+ log_error("spi_read failed %d", tmp);
+ return tmp;
+ }
+ rx_data = ((rx[0] & 0x0F) << 8) | (rx[1] & 0xFF);
+
+ return sprintf(buf, "%lu\n", (unsigned long) rx_data);
+}
+
+/* accessory port 1 gpiob attributes */
+static DEVICE_ATTR_RO_MTS(dev_attr_ap1_gpio_din0, "din0:0", mts_attr_show_ap_din);
+static DEVICE_ATTR_RO_MTS(dev_attr_ap1_gpio_din1, "din1:0", mts_attr_show_ap_din);
+static DEVICE_ATTR_RO_MTS(dev_attr_ap1_gpio_din2, "din2:0", mts_attr_show_ap_din);
+static DEVICE_ATTR_RO_MTS(dev_attr_ap1_gpio_din3, "din3:0", mts_attr_show_ap_din);
+static DEVICE_ATTR_MTS(dev_attr_ap1_gpio_dout0, "dout0:0", mts_attr_show_ap_dout, mts_attr_store_ap_dout);
+static DEVICE_ATTR_MTS(dev_attr_ap1_gpio_dout1, "dout1:0", mts_attr_show_ap_dout, mts_attr_store_ap_dout);
+static DEVICE_ATTR_MTS(dev_attr_ap1_gpio_dout2, "dout2:0", mts_attr_show_ap_dout, mts_attr_store_ap_dout);
+static DEVICE_ATTR_MTS(dev_attr_ap1_gpio_dout3, "dout3:0", mts_attr_show_ap_dout, mts_attr_store_ap_dout);
+static DEVICE_ATTR_RO_MTS(dev_attr_ap1_gpio_adc0, "adc0:0", mts_attr_show_ap_adc);
+static DEVICE_ATTR_RO_MTS(dev_attr_ap1_gpio_adc1, "adc1:0", mts_attr_show_ap_adc);
+static DEVICE_ATTR_RO_MTS(dev_attr_ap1_gpio_adc2, "adc2:0", mts_attr_show_ap_adc);
+static DEVICE_ATTR_MTS(dev_attr_ap1_gpio_led1, "ap1-led1", mts_attr_show_ap_gpio_pin, mts_attr_store_ap_gpio_pin);
+static DEVICE_ATTR_MTS(dev_attr_ap1_gpio_led2, "ap1-led2", mts_attr_show_ap_gpio_pin, mts_attr_store_ap_gpio_pin);
+static DEVICE_ATTR_MTS(dev_attr_ap1_gpio_oe, "ap1-dout-enable", mts_attr_show_ap_gpio_pin, mts_attr_store_ap_gpio_pin);
+static DEVICE_ATTR_MTS(dev_attr_ap1_reset, "ap1-reset", mts_attr_show_ap_gpio_pin, mts_attr_store_ap_gpio_pin);
+
+static int ap1_gpio_attributes_size = 15; // not including NULL at end
+
+static struct attribute *ap1_gpio_attributes[] = {
+ &dev_attr_ap1_reset.attr,
+
+ &dev_attr_ap1_gpio_oe.attr, // gpio1
+ &dev_attr_ap1_gpio_led1.attr, // gpio3
+ &dev_attr_ap1_gpio_led2.attr, // gpio4
+
+ &dev_attr_ap1_gpio_din0.attr,
+ &dev_attr_ap1_gpio_din1.attr,
+ &dev_attr_ap1_gpio_din2.attr,
+ &dev_attr_ap1_gpio_din3.attr,
+
+ &dev_attr_ap1_gpio_dout0.attr,
+ &dev_attr_ap1_gpio_dout1.attr,
+ &dev_attr_ap1_gpio_dout2.attr,
+ &dev_attr_ap1_gpio_dout3.attr,
+
+ &dev_attr_ap1_gpio_adc0.attr,
+ &dev_attr_ap1_gpio_adc1.attr,
+ &dev_attr_ap1_gpio_adc2.attr,
+
+ NULL,
+};
+
+/* accessory port 2 gpiob attributes */
+static DEVICE_ATTR_RO_MTS(dev_attr_ap2_gpio_din0, "din0:1", mts_attr_show_ap_din);
+static DEVICE_ATTR_RO_MTS(dev_attr_ap2_gpio_din1, "din1:1", mts_attr_show_ap_din);
+static DEVICE_ATTR_RO_MTS(dev_attr_ap2_gpio_din2, "din2:1", mts_attr_show_ap_din);
+static DEVICE_ATTR_RO_MTS(dev_attr_ap2_gpio_din3, "din3:1", mts_attr_show_ap_din);
+static DEVICE_ATTR_MTS(dev_attr_ap2_gpio_dout0, "dout0:1", mts_attr_show_ap_dout, mts_attr_store_ap_dout);
+static DEVICE_ATTR_MTS(dev_attr_ap2_gpio_dout1, "dout1:1", mts_attr_show_ap_dout, mts_attr_store_ap_dout);
+static DEVICE_ATTR_MTS(dev_attr_ap2_gpio_dout2, "dout2:1", mts_attr_show_ap_dout, mts_attr_store_ap_dout);
+static DEVICE_ATTR_MTS(dev_attr_ap2_gpio_dout3, "dout3:1", mts_attr_show_ap_dout, mts_attr_store_ap_dout);
+static DEVICE_ATTR_RO_MTS(dev_attr_ap2_gpio_adc0, "adc0:1", mts_attr_show_ap_adc);
+static DEVICE_ATTR_RO_MTS(dev_attr_ap2_gpio_adc1, "adc1:1", mts_attr_show_ap_adc);
+static DEVICE_ATTR_RO_MTS(dev_attr_ap2_gpio_adc2, "adc2:1", mts_attr_show_ap_adc);
+static DEVICE_ATTR_MTS(dev_attr_ap2_gpio_led1, "ap2-led1", mts_attr_show_ap_gpio_pin, mts_attr_store_ap_gpio_pin);
+static DEVICE_ATTR_MTS(dev_attr_ap2_gpio_led2, "ap2-led2", mts_attr_show_ap_gpio_pin, mts_attr_store_ap_gpio_pin);
+static DEVICE_ATTR_MTS(dev_attr_ap2_gpio_oe, "ap2-dout-enable", mts_attr_show_ap_gpio_pin, mts_attr_store_ap_gpio_pin);
+static DEVICE_ATTR_MTS(dev_attr_ap2_reset, "ap2-reset", mts_attr_show_ap_gpio_pin, mts_attr_store_ap_gpio_pin);
+
+static int ap2_gpio_attributes_size = 15; // not including NULL at end
+
+static struct attribute *ap2_gpio_attributes[] = {
+ &dev_attr_ap2_reset.attr,
+
+ &dev_attr_ap2_gpio_oe.attr, // gpio1
+ &dev_attr_ap2_gpio_led1.attr, // gpio3
+ &dev_attr_ap2_gpio_led2.attr, // gpio4
+
+ &dev_attr_ap2_gpio_din0.attr,
+ &dev_attr_ap2_gpio_din1.attr,
+ &dev_attr_ap2_gpio_din2.attr,
+ &dev_attr_ap2_gpio_din3.attr,
+
+ &dev_attr_ap2_gpio_dout0.attr,
+ &dev_attr_ap2_gpio_dout1.attr,
+ &dev_attr_ap2_gpio_dout2.attr,
+ &dev_attr_ap2_gpio_dout3.attr,
+
+ &dev_attr_ap2_gpio_adc0.attr,
+ &dev_attr_ap2_gpio_adc1.attr,
+ &dev_attr_ap2_gpio_adc2.attr,
+
+ NULL,
+};