staging: comedi: me4000: simplify analog input range programming
authorH Hartley Sweeten <hsweeten@visionengravers.com>
Wed, 5 Aug 2015 17:44:51 +0000 (10:44 -0700)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 7 Aug 2015 22:03:28 +0000 (15:03 -0700)
The comedi_lrange table for the analog inputs is inverted compared
to the values that need to be written to the ME4000_AI_CHANNEL_LIST_REG
to select the range.

Create a macro, ME4000_AI_LIST_RANGE(), to handle the inversion. Remove
the old defines and simplify the code a bit.

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/me4000.c

index ba3313c..1001d63 100644 (file)
@@ -111,10 +111,7 @@ broken.
 #define ME4000_AI_CTRL_BIT_EX_TRIG_BOTH                (1 << 31)
 #define ME4000_AI_CHANNEL_LIST_REG             0x78
 #define ME4000_AI_LIST_INPUT_DIFFERENTIAL      (1 << 5)
-#define ME4000_AI_LIST_RANGE_BIPOLAR_10                (0 << 6)
-#define ME4000_AI_LIST_RANGE_BIPOLAR_2_5       (1 << 6)
-#define ME4000_AI_LIST_RANGE_UNIPOLAR_10       (2 << 6)
-#define ME4000_AI_LIST_RANGE_UNIPOLAR_2_5      (3 << 6)
+#define ME4000_AI_LIST_RANGE(x)                        ((3 - ((x) & 3)) << 6)
 #define ME4000_AI_LIST_LAST_ENTRY              (1 << 8)
 #define ME4000_AI_DATA_REG                     0x7c
 #define ME4000_AI_CHAN_TIMER_REG               0x80
@@ -301,6 +298,12 @@ static const struct me4000_board me4000_boards[] = {
        },
 };
 
+/*
+ * NOTE: the ranges here are inverted compared to the values
+ * written to the ME4000_AI_CHANNEL_LIST_REG,
+ *
+ * The ME4000_AI_LIST_RANGE() macro handles the inversion.
+ */
 static const struct comedi_lrange me4000_ai_range = {
        4, {
                UNI_RANGE(2.5),
@@ -455,24 +458,7 @@ static int me4000_ai_insn_read(struct comedi_device *dev,
                return -EINVAL;
        }
 
-       switch (rang) {
-       case 0:
-               entry |= ME4000_AI_LIST_RANGE_UNIPOLAR_2_5;
-               break;
-       case 1:
-               entry |= ME4000_AI_LIST_RANGE_UNIPOLAR_10;
-               break;
-       case 2:
-               entry |= ME4000_AI_LIST_RANGE_BIPOLAR_2_5;
-               break;
-       case 3:
-               entry |= ME4000_AI_LIST_RANGE_BIPOLAR_10;
-               break;
-       default:
-               dev_err(dev->class_dev, "Invalid range specified\n");
-               return -EINVAL;
-       }
-
+       entry |= ME4000_AI_LIST_RANGE(rang);
        entry |= chan;
        if (aref == AREF_DIFF) {
                if (!(s->subdev_flags && SDF_DIFF)) {
@@ -680,16 +666,7 @@ static int ai_write_chanlist(struct comedi_device *dev,
                rang = CR_RANGE(cmd->chanlist[i]);
                aref = CR_AREF(cmd->chanlist[i]);
 
-               entry = chan;
-
-               if (rang == 0)
-                       entry |= ME4000_AI_LIST_RANGE_UNIPOLAR_2_5;
-               else if (rang == 1)
-                       entry |= ME4000_AI_LIST_RANGE_UNIPOLAR_10;
-               else if (rang == 2)
-                       entry |= ME4000_AI_LIST_RANGE_BIPOLAR_2_5;
-               else
-                       entry |= ME4000_AI_LIST_RANGE_BIPOLAR_10;
+               entry = chan | ME4000_AI_LIST_RANGE(rang);
 
                if (aref == AREF_DIFF)
                        entry |= ME4000_AI_LIST_INPUT_DIFFERENTIAL;