From 541544712ea9f1f4511ecc1ec7dcabf6ce042909 Mon Sep 17 00:00:00 2001 From: Peter Meerwald Date: Sat, 6 Dec 2014 05:54:00 +0000 Subject: iio:adc:ad799x: Drop I2C access helper functions Signed-off-by: Peter Meerwald Acked-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/adc/ad799x.c | 119 +++++++++++------------------------------------ 1 file changed, 26 insertions(+), 93 deletions(-) (limited to 'drivers/iio/adc') diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c index 6cf9ee18a216..34d93a5eefb1 100644 --- a/drivers/iio/adc/ad799x.c +++ b/drivers/iio/adc/ad799x.c @@ -175,65 +175,6 @@ out: return IRQ_HANDLED; } -/* - * ad799x register access by I2C - */ -static int ad799x_i2c_read16(struct ad799x_state *st, u8 reg, u16 *data) -{ - struct i2c_client *client = st->client; - int ret = 0; - - ret = i2c_smbus_read_word_swapped(client, reg); - if (ret < 0) { - dev_err(&client->dev, "I2C read error\n"); - return ret; - } - - *data = (u16)ret; - - return 0; -} - -static int ad799x_i2c_read8(struct ad799x_state *st, u8 reg, u8 *data) -{ - struct i2c_client *client = st->client; - int ret = 0; - - ret = i2c_smbus_read_byte_data(client, reg); - if (ret < 0) { - dev_err(&client->dev, "I2C read error\n"); - return ret; - } - - *data = (u8)ret; - - return 0; -} - -static int ad799x_i2c_write16(struct ad799x_state *st, u8 reg, u16 data) -{ - struct i2c_client *client = st->client; - int ret = 0; - - ret = i2c_smbus_write_word_swapped(client, reg, data); - if (ret < 0) - dev_err(&client->dev, "I2C write error\n"); - - return ret; -} - -static int ad799x_i2c_write8(struct ad799x_state *st, u8 reg, u8 data) -{ - struct i2c_client *client = st->client; - int ret = 0; - - ret = i2c_smbus_write_byte_data(client, reg, data); - if (ret < 0) - dev_err(&client->dev, "I2C write error\n"); - - return ret; -} - static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev, const unsigned long *scan_mask) { @@ -249,7 +190,7 @@ static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev, switch (st->id) { case ad7997: case ad7998: - return ad799x_i2c_write16(st, AD7998_CONF_REG, + return i2c_smbus_write_word_swapped(st->client, AD7998_CONF_REG, st->config | (*scan_mask << AD799X_CHANNEL_SHIFT)); default: break; @@ -260,9 +201,7 @@ static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev, static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch) { - u16 rxbuf; u8 cmd; - int ret; switch (st->id) { case ad7991: @@ -283,11 +222,7 @@ static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch) return -EINVAL; } - ret = ad799x_i2c_read16(st, cmd, &rxbuf); - if (ret < 0) - return ret; - - return rxbuf; + return i2c_smbus_read_word_swapped(st->client, cmd); } static int ad799x_read_raw(struct iio_dev *indio_dev, @@ -332,6 +267,7 @@ static const unsigned int ad7998_frequencies[] = { [AD7998_CYC_TCONF_1024] = 488, [AD7998_CYC_TCONF_2048] = 244, }; + static ssize_t ad799x_read_frequency(struct device *dev, struct device_attribute *attr, char *buf) @@ -339,15 +275,11 @@ static ssize_t ad799x_read_frequency(struct device *dev, struct iio_dev *indio_dev = dev_to_iio_dev(dev); struct ad799x_state *st = iio_priv(indio_dev); - int ret; - u8 val; - ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &val); - if (ret) + int ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG); + if (ret < 0) return ret; - val &= AD7998_CYC_MASK; - - return sprintf(buf, "%u\n", ad7998_frequencies[val]); + return sprintf(buf, "%u\n", ad7998_frequencies[ret & AD7998_CYC_MASK]); } static ssize_t ad799x_write_frequency(struct device *dev, @@ -360,18 +292,17 @@ static ssize_t ad799x_write_frequency(struct device *dev, long val; int ret, i; - u8 t; ret = kstrtol(buf, 10, &val); if (ret) return ret; mutex_lock(&indio_dev->mlock); - ret = ad799x_i2c_read8(st, AD7998_CYCLE_TMR_REG, &t); - if (ret) + ret = i2c_smbus_read_byte_data(st->client, AD7998_CYCLE_TMR_REG); + if (ret < 0) goto error_ret_mutex; /* Wipe the bits clean */ - t &= ~AD7998_CYC_MASK; + ret &= ~AD7998_CYC_MASK; for (i = 0; i < ARRAY_SIZE(ad7998_frequencies); i++) if (val == ad7998_frequencies[i]) @@ -380,13 +311,17 @@ static ssize_t ad799x_write_frequency(struct device *dev, ret = -EINVAL; goto error_ret_mutex; } - t |= i; - ret = ad799x_i2c_write8(st, AD7998_CYCLE_TMR_REG, t); + + ret = i2c_smbus_write_byte_data(st->client, AD7998_CYCLE_TMR_REG, + ret | i); + if (ret < 0) + goto error_ret_mutex; + ret = len; error_ret_mutex: mutex_unlock(&indio_dev->mlock); - return ret ? ret : len; + return ret; } static int ad799x_read_event_config(struct iio_dev *indio_dev, @@ -430,7 +365,8 @@ static int ad799x_write_event_value(struct iio_dev *indio_dev, return -EINVAL; mutex_lock(&indio_dev->mlock); - ret = ad799x_i2c_write16(st, ad799x_threshold_reg(chan, dir, info), + ret = i2c_smbus_write_word_swapped(st->client, + ad799x_threshold_reg(chan, dir, info), val << chan->scan_type.shift); mutex_unlock(&indio_dev->mlock); @@ -446,15 +382,14 @@ static int ad799x_read_event_value(struct iio_dev *indio_dev, { int ret; struct ad799x_state *st = iio_priv(indio_dev); - u16 valin; mutex_lock(&indio_dev->mlock); - ret = ad799x_i2c_read16(st, ad799x_threshold_reg(chan, dir, info), - &valin); + ret = i2c_smbus_read_word_swapped(st->client, + ad799x_threshold_reg(chan, dir, info)); mutex_unlock(&indio_dev->mlock); if (ret < 0) return ret; - *val = (valin >> chan->scan_type.shift) & + *val = (ret >> chan->scan_type.shift) & RES_MASK(chan->scan_type.realbits); return IIO_VAL_INT; @@ -464,20 +399,18 @@ static irqreturn_t ad799x_event_handler(int irq, void *private) { struct iio_dev *indio_dev = private; struct ad799x_state *st = iio_priv(private); - u8 status; int i, ret; - ret = ad799x_i2c_read8(st, AD7998_ALERT_STAT_REG, &status); - if (ret) + ret = i2c_smbus_read_byte_data(st->client, AD7998_ALERT_STAT_REG); + if (ret <= 0) goto done; - if (!status) + if (i2c_smbus_write_byte_data(st->client, AD7998_ALERT_STAT_REG, + AD7998_ALERT_STAT_CLEAR) < 0) goto done; - ad799x_i2c_write8(st, AD7998_ALERT_STAT_REG, AD7998_ALERT_STAT_CLEAR); - for (i = 0; i < 8; i++) { - if (status & (1 << i)) + if (ret & (1 << i)) iio_push_event(indio_dev, i & 0x1 ? IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, -- cgit v1.2.3 From bd3bd4328a6743b836c9759f7510cd0ad6580a23 Mon Sep 17 00:00:00 2001 From: Peter Meerwald Date: Sat, 6 Dec 2014 05:54:00 +0000 Subject: iio:adc:ad799x: Save some lines in ad7997_8_update_scan_mode() exit handling Signed-off-by: Peter Meerwald Acked-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/adc/ad799x.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'drivers/iio/adc') diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c index 34d93a5eefb1..878400c33137 100644 --- a/drivers/iio/adc/ad799x.c +++ b/drivers/iio/adc/ad799x.c @@ -193,10 +193,8 @@ static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev, return i2c_smbus_write_word_swapped(st->client, AD7998_CONF_REG, st->config | (*scan_mask << AD799X_CHANNEL_SHIFT)); default: - break; + return 0; } - - return 0; } static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch) -- cgit v1.2.3 From e1c6e2a2173c88c1be6a43df407567ae1c11cc22 Mon Sep 17 00:00:00 2001 From: Peter Meerwald Date: Sat, 6 Dec 2014 05:54:00 +0000 Subject: iio:adc:ad799x: Use BIT() and GENMASK() Signed-off-by: Peter Meerwald Acked-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/adc/ad799x.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) (limited to 'drivers/iio/adc') diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c index 878400c33137..b3799a8056c8 100644 --- a/drivers/iio/adc/ad799x.c +++ b/drivers/iio/adc/ad799x.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include @@ -41,7 +42,7 @@ #include #define AD799X_CHANNEL_SHIFT 4 -#define AD799X_STORAGEBITS 16 + /* * AD7991, AD7995 and AD7999 defines */ @@ -55,10 +56,10 @@ * AD7992, AD7993, AD7994, AD7997 and AD7998 defines */ -#define AD7998_FLTR 0x08 -#define AD7998_ALERT_EN 0x04 -#define AD7998_BUSY_ALERT 0x02 -#define AD7998_BUSY_ALERT_POL 0x01 +#define AD7998_FLTR BIT(3) +#define AD7998_ALERT_EN BIT(2) +#define AD7998_BUSY_ALERT BIT(1) +#define AD7998_BUSY_ALERT_POL BIT(0) #define AD7998_CONV_RES_REG 0x0 #define AD7998_ALERT_STAT_REG 0x1 @@ -69,7 +70,7 @@ #define AD7998_DATAHIGH_REG(x) ((x) * 3 + 0x5) #define AD7998_HYST_REG(x) ((x) * 3 + 0x6) -#define AD7998_CYC_MASK 0x7 +#define AD7998_CYC_MASK GENMASK(2, 0) #define AD7998_CYC_DIS 0x0 #define AD7998_CYC_TCONF_32 0x1 #define AD7998_CYC_TCONF_64 0x2 @@ -85,10 +86,8 @@ * AD7997 and AD7997 defines */ -#define AD7997_8_READ_SINGLE 0x80 -#define AD7997_8_READ_SEQUENCE 0x70 -/* TODO: move this into a common header */ -#define RES_MASK(bits) ((1 << (bits)) - 1) +#define AD7997_8_READ_SINGLE BIT(7) +#define AD7997_8_READ_SEQUENCE (BIT(6) | BIT(5) | BIT(4)) enum { ad7991, @@ -205,12 +204,12 @@ static int ad799x_scan_direct(struct ad799x_state *st, unsigned ch) case ad7991: case ad7995: case ad7999: - cmd = st->config | ((1 << ch) << AD799X_CHANNEL_SHIFT); + cmd = st->config | (BIT(ch) << AD799X_CHANNEL_SHIFT); break; case ad7992: case ad7993: case ad7994: - cmd = (1 << ch) << AD799X_CHANNEL_SHIFT; + cmd = BIT(ch) << AD799X_CHANNEL_SHIFT; break; case ad7997: case ad7998: @@ -244,7 +243,7 @@ static int ad799x_read_raw(struct iio_dev *indio_dev, if (ret < 0) return ret; *val = (ret >> chan->scan_type.shift) & - RES_MASK(chan->scan_type.realbits); + GENMASK(chan->scan_type.realbits - 1, 0); return IIO_VAL_INT; case IIO_CHAN_INFO_SCALE: ret = regulator_get_voltage(st->vref); @@ -359,7 +358,7 @@ static int ad799x_write_event_value(struct iio_dev *indio_dev, int ret; struct ad799x_state *st = iio_priv(indio_dev); - if (val < 0 || val > RES_MASK(chan->scan_type.realbits)) + if (val < 0 || val > GENMASK(chan->scan_type.realbits - 1, 0)) return -EINVAL; mutex_lock(&indio_dev->mlock); @@ -388,7 +387,7 @@ static int ad799x_read_event_value(struct iio_dev *indio_dev, if (ret < 0) return ret; *val = (ret >> chan->scan_type.shift) & - RES_MASK(chan->scan_type.realbits); + GENMASK(chan->scan_type.realbits - 1 , 0); return IIO_VAL_INT; } @@ -408,7 +407,7 @@ static irqreturn_t ad799x_event_handler(int irq, void *private) goto done; for (i = 0; i < 8; i++) { - if (ret & (1 << i)) + if (ret & BIT(i)) iio_push_event(indio_dev, i & 0x1 ? IIO_UNMOD_EVENT_CODE(IIO_VOLTAGE, -- cgit v1.2.3 From 91556c69394d08ad32525b76436a8deab6155cbd Mon Sep 17 00:00:00 2001 From: Peter Meerwald Date: Sat, 6 Dec 2014 05:54:00 +0000 Subject: iio:adc:ad799x: Only expose event interface when IRQ is available an IRQ is necessary to handle the ALERT condition; without IRQ, the IIO event interface serves no purpose Signed-off-by: Peter Meerwald Acked-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/adc/ad799x.c | 265 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 179 insertions(+), 86 deletions(-) (limited to 'drivers/iio/adc') diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c index b3799a8056c8..b8a811726d2f 100644 --- a/drivers/iio/adc/ad799x.c +++ b/drivers/iio/adc/ad799x.c @@ -101,22 +101,32 @@ enum { }; /** - * struct ad799x_chip_info - chip specific information + * struct ad799x_chip_config - chip specific information * @channel: channel specification - * @num_channels: number of channels * @default_config: device default configuration * @info: pointer to iio_info struct */ -struct ad799x_chip_info { +struct ad799x_chip_config { struct iio_chan_spec channel[9]; - int num_channels; u16 default_config; const struct iio_info *info; }; +/** + * struct ad799x_chip_info - chip specific information + * @num_channels: number of channels + * @noirq_config: device configuration w/o IRQ + * @irq_config: device configuration w/IRQ + */ +struct ad799x_chip_info { + int num_channels; + const struct ad799x_chip_config noirq_config; + const struct ad799x_chip_config irq_config; +}; + struct ad799x_state { struct i2c_client *client; - const struct ad799x_chip_info *chip_info; + const struct ad799x_chip_config *chip_config; struct regulator *reg; struct regulator *vref; unsigned id; @@ -446,7 +456,13 @@ static const struct iio_info ad7991_info = { .driver_module = THIS_MODULE, }; -static const struct iio_info ad7993_4_7_8_info = { +static const struct iio_info ad7993_4_7_8_noirq_info = { + .read_raw = &ad799x_read_raw, + .driver_module = THIS_MODULE, + .update_scan_mode = ad7997_8_update_scan_mode, +}; + +static const struct iio_info ad7993_4_7_8_irq_info = { .read_raw = &ad799x_read_raw, .event_attrs = &ad799x_event_attrs_group, .read_event_config = &ad799x_read_event_config, @@ -501,103 +517,175 @@ static const struct iio_event_spec ad799x_events[] = { static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { [ad7991] = { - .channel = { - AD799X_CHANNEL(0, 12), - AD799X_CHANNEL(1, 12), - AD799X_CHANNEL(2, 12), - AD799X_CHANNEL(3, 12), - IIO_CHAN_SOFT_TIMESTAMP(4), - }, .num_channels = 5, - .info = &ad7991_info, + .noirq_config = { + .channel = { + AD799X_CHANNEL(0, 12), + AD799X_CHANNEL(1, 12), + AD799X_CHANNEL(2, 12), + AD799X_CHANNEL(3, 12), + IIO_CHAN_SOFT_TIMESTAMP(4), + }, + .info = &ad7991_info, + }, }, [ad7995] = { - .channel = { - AD799X_CHANNEL(0, 10), - AD799X_CHANNEL(1, 10), - AD799X_CHANNEL(2, 10), - AD799X_CHANNEL(3, 10), - IIO_CHAN_SOFT_TIMESTAMP(4), - }, .num_channels = 5, - .info = &ad7991_info, + .noirq_config = { + .channel = { + AD799X_CHANNEL(0, 10), + AD799X_CHANNEL(1, 10), + AD799X_CHANNEL(2, 10), + AD799X_CHANNEL(3, 10), + IIO_CHAN_SOFT_TIMESTAMP(4), + }, + .info = &ad7991_info, + }, }, [ad7999] = { - .channel = { - AD799X_CHANNEL(0, 8), - AD799X_CHANNEL(1, 8), - AD799X_CHANNEL(2, 8), - AD799X_CHANNEL(3, 8), - IIO_CHAN_SOFT_TIMESTAMP(4), - }, .num_channels = 5, - .info = &ad7991_info, + .noirq_config = { + .channel = { + AD799X_CHANNEL(0, 8), + AD799X_CHANNEL(1, 8), + AD799X_CHANNEL(2, 8), + AD799X_CHANNEL(3, 8), + IIO_CHAN_SOFT_TIMESTAMP(4), + }, + .info = &ad7991_info, + }, }, [ad7992] = { - .channel = { - AD799X_CHANNEL_WITH_EVENTS(0, 12), - AD799X_CHANNEL_WITH_EVENTS(1, 12), - IIO_CHAN_SOFT_TIMESTAMP(3), - }, .num_channels = 3, - .default_config = AD7998_ALERT_EN, - .info = &ad7993_4_7_8_info, + .noirq_config = { + .channel = { + AD799X_CHANNEL(0, 12), + AD799X_CHANNEL(1, 12), + IIO_CHAN_SOFT_TIMESTAMP(3), + }, + .info = &ad7993_4_7_8_noirq_info, + }, + .irq_config = { + .channel = { + AD799X_CHANNEL_WITH_EVENTS(0, 12), + AD799X_CHANNEL_WITH_EVENTS(1, 12), + IIO_CHAN_SOFT_TIMESTAMP(3), + }, + .default_config = AD7998_ALERT_EN, + .info = &ad7993_4_7_8_irq_info, + }, }, [ad7993] = { - .channel = { - AD799X_CHANNEL_WITH_EVENTS(0, 10), - AD799X_CHANNEL_WITH_EVENTS(1, 10), - AD799X_CHANNEL_WITH_EVENTS(2, 10), - AD799X_CHANNEL_WITH_EVENTS(3, 10), - IIO_CHAN_SOFT_TIMESTAMP(4), - }, .num_channels = 5, - .default_config = AD7998_ALERT_EN, - .info = &ad7993_4_7_8_info, + .noirq_config = { + .channel = { + AD799X_CHANNEL(0, 10), + AD799X_CHANNEL(1, 10), + AD799X_CHANNEL(2, 10), + AD799X_CHANNEL(3, 10), + IIO_CHAN_SOFT_TIMESTAMP(4), + }, + .info = &ad7993_4_7_8_noirq_info, + }, + .irq_config = { + .channel = { + AD799X_CHANNEL_WITH_EVENTS(0, 10), + AD799X_CHANNEL_WITH_EVENTS(1, 10), + AD799X_CHANNEL_WITH_EVENTS(2, 10), + AD799X_CHANNEL_WITH_EVENTS(3, 10), + IIO_CHAN_SOFT_TIMESTAMP(4), + }, + .default_config = AD7998_ALERT_EN, + .info = &ad7993_4_7_8_irq_info, + }, }, [ad7994] = { - .channel = { - AD799X_CHANNEL_WITH_EVENTS(0, 12), - AD799X_CHANNEL_WITH_EVENTS(1, 12), - AD799X_CHANNEL_WITH_EVENTS(2, 12), - AD799X_CHANNEL_WITH_EVENTS(3, 12), - IIO_CHAN_SOFT_TIMESTAMP(4), - }, .num_channels = 5, - .default_config = AD7998_ALERT_EN, - .info = &ad7993_4_7_8_info, + .noirq_config = { + .channel = { + AD799X_CHANNEL(0, 12), + AD799X_CHANNEL(1, 12), + AD799X_CHANNEL(2, 12), + AD799X_CHANNEL(3, 12), + IIO_CHAN_SOFT_TIMESTAMP(4), + }, + .info = &ad7993_4_7_8_noirq_info, + }, + .irq_config = { + .channel = { + AD799X_CHANNEL_WITH_EVENTS(0, 12), + AD799X_CHANNEL_WITH_EVENTS(1, 12), + AD799X_CHANNEL_WITH_EVENTS(2, 12), + AD799X_CHANNEL_WITH_EVENTS(3, 12), + IIO_CHAN_SOFT_TIMESTAMP(4), + }, + .default_config = AD7998_ALERT_EN, + .info = &ad7993_4_7_8_irq_info, + }, }, [ad7997] = { - .channel = { - AD799X_CHANNEL_WITH_EVENTS(0, 10), - AD799X_CHANNEL_WITH_EVENTS(1, 10), - AD799X_CHANNEL_WITH_EVENTS(2, 10), - AD799X_CHANNEL_WITH_EVENTS(3, 10), - AD799X_CHANNEL(4, 10), - AD799X_CHANNEL(5, 10), - AD799X_CHANNEL(6, 10), - AD799X_CHANNEL(7, 10), - IIO_CHAN_SOFT_TIMESTAMP(8), - }, .num_channels = 9, - .default_config = AD7998_ALERT_EN, - .info = &ad7993_4_7_8_info, + .noirq_config = { + .channel = { + AD799X_CHANNEL(0, 10), + AD799X_CHANNEL(1, 10), + AD799X_CHANNEL(2, 10), + AD799X_CHANNEL(3, 10), + AD799X_CHANNEL(4, 10), + AD799X_CHANNEL(5, 10), + AD799X_CHANNEL(6, 10), + AD799X_CHANNEL(7, 10), + IIO_CHAN_SOFT_TIMESTAMP(8), + }, + .info = &ad7993_4_7_8_noirq_info, + }, + .irq_config = { + .channel = { + AD799X_CHANNEL_WITH_EVENTS(0, 10), + AD799X_CHANNEL_WITH_EVENTS(1, 10), + AD799X_CHANNEL_WITH_EVENTS(2, 10), + AD799X_CHANNEL_WITH_EVENTS(3, 10), + AD799X_CHANNEL(4, 10), + AD799X_CHANNEL(5, 10), + AD799X_CHANNEL(6, 10), + AD799X_CHANNEL(7, 10), + IIO_CHAN_SOFT_TIMESTAMP(8), + }, + .default_config = AD7998_ALERT_EN, + .info = &ad7993_4_7_8_irq_info, + }, }, [ad7998] = { - .channel = { - AD799X_CHANNEL_WITH_EVENTS(0, 12), - AD799X_CHANNEL_WITH_EVENTS(1, 12), - AD799X_CHANNEL_WITH_EVENTS(2, 12), - AD799X_CHANNEL_WITH_EVENTS(3, 12), - AD799X_CHANNEL(4, 12), - AD799X_CHANNEL(5, 12), - AD799X_CHANNEL(6, 12), - AD799X_CHANNEL(7, 12), - IIO_CHAN_SOFT_TIMESTAMP(8), - }, .num_channels = 9, - .default_config = AD7998_ALERT_EN, - .info = &ad7993_4_7_8_info, + .noirq_config = { + .channel = { + AD799X_CHANNEL(0, 12), + AD799X_CHANNEL(1, 12), + AD799X_CHANNEL(2, 12), + AD799X_CHANNEL(3, 12), + AD799X_CHANNEL(4, 12), + AD799X_CHANNEL(5, 12), + AD799X_CHANNEL(6, 12), + AD799X_CHANNEL(7, 12), + IIO_CHAN_SOFT_TIMESTAMP(8), + }, + .info = &ad7993_4_7_8_noirq_info, + }, + .irq_config = { + .channel = { + AD799X_CHANNEL_WITH_EVENTS(0, 12), + AD799X_CHANNEL_WITH_EVENTS(1, 12), + AD799X_CHANNEL_WITH_EVENTS(2, 12), + AD799X_CHANNEL_WITH_EVENTS(3, 12), + AD799X_CHANNEL(4, 12), + AD799X_CHANNEL(5, 12), + AD799X_CHANNEL(6, 12), + AD799X_CHANNEL(7, 12), + IIO_CHAN_SOFT_TIMESTAMP(8), + }, + .default_config = AD7998_ALERT_EN, + .info = &ad7993_4_7_8_irq_info, + }, }, }; @@ -607,6 +695,8 @@ static int ad799x_probe(struct i2c_client *client, int ret; struct ad799x_state *st; struct iio_dev *indio_dev; + const struct ad799x_chip_info *chip_info = + &ad799x_chip_info_tbl[id->driver_data]; indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*st)); if (indio_dev == NULL) @@ -617,8 +707,11 @@ static int ad799x_probe(struct i2c_client *client, i2c_set_clientdata(client, indio_dev); st->id = id->driver_data; - st->chip_info = &ad799x_chip_info_tbl[st->id]; - st->config = st->chip_info->default_config; + if (client->irq > 0 && chip_info->irq_config.info) + st->chip_config = &chip_info->irq_config; + else + st->chip_config = &chip_info->noirq_config; + st->config = st->chip_config->default_config; /* TODO: Add pdata options for filtering and bit delay */ @@ -641,11 +734,11 @@ static int ad799x_probe(struct i2c_client *client, indio_dev->dev.parent = &client->dev; indio_dev->name = id->name; - indio_dev->info = st->chip_info->info; + indio_dev->info = st->chip_config->info; indio_dev->modes = INDIO_DIRECT_MODE; - indio_dev->channels = st->chip_info->channel; - indio_dev->num_channels = st->chip_info->num_channels; + indio_dev->channels = st->chip_config->channel; + indio_dev->num_channels = chip_info->num_channels; ret = iio_triggered_buffer_setup(indio_dev, NULL, &ad799x_trigger_handler, NULL); -- cgit v1.2.3 From ef0bf6f82d4bc2edaa88eaf679b5acc355f27037 Mon Sep 17 00:00:00 2001 From: Peter Meerwald Date: Sat, 6 Dec 2014 05:54:00 +0000 Subject: iio:adc:ad799x: Make chan_spec const in ad799x_chip_config struct Signed-off-by: Peter Meerwald Acked-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/adc/ad799x.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'drivers/iio/adc') diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c index b8a811726d2f..92401c958ea2 100644 --- a/drivers/iio/adc/ad799x.c +++ b/drivers/iio/adc/ad799x.c @@ -107,7 +107,7 @@ enum { * @info: pointer to iio_info struct */ struct ad799x_chip_config { - struct iio_chan_spec channel[9]; + const struct iio_chan_spec channel[9]; u16 default_config; const struct iio_info *info; }; -- cgit v1.2.3 From 89ca79afe50e9e9608ad3315bf099fdbb82c4eb5 Mon Sep 17 00:00:00 2001 From: Peter Meerwald Date: Sat, 6 Dec 2014 05:54:00 +0000 Subject: iio:adc:ad799x: Add helper function to read/write config register 16-bit on ad7997/ad7998, 8-bit elsewhere Signed-off-by: Peter Meerwald Acked-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/adc/ad799x.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'drivers/iio/adc') diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c index 92401c958ea2..fe2a9b1170b0 100644 --- a/drivers/iio/adc/ad799x.c +++ b/drivers/iio/adc/ad799x.c @@ -136,6 +136,30 @@ struct ad799x_state { unsigned int transfer_size; }; +static int ad799x_write_config(struct ad799x_state *st, u16 val) +{ + switch (st->id) { + case ad7997: + case ad7998: + return i2c_smbus_write_word_swapped(st->client, AD7998_CONF_REG, + val); + default: + return i2c_smbus_write_byte_data(st->client, AD7998_CONF_REG, + val); + } +} + +static int ad799x_read_config(struct ad799x_state *st) +{ + switch (st->id) { + case ad7997: + case ad7998: + return i2c_smbus_read_word_swapped(st->client, AD7998_CONF_REG); + default: + return i2c_smbus_read_byte_data(st->client, AD7998_CONF_REG); + } +} + /** * ad799x_trigger_handler() bh of trigger launched polling to ring buffer * -- cgit v1.2.3 From 0f7ddcc1bff1983d0be1573f971995371669fa4f Mon Sep 17 00:00:00 2001 From: Peter Meerwald Date: Sat, 6 Dec 2014 05:54:00 +0000 Subject: iio:adc:ad799x: Write default config on probe and reset alert status on probe writing ALERT_EN and BUSY_ALERT to the chip config register clears pending alerts, BUSY_ALERT is cleared when reading back the register Signed-off-by: Peter Meerwald Acked-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/adc/ad799x.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'drivers/iio/adc') diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c index fe2a9b1170b0..84ce9b426a48 100644 --- a/drivers/iio/adc/ad799x.c +++ b/drivers/iio/adc/ad799x.c @@ -595,7 +595,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { AD799X_CHANNEL_WITH_EVENTS(1, 12), IIO_CHAN_SOFT_TIMESTAMP(3), }, - .default_config = AD7998_ALERT_EN, + .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT, .info = &ad7993_4_7_8_irq_info, }, }, @@ -619,7 +619,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { AD799X_CHANNEL_WITH_EVENTS(3, 10), IIO_CHAN_SOFT_TIMESTAMP(4), }, - .default_config = AD7998_ALERT_EN, + .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT, .info = &ad7993_4_7_8_irq_info, }, }, @@ -643,7 +643,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { AD799X_CHANNEL_WITH_EVENTS(3, 12), IIO_CHAN_SOFT_TIMESTAMP(4), }, - .default_config = AD7998_ALERT_EN, + .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT, .info = &ad7993_4_7_8_irq_info, }, }, @@ -675,7 +675,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { AD799X_CHANNEL(7, 10), IIO_CHAN_SOFT_TIMESTAMP(8), }, - .default_config = AD7998_ALERT_EN, + .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT, .info = &ad7993_4_7_8_irq_info, }, }, @@ -707,7 +707,7 @@ static const struct ad799x_chip_info ad799x_chip_info_tbl[] = { AD799X_CHANNEL(7, 12), IIO_CHAN_SOFT_TIMESTAMP(8), }, - .default_config = AD7998_ALERT_EN, + .default_config = AD7998_ALERT_EN | AD7998_BUSY_ALERT, .info = &ad7993_4_7_8_irq_info, }, }, @@ -735,7 +735,6 @@ static int ad799x_probe(struct i2c_client *client, st->chip_config = &chip_info->irq_config; else st->chip_config = &chip_info->noirq_config; - st->config = st->chip_config->default_config; /* TODO: Add pdata options for filtering and bit delay */ @@ -764,6 +763,14 @@ static int ad799x_probe(struct i2c_client *client, indio_dev->channels = st->chip_config->channel; indio_dev->num_channels = chip_info->num_channels; + ret = ad799x_write_config(st, st->chip_config->default_config); + if (ret < 0) + goto error_disable_reg; + ret = ad799x_read_config(st); + if (ret < 0) + goto error_disable_reg; + st->config = ret; + ret = iio_triggered_buffer_setup(indio_dev, NULL, &ad799x_trigger_handler, NULL); if (ret) -- cgit v1.2.3 From 8235841b95f5c2b88b2fd012b419c2f4adaeb898 Mon Sep 17 00:00:00 2001 From: Peter Meerwald Date: Sat, 6 Dec 2014 05:54:00 +0000 Subject: iio:adc:ad799x: Set conversion channels and rename ad7997_8_update_scan_mode() rename since function is used by all chips with ALERT pin, not just ad7997/8 Signed-off-by: Peter Meerwald Acked-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/adc/ad799x.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'drivers/iio/adc') diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c index 84ce9b426a48..abc36c896470 100644 --- a/drivers/iio/adc/ad799x.c +++ b/drivers/iio/adc/ad799x.c @@ -208,7 +208,7 @@ out: return IRQ_HANDLED; } -static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev, +static int ad799x_update_scan_mode(struct iio_dev *indio_dev, const unsigned long *scan_mask) { struct ad799x_state *st = iio_priv(indio_dev); @@ -221,10 +221,14 @@ static int ad7997_8_update_scan_mode(struct iio_dev *indio_dev, st->transfer_size = bitmap_weight(scan_mask, indio_dev->masklength) * 2; switch (st->id) { + case ad7992: + case ad7993: + case ad7994: case ad7997: case ad7998: - return i2c_smbus_write_word_swapped(st->client, AD7998_CONF_REG, - st->config | (*scan_mask << AD799X_CHANNEL_SHIFT)); + st->config &= ~(GENMASK(7, 0) << AD799X_CHANNEL_SHIFT); + st->config |= (*scan_mask << AD799X_CHANNEL_SHIFT); + return ad799x_write_config(st, st->config); default: return 0; } @@ -483,7 +487,7 @@ static const struct iio_info ad7991_info = { static const struct iio_info ad7993_4_7_8_noirq_info = { .read_raw = &ad799x_read_raw, .driver_module = THIS_MODULE, - .update_scan_mode = ad7997_8_update_scan_mode, + .update_scan_mode = ad799x_update_scan_mode, }; static const struct iio_info ad7993_4_7_8_irq_info = { @@ -493,7 +497,7 @@ static const struct iio_info ad7993_4_7_8_irq_info = { .read_event_value = &ad799x_read_event_value, .write_event_value = &ad799x_write_event_value, .driver_module = THIS_MODULE, - .update_scan_mode = ad7997_8_update_scan_mode, + .update_scan_mode = ad799x_update_scan_mode, }; static const struct iio_event_spec ad799x_events[] = { -- cgit v1.2.3 From 1d15330a399217500d496f8cd1852e11225000a1 Mon Sep 17 00:00:00 2001 From: Peter Meerwald Date: Sat, 6 Dec 2014 05:54:00 +0000 Subject: iio:adc:ad799x: Return more meaningful event enabled state only report an event as enabled if it actually is enabled Signed-off-by: Peter Meerwald Acked-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/adc/ad799x.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'drivers/iio/adc') diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c index abc36c896470..08bcada39487 100644 --- a/drivers/iio/adc/ad799x.c +++ b/drivers/iio/adc/ad799x.c @@ -364,7 +364,15 @@ static int ad799x_read_event_config(struct iio_dev *indio_dev, enum iio_event_type type, enum iio_event_direction dir) { - return 1; + struct ad799x_state *st = iio_priv(indio_dev); + + if (!(st->config & AD7998_ALERT_EN)) + return 0; + + if ((st->config >> AD799X_CHANNEL_SHIFT) & BIT(chan->scan_index)) + return 1; + + return 0; } static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan, -- cgit v1.2.3 From 3008d082f2fa27249d6318cf46f835ba443d3950 Mon Sep 17 00:00:00 2001 From: Peter Meerwald Date: Sat, 6 Dec 2014 05:54:00 +0000 Subject: iio:adc:ad799x: Allow writing of event config Allows explicity enabling of events Previously, events were always reported as enabled, but actually only implicitly enabled when updating the buffer scan mode Signed-off-by: Peter Meerwald Acked-by: Lars-Peter Clausen Signed-off-by: Jonathan Cameron --- drivers/iio/adc/ad799x.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'drivers/iio/adc') diff --git a/drivers/iio/adc/ad799x.c b/drivers/iio/adc/ad799x.c index 08bcada39487..e37412da15f5 100644 --- a/drivers/iio/adc/ad799x.c +++ b/drivers/iio/adc/ad799x.c @@ -375,6 +375,39 @@ static int ad799x_read_event_config(struct iio_dev *indio_dev, return 0; } +static int ad799x_write_event_config(struct iio_dev *indio_dev, + const struct iio_chan_spec *chan, + enum iio_event_type type, + enum iio_event_direction dir, + int state) +{ + struct ad799x_state *st = iio_priv(indio_dev); + int ret; + + mutex_lock(&indio_dev->mlock); + if (iio_buffer_enabled(indio_dev)) { + ret = -EBUSY; + goto done; + } + + if (state) + st->config |= BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT; + else + st->config &= ~(BIT(chan->scan_index) << AD799X_CHANNEL_SHIFT); + + if (st->config >> AD799X_CHANNEL_SHIFT) + st->config |= AD7998_ALERT_EN; + else + st->config &= ~AD7998_ALERT_EN; + + ret = ad799x_write_config(st, st->config); + +done: + mutex_unlock(&indio_dev->mlock); + + return ret; +} + static unsigned int ad799x_threshold_reg(const struct iio_chan_spec *chan, enum iio_event_direction dir, enum iio_event_info info) @@ -502,6 +535,7 @@ static const struct iio_info ad7993_4_7_8_irq_info = { .read_raw = &ad799x_read_raw, .event_attrs = &ad799x_event_attrs_group, .read_event_config = &ad799x_read_event_config, + .write_event_config = &ad799x_write_event_config, .read_event_value = &ad799x_read_event_value, .write_event_value = &ad799x_write_event_value, .driver_module = THIS_MODULE, -- cgit v1.2.3 From e49d99e0ecc88191c2e51a6535b1d0df635f1f3b Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 22 Jul 2014 03:04:00 +0100 Subject: iio: adc: exynos_adc: Add exynos_adc_data structure to improve readability This patchset add 'exynos_adc_data' structure which includes some functions to control ADC operation and specific data according to ADC version (v1 or v2). Signed-off-by: Chanwoo Choi Acked-by: Kyungmin Park Reviewed-by: Naveen Krishna Chatradhi Reviewed-by: Tomasz Figa Acked-by: Arnd Bergmann Signed-off-by: Jonathan Cameron --- drivers/iio/adc/exynos_adc.c | 226 ++++++++++++++++++++++++++++--------------- 1 file changed, 147 insertions(+), 79 deletions(-) (limited to 'drivers/iio/adc') diff --git a/drivers/iio/adc/exynos_adc.c b/drivers/iio/adc/exynos_adc.c index 010578f1d762..66d27c638d83 100644 --- a/drivers/iio/adc/exynos_adc.c +++ b/drivers/iio/adc/exynos_adc.c @@ -39,11 +39,6 @@ #include #include -enum adc_version { - ADC_V1, - ADC_V2 -}; - /* EXYNOS4412/5250 ADC_V1 registers definitions */ #define ADC_V1_CON(x) ((x) + 0x00) #define ADC_V1_DLY(x) ((x) + 0x08) @@ -85,6 +80,7 @@ enum adc_version { #define EXYNOS_ADC_TIMEOUT (msecs_to_jiffies(100)) struct exynos_adc { + struct exynos_adc_data *data; void __iomem *regs; void __iomem *enable_reg; struct clk *clk; @@ -97,43 +93,139 @@ struct exynos_adc { unsigned int version; }; -static const struct of_device_id exynos_adc_match[] = { - { .compatible = "samsung,exynos-adc-v1", .data = (void *)ADC_V1 }, - { .compatible = "samsung,exynos-adc-v2", .data = (void *)ADC_V2 }, - {}, +struct exynos_adc_data { + int num_channels; + + void (*init_hw)(struct exynos_adc *info); + void (*exit_hw)(struct exynos_adc *info); + void (*clear_irq)(struct exynos_adc *info); + void (*start_conv)(struct exynos_adc *info, unsigned long addr); }; -MODULE_DEVICE_TABLE(of, exynos_adc_match); -static inline unsigned int exynos_adc_get_version(struct platform_device *pdev) +static void exynos_adc_v1_init_hw(struct exynos_adc *info) { - const struct of_device_id *match; + u32 con1; - match = of_match_node(exynos_adc_match, pdev->dev.of_node); - return (unsigned int)match->data; + writel(1, info->enable_reg); + + /* set default prescaler values and Enable prescaler */ + con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN; + + /* Enable 12-bit ADC resolution */ + con1 |= ADC_V1_CON_RES; + writel(con1, ADC_V1_CON(info->regs)); +} + +static void exynos_adc_v1_exit_hw(struct exynos_adc *info) +{ + u32 con; + + writel(0, info->enable_reg); + + con = readl(ADC_V1_CON(info->regs)); + con |= ADC_V1_CON_STANDBY; + writel(con, ADC_V1_CON(info->regs)); +} + +static void exynos_adc_v1_clear_irq(struct exynos_adc *info) +{ + writel(1, ADC_V1_INTCLR(info->regs)); } -static void exynos_adc_hw_init(struct exynos_adc *info) +static void exynos_adc_v1_start_conv(struct exynos_adc *info, + unsigned long addr) +{ + u32 con1; + + writel(addr, ADC_V1_MUX(info->regs)); + + con1 = readl(ADC_V1_CON(info->regs)); + writel(con1 | ADC_CON_EN_START, ADC_V1_CON(info->regs)); +} + +static const struct exynos_adc_data exynos_adc_v1_data = { + .num_channels = MAX_ADC_V1_CHANNELS, + + .init_hw = exynos_adc_v1_init_hw, + .exit_hw = exynos_adc_v1_exit_hw, + .clear_irq = exynos_adc_v1_clear_irq, + .start_conv = exynos_adc_v1_start_conv, +}; + +static void exynos_adc_v2_init_hw(struct exynos_adc *info) { u32 con1, con2; - if (info->version == ADC_V2) { - con1 = ADC_V2_CON1_SOFT_RESET; - writel(con1, ADC_V2_CON1(info->regs)); + writel(1, info->enable_reg); - con2 = ADC_V2_CON2_OSEL | ADC_V2_CON2_ESEL | - ADC_V2_CON2_HIGHF | ADC_V2_CON2_C_TIME(0); - writel(con2, ADC_V2_CON2(info->regs)); + con1 = ADC_V2_CON1_SOFT_RESET; + writel(con1, ADC_V2_CON1(info->regs)); - /* Enable interrupts */ - writel(1, ADC_V2_INT_EN(info->regs)); - } else { - /* set default prescaler values and Enable prescaler */ - con1 = ADC_V1_CON_PRSCLV(49) | ADC_V1_CON_PRSCEN; + con2 = ADC_V2_CON2_OSEL | ADC_V2_CON2_ESEL | + ADC_V2_CON2_HIGHF | ADC_V2_CON2_C_TIME(0); + writel(con2, ADC_V2_CON2(info->regs)); - /* Enable 12-bit ADC resolution */ - con1 |= ADC_V1_CON_RES; - writel(con1, ADC_V1_CON(info->regs)); - } + /* Enable interrupts */ + writel(1, ADC_V2_INT_EN(info->regs)); +} + +static void exynos_adc_v2_exit_hw(struct exynos_adc *info) +{ + u32 con; + + writel(0, info->enable_reg); + + con = readl(ADC_V2_CON1(info->regs)); + con &= ~ADC_CON_EN_START; + writel(con, ADC_V2_CON1(info->regs)); +} + +static void exynos_adc_v2_clear_irq(struct exynos_adc *info) +{ + writel(1, ADC_V2_INT_ST(info->regs)); +} + +static void exynos_adc_v2_start_conv(struct exynos_adc *info, + unsigned long addr) +{ + u32 con1, con2; + + con2 = readl(ADC_V2_CON2(info->regs)); + con2 &= ~ADC_V2_CON2_ACH_MASK; + con2 |= ADC_V2_CON2_ACH_SEL(addr); + writel(con2, ADC_V2_CON2(info->regs)); + + con1 = readl(ADC_V2_CON1(info->regs)); + writel(con1 | ADC_CON_EN_START, ADC_V2_CON1(info->regs)); +} + +static const struct exynos_adc_data exynos_adc_v2_data = { + .num_channels = MAX_ADC_V2_CHANNELS, + + .init_hw = exynos_adc_v2_init_hw, + .exit_hw = exynos_adc_v2_exit_hw, + .clear_irq = exynos_adc_v2_clear_irq, + .start_conv = exynos_adc_v2_start_conv, +}; + +static const struct of_device_id exynos_adc_match[] = { + { + .compatible = "samsung,exynos-adc-v1", + .data = &exynos_adc_v1_data, + }, { + .compatible = "samsung,exynos-adc-v2", + .data = &exynos_adc_v2_data, + }, + {}, +}; +MODULE_DEVICE_TABLE(of, exynos_adc_match); + +static struct exynos_adc_data *exynos_adc_get_data(struct platform_device *pdev) +{ + const struct of_device_id *match; + + match = of_match_node(exynos_adc_match, pdev->dev.of_node); + return (struct exynos_adc_data *)match->data; } static int exynos_read_raw(struct iio_dev *indio_dev, @@ -144,7 +236,6 @@ static int exynos_read_raw(struct iio_dev *indio_dev, { struct exynos_adc *info = iio_priv(indio_dev); unsigned long timeout; - u32 con1, con2; int ret; if (mask != IIO_CHAN_INFO_RAW) @@ -154,28 +245,15 @@ static int exynos_read_raw(struct iio_dev *indio_dev, reinit_completion(&info->completion); /* Select the channel to be used and Trigger conversion */ - if (info->version == ADC_V2) { - con2 = readl(ADC_V2_CON2(info->regs)); - con2 &= ~ADC_V2_CON2_ACH_MASK; - con2 |= ADC_V2_CON2_ACH_SEL(chan->address); - writel(con2, ADC_V2_CON2(info->regs)); - - con1 = readl(ADC_V2_CON1(info->regs)); - writel(con1 | ADC_CON_EN_START, - ADC_V2_CON1(info->regs)); - } else { - writel(chan->address, ADC_V1_MUX(info->regs)); - - con1 = readl(ADC_V1_CON(info->regs)); - writel(con1 | ADC_CON_EN_START, - ADC_V1_CON(info->regs)); - } + if (info->data->start_conv) + info->data->start_conv(info, chan->address); timeout = wait_for_completion_timeout (&info->completion, EXYNOS_ADC_TIMEOUT); if (timeout == 0) { dev_warn(&indio_dev->dev, "Conversion timed out! Resetting\n"); - exynos_adc_hw_init(info); + if (info->data->init_hw) + info->data->init_hw(info); ret = -ETIMEDOUT; } else { *val = info->value; @@ -193,13 +271,11 @@ static irqreturn_t exynos_adc_isr(int irq, void *dev_id) struct exynos_adc *info = (struct exynos_adc *)dev_id; /* Read value */ - info->value = readl(ADC_V1_DATX(info->regs)) & - ADC_DATX_MASK; + info->value = readl(ADC_V1_DATX(info->regs)) & ADC_DATX_MASK; + /* clear irq */ - if (info->version == ADC_V2) - writel(1, ADC_V2_INT_ST(info->regs)); - else - writel(1, ADC_V1_INTCLR(info->regs)); + if (info->data->clear_irq) + info->data->clear_irq(info); complete(&info->completion); @@ -277,6 +353,12 @@ static int exynos_adc_probe(struct platform_device *pdev) info = iio_priv(indio_dev); + info->data = exynos_adc_get_data(pdev); + if (!info->data) { + dev_err(&pdev->dev, "failed getting exynos_adc_data\n"); + return -EINVAL; + } + mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); info->regs = devm_ioremap_resource(&pdev->dev, mem); if (IS_ERR(info->regs)) @@ -319,10 +401,6 @@ static int exynos_adc_probe(struct platform_device *pdev) if (ret) goto err_disable_reg; - writel(1, info->enable_reg); - - info->version = exynos_adc_get_version(pdev); - platform_set_drvdata(pdev, indio_dev); indio_dev->name = dev_name(&pdev->dev); @@ -331,11 +409,7 @@ static int exynos_adc_probe(struct platform_device *pdev) indio_dev->info = &exynos_adc_iio_info; indio_dev->modes = INDIO_DIRECT_MODE; indio_dev->channels = exynos_adc_iio_channels; - - if (info->version == ADC_V1) - indio_dev->num_channels = MAX_ADC_V1_CHANNELS; - else - indio_dev->num_channels = MAX_ADC_V2_CHANNELS; + indio_dev->num_channels = info->data->num_channels; ret = request_irq(info->irq, exynos_adc_isr, 0, dev_name(&pdev->dev), info); @@ -349,7 +423,8 @@ static int exynos_adc_probe(struct platform_device *pdev) if (ret) goto err_irq; - exynos_adc_hw_init(info); + if (info->data->init_hw) + info->data->init_hw(info); ret = of_platform_populate(np, exynos_adc_match, NULL, &indio_dev->dev); if (ret < 0) { @@ -366,7 +441,8 @@ err_of_populate: err_irq: free_irq(info->irq, info); err_disable_clk: - writel(0, info->enable_reg); + if (info->data->exit_hw) + info->data->exit_hw(info); clk_disable_unprepare(info->clk); err_disable_reg: regulator_disable(info->vdd); @@ -382,7 +458,8 @@ static int exynos_adc_remove(struct platform_device *pdev) exynos_adc_remove_devices); iio_device_unregister(indio_dev); free_irq(info->irq, info); - writel(0, info->enable_reg); + if (info->data->exit_hw) + info->data->exit_hw(info); clk_disable_unprepare(info->clk); regulator_disable(info->vdd); @@ -394,19 +471,10 @@ static int exynos_adc_suspend(struct device *dev) { struct iio_dev *indio_dev = dev_get_drvdata(dev); struct exynos_adc *info = iio_priv(indio_dev); - u32 con; - if (info->version == ADC_V2) { - con = readl(ADC_V2_CON1(info->regs)); - con &= ~ADC_CON_EN_START; - writel(con, ADC_V2_CON1(info->regs)); - } else { - con = readl(ADC_V1_CON(info->regs)); - con |= ADC_V1_CON_STANDBY; - writel(con, ADC_V1_CON(info->regs)); - } + if (info->data->exit_hw) + info->data->exit_hw(info); - writel(0, info->enable_reg); clk_disable_unprepare(info->clk); regulator_disable(info->vdd); @@ -427,8 +495,8 @@ static int exynos_adc_resume(struct device *dev) if (ret) return ret; - writel(1, info->enable_reg); - exynos_adc_hw_init(info); + if (info->data->init_hw) + info->data->init_hw(info); return 0; } -- cgit v1.2.3 From adb4e3f4cf9ed50c28cc8fb02e3f04e962ff184d Mon Sep 17 00:00:00 2001 From: Chanwoo Choi Date: Tue, 22 Jul 2014 03:04:00 +0100 Subject: iio: adc: exynos_adc: Control special clock of ADC to support Exynos3250 ADC This patch control special clock for ADC in Exynos series's FSYS block. If special clock of ADC is registerd on clock list of common clk framework, Exynos ADC drvier have to control this clock. Exynos3250/Exynos4/Exynos5 has 'adc' clock as following: - 'adc' clock: bus clock for ADC Exynos3250 has additional 'sclk_adc' clock as following: - 'sclk_adc' clock: special clock for ADC which provide clock to internal ADC Exynos 4210/4212/4412 and Exynos5250/5420 has not included 'sclk_adc' clock in FSYS_BLK. But, Exynos3250 based on Cortex-A7 has only included 'sclk_adc' clock in FSYS_BLK. Signed-off-by: Chanwoo Choi Acked-by: Kyungmin Park Reviewed-by: Tomasz Figa Acked-by: Arnd Bergmann Signed-off-by: Jonathan Cameron --- drivers/iio/adc/exynos_adc.c | 111 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 103 insertions(+), 8 deletions(-) (limited to 'drivers/iio/adc') diff --git a/drivers/iio/adc/exynos_adc.c b/drivers/iio/adc/exynos_adc.c index 66d27c638d83..fc9dfc23ecb7 100644 --- a/drivers/iio/adc/exynos_adc.c +++ b/drivers/iio/adc/exynos_adc.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -70,8 +71,9 @@ #define ADC_V2_CON2_ACH_SEL(x) (((x) & 0xF) << 0) #define ADC_V2_CON2_ACH_MASK 0xF -#define MAX_ADC_V2_CHANNELS 10 -#define MAX_ADC_V1_CHANNELS 8 +#define MAX_ADC_V2_CHANNELS 10 +#define MAX_ADC_V1_CHANNELS 8 +#define MAX_EXYNOS3250_ADC_CHANNELS 2 /* Bit definitions common for ADC_V1 and ADC_V2 */ #define ADC_CON_EN_START (1u << 0) @@ -81,9 +83,11 @@ struct exynos_adc { struct exynos_adc_data *data; + struct device *dev; void __iomem *regs; void __iomem *enable_reg; struct clk *clk; + struct clk *sclk; unsigned int irq; struct regulator *vdd; @@ -95,6 +99,7 @@ struct exynos_adc { struct exynos_adc_data { int num_channels; + bool needs_sclk; void (*init_hw)(struct exynos_adc *info); void (*exit_hw)(struct exynos_adc *info); @@ -102,6 +107,66 @@ struct exynos_adc_data { void (*start_conv)(struct exynos_adc *info, unsigned long addr); }; +static void exynos_adc_unprepare_clk(struct exynos_adc *info) +{ + if (info->data->needs_sclk) + clk_unprepare(info->sclk); + clk_unprepare(info->clk); +} + +static int exynos_adc_prepare_clk(struct exynos_adc *info) +{ + int ret; + + ret = clk_prepare(info->clk); + if (ret) { + dev_err(info->dev, "failed preparing adc clock: %d\n", ret); + return ret; + } + + if (info->data->needs_sclk) { + ret = clk_prepare(info->sclk); + if (ret) { + clk_unprepare(info->clk); + dev_err(info->dev, + "failed preparing sclk_adc clock: %d\n", ret); + return ret; + } + } + + return 0; +} + +static void exynos_adc_disable_clk(struct exynos_adc *info) +{ + if (info->data->needs_sclk) + clk_disable(info->sclk); + clk_disable(info->clk); +} + +static int exynos_adc_enable_clk(struct exynos_adc *info) +{ + int ret; + + ret = clk_enable(info->clk); + if (ret) { + dev_err(info->dev, "failed enabling adc clock: %d\n", ret); + return ret; + } + + if (info->data->needs_sclk) { + ret = clk_enable(info->sclk); + if (ret) { + clk_disable(info->clk); + dev_err(info->dev, + "failed enabling sclk_adc clock: %d\n", ret); + return ret; + } + } + + return 0; +} + static void exynos_adc_v1_init_hw(struct exynos_adc *info) { u32 con1; @@ -208,6 +273,16 @@ static const struct exynos_adc_data exynos_adc_v2_data = { .start_conv = exynos_adc_v2_start_conv, }; +static const struct exynos_adc_data exynos3250_adc_data = { + .num_channels = MAX_EXYNOS3250_ADC_CHANNELS, + .needs_sclk = true, + + .init_hw = exynos_adc_v2_init_hw, + .exit_hw = exynos_adc_v2_exit_hw, + .clear_irq = exynos_adc_v2_clear_irq, + .start_conv = exynos_adc_v2_start_conv, +}; + static const struct of_device_id exynos_adc_match[] = { { .compatible = "samsung,exynos-adc-v1", @@ -215,6 +290,9 @@ static const struct of_device_id exynos_adc_match[] = { }, { .compatible = "samsung,exynos-adc-v2", .data = &exynos_adc_v2_data, + }, { + .compatible = "samsung,exynos3250-adc", + .data = &exynos3250_adc_data, }, {}, }; @@ -376,6 +454,7 @@ static int exynos_adc_probe(struct platform_device *pdev) } info->irq = irq; + info->dev = &pdev->dev; init_completion(&info->completion); @@ -386,6 +465,16 @@ static int exynos_adc_probe(struct platform_device *pdev) return PTR_ERR(info->clk); } + if (info->data->needs_sclk) { + info->sclk = devm_clk_get(&pdev->dev, "sclk"); + if (IS_ERR(info->sclk)) { + dev_err(&pdev->dev, + "failed getting sclk clock, err = %ld\n", + PTR_ERR(info->sclk)); + return PTR_ERR(info->sclk); + } + } + info->vdd = devm_regulator_get(&pdev->dev, "vdd"); if (IS_ERR(info->vdd)) { dev_err(&pdev->dev, "failed getting regulator, err = %ld\n", @@ -397,10 +486,14 @@ static int exynos_adc_probe(struct platform_device *pdev) if (ret) return ret; - ret = clk_prepare_enable(info->clk); + ret = exynos_adc_prepare_clk(info); if (ret) goto err_disable_reg; + ret = exynos_adc_enable_clk(info); + if (ret) + goto err_unprepare_clk; + platform_set_drvdata(pdev, indio_dev); indio_dev->name = dev_name(&pdev->dev); @@ -443,7 +536,9 @@ err_irq: err_disable_clk: if (info->data->exit_hw) info->data->exit_hw(info); - clk_disable_unprepare(info->clk); + exynos_adc_disable_clk(info); +err_unprepare_clk: + exynos_adc_unprepare_clk(info); err_disable_reg: regulator_disable(info->vdd); return ret; @@ -460,7 +555,8 @@ static int exynos_adc_remove(struct platform_device *pdev) free_irq(info->irq, info); if (info->data->exit_hw) info->data->exit_hw(info); - clk_disable_unprepare(info->clk); + exynos_adc_disable_clk(info); + exynos_adc_unprepare_clk(info); regulator_disable(info->vdd); return 0; @@ -474,8 +570,7 @@ static int exynos_adc_suspend(struct device *dev) if (info->data->exit_hw) info->data->exit_hw(info); - - clk_disable_unprepare(info->clk); + exynos_adc_disable_clk(info); regulator_disable(info->vdd); return 0; @@ -491,7 +586,7 @@ static int exynos_adc_resume(struct device *dev) if (ret) return ret; - ret = clk_prepare_enable(info->clk); + ret = exynos_adc_enable_clk(info); if (ret) return ret; -- cgit v1.2.3