staging:iio:dac:ad5064: Use iio_enum for powerdown modes

This allows us to remove some boilerplate code for comparing and formatting the
enum strings.

Signed-off-by: Lars-Peter Clausen <lars@metafoo.de>
Acked-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Lars-Peter Clausen 2012-06-04 11:36:12 +02:00 committed by Greg Kroah-Hartman
parent 5212cc8a9d
commit 26628f6b53
1 changed files with 21 additions and 44 deletions

View File

@ -136,57 +136,42 @@ static int ad5064_sync_powerdown_mode(struct ad5064_state *st,
return ret; return ret;
} }
static const char ad5064_powerdown_modes[][15] = { static const char * const ad5064_powerdown_modes[] = {
[AD5064_LDAC_PWRDN_NONE] = "", "1kohm_to_gnd",
[AD5064_LDAC_PWRDN_1K] = "1kohm_to_gnd", "100kohm_to_gnd",
[AD5064_LDAC_PWRDN_100K] = "100kohm_to_gnd", "three_state",
[AD5064_LDAC_PWRDN_3STATE] = "three_state",
}; };
static ssize_t ad5064_read_powerdown_mode_available(struct iio_dev *indio_dev, static int ad5064_get_powerdown_mode(struct iio_dev *indio_dev,
uintptr_t private, const struct iio_chan_spec *chan, char *buf) const struct iio_chan_spec *chan)
{
return sprintf(buf, "%s %s %s\n", ad5064_powerdown_modes[1],
ad5064_powerdown_modes[2], ad5064_powerdown_modes[3]);
}
static ssize_t ad5064_read_powerdown_mode(struct iio_dev *indio_dev,
uintptr_t private, const struct iio_chan_spec *chan, char *buf)
{ {
struct ad5064_state *st = iio_priv(indio_dev); struct ad5064_state *st = iio_priv(indio_dev);
return sprintf(buf, "%s\n", return st->pwr_down_mode[chan->channel] - 1;
ad5064_powerdown_modes[st->pwr_down_mode[chan->channel]]);
} }
static ssize_t ad5064_write_powerdown_mode(struct iio_dev *indio_dev, static int ad5064_set_powerdown_mode(struct iio_dev *indio_dev,
uintptr_t private, const struct iio_chan_spec *chan, const char *buf, const struct iio_chan_spec *chan, unsigned int mode)
size_t len)
{ {
struct ad5064_state *st = iio_priv(indio_dev); struct ad5064_state *st = iio_priv(indio_dev);
unsigned int mode, i;
int ret; int ret;
mode = 0;
for (i = 1; i < ARRAY_SIZE(ad5064_powerdown_modes); ++i) {
if (sysfs_streq(buf, ad5064_powerdown_modes[i])) {
mode = i;
break;
}
}
if (mode == 0)
return -EINVAL;
mutex_lock(&indio_dev->mlock); mutex_lock(&indio_dev->mlock);
st->pwr_down_mode[chan->channel] = mode; st->pwr_down_mode[chan->channel] = mode + 1;
ret = ad5064_sync_powerdown_mode(st, chan->channel); ret = ad5064_sync_powerdown_mode(st, chan->channel);
mutex_unlock(&indio_dev->mlock); mutex_unlock(&indio_dev->mlock);
return ret ? ret : len; return ret;
} }
static const struct iio_enum ad5064_powerdown_mode_enum = {
.items = ad5064_powerdown_modes,
.num_items = ARRAY_SIZE(ad5064_powerdown_modes),
.get = ad5064_get_powerdown_mode,
.set = ad5064_set_powerdown_mode,
};
static ssize_t ad5064_read_dac_powerdown(struct iio_dev *indio_dev, static ssize_t ad5064_read_dac_powerdown(struct iio_dev *indio_dev,
uintptr_t private, const struct iio_chan_spec *chan, char *buf) uintptr_t private, const struct iio_chan_spec *chan, char *buf)
{ {
@ -286,22 +271,14 @@ static const struct iio_info ad5064_info = {
.driver_module = THIS_MODULE, .driver_module = THIS_MODULE,
}; };
static struct iio_chan_spec_ext_info ad5064_ext_info[] = { static const struct iio_chan_spec_ext_info ad5064_ext_info[] = {
{ {
.name = "powerdown", .name = "powerdown",
.read = ad5064_read_dac_powerdown, .read = ad5064_read_dac_powerdown,
.write = ad5064_write_dac_powerdown, .write = ad5064_write_dac_powerdown,
}, },
{ IIO_ENUM("powerdown_mode", false, &ad5064_powerdown_mode_enum),
.name = "powerdown_mode", IIO_ENUM_AVAILABLE("powerdown_mode", &ad5064_powerdown_mode_enum),
.read = ad5064_read_powerdown_mode,
.write = ad5064_write_powerdown_mode,
},
{
.name = "powerdown_mode_available",
.shared = true,
.read = ad5064_read_powerdown_mode_available,
},
{ }, { },
}; };