Skip to content

Commit 7603900

Browse files
bigguinessgregkh
authored andcommitted
staging: comedi: adv_pci1710: refactor ai range programming
The gain codes used to program the analog output range are currently stored in const char arrays. The values look a bit "magic" and it's not clear how they associate with the comedi_lrange without looking through user manuals. Refactor the ai range programming to clarify the driver and remove the magic numbers. Also, refine the bits in the range register that set the differential and unipolar modes. Signed-off-by: H Hartley Sweeten <[email protected]> Reviewed-by: Ian Abbott <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 19cbb8f commit 7603900

File tree

1 file changed

+55
-53
lines changed

1 file changed

+55
-53
lines changed

drivers/staging/comedi/drivers/adv_pci1710.c

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
#define PCI171X_AD_DATA_REG 0x00 /* R: A/D data */
4242
#define PCI171X_SOFTTRG_REG 0x00 /* W: soft trigger for A/D */
4343
#define PCI171X_RANGE_REG 0x02 /* W: A/D gain/range register */
44+
#define PCI171X_RANGE_DIFF BIT(5)
45+
#define PCI171X_RANGE_UNI BIT(4)
46+
#define PCI171X_RANGE_GAIN(x) (((x) & 0x7) << 0)
4447
#define PCI171X_MUX_REG 0x04 /* W: A/D multiplexor control */
4548
#define PCI171X_STATUS_REG 0x06 /* R: status register */
4649
#define PCI171X_STATUS_IRQ BIT(11) /* 1=IRQ occurred */
@@ -63,56 +66,47 @@
6366
#define PCI171X_DO_REG 0x10 /* W: digital outputs */
6467
#define PCI171X_TIMER_BASE 0x18 /* R/W: 8254 timer */
6568

66-
static const struct comedi_lrange range_pci1710_3 = {
69+
static const struct comedi_lrange pci1710_ai_range = {
6770
9, {
68-
BIP_RANGE(5),
69-
BIP_RANGE(2.5),
70-
BIP_RANGE(1.25),
71-
BIP_RANGE(0.625),
72-
BIP_RANGE(10),
73-
UNI_RANGE(10),
74-
UNI_RANGE(5),
75-
UNI_RANGE(2.5),
76-
UNI_RANGE(1.25)
71+
BIP_RANGE(5), /* gain 1 (0x00) */
72+
BIP_RANGE(2.5), /* gain 2 (0x01) */
73+
BIP_RANGE(1.25), /* gain 4 (0x02) */
74+
BIP_RANGE(0.625), /* gain 8 (0x03) */
75+
BIP_RANGE(10), /* gain 0.5 (0x04) */
76+
UNI_RANGE(10), /* gain 1 (0x00 | UNI) */
77+
UNI_RANGE(5), /* gain 2 (0x01 | UNI) */
78+
UNI_RANGE(2.5), /* gain 4 (0x02 | UNI) */
79+
UNI_RANGE(1.25) /* gain 8 (0x03 | UNI) */
7780
}
7881
};
7982

80-
static const char range_codes_pci1710_3[] = { 0x00, 0x01, 0x02, 0x03, 0x04,
81-
0x10, 0x11, 0x12, 0x13 };
82-
83-
static const struct comedi_lrange range_pci1710hg = {
83+
static const struct comedi_lrange pci1710hg_ai_range = {
8484
12, {
85-
BIP_RANGE(5),
86-
BIP_RANGE(0.5),
87-
BIP_RANGE(0.05),
88-
BIP_RANGE(0.005),
89-
BIP_RANGE(10),
90-
BIP_RANGE(1),
91-
BIP_RANGE(0.1),
92-
BIP_RANGE(0.01),
93-
UNI_RANGE(10),
94-
UNI_RANGE(1),
95-
UNI_RANGE(0.1),
96-
UNI_RANGE(0.01)
85+
BIP_RANGE(5), /* gain 1 (0x00) */
86+
BIP_RANGE(0.5), /* gain 10 (0x01) */
87+
BIP_RANGE(0.05), /* gain 100 (0x02) */
88+
BIP_RANGE(0.005), /* gain 1000 (0x03) */
89+
BIP_RANGE(10), /* gain 0.5 (0x04) */
90+
BIP_RANGE(1), /* gain 5 (0x05) */
91+
BIP_RANGE(0.1), /* gain 50 (0x06) */
92+
BIP_RANGE(0.01), /* gain 500 (0x07) */
93+
UNI_RANGE(10), /* gain 1 (0x00 | UNI) */
94+
UNI_RANGE(1), /* gain 10 (0x01 | UNI) */
95+
UNI_RANGE(0.1), /* gain 100 (0x02 | UNI) */
96+
UNI_RANGE(0.01) /* gain 1000 (0x03 | UNI) */
9797
}
9898
};
9999

100-
static const char range_codes_pci1710hg[] = { 0x00, 0x01, 0x02, 0x03, 0x04,
101-
0x05, 0x06, 0x07, 0x10, 0x11,
102-
0x12, 0x13 };
103-
104-
static const struct comedi_lrange range_pci17x1 = {
100+
static const struct comedi_lrange pci1711_ai_range = {
105101
5, {
106-
BIP_RANGE(10),
107-
BIP_RANGE(5),
108-
BIP_RANGE(2.5),
109-
BIP_RANGE(1.25),
110-
BIP_RANGE(0.625)
102+
BIP_RANGE(10), /* gain 1 (0x00) */
103+
BIP_RANGE(5), /* gain 2 (0x01) */
104+
BIP_RANGE(2.5), /* gain 4 (0x02) */
105+
BIP_RANGE(1.25), /* gain 8 (0x03) */
106+
BIP_RANGE(0.625) /* gain 16 (0x04) */
111107
}
112108
};
113109

114-
static const char range_codes_pci17x1[] = { 0x00, 0x01, 0x02, 0x03, 0x04 };
115-
116110
static const struct comedi_lrange pci171x_ao_range = {
117111
2, {
118112
UNI_RANGE(5),
@@ -132,7 +126,6 @@ struct boardtype {
132126
const char *name; /* board name */
133127
int n_aichan; /* num of A/D chans */
134128
const struct comedi_lrange *rangelist_ai; /* rangelist for A/D */
135-
const char *rangecode_ai; /* range codes for programming */
136129
unsigned int is_pci1713:1;
137130
unsigned int has_large_fifo:1; /* 4K or 1K FIFO */
138131
unsigned int has_diff_ai:1;
@@ -144,8 +137,7 @@ static const struct boardtype boardtypes[] = {
144137
[BOARD_PCI1710] = {
145138
.name = "pci1710",
146139
.n_aichan = 16,
147-
.rangelist_ai = &range_pci1710_3,
148-
.rangecode_ai = range_codes_pci1710_3,
140+
.rangelist_ai = &pci1710_ai_range,
149141
.has_large_fifo = 1,
150142
.has_diff_ai = 1,
151143
.has_ao = 1,
@@ -154,8 +146,7 @@ static const struct boardtype boardtypes[] = {
154146
[BOARD_PCI1710HG] = {
155147
.name = "pci1710hg",
156148
.n_aichan = 16,
157-
.rangelist_ai = &range_pci1710hg,
158-
.rangecode_ai = range_codes_pci1710hg,
149+
.rangelist_ai = &pci1710hg_ai_range,
159150
.has_large_fifo = 1,
160151
.has_diff_ai = 1,
161152
.has_ao = 1,
@@ -164,25 +155,22 @@ static const struct boardtype boardtypes[] = {
164155
[BOARD_PCI1711] = {
165156
.name = "pci1711",
166157
.n_aichan = 16,
167-
.rangelist_ai = &range_pci17x1,
168-
.rangecode_ai = range_codes_pci17x1,
158+
.rangelist_ai = &pci1711_ai_range,
169159
.has_ao = 1,
170160
.has_di_do = 1,
171161
},
172162
[BOARD_PCI1713] = {
173163
.name = "pci1713",
174164
.n_aichan = 32,
175-
.rangelist_ai = &range_pci1710_3,
176-
.rangecode_ai = range_codes_pci1710_3,
165+
.rangelist_ai = &pci1710_ai_range,
177166
.is_pci1713 = 1,
178167
.has_large_fifo = 1,
179168
.has_diff_ai = 1,
180169
},
181170
[BOARD_PCI1731] = {
182171
.name = "pci1731",
183172
.n_aichan = 16,
184-
.rangelist_ai = &range_pci17x1,
185-
.rangecode_ai = range_codes_pci17x1,
173+
.rangelist_ai = &pci1711_ai_range,
186174
.has_di_do = 1,
187175
},
188176
};
@@ -196,6 +184,7 @@ struct pci1710_private {
196184
unsigned int act_chanlist[32]; /* list of scanned channel */
197185
unsigned char saved_seglen; /* len of the non-repeating chanlist */
198186
unsigned char da_ranges; /* copy of D/A outpit range register */
187+
unsigned char unipolar_gain; /* adjust for unipolar gain codes */
199188
};
200189

201190
static int pci171x_ai_check_chanlist(struct comedi_device *dev,
@@ -270,7 +259,6 @@ static void pci171x_ai_setup_chanlist(struct comedi_device *dev,
270259
unsigned int n_chan,
271260
unsigned int seglen)
272261
{
273-
const struct boardtype *board = dev->board_ptr;
274262
struct pci1710_private *devpriv = dev->private;
275263
unsigned int first_chan = CR_CHAN(chanlist[0]);
276264
unsigned int last_chan = CR_CHAN(chanlist[seglen - 1]);
@@ -280,11 +268,15 @@ static void pci171x_ai_setup_chanlist(struct comedi_device *dev,
280268
unsigned int chan = CR_CHAN(chanlist[i]);
281269
unsigned int range = CR_RANGE(chanlist[i]);
282270
unsigned int aref = CR_AREF(chanlist[i]);
283-
unsigned int rangeval;
271+
unsigned int rangeval = 0;
284272

285-
rangeval = board->rangecode_ai[range];
286273
if (aref == AREF_DIFF)
287-
rangeval |= 0x0020;
274+
rangeval |= PCI171X_RANGE_DIFF;
275+
if (comedi_range_is_unipolar(s, range)) {
276+
rangeval |= PCI171X_RANGE_UNI;
277+
range -= devpriv->unipolar_gain;
278+
}
279+
rangeval |= PCI171X_RANGE_GAIN(range);
288280

289281
/* select channel and set range */
290282
outw(chan | (chan << 8), dev->iobase + PCI171X_MUX_REG);
@@ -758,6 +750,7 @@ static int pci1710_auto_attach(struct comedi_device *dev,
758750
struct pci1710_private *devpriv;
759751
struct comedi_subdevice *s;
760752
int ret, subdev, n_subdevices;
753+
int i;
761754

762755
if (context < ARRAY_SIZE(boardtypes))
763756
board = &boardtypes[context];
@@ -823,6 +816,15 @@ static int pci1710_auto_attach(struct comedi_device *dev,
823816
s->do_cmd = pci171x_ai_cmd;
824817
s->cancel = pci171x_ai_cancel;
825818
}
819+
820+
/* find the value needed to adjust for unipolar gain codes */
821+
for (i = 0; i < s->range_table->length; i++) {
822+
if (comedi_range_is_unipolar(s, i)) {
823+
devpriv->unipolar_gain = i;
824+
break;
825+
}
826+
}
827+
826828
subdev++;
827829
}
828830

0 commit comments

Comments
 (0)