From: Lars-Peter Clausen Date: Mon, 4 Jun 2012 09:36:15 +0000 (+0200) Subject: staging:iio:dac:ad5504: Convert to extended channel attributes X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=8d05d7773d644bbde98e27eeba0e8f6251b30d1e;p=openwrt%2Fstaging%2Fblogic.git staging:iio:dac:ad5504: Convert to extended channel attributes Use extended channel attributes instead of raw sysfs files for the additional channel attributes. This allows us to remove some boilerplate code. Signed-off-by: Lars-Peter Clausen Acked-by: Jonathan Cameron Signed-off-by: Greg Kroah-Hartman --- diff --git a/drivers/staging/iio/dac/ad5504.c b/drivers/staging/iio/dac/ad5504.c index 019cf15cd24a..acb1d59b2de4 100644 --- a/drivers/staging/iio/dac/ad5504.c +++ b/drivers/staging/iio/dac/ad5504.c @@ -22,24 +22,6 @@ #include "dac.h" #include "ad5504.h" -#define AD5504_CHANNEL(_chan) { \ - .type = IIO_VOLTAGE, \ - .indexed = 1, \ - .output = 1, \ - .channel = (_chan), \ - .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \ - IIO_CHAN_INFO_SCALE_SHARED_BIT, \ - .address = AD5504_ADDR_DAC(_chan), \ - .scan_type = IIO_ST('u', 12, 16, 0), \ -} - -static const struct iio_chan_spec ad5504_channels[] = { - AD5504_CHANNEL(0), - AD5504_CHANNEL(1), - AD5504_CHANNEL(2), - AD5504_CHANNEL(3), -}; - static int ad5504_spi_write(struct spi_device *spi, u8 addr, u16 val) { u16 tmp = cpu_to_be16(AD5504_CMD_WRITE | @@ -122,65 +104,61 @@ static int ad5504_write_raw(struct iio_dev *indio_dev, return -EINVAL; } -static ssize_t ad5504_read_powerdown_mode(struct device *dev, - struct device_attribute *attr, char *buf) +static const char * const ad5504_powerdown_modes[] = { + "20kohm_to_gnd", + "three_state", +}; + +static int ad5504_get_powerdown_mode(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan) { - struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ad5504_state *st = iio_priv(indio_dev); - const char mode[][14] = {"20kohm_to_gnd", "three_state"}; - - return sprintf(buf, "%s\n", mode[st->pwr_down_mode]); + return st->pwr_down_mode; } -static ssize_t ad5504_write_powerdown_mode(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t len) +static int ad5504_set_powerdown_mode(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, unsigned int mode) { - struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ad5504_state *st = iio_priv(indio_dev); - int ret; - if (sysfs_streq(buf, "20kohm_to_gnd")) - st->pwr_down_mode = AD5504_DAC_PWRDN_20K; - else if (sysfs_streq(buf, "three_state")) - st->pwr_down_mode = AD5504_DAC_PWRDN_3STATE; - else - ret = -EINVAL; + st->pwr_down_mode = mode; - return ret ? ret : len; + return 0; } -static ssize_t ad5504_read_dac_powerdown(struct device *dev, - struct device_attribute *attr, - char *buf) +static const struct iio_enum ad5504_powerdown_mode_enum = { + .items = ad5504_powerdown_modes, + .num_items = ARRAY_SIZE(ad5504_powerdown_modes), + .get = ad5504_get_powerdown_mode, + .set = ad5504_set_powerdown_mode, +}; + +static ssize_t ad5504_read_dac_powerdown(struct iio_dev *indio_dev, + uintptr_t private, const struct iio_chan_spec *chan, char *buf) { - struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ad5504_state *st = iio_priv(indio_dev); - struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); return sprintf(buf, "%d\n", - !(st->pwr_down_mask & (1 << this_attr->address))); + !(st->pwr_down_mask & (1 << chan->channel))); } -static ssize_t ad5504_write_dac_powerdown(struct device *dev, - struct device_attribute *attr, - const char *buf, size_t len) +static ssize_t ad5504_write_dac_powerdown(struct iio_dev *indio_dev, + uintptr_t private, const struct iio_chan_spec *chan, const char *buf, + size_t len) { long readin; int ret; - struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ad5504_state *st = iio_priv(indio_dev); - struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); ret = strict_strtol(buf, 10, &readin); if (ret) return ret; if (readin == 0) - st->pwr_down_mask |= (1 << this_attr->address); + st->pwr_down_mask |= (1 << chan->channel); else if (readin == 1) - st->pwr_down_mask &= ~(1 << this_attr->address); + st->pwr_down_mask &= ~(1 << chan->channel); else ret = -EINVAL; @@ -194,50 +172,6 @@ static ssize_t ad5504_write_dac_powerdown(struct device *dev, return ret ? ret : len; } -static IIO_DEVICE_ATTR(out_voltage_powerdown_mode, S_IRUGO | - S_IWUSR, ad5504_read_powerdown_mode, - ad5504_write_powerdown_mode, 0); - -static IIO_CONST_ATTR(out_voltage_powerdown_mode_available, - "20kohm_to_gnd three_state"); - -#define IIO_DEV_ATTR_DAC_POWERDOWN(_num, _show, _store, _addr) \ - IIO_DEVICE_ATTR(out_voltage##_num##_powerdown, \ - S_IRUGO | S_IWUSR, _show, _store, _addr) -static IIO_DEV_ATTR_DAC_POWERDOWN(0, ad5504_read_dac_powerdown, - ad5504_write_dac_powerdown, 0); -static IIO_DEV_ATTR_DAC_POWERDOWN(1, ad5504_read_dac_powerdown, - ad5504_write_dac_powerdown, 1); -static IIO_DEV_ATTR_DAC_POWERDOWN(2, ad5504_read_dac_powerdown, - ad5504_write_dac_powerdown, 2); -static IIO_DEV_ATTR_DAC_POWERDOWN(3, ad5504_read_dac_powerdown, - ad5504_write_dac_powerdown, 3); - -static struct attribute *ad5504_attributes[] = { - &iio_dev_attr_out_voltage0_powerdown.dev_attr.attr, - &iio_dev_attr_out_voltage1_powerdown.dev_attr.attr, - &iio_dev_attr_out_voltage2_powerdown.dev_attr.attr, - &iio_dev_attr_out_voltage3_powerdown.dev_attr.attr, - &iio_dev_attr_out_voltage_powerdown_mode.dev_attr.attr, - &iio_const_attr_out_voltage_powerdown_mode_available.dev_attr.attr, - NULL, -}; - -static const struct attribute_group ad5504_attribute_group = { - .attrs = ad5504_attributes, -}; - -static struct attribute *ad5501_attributes[] = { - &iio_dev_attr_out_voltage0_powerdown.dev_attr.attr, - &iio_dev_attr_out_voltage_powerdown_mode.dev_attr.attr, - &iio_const_attr_out_voltage_powerdown_mode_available.dev_attr.attr, - NULL, -}; - -static const struct attribute_group ad5501_attribute_group = { - .attrs = ad5501_attributes, -}; - static IIO_CONST_ATTR(temp0_thresh_rising_value, "110000"); static IIO_CONST_ATTR(temp0_thresh_rising_en, "1"); @@ -267,17 +201,38 @@ static irqreturn_t ad5504_event_handler(int irq, void *private) static const struct iio_info ad5504_info = { .write_raw = ad5504_write_raw, .read_raw = ad5504_read_raw, - .attrs = &ad5504_attribute_group, .event_attrs = &ad5504_ev_attribute_group, .driver_module = THIS_MODULE, }; -static const struct iio_info ad5501_info = { - .write_raw = ad5504_write_raw, - .read_raw = ad5504_read_raw, - .attrs = &ad5501_attribute_group, - .event_attrs = &ad5504_ev_attribute_group, - .driver_module = THIS_MODULE, +static const struct iio_chan_spec_ext_info ad5504_ext_info[] = { + { + .name = "powerdown", + .read = ad5504_read_dac_powerdown, + .write = ad5504_write_dac_powerdown, + }, + IIO_ENUM("powerdown_mode", true, &ad5504_powerdown_mode_enum), + IIO_ENUM_AVAILABLE("powerdown_mode", &ad5504_powerdown_mode_enum), + { }, +}; + +#define AD5504_CHANNEL(_chan) { \ + .type = IIO_VOLTAGE, \ + .indexed = 1, \ + .output = 1, \ + .channel = (_chan), \ + .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \ + IIO_CHAN_INFO_SCALE_SHARED_BIT, \ + .address = AD5504_ADDR_DAC(_chan), \ + .scan_type = IIO_ST('u', 12, 16, 0), \ + .ext_info = ad5504_ext_info, \ +} + +static const struct iio_chan_spec ad5504_channels[] = { + AD5504_CHANNEL(0), + AD5504_CHANNEL(1), + AD5504_CHANNEL(2), + AD5504_CHANNEL(3), }; static int __devinit ad5504_probe(struct spi_device *spi) @@ -315,13 +270,11 @@ static int __devinit ad5504_probe(struct spi_device *spi) st->spi = spi; indio_dev->dev.parent = &spi->dev; indio_dev->name = spi_get_device_id(st->spi)->name; - if (spi_get_device_id(st->spi)->driver_data == ID_AD5501) { - indio_dev->info = &ad5501_info; + indio_dev->info = &ad5504_info; + if (spi_get_device_id(st->spi)->driver_data == ID_AD5501) indio_dev->num_channels = 1; - } else { - indio_dev->info = &ad5504_info; + else indio_dev->num_channels = 4; - } indio_dev->channels = ad5504_channels; indio_dev->modes = INDIO_DIRECT_MODE;