From 7e07fe3959a903dd3fbcc1546058dc8a84241b28 Mon Sep 17 00:00:00 2001 From: Jesse Gilles Date: Wed, 18 Jun 2014 11:03:04 -0500 Subject: add mtdc_gpiob.c --- io-module/mtdc_gpiob.c | 270 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 io-module/mtdc_gpiob.c diff --git a/io-module/mtdc_gpiob.c b/io-module/mtdc_gpiob.c new file mode 100644 index 0000000..c207483 --- /dev/null +++ b/io-module/mtdc_gpiob.c @@ -0,0 +1,270 @@ + +struct gpio_pin *dc_gpio_pin_by_attr_name(const char *name) { + struct gpio_pin *pin; + char *pin_attr_name; + + if (!strcmp(name, "led1")) { + pin_attr_name = "dc-gpio1"; + } else if (!strcmp(name, "led2")) { + pin_attr_name = "dc-gpio2"; + } else if (!strcmp(name, "dout-enable")) { + pin_attr_name = "dc-gpio3"; + } else { + log_error("daughter 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_dc_gpio_pin(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + int value; + struct gpio_pin *pin = dc_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_dc_gpio_pin(struct device *dev, + struct device_attribute *attr, const char *buf, size_t count) +{ + int value; + struct gpio_pin *pin = dc_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_dc_din(struct device *dev, struct device_attribute *attr, char *buf) +{ + int tmp; + u8 bit; + u8 byte; + + if (!spi_dc_din_dev) { + log_error("dc din device not present"); + return -ENODEV; + } + + if (!strcmp(attr->attr.name, "din0")) { + bit = BIT(0); + } else if (!strcmp(attr->attr.name, "din1")) { + bit = BIT(1); + } else if (!strcmp(attr->attr.name, "din2")) { + bit = BIT(2); + } else if (!strcmp(attr->attr.name, "din3")) { + bit = BIT(3); + } else { + log_error("dc din attr does not exist"); + return -ENOENT; + } + + tmp = spi_readn(spi_dc_din_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_dc_dout(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) +{ + int value; + u8 bit; + + if (!spi_dc_dout_dev) { + log_error("dc dout device not present"); + return -ENODEV; + } + + if (!strcmp(attr->attr.name, "dout0")) { + bit = BIT(0); + } else if (!strcmp(attr->attr.name, "dout1")) { + bit = BIT(1); + } else if (!strcmp(attr->attr.name, "dout2")) { + bit = BIT(2); + } else if (!strcmp(attr->attr.name, "dout3")) { + bit = BIT(3); + } else { + log_error("dc dout attr does not exist"); + return -ENOENT; + } + + if (sscanf(buf, "%i", &value) != 1) { + log_error("dc dout attr invalid argument"); + return -EINVAL; + } + + mutex_lock(&spi_dc_dout_mutex); + + if (value) { + spi_dc_dout_value &= ~bit; + } else { + spi_dc_dout_value |= bit; + } + + spi_writen(spi_dc_dout_dev, &spi_dc_dout_value, 1); + + mutex_unlock(&spi_dc_dout_mutex); + + return count; +} + +static ssize_t mts_attr_show_dc_dout(struct device *dev, struct device_attribute *attr, char *buf) +{ + int value; + u8 bit; + + if (!spi_dc_dout_dev) { + log_error("dc dout device not present"); + return -ENODEV; + } + + if (!strcmp(attr->attr.name, "dout0")) { + bit = BIT(0); + } else if (!strcmp(attr->attr.name, "dout1")) { + bit = BIT(1); + } else if (!strcmp(attr->attr.name, "dout2")) { + bit = BIT(2); + } else if (!strcmp(attr->attr.name, "dout3")) { + bit = BIT(3); + } else { + log_error("dc dout attr does not exist"); + return -ENOENT; + } + + mutex_lock(&spi_dc_dout_mutex); + + value = spi_dc_dout_value & bit ? 0 : 1; + + mutex_unlock(&spi_dc_dout_mutex); + + return sprintf(buf, "%d\n", value); +} + +static ssize_t mts_attr_show_dc_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]; + + if (!spi_dc_adc_dev) { + log_error("dc adc device not present"); + return -ENODEV; + } + + memset(tx, 0, sizeof(tx)); + memset(rx, 0, sizeof(rx)); + + if (!strcmp(attr->attr.name, "adc0")) { + channel = 0; + } else if (!strcmp(attr->attr.name, "adc1")) { + channel = 1; + } else if (! strcmp(attr->attr.name, "adc2")) { + channel = 2; + } else { + log_error("dc 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_dc_adc_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_dc_adc_dev, tx, 2); + if (tmp) { + log_error("2nd spi_write failed %d", tmp); + return tmp; + } + + /* 3rd transfer to read data */ + tmp = spi_readn(spi_dc_adc_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); +} + +/* MTDC-GPIOB */ +static DEVICE_ATTR_RO_MTS(dev_attr_dc_din0, "din0", mts_attr_show_dc_din); +static DEVICE_ATTR_RO_MTS(dev_attr_dc_din1, "din1", mts_attr_show_dc_din); +static DEVICE_ATTR_RO_MTS(dev_attr_dc_din2, "din2", mts_attr_show_dc_din); +static DEVICE_ATTR_RO_MTS(dev_attr_dc_din3, "din3", mts_attr_show_dc_din); +static DEVICE_ATTR_MTS(dev_attr_dc_dout0, "dout0", mts_attr_show_dc_dout, mts_attr_store_dc_dout); +static DEVICE_ATTR_MTS(dev_attr_dc_dout1, "dout1", mts_attr_show_dc_dout, mts_attr_store_dc_dout); +static DEVICE_ATTR_MTS(dev_attr_dc_dout2, "dout2", mts_attr_show_dc_dout, mts_attr_store_dc_dout); +static DEVICE_ATTR_MTS(dev_attr_dc_dout3, "dout3", mts_attr_show_dc_dout, mts_attr_store_dc_dout); +static DEVICE_ATTR_RO_MTS(dev_attr_dc_adc0, "adc0", mts_attr_show_dc_adc); +static DEVICE_ATTR_RO_MTS(dev_attr_dc_adc1, "adc1", mts_attr_show_dc_adc); +static DEVICE_ATTR_RO_MTS(dev_attr_dc_adc2, "adc2", mts_attr_show_dc_adc); +static DEVICE_ATTR_MTS(dev_attr_dc_led1, "led1", mts_attr_show_dc_gpio_pin, mts_attr_store_dc_gpio_pin); +static DEVICE_ATTR_MTS(dev_attr_dc_led2, "led2", mts_attr_show_dc_gpio_pin, mts_attr_store_dc_gpio_pin); +static DEVICE_ATTR_MTS(dev_attr_dc_oe, "dout-enable", mts_attr_show_dc_gpio_pin, mts_attr_store_dc_gpio_pin); -- cgit v1.2.3