staging: comedi: cb_pcidas: refactor write_calibration_bitstream()
authorH Hartley Sweeten <hsweeten@visionengravers.com>
Wed, 14 Oct 2015 00:47:49 +0000 (17:47 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 17 Oct 2015 06:20:10 +0000 (23:20 -0700)
Refactor this function to handle the common code used to select the
calibration device (trimpot or caldac) and latch the data after
sending the bitstream.

Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com>
Reviewed-by: Ian Abbott <abbotti@mev.co.uk>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/staging/comedi/drivers/cb_pcidas.c

index fb10fe6d380620264421ca182e6c9f6fc53d115c..c034de3fca423693d82df23d3248e78c314de640 100644 (file)
@@ -533,42 +533,49 @@ static int eeprom_read_insn(struct comedi_device *dev,
        return 1;
 }
 
-static void write_calibration_bitstream(struct comedi_device *dev,
-                                       unsigned int register_bits,
-                                       unsigned int bitstream,
-                                       unsigned int bitstream_length)
+static void cb_pcidas_calib_write(struct comedi_device *dev,
+                                 unsigned int val, unsigned int len,
+                                 bool trimpot)
 {
        struct cb_pcidas_private *devpriv = dev->private;
-       static const int write_delay = 1;
+       unsigned int calib_bits = cal_enable_bits(dev);
        unsigned int bit;
 
-       for (bit = 1 << (bitstream_length - 1); bit; bit >>= 1) {
-               if (bitstream & bit)
-                       register_bits |= PCIDAS_CALIB_DATA;
+       if (trimpot) {
+               /* select trimpot */
+               calib_bits |= PCIDAS_CALIB_TRIM_SEL;
+               outw(calib_bits, devpriv->pcibar1 + PCIDAS_CALIB_REG);
+       }
+
+       /* write bitstream to calibration device */
+       for (bit = 1 << (len - 1); bit; bit >>= 1) {
+               if (val & bit)
+                       calib_bits |= PCIDAS_CALIB_DATA;
                else
-                       register_bits &= ~PCIDAS_CALIB_DATA;
-               udelay(write_delay);
-               outw(register_bits, devpriv->pcibar1 + PCIDAS_CALIB_REG);
+                       calib_bits &= ~PCIDAS_CALIB_DATA;
+               udelay(1);
+               outw(calib_bits, devpriv->pcibar1 + PCIDAS_CALIB_REG);
+       }
+       udelay(1);
+
+       calib_bits = cal_enable_bits(dev);
+
+       if (!trimpot) {
+               /* select caldac */
+               outw(calib_bits | PCIDAS_CALIB_8800_SEL,
+                    devpriv->pcibar1 + PCIDAS_CALIB_REG);
+               udelay(1);
        }
+
+       /* latch value to trimpot/caldac */
+       outw(calib_bits, devpriv->pcibar1 + PCIDAS_CALIB_REG);
 }
 
 static void cb_pcidas_caldac_8800_write(struct comedi_device *dev,
                                        unsigned int chan, unsigned int val)
 {
-       struct cb_pcidas_private *devpriv = dev->private;
-
-       /* write 11-bit value */
-       write_calibration_bitstream(dev, cal_enable_bits(dev),
-                                   ((chan & 0x7) << 8) | val, 11);
-       udelay(1);
-
-       /* select caldac */
-       outw(cal_enable_bits(dev) | PCIDAS_CALIB_8800_SEL,
-            devpriv->pcibar1 + PCIDAS_CALIB_REG);
-       udelay(1);
-
-       /* latch value */
-       outw(cal_enable_bits(dev), devpriv->pcibar1 + PCIDAS_CALIB_REG);
+       /* write 11-bit value to caldac */
+       cb_pcidas_calib_write(dev, ((chan & 0x7) << 8) | val, 11, false);
 }
 
 static int cb_pcidas_caldac_insn_write(struct comedi_device *dev,
@@ -630,40 +637,15 @@ static int cb_pcidas_dac08_insn_write(struct comedi_device *dev,
 static void cb_pcidas_trimpot_7376_write(struct comedi_device *dev,
                                         unsigned int val)
 {
-       struct cb_pcidas_private *devpriv = dev->private;
-       unsigned int calib_bits;
-
-       /* select trimpot */
-       calib_bits = cal_enable_bits(dev) | PCIDAS_CALIB_TRIM_SEL;
-       udelay(1);
-       outw(calib_bits, devpriv->pcibar1 + PCIDAS_CALIB_REG);
-
-       /* write 7-bit value */
-       write_calibration_bitstream(dev, calib_bits, val, 7);
-       udelay(1);
-
-       /* latch value */
-       outw(cal_enable_bits(dev), devpriv->pcibar1 + PCIDAS_CALIB_REG);
+       /* write 7-bit value to trimpot */
+       cb_pcidas_calib_write(dev, val, 7, true);
 }
 
 static void cb_pcidas_trimpot_8402_write(struct comedi_device *dev,
                                         unsigned int chan, unsigned int val)
 {
-       struct cb_pcidas_private *devpriv = dev->private;
-       unsigned int calib_bits;
-
-       /* select trimpot */
-       calib_bits = cal_enable_bits(dev) | PCIDAS_CALIB_TRIM_SEL;
-       udelay(1);
-       outw(calib_bits, devpriv->pcibar1 + PCIDAS_CALIB_REG);
-
-       /* write 10-bit value */
-       write_calibration_bitstream(dev, calib_bits,
-                                   ((chan & 0x3) << 8) | val, 10);
-       udelay(1);
-
-       /* latch value */
-       outw(cal_enable_bits(dev), devpriv->pcibar1 + PCIDAS_CALIB_REG);
+       /* write 10-bit value to trimpot */
+       cb_pcidas_calib_write(dev, ((chan & 0x3) << 8) | val, 10, true);
 }
 
 static void cb_pcidas_trimpot_write(struct comedi_device *dev,