Commit f5fbccb9 authored by H Hartley Sweeten's avatar H Hartley Sweeten Committed by Greg Kroah-Hartman

staging: comedi: mpc624: introduce mpc624_ai_get_sample()

Introduce a helper function to read the analog sample from the serially
connected A/D converter and handle the munging of the data.
Signed-off-by: default avatarH Hartley Sweeten <hsweeten@visionengravers.com>
Reviewed-by: default avatarIan Abbott <abbotti@mev.co.uk>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent aa0cf376
...@@ -115,78 +115,34 @@ static const struct comedi_lrange range_mpc624_bipolar10 = { ...@@ -115,78 +115,34 @@ static const struct comedi_lrange range_mpc624_bipolar10 = {
} }
}; };
static int mpc624_ai_eoc(struct comedi_device *dev, static unsigned int mpc624_ai_get_sample(struct comedi_device *dev,
struct comedi_subdevice *s, struct comedi_subdevice *s)
struct comedi_insn *insn,
unsigned long context)
{
unsigned char status;
status = inb(dev->iobase + MPC624_ADC);
if ((status & MPC624_ADBUSY) == 0)
return 0;
return -EBUSY;
}
static int mpc624_ai_insn_read(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_insn *insn,
unsigned int *data)
{ {
struct mpc624_private *devpriv = dev->private; struct mpc624_private *devpriv = dev->private;
int n, i; unsigned int data_out = devpriv->ai_speed;
unsigned long int data_in, data_out; unsigned int data_in = 0;
int ret; unsigned int bit;
int i;
/*
* WARNING:
* We always write 0 to GNSWA bit, so the channel range is +-/10.1Vdc
*/
outb(insn->chanspec, dev->iobase + MPC624_GNMUXCH);
for (n = 0; n < insn->n; n++) {
/* Trigger the conversion */
outb(MPC624_ADSCK, dev->iobase + MPC624_ADC);
udelay(1);
outb(MPC624_ADCS | MPC624_ADSCK, dev->iobase + MPC624_ADC);
udelay(1);
outb(0, dev->iobase + MPC624_ADC);
udelay(1);
/* Wait for the conversion to end */
ret = comedi_timeout(dev, s, insn, mpc624_ai_eoc, 0);
if (ret)
return ret;
/* Start reading data */ /* Start reading data */
data_in = 0;
data_out = devpriv->ai_speed;
udelay(1); udelay(1);
for (i = 0; i < 32; i++) { for (i = 0; i < 32; i++) {
/* Set the clock low */ /* Set the clock low */
outb(0, dev->iobase + MPC624_ADC); outb(0, dev->iobase + MPC624_ADC);
udelay(1); udelay(1);
if (data_out & BIT(31)) { /* the next bit is a 1 */ /* Set the ADSDI line for the next bit (send to MPC624) */
/* Set the ADSDI line (send to MPC624) */ bit = (data_out & BIT(31)) ? MPC624_ADSDI : 0;
outb(MPC624_ADSDI, dev->iobase + MPC624_ADC); outb(bit, dev->iobase + MPC624_ADC);
udelay(1); udelay(1);
/* Set the clock high */
outb(MPC624_ADSCK | MPC624_ADSDI,
dev->iobase + MPC624_ADC);
} else { /* the next bit is a 0 */
/* Set the ADSDI line (send to MPC624) */
outb(0, dev->iobase + MPC624_ADC);
udelay(1);
/* Set the clock high */ /* Set the clock high */
outb(MPC624_ADSCK, dev->iobase + MPC624_ADC); outb(MPC624_ADSCK | bit, dev->iobase + MPC624_ADC);
}
/* Read ADSDO on high clock (receive from MPC624) */
udelay(1); udelay(1);
/* Read ADSDO on high clock (receive from MPC624) */
data_in <<= 1; data_in <<= 1;
data_in |= data_in |= (inb(dev->iobase + MPC624_ADC) & MPC624_ADSDO) >> 4;
(inb(dev->iobase + MPC624_ADC) & MPC624_ADSDO) >> 4;
udelay(1); udelay(1);
data_out <<= 1; data_out <<= 1;
...@@ -194,57 +150,95 @@ static int mpc624_ai_insn_read(struct comedi_device *dev, ...@@ -194,57 +150,95 @@ static int mpc624_ai_insn_read(struct comedi_device *dev,
/* /*
* Received 32-bit long value consist of: * Received 32-bit long value consist of:
* 31: EOC - * 31: EOC - (End Of Transmission) bit - should be 0
* (End Of Transmission) bit - should be 0 * 30: DMY - (Dummy) bit - should be 0
* 30: DMY * 29: SIG - (Sign) bit - 1 if positive, 0 if negative
* (Dummy) bit - should be 0 * 28: MSB - (Most Significant Bit) - the first bit of the
* 29: SIG * conversion result
* (Sign) bit- 1 if the voltage is positive,
* 0 if negative
* 28: MSB
* (Most Significant Bit) - the first bit of
* the conversion result
* .... * ....
* 05: LSB * 05: LSB - (Least Significant Bit)- the last bit of the
* (Least Significant Bit)- the last bit of the
* conversion result * conversion result
* 04-00: sub-LSB * 04-00: sub-LSB - sub-LSBs are basically noise, but when
* - sub-LSBs are basically noise, but when * averaged properly, they can increase
* averaged properly, they can increase conversion * conversion precision up to 29 bits;
* precision up to 29 bits; they can be discarded * they can be discarded without loss of
* without loss of resolution. * resolution.
*/ */
if (data_in & MPC624_EOC_BIT) if (data_in & MPC624_EOC_BIT)
dev_dbg(dev->class_dev, dev_dbg(dev->class_dev, "EOC bit is set!");
"EOC bit is set (data_in=%lu)!", data_in);
if (data_in & MPC624_DMY_BIT) if (data_in & MPC624_DMY_BIT)
dev_dbg(dev->class_dev, dev_dbg(dev->class_dev, "DMY bit is set!");
"DMY bit is set (data_in=%lu)!", data_in);
if (data_in & MPC624_SGN_BIT) { /* Volatge is positive */ if (data_in & MPC624_SGN_BIT) {
/* /*
* Voltage is positive
*
* comedi operates on unsigned numbers, so mask off EOC * comedi operates on unsigned numbers, so mask off EOC
* and DMY and don't clear the SGN bit * and DMY and don't clear the SGN bit
*/ */
data_in &= 0x3FFFFFFF; data_in &= 0x3fffffff;
data[n] = data_in; } else {
} else { /* The voltage is negative */
/* /*
* The voltage is negative
*
* data_in contains a number in 30-bit two's complement * data_in contains a number in 30-bit two's complement
* code and we must deal with it * code and we must deal with it
*/ */
data_in |= MPC624_SGN_BIT; data_in |= MPC624_SGN_BIT;
data_in = ~data_in; data_in = ~data_in;
data_in += 1; data_in += 1;
data_in &= ~(MPC624_EOC_BIT | MPC624_DMY_BIT);
/* clear EOC and DMY bits */ /* clear EOC and DMY bits */
data_in &= ~(MPC624_EOC_BIT | MPC624_DMY_BIT);
data_in = 0x20000000 - data_in; data_in = 0x20000000 - data_in;
data[n] = data_in;
} }
return data_in;
}
static int mpc624_ai_eoc(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_insn *insn,
unsigned long context)
{
unsigned char status;
status = inb(dev->iobase + MPC624_ADC);
if ((status & MPC624_ADBUSY) == 0)
return 0;
return -EBUSY;
}
static int mpc624_ai_insn_read(struct comedi_device *dev,
struct comedi_subdevice *s,
struct comedi_insn *insn,
unsigned int *data)
{
int ret;
int i;
/*
* WARNING:
* We always write 0 to GNSWA bit, so the channel range is +-/10.1Vdc
*/
outb(insn->chanspec, dev->iobase + MPC624_GNMUXCH);
for (i = 0; i < insn->n; i++) {
/* Trigger the conversion */
outb(MPC624_ADSCK, dev->iobase + MPC624_ADC);
udelay(1);
outb(MPC624_ADCS | MPC624_ADSCK, dev->iobase + MPC624_ADC);
udelay(1);
outb(0, dev->iobase + MPC624_ADC);
udelay(1);
/* Wait for the conversion to end */
ret = comedi_timeout(dev, s, insn, mpc624_ai_eoc, 0);
if (ret)
return ret;
data[i] = mpc624_ai_get_sample(dev, s);
} }
/* Return the number of samples read/written */ return insn->n;
return n;
} }
static int mpc624_attach(struct comedi_device *dev, struct comedi_devconfig *it) static int mpc624_attach(struct comedi_device *dev, struct comedi_devconfig *it)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment