2 * IIO driver for the ACCES 104-QUAD-8
3 * Copyright (C) 2016 William Breathitt Gray
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * This driver supports the ACCES 104-QUAD-8 and ACCES 104-QUAD-4.
16 #include <linux/bitops.h>
17 #include <linux/device.h>
18 #include <linux/errno.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/types.h>
22 #include <linux/ioport.h>
23 #include <linux/isa.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/types.h>
29 #define QUAD8_EXTENT 32
31 static unsigned int base
[max_num_isa_dev(QUAD8_EXTENT
)];
32 static unsigned int num_quad8
;
33 module_param_array(base
, uint
, &num_quad8
, 0);
34 MODULE_PARM_DESC(base
, "ACCES 104-QUAD-8 base addresses");
36 #define QUAD8_NUM_COUNTERS 8
39 * struct quad8_iio - IIO device private data structure
40 * @preset: array of preset values
41 * @count_mode: array of count mode configurations
42 * @quadrature_mode: array of quadrature mode configurations
43 * @quadrature_scale: array of quadrature mode scale configurations
44 * @ab_enable: array of A and B inputs enable configurations
45 * @preset_enable: array of set_to_preset_on_index attribute configurations
46 * @synchronous_mode: array of index function synchronous mode configurations
47 * @index_polarity: array of index function polarity configurations
48 * @base: base port address of the IIO device
51 unsigned int preset
[QUAD8_NUM_COUNTERS
];
52 unsigned int count_mode
[QUAD8_NUM_COUNTERS
];
53 unsigned int quadrature_mode
[QUAD8_NUM_COUNTERS
];
54 unsigned int quadrature_scale
[QUAD8_NUM_COUNTERS
];
55 unsigned int ab_enable
[QUAD8_NUM_COUNTERS
];
56 unsigned int preset_enable
[QUAD8_NUM_COUNTERS
];
57 unsigned int synchronous_mode
[QUAD8_NUM_COUNTERS
];
58 unsigned int index_polarity
[QUAD8_NUM_COUNTERS
];
62 #define QUAD8_REG_CHAN_OP 0x11
63 #define QUAD8_REG_INDEX_INPUT_LEVELS 0x16
64 /* Borrow Toggle flip-flop */
65 #define QUAD8_FLAG_BT BIT(0)
66 /* Carry Toggle flip-flop */
67 #define QUAD8_FLAG_CT BIT(1)
69 #define QUAD8_FLAG_E BIT(4)
71 #define QUAD8_FLAG_UD BIT(5)
72 /* Reset and Load Signal Decoders */
73 #define QUAD8_CTR_RLD 0x00
74 /* Counter Mode Register */
75 #define QUAD8_CTR_CMR 0x20
76 /* Input / Output Control Register */
77 #define QUAD8_CTR_IOR 0x40
78 /* Index Control Register */
79 #define QUAD8_CTR_IDR 0x60
80 /* Reset Byte Pointer (three byte data pointer) */
81 #define QUAD8_RLD_RESET_BP 0x01
83 #define QUAD8_RLD_RESET_CNTR 0x02
84 /* Reset Borrow Toggle, Carry Toggle, Compare Toggle, and Sign flags */
85 #define QUAD8_RLD_RESET_FLAGS 0x04
86 /* Reset Error flag */
87 #define QUAD8_RLD_RESET_E 0x06
88 /* Preset Register to Counter */
89 #define QUAD8_RLD_PRESET_CNTR 0x08
90 /* Transfer Counter to Output Latch */
91 #define QUAD8_RLD_CNTR_OUT 0x10
92 #define QUAD8_CHAN_OP_ENABLE_COUNTERS 0x00
93 #define QUAD8_CHAN_OP_RESET_COUNTERS 0x01
95 static int quad8_read_raw(struct iio_dev
*indio_dev
,
96 struct iio_chan_spec
const *chan
, int *val
, int *val2
, long mask
)
98 struct quad8_iio
*const priv
= iio_priv(indio_dev
);
99 const int base_offset
= priv
->base
+ 2 * chan
->channel
;
106 case IIO_CHAN_INFO_RAW
:
107 if (chan
->type
== IIO_INDEX
) {
108 *val
= !!(inb(priv
->base
+ QUAD8_REG_INDEX_INPUT_LEVELS
)
109 & BIT(chan
->channel
));
113 flags
= inb(base_offset
+ 1);
114 borrow
= flags
& QUAD8_FLAG_BT
;
115 carry
= !!(flags
& QUAD8_FLAG_CT
);
117 /* Borrow XOR Carry effectively doubles count range */
118 *val
= (borrow
^ carry
) << 24;
120 /* Reset Byte Pointer; transfer Counter to Output Latch */
121 outb(QUAD8_CTR_RLD
| QUAD8_RLD_RESET_BP
| QUAD8_RLD_CNTR_OUT
,
124 for (i
= 0; i
< 3; i
++)
125 *val
|= (unsigned int)inb(base_offset
) << (8 * i
);
128 case IIO_CHAN_INFO_ENABLE
:
129 *val
= priv
->ab_enable
[chan
->channel
];
131 case IIO_CHAN_INFO_SCALE
:
133 *val2
= priv
->quadrature_scale
[chan
->channel
];
134 return IIO_VAL_FRACTIONAL_LOG2
;
140 static int quad8_write_raw(struct iio_dev
*indio_dev
,
141 struct iio_chan_spec
const *chan
, int val
, int val2
, long mask
)
143 struct quad8_iio
*const priv
= iio_priv(indio_dev
);
144 const int base_offset
= priv
->base
+ 2 * chan
->channel
;
146 unsigned int ior_cfg
;
149 case IIO_CHAN_INFO_RAW
:
150 if (chan
->type
== IIO_INDEX
)
153 /* Only 24-bit values are supported */
154 if ((unsigned int)val
> 0xFFFFFF)
157 /* Reset Byte Pointer */
158 outb(QUAD8_CTR_RLD
| QUAD8_RLD_RESET_BP
, base_offset
+ 1);
160 /* Counter can only be set via Preset Register */
161 for (i
= 0; i
< 3; i
++)
162 outb(val
>> (8 * i
), base_offset
);
164 /* Transfer Preset Register to Counter */
165 outb(QUAD8_CTR_RLD
| QUAD8_RLD_PRESET_CNTR
, base_offset
+ 1);
167 /* Reset Byte Pointer */
168 outb(QUAD8_CTR_RLD
| QUAD8_RLD_RESET_BP
, base_offset
+ 1);
170 /* Set Preset Register back to original value */
171 val
= priv
->preset
[chan
->channel
];
172 for (i
= 0; i
< 3; i
++)
173 outb(val
>> (8 * i
), base_offset
);
175 /* Reset Borrow, Carry, Compare, and Sign flags */
176 outb(QUAD8_CTR_RLD
| QUAD8_RLD_RESET_FLAGS
, base_offset
+ 1);
177 /* Reset Error flag */
178 outb(QUAD8_CTR_RLD
| QUAD8_RLD_RESET_E
, base_offset
+ 1);
181 case IIO_CHAN_INFO_ENABLE
:
182 /* only boolean values accepted */
183 if (val
< 0 || val
> 1)
186 priv
->ab_enable
[chan
->channel
] = val
;
188 ior_cfg
= val
| priv
->preset_enable
[chan
->channel
] << 1;
190 /* Load I/O control configuration */
191 outb(QUAD8_CTR_IOR
| ior_cfg
, base_offset
+ 1);
194 case IIO_CHAN_INFO_SCALE
:
195 /* Quadrature scaling only available in quadrature mode */
196 if (!priv
->quadrature_mode
[chan
->channel
] && (val2
|| val
!= 1))
199 /* Only three gain states (1, 0.5, 0.25) */
200 if (val
== 1 && !val2
)
201 priv
->quadrature_scale
[chan
->channel
] = 0;
205 priv
->quadrature_scale
[chan
->channel
] = 1;
208 priv
->quadrature_scale
[chan
->channel
] = 2;
222 static const struct iio_info quad8_info
= {
223 .read_raw
= quad8_read_raw
,
224 .write_raw
= quad8_write_raw
227 static ssize_t
quad8_read_preset(struct iio_dev
*indio_dev
, uintptr_t private,
228 const struct iio_chan_spec
*chan
, char *buf
)
230 const struct quad8_iio
*const priv
= iio_priv(indio_dev
);
232 return snprintf(buf
, PAGE_SIZE
, "%u\n", priv
->preset
[chan
->channel
]);
235 static ssize_t
quad8_write_preset(struct iio_dev
*indio_dev
, uintptr_t private,
236 const struct iio_chan_spec
*chan
, const char *buf
, size_t len
)
238 struct quad8_iio
*const priv
= iio_priv(indio_dev
);
239 const int base_offset
= priv
->base
+ 2 * chan
->channel
;
244 ret
= kstrtouint(buf
, 0, &preset
);
248 /* Only 24-bit values are supported */
249 if (preset
> 0xFFFFFF)
252 priv
->preset
[chan
->channel
] = preset
;
254 /* Reset Byte Pointer */
255 outb(QUAD8_CTR_RLD
| QUAD8_RLD_RESET_BP
, base_offset
+ 1);
257 /* Set Preset Register */
258 for (i
= 0; i
< 3; i
++)
259 outb(preset
>> (8 * i
), base_offset
);
264 static ssize_t
quad8_read_set_to_preset_on_index(struct iio_dev
*indio_dev
,
265 uintptr_t private, const struct iio_chan_spec
*chan
, char *buf
)
267 const struct quad8_iio
*const priv
= iio_priv(indio_dev
);
269 return snprintf(buf
, PAGE_SIZE
, "%u\n",
270 !priv
->preset_enable
[chan
->channel
]);
273 static ssize_t
quad8_write_set_to_preset_on_index(struct iio_dev
*indio_dev
,
274 uintptr_t private, const struct iio_chan_spec
*chan
, const char *buf
,
277 struct quad8_iio
*const priv
= iio_priv(indio_dev
);
278 const int base_offset
= priv
->base
+ 2 * chan
->channel
+ 1;
281 unsigned int ior_cfg
;
283 ret
= kstrtobool(buf
, &preset_enable
);
287 /* Preset enable is active low in Input/Output Control register */
288 preset_enable
= !preset_enable
;
290 priv
->preset_enable
[chan
->channel
] = preset_enable
;
292 ior_cfg
= priv
->ab_enable
[chan
->channel
] |
293 (unsigned int)preset_enable
<< 1;
295 /* Load I/O control configuration to Input / Output Control Register */
296 outb(QUAD8_CTR_IOR
| ior_cfg
, base_offset
);
301 static const char *const quad8_noise_error_states
[] = {
302 "No excessive noise is present at the count inputs",
303 "Excessive noise is present at the count inputs"
306 static int quad8_get_noise_error(struct iio_dev
*indio_dev
,
307 const struct iio_chan_spec
*chan
)
309 struct quad8_iio
*const priv
= iio_priv(indio_dev
);
310 const int base_offset
= priv
->base
+ 2 * chan
->channel
+ 1;
312 return !!(inb(base_offset
) & QUAD8_FLAG_E
);
315 static const struct iio_enum quad8_noise_error_enum
= {
316 .items
= quad8_noise_error_states
,
317 .num_items
= ARRAY_SIZE(quad8_noise_error_states
),
318 .get
= quad8_get_noise_error
321 static const char *const quad8_count_direction_states
[] = {
326 static int quad8_get_count_direction(struct iio_dev
*indio_dev
,
327 const struct iio_chan_spec
*chan
)
329 struct quad8_iio
*const priv
= iio_priv(indio_dev
);
330 const int base_offset
= priv
->base
+ 2 * chan
->channel
+ 1;
332 return !!(inb(base_offset
) & QUAD8_FLAG_UD
);
335 static const struct iio_enum quad8_count_direction_enum
= {
336 .items
= quad8_count_direction_states
,
337 .num_items
= ARRAY_SIZE(quad8_count_direction_states
),
338 .get
= quad8_get_count_direction
341 static const char *const quad8_count_modes
[] = {
348 static int quad8_set_count_mode(struct iio_dev
*indio_dev
,
349 const struct iio_chan_spec
*chan
, unsigned int count_mode
)
351 struct quad8_iio
*const priv
= iio_priv(indio_dev
);
352 unsigned int mode_cfg
= count_mode
<< 1;
353 const int base_offset
= priv
->base
+ 2 * chan
->channel
+ 1;
355 priv
->count_mode
[chan
->channel
] = count_mode
;
357 /* Add quadrature mode configuration */
358 if (priv
->quadrature_mode
[chan
->channel
])
359 mode_cfg
|= (priv
->quadrature_scale
[chan
->channel
] + 1) << 3;
361 /* Load mode configuration to Counter Mode Register */
362 outb(QUAD8_CTR_CMR
| mode_cfg
, base_offset
);
367 static int quad8_get_count_mode(struct iio_dev
*indio_dev
,
368 const struct iio_chan_spec
*chan
)
370 const struct quad8_iio
*const priv
= iio_priv(indio_dev
);
372 return priv
->count_mode
[chan
->channel
];
375 static const struct iio_enum quad8_count_mode_enum
= {
376 .items
= quad8_count_modes
,
377 .num_items
= ARRAY_SIZE(quad8_count_modes
),
378 .set
= quad8_set_count_mode
,
379 .get
= quad8_get_count_mode
382 static const char *const quad8_synchronous_modes
[] = {
387 static int quad8_set_synchronous_mode(struct iio_dev
*indio_dev
,
388 const struct iio_chan_spec
*chan
, unsigned int synchronous_mode
)
390 struct quad8_iio
*const priv
= iio_priv(indio_dev
);
391 const unsigned int idr_cfg
= synchronous_mode
|
392 priv
->index_polarity
[chan
->channel
] << 1;
393 const int base_offset
= priv
->base
+ 2 * chan
->channel
+ 1;
395 /* Index function must be non-synchronous in non-quadrature mode */
396 if (synchronous_mode
&& !priv
->quadrature_mode
[chan
->channel
])
399 priv
->synchronous_mode
[chan
->channel
] = synchronous_mode
;
401 /* Load Index Control configuration to Index Control Register */
402 outb(QUAD8_CTR_IDR
| idr_cfg
, base_offset
);
407 static int quad8_get_synchronous_mode(struct iio_dev
*indio_dev
,
408 const struct iio_chan_spec
*chan
)
410 const struct quad8_iio
*const priv
= iio_priv(indio_dev
);
412 return priv
->synchronous_mode
[chan
->channel
];
415 static const struct iio_enum quad8_synchronous_mode_enum
= {
416 .items
= quad8_synchronous_modes
,
417 .num_items
= ARRAY_SIZE(quad8_synchronous_modes
),
418 .set
= quad8_set_synchronous_mode
,
419 .get
= quad8_get_synchronous_mode
422 static const char *const quad8_quadrature_modes
[] = {
427 static int quad8_set_quadrature_mode(struct iio_dev
*indio_dev
,
428 const struct iio_chan_spec
*chan
, unsigned int quadrature_mode
)
430 struct quad8_iio
*const priv
= iio_priv(indio_dev
);
431 unsigned int mode_cfg
= priv
->count_mode
[chan
->channel
] << 1;
432 const int base_offset
= priv
->base
+ 2 * chan
->channel
+ 1;
435 mode_cfg
|= (priv
->quadrature_scale
[chan
->channel
] + 1) << 3;
437 /* Quadrature scaling only available in quadrature mode */
438 priv
->quadrature_scale
[chan
->channel
] = 0;
440 /* Synchronous function not supported in non-quadrature mode */
441 if (priv
->synchronous_mode
[chan
->channel
])
442 quad8_set_synchronous_mode(indio_dev
, chan
, 0);
445 priv
->quadrature_mode
[chan
->channel
] = quadrature_mode
;
447 /* Load mode configuration to Counter Mode Register */
448 outb(QUAD8_CTR_CMR
| mode_cfg
, base_offset
);
453 static int quad8_get_quadrature_mode(struct iio_dev
*indio_dev
,
454 const struct iio_chan_spec
*chan
)
456 const struct quad8_iio
*const priv
= iio_priv(indio_dev
);
458 return priv
->quadrature_mode
[chan
->channel
];
461 static const struct iio_enum quad8_quadrature_mode_enum
= {
462 .items
= quad8_quadrature_modes
,
463 .num_items
= ARRAY_SIZE(quad8_quadrature_modes
),
464 .set
= quad8_set_quadrature_mode
,
465 .get
= quad8_get_quadrature_mode
468 static const char *const quad8_index_polarity_modes
[] = {
473 static int quad8_set_index_polarity(struct iio_dev
*indio_dev
,
474 const struct iio_chan_spec
*chan
, unsigned int index_polarity
)
476 struct quad8_iio
*const priv
= iio_priv(indio_dev
);
477 const unsigned int idr_cfg
= priv
->synchronous_mode
[chan
->channel
] |
479 const int base_offset
= priv
->base
+ 2 * chan
->channel
+ 1;
481 priv
->index_polarity
[chan
->channel
] = index_polarity
;
483 /* Load Index Control configuration to Index Control Register */
484 outb(QUAD8_CTR_IDR
| idr_cfg
, base_offset
);
489 static int quad8_get_index_polarity(struct iio_dev
*indio_dev
,
490 const struct iio_chan_spec
*chan
)
492 const struct quad8_iio
*const priv
= iio_priv(indio_dev
);
494 return priv
->index_polarity
[chan
->channel
];
497 static const struct iio_enum quad8_index_polarity_enum
= {
498 .items
= quad8_index_polarity_modes
,
499 .num_items
= ARRAY_SIZE(quad8_index_polarity_modes
),
500 .set
= quad8_set_index_polarity
,
501 .get
= quad8_get_index_polarity
504 static const struct iio_chan_spec_ext_info quad8_count_ext_info
[] = {
507 .shared
= IIO_SEPARATE
,
508 .read
= quad8_read_preset
,
509 .write
= quad8_write_preset
512 .name
= "set_to_preset_on_index",
513 .shared
= IIO_SEPARATE
,
514 .read
= quad8_read_set_to_preset_on_index
,
515 .write
= quad8_write_set_to_preset_on_index
517 IIO_ENUM("noise_error", IIO_SEPARATE
, &quad8_noise_error_enum
),
518 IIO_ENUM_AVAILABLE("noise_error", &quad8_noise_error_enum
),
519 IIO_ENUM("count_direction", IIO_SEPARATE
, &quad8_count_direction_enum
),
520 IIO_ENUM_AVAILABLE("count_direction", &quad8_count_direction_enum
),
521 IIO_ENUM("count_mode", IIO_SEPARATE
, &quad8_count_mode_enum
),
522 IIO_ENUM_AVAILABLE("count_mode", &quad8_count_mode_enum
),
523 IIO_ENUM("quadrature_mode", IIO_SEPARATE
, &quad8_quadrature_mode_enum
),
524 IIO_ENUM_AVAILABLE("quadrature_mode", &quad8_quadrature_mode_enum
),
528 static const struct iio_chan_spec_ext_info quad8_index_ext_info
[] = {
529 IIO_ENUM("synchronous_mode", IIO_SEPARATE
,
530 &quad8_synchronous_mode_enum
),
531 IIO_ENUM_AVAILABLE("synchronous_mode", &quad8_synchronous_mode_enum
),
532 IIO_ENUM("index_polarity", IIO_SEPARATE
, &quad8_index_polarity_enum
),
533 IIO_ENUM_AVAILABLE("index_polarity", &quad8_index_polarity_enum
),
537 #define QUAD8_COUNT_CHAN(_chan) { \
539 .channel = (_chan), \
540 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
541 BIT(IIO_CHAN_INFO_ENABLE) | BIT(IIO_CHAN_INFO_SCALE), \
542 .ext_info = quad8_count_ext_info, \
546 #define QUAD8_INDEX_CHAN(_chan) { \
548 .channel = (_chan), \
549 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
550 .ext_info = quad8_index_ext_info, \
554 static const struct iio_chan_spec quad8_channels
[] = {
555 QUAD8_COUNT_CHAN(0), QUAD8_INDEX_CHAN(0),
556 QUAD8_COUNT_CHAN(1), QUAD8_INDEX_CHAN(1),
557 QUAD8_COUNT_CHAN(2), QUAD8_INDEX_CHAN(2),
558 QUAD8_COUNT_CHAN(3), QUAD8_INDEX_CHAN(3),
559 QUAD8_COUNT_CHAN(4), QUAD8_INDEX_CHAN(4),
560 QUAD8_COUNT_CHAN(5), QUAD8_INDEX_CHAN(5),
561 QUAD8_COUNT_CHAN(6), QUAD8_INDEX_CHAN(6),
562 QUAD8_COUNT_CHAN(7), QUAD8_INDEX_CHAN(7)
565 static int quad8_probe(struct device
*dev
, unsigned int id
)
567 struct iio_dev
*indio_dev
;
568 struct quad8_iio
*priv
;
570 unsigned int base_offset
;
572 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
576 if (!devm_request_region(dev
, base
[id
], QUAD8_EXTENT
,
578 dev_err(dev
, "Unable to lock port addresses (0x%X-0x%X)\n",
579 base
[id
], base
[id
] + QUAD8_EXTENT
);
583 indio_dev
->info
= &quad8_info
;
584 indio_dev
->modes
= INDIO_DIRECT_MODE
;
585 indio_dev
->num_channels
= ARRAY_SIZE(quad8_channels
);
586 indio_dev
->channels
= quad8_channels
;
587 indio_dev
->name
= dev_name(dev
);
588 indio_dev
->dev
.parent
= dev
;
590 priv
= iio_priv(indio_dev
);
591 priv
->base
= base
[id
];
593 /* Reset all counters and disable interrupt function */
594 outb(QUAD8_CHAN_OP_RESET_COUNTERS
, base
[id
] + QUAD8_REG_CHAN_OP
);
595 /* Set initial configuration for all counters */
596 for (i
= 0; i
< QUAD8_NUM_COUNTERS
; i
++) {
597 base_offset
= base
[id
] + 2 * i
;
598 /* Reset Byte Pointer */
599 outb(QUAD8_CTR_RLD
| QUAD8_RLD_RESET_BP
, base_offset
+ 1);
600 /* Reset Preset Register */
601 for (j
= 0; j
< 3; j
++)
602 outb(0x00, base_offset
);
603 /* Reset Borrow, Carry, Compare, and Sign flags */
604 outb(QUAD8_CTR_RLD
| QUAD8_RLD_RESET_FLAGS
, base_offset
+ 1);
605 /* Reset Error flag */
606 outb(QUAD8_CTR_RLD
| QUAD8_RLD_RESET_E
, base_offset
+ 1);
607 /* Binary encoding; Normal count; non-quadrature mode */
608 outb(QUAD8_CTR_CMR
, base_offset
+ 1);
609 /* Disable A and B inputs; preset on index; FLG1 as Carry */
610 outb(QUAD8_CTR_IOR
, base_offset
+ 1);
611 /* Disable index function; negative index polarity */
612 outb(QUAD8_CTR_IDR
, base_offset
+ 1);
614 /* Enable all counters */
615 outb(QUAD8_CHAN_OP_ENABLE_COUNTERS
, base
[id
] + QUAD8_REG_CHAN_OP
);
617 return devm_iio_device_register(dev
, indio_dev
);
620 static struct isa_driver quad8_driver
= {
621 .probe
= quad8_probe
,
627 module_isa_driver(quad8_driver
, num_quad8
);
629 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
630 MODULE_DESCRIPTION("ACCES 104-QUAD-8 IIO driver");
631 MODULE_LICENSE("GPL v2");