/* * Conversions are synchronised on the rising edge of NAU7802_PUCTRL_CS_BIT
*/ staticint nau7802_sync(struct nau7802_state *st)
{ int ret;
ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL); if (ret < 0) return ret;
ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL,
ret | NAU7802_PUCTRL_CS_BIT);
status = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL); if (status < 0) return IRQ_HANDLED;
if (!(status & NAU7802_PUCTRL_CR_BIT)) return IRQ_NONE;
if (nau7802_read_conversion(st) < 0) return IRQ_HANDLED;
/* * Because there is actually only one ADC for both channels, we have to * wait for enough conversions to happen before getting a significant * value when changing channels and the values are far apart.
*/ if (st->conversion_count < NAU7802_MIN_CONVERSIONS)
st->conversion_count++; if (st->conversion_count >= NAU7802_MIN_CONVERSIONS)
complete(&st->value_ok);
return IRQ_HANDLED;
}
staticint nau7802_read_irq(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val)
{ struct nau7802_state *st = iio_priv(indio_dev); int ret;
/* read registers to ensure we flush everything */
ret = nau7802_read_conversion(st); if (ret < 0) goto read_chan_info_failure;
/* Wait for a conversion to finish */
ret = wait_for_completion_interruptible_timeout(&st->value_ok,
msecs_to_jiffies(1000)); if (ret == 0)
ret = -ETIMEDOUT;
staticint nau7802_read_poll(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val)
{ struct nau7802_state *st = iio_priv(indio_dev); int ret;
nau7802_sync(st);
/* read registers to ensure we flush everything */
ret = nau7802_read_conversion(st); if (ret < 0) return ret;
/* * Because there is actually only one ADC for both channels, we have to * wait for enough conversions to happen before getting a significant * value when changing channels and the values are far appart.
*/ do {
ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL); if (ret < 0) return ret;
while (!(ret & NAU7802_PUCTRL_CR_BIT)) { if (st->sample_rate != NAU7802_SAMP_FREQ_320)
msleep(20); else
mdelay(4);
ret = i2c_smbus_read_byte_data(st->client,
NAU7802_REG_PUCTRL); if (ret < 0) return ret;
}
ret = nau7802_read_conversion(st); if (ret < 0) return ret; if (st->conversion_count < NAU7802_MIN_CONVERSIONS)
st->conversion_count++;
} while (st->conversion_count < NAU7802_MIN_CONVERSIONS);
*val = st->last_value;
return IIO_VAL_INT;
}
staticint nau7802_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int *val, int *val2, long mask)
{ struct nau7802_state *st = iio_priv(indio_dev); int ret;
switch (mask) { case IIO_CHAN_INFO_RAW:
mutex_lock(&st->lock); /* * Select the channel to use * - Channel 1 is value 0 in the CHS register * - Channel 2 is value 1 in the CHS register
*/
ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_CTRL2); if (ret < 0) {
mutex_unlock(&st->lock); return ret;
}
if (ret < 0) {
mutex_unlock(&st->lock); return ret;
}
}
if (st->client->irq)
ret = nau7802_read_irq(indio_dev, chan, val); else
ret = nau7802_read_poll(indio_dev, chan, val);
mutex_unlock(&st->lock); return ret;
case IIO_CHAN_INFO_SCALE:
ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_CTRL1); if (ret < 0) return ret;
/* * We have 24 bits of signed data, that means 23 bits of data * plus the sign bit
*/
*val = st->vref_mv;
*val2 = 23 + (ret & NAU7802_CTRL1_GAINS_BITS);
return IIO_VAL_FRACTIONAL_LOG2;
case IIO_CHAN_INFO_SAMP_FREQ:
*val = nau7802_sample_freq_avail[st->sample_rate];
*val2 = 0; return IIO_VAL_INT;
default: break;
}
return -EINVAL;
}
staticint nau7802_write_raw(struct iio_dev *indio_dev, struct iio_chan_spec const *chan, int val, int val2, long mask)
{ struct nau7802_state *st = iio_priv(indio_dev); int i, ret;
switch (mask) { case IIO_CHAN_INFO_SCALE: for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++) if (val2 == st->scale_avail[i]) return nau7802_set_gain(st, i);
break;
case IIO_CHAN_INFO_SAMP_FREQ: for (i = 0; i < ARRAY_SIZE(nau7802_sample_freq_avail); i++) if (val == nau7802_sample_freq_avail[i]) {
mutex_lock(&st->lock);
st->sample_rate = i;
st->conversion_count = 0;
ret = i2c_smbus_write_byte_data(st->client,
NAU7802_REG_CTRL2,
NAU7802_CTRL2_CRS(st->sample_rate));
mutex_unlock(&st->lock); return ret;
}
/* Reset the device */
ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL,
NAU7802_PUCTRL_RR_BIT); if (ret < 0) return ret;
/* Enter normal operation mode */
ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL,
NAU7802_PUCTRL_PUD_BIT); if (ret < 0) return ret;
/* * After about 200 usecs, the device should be ready and then * the Power Up bit will be set to 1. If not, wait for it.
*/
udelay(210);
ret = i2c_smbus_read_byte_data(st->client, NAU7802_REG_PUCTRL); if (ret < 0) return ret; if (!(ret & NAU7802_PUCTRL_PUR_BIT)) return ret;
data = NAU7802_PUCTRL_PUD_BIT | NAU7802_PUCTRL_PUA_BIT |
NAU7802_PUCTRL_CS_BIT; if (tmp >= 2400)
data |= NAU7802_PUCTRL_AVDDS_BIT;
ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_PUCTRL, data); if (ret < 0) return ret;
ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_ADC_CTRL, 0x30); if (ret < 0) return ret;
if (tmp >= 2400) {
data = NAU7802_CTRL1_VLDO((4500 - tmp) / 300);
ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_CTRL1,
data); if (ret < 0) return ret;
}
/* Populate available ADC input ranges */ for (i = 0; i < ARRAY_SIZE(st->scale_avail); i++)
st->scale_avail[i] = (((u64)st->vref_mv) * 1000000000ULL)
>> (23 + i);
init_completion(&st->value_ok);
/* * The ADC fires continuously and we can't do anything about * it. So we need to have the IRQ disabled by default, and we * will enable them back when we will need them..
*/ if (client->irq) {
ret = devm_request_threaded_irq(&client->dev, client->irq,
NULL,
nau7802_eoc_trigger,
IRQF_TRIGGER_HIGH | IRQF_ONESHOT |
IRQF_NO_AUTOEN,
client->dev.driver->name,
indio_dev); if (ret) { /* * What may happen here is that our IRQ controller is * not able to get level interrupt but this is required * by this ADC as when going over 40 sample per second, * the interrupt line may stay high between conversions. * So, we continue no matter what but we switch to * polling mode.
*/
dev_info(&client->dev, "Failed to allocate IRQ, using polling mode\n");
client->irq = 0;
}
}
if (!client->irq) { /* * We are polling, use the fastest sample rate by * default
*/
st->sample_rate = NAU7802_SAMP_FREQ_320;
ret = i2c_smbus_write_byte_data(st->client, NAU7802_REG_CTRL2,
NAU7802_CTRL2_CRS(st->sample_rate)); if (ret) return ret;
}
/* Setup the ADC channels available on the board */
indio_dev->num_channels = ARRAY_SIZE(nau7802_chan_array);
indio_dev->channels = nau7802_chan_array;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.