1 // SPDX-License-Identifier: GPL-2.0
3 * Counter driver for the ACCES 104-QUAD-8
4 * Copyright (C) 2016 William Breathitt Gray
6 * This driver supports the ACCES 104-QUAD-8 and ACCES 104-QUAD-4.
8 #include <linux/bitops.h>
9 #include <linux/counter.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/types.h>
15 #include <linux/ioport.h>
16 #include <linux/isa.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/types.h>
22 #define QUAD8_EXTENT 32
24 static unsigned int base[max_num_isa_dev(QUAD8_EXTENT)];
25 static unsigned int num_quad8;
26 module_param_array(base, uint, &num_quad8, 0);
27 MODULE_PARM_DESC(base, "ACCES 104-QUAD-8 base addresses");
29 #define QUAD8_NUM_COUNTERS 8
32 * struct quad8_iio - IIO device private data structure
33 * @counter: instance of the counter_device
34 * @preset: array of preset values
35 * @count_mode: array of count mode configurations
36 * @quadrature_mode: array of quadrature mode configurations
37 * @quadrature_scale: array of quadrature mode scale configurations
38 * @ab_enable: array of A and B inputs enable configurations
39 * @preset_enable: array of set_to_preset_on_index attribute configurations
40 * @synchronous_mode: array of index function synchronous mode configurations
41 * @index_polarity: array of index function polarity configurations
42 * @base: base port address of the IIO device
46 struct counter_device counter;
47 unsigned int preset[QUAD8_NUM_COUNTERS];
48 unsigned int count_mode[QUAD8_NUM_COUNTERS];
49 unsigned int quadrature_mode[QUAD8_NUM_COUNTERS];
50 unsigned int quadrature_scale[QUAD8_NUM_COUNTERS];
51 unsigned int ab_enable[QUAD8_NUM_COUNTERS];
52 unsigned int preset_enable[QUAD8_NUM_COUNTERS];
53 unsigned int synchronous_mode[QUAD8_NUM_COUNTERS];
54 unsigned int index_polarity[QUAD8_NUM_COUNTERS];
58 #define QUAD8_REG_CHAN_OP 0x11
59 #define QUAD8_REG_INDEX_INPUT_LEVELS 0x16
60 /* Borrow Toggle flip-flop */
61 #define QUAD8_FLAG_BT BIT(0)
62 /* Carry Toggle flip-flop */
63 #define QUAD8_FLAG_CT BIT(1)
65 #define QUAD8_FLAG_E BIT(4)
67 #define QUAD8_FLAG_UD BIT(5)
68 /* Reset and Load Signal Decoders */
69 #define QUAD8_CTR_RLD 0x00
70 /* Counter Mode Register */
71 #define QUAD8_CTR_CMR 0x20
72 /* Input / Output Control Register */
73 #define QUAD8_CTR_IOR 0x40
74 /* Index Control Register */
75 #define QUAD8_CTR_IDR 0x60
76 /* Reset Byte Pointer (three byte data pointer) */
77 #define QUAD8_RLD_RESET_BP 0x01
79 #define QUAD8_RLD_RESET_CNTR 0x02
80 /* Reset Borrow Toggle, Carry Toggle, Compare Toggle, and Sign flags */
81 #define QUAD8_RLD_RESET_FLAGS 0x04
82 /* Reset Error flag */
83 #define QUAD8_RLD_RESET_E 0x06
84 /* Preset Register to Counter */
85 #define QUAD8_RLD_PRESET_CNTR 0x08
86 /* Transfer Counter to Output Latch */
87 #define QUAD8_RLD_CNTR_OUT 0x10
88 #define QUAD8_CHAN_OP_ENABLE_COUNTERS 0x00
89 #define QUAD8_CHAN_OP_RESET_COUNTERS 0x01
90 #define QUAD8_CMR_QUADRATURE_X1 0x08
91 #define QUAD8_CMR_QUADRATURE_X2 0x10
92 #define QUAD8_CMR_QUADRATURE_X4 0x18
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 mutex_lock(&priv->lock);
122 /* Reset Byte Pointer; transfer Counter to Output Latch */
123 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_CNTR_OUT,
126 for (i = 0; i < 3; i++)
127 *val |= (unsigned int)inb(base_offset) << (8 * i);
129 mutex_unlock(&priv->lock);
132 case IIO_CHAN_INFO_ENABLE:
133 *val = priv->ab_enable[chan->channel];
135 case IIO_CHAN_INFO_SCALE:
137 *val2 = priv->quadrature_scale[chan->channel];
138 return IIO_VAL_FRACTIONAL_LOG2;
144 static int quad8_write_raw(struct iio_dev *indio_dev,
145 struct iio_chan_spec const *chan, int val, int val2, long mask)
147 struct quad8_iio *const priv = iio_priv(indio_dev);
148 const int base_offset = priv->base + 2 * chan->channel;
150 unsigned int ior_cfg;
153 case IIO_CHAN_INFO_RAW:
154 if (chan->type == IIO_INDEX)
157 /* Only 24-bit values are supported */
158 if ((unsigned int)val > 0xFFFFFF)
161 mutex_lock(&priv->lock);
163 /* Reset Byte Pointer */
164 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
166 /* Counter can only be set via Preset Register */
167 for (i = 0; i < 3; i++)
168 outb(val >> (8 * i), base_offset);
170 /* Transfer Preset Register to Counter */
171 outb(QUAD8_CTR_RLD | QUAD8_RLD_PRESET_CNTR, base_offset + 1);
173 /* Reset Byte Pointer */
174 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
176 /* Set Preset Register back to original value */
177 val = priv->preset[chan->channel];
178 for (i = 0; i < 3; i++)
179 outb(val >> (8 * i), base_offset);
181 /* Reset Borrow, Carry, Compare, and Sign flags */
182 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_FLAGS, base_offset + 1);
183 /* Reset Error flag */
184 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_E, base_offset + 1);
186 mutex_unlock(&priv->lock);
189 case IIO_CHAN_INFO_ENABLE:
190 /* only boolean values accepted */
191 if (val < 0 || val > 1)
194 mutex_lock(&priv->lock);
196 priv->ab_enable[chan->channel] = val;
198 ior_cfg = val | priv->preset_enable[chan->channel] << 1;
200 /* Load I/O control configuration */
201 outb(QUAD8_CTR_IOR | ior_cfg, base_offset + 1);
203 mutex_unlock(&priv->lock);
206 case IIO_CHAN_INFO_SCALE:
207 mutex_lock(&priv->lock);
209 /* Quadrature scaling only available in quadrature mode */
210 if (!priv->quadrature_mode[chan->channel] &&
211 (val2 || val != 1)) {
212 mutex_unlock(&priv->lock);
216 /* Only three gain states (1, 0.5, 0.25) */
217 if (val == 1 && !val2)
218 priv->quadrature_scale[chan->channel] = 0;
222 priv->quadrature_scale[chan->channel] = 1;
225 priv->quadrature_scale[chan->channel] = 2;
228 mutex_unlock(&priv->lock);
232 mutex_unlock(&priv->lock);
236 mutex_unlock(&priv->lock);
243 static const struct iio_info quad8_info = {
244 .read_raw = quad8_read_raw,
245 .write_raw = quad8_write_raw
248 static ssize_t quad8_read_preset(struct iio_dev *indio_dev, uintptr_t private,
249 const struct iio_chan_spec *chan, char *buf)
251 const struct quad8_iio *const priv = iio_priv(indio_dev);
253 return snprintf(buf, PAGE_SIZE, "%u\n", priv->preset[chan->channel]);
256 static ssize_t quad8_write_preset(struct iio_dev *indio_dev, uintptr_t private,
257 const struct iio_chan_spec *chan, const char *buf, size_t len)
259 struct quad8_iio *const priv = iio_priv(indio_dev);
260 const int base_offset = priv->base + 2 * chan->channel;
265 ret = kstrtouint(buf, 0, &preset);
269 /* Only 24-bit values are supported */
270 if (preset > 0xFFFFFF)
273 mutex_lock(&priv->lock);
275 priv->preset[chan->channel] = preset;
277 /* Reset Byte Pointer */
278 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
280 /* Set Preset Register */
281 for (i = 0; i < 3; i++)
282 outb(preset >> (8 * i), base_offset);
284 mutex_unlock(&priv->lock);
289 static ssize_t quad8_read_set_to_preset_on_index(struct iio_dev *indio_dev,
290 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
292 const struct quad8_iio *const priv = iio_priv(indio_dev);
294 return snprintf(buf, PAGE_SIZE, "%u\n",
295 !priv->preset_enable[chan->channel]);
298 static ssize_t quad8_write_set_to_preset_on_index(struct iio_dev *indio_dev,
299 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
302 struct quad8_iio *const priv = iio_priv(indio_dev);
303 const int base_offset = priv->base + 2 * chan->channel + 1;
306 unsigned int ior_cfg;
308 ret = kstrtobool(buf, &preset_enable);
312 /* Preset enable is active low in Input/Output Control register */
313 preset_enable = !preset_enable;
315 mutex_lock(&priv->lock);
317 priv->preset_enable[chan->channel] = preset_enable;
319 ior_cfg = priv->ab_enable[chan->channel] |
320 (unsigned int)preset_enable << 1;
322 /* Load I/O control configuration to Input / Output Control Register */
323 outb(QUAD8_CTR_IOR | ior_cfg, base_offset);
325 mutex_unlock(&priv->lock);
330 static const char *const quad8_noise_error_states[] = {
331 "No excessive noise is present at the count inputs",
332 "Excessive noise is present at the count inputs"
335 static int quad8_get_noise_error(struct iio_dev *indio_dev,
336 const struct iio_chan_spec *chan)
338 struct quad8_iio *const priv = iio_priv(indio_dev);
339 const int base_offset = priv->base + 2 * chan->channel + 1;
341 return !!(inb(base_offset) & QUAD8_FLAG_E);
344 static const struct iio_enum quad8_noise_error_enum = {
345 .items = quad8_noise_error_states,
346 .num_items = ARRAY_SIZE(quad8_noise_error_states),
347 .get = quad8_get_noise_error
350 static const char *const quad8_count_direction_states[] = {
355 static int quad8_get_count_direction(struct iio_dev *indio_dev,
356 const struct iio_chan_spec *chan)
358 struct quad8_iio *const priv = iio_priv(indio_dev);
359 const int base_offset = priv->base + 2 * chan->channel + 1;
361 return !!(inb(base_offset) & QUAD8_FLAG_UD);
364 static const struct iio_enum quad8_count_direction_enum = {
365 .items = quad8_count_direction_states,
366 .num_items = ARRAY_SIZE(quad8_count_direction_states),
367 .get = quad8_get_count_direction
370 static const char *const quad8_count_modes[] = {
377 static int quad8_set_count_mode(struct iio_dev *indio_dev,
378 const struct iio_chan_spec *chan, unsigned int cnt_mode)
380 struct quad8_iio *const priv = iio_priv(indio_dev);
381 unsigned int mode_cfg = cnt_mode << 1;
382 const int base_offset = priv->base + 2 * chan->channel + 1;
384 mutex_lock(&priv->lock);
386 priv->count_mode[chan->channel] = cnt_mode;
388 /* Add quadrature mode configuration */
389 if (priv->quadrature_mode[chan->channel])
390 mode_cfg |= (priv->quadrature_scale[chan->channel] + 1) << 3;
392 /* Load mode configuration to Counter Mode Register */
393 outb(QUAD8_CTR_CMR | mode_cfg, base_offset);
395 mutex_unlock(&priv->lock);
400 static int quad8_get_count_mode(struct iio_dev *indio_dev,
401 const struct iio_chan_spec *chan)
403 const struct quad8_iio *const priv = iio_priv(indio_dev);
405 return priv->count_mode[chan->channel];
408 static const struct iio_enum quad8_count_mode_enum = {
409 .items = quad8_count_modes,
410 .num_items = ARRAY_SIZE(quad8_count_modes),
411 .set = quad8_set_count_mode,
412 .get = quad8_get_count_mode
415 static const char *const quad8_synchronous_modes[] = {
420 static int quad8_set_synchronous_mode(struct iio_dev *indio_dev,
421 const struct iio_chan_spec *chan, unsigned int synchronous_mode)
423 struct quad8_iio *const priv = iio_priv(indio_dev);
424 const int base_offset = priv->base + 2 * chan->channel + 1;
425 unsigned int idr_cfg = synchronous_mode;
427 mutex_lock(&priv->lock);
429 idr_cfg |= priv->index_polarity[chan->channel] << 1;
431 /* Index function must be non-synchronous in non-quadrature mode */
432 if (synchronous_mode && !priv->quadrature_mode[chan->channel]) {
433 mutex_unlock(&priv->lock);
437 priv->synchronous_mode[chan->channel] = synchronous_mode;
439 /* Load Index Control configuration to Index Control Register */
440 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
442 mutex_unlock(&priv->lock);
447 static int quad8_get_synchronous_mode(struct iio_dev *indio_dev,
448 const struct iio_chan_spec *chan)
450 const struct quad8_iio *const priv = iio_priv(indio_dev);
452 return priv->synchronous_mode[chan->channel];
455 static const struct iio_enum quad8_synchronous_mode_enum = {
456 .items = quad8_synchronous_modes,
457 .num_items = ARRAY_SIZE(quad8_synchronous_modes),
458 .set = quad8_set_synchronous_mode,
459 .get = quad8_get_synchronous_mode
462 static const char *const quad8_quadrature_modes[] = {
467 static int quad8_set_quadrature_mode(struct iio_dev *indio_dev,
468 const struct iio_chan_spec *chan, unsigned int quadrature_mode)
470 struct quad8_iio *const priv = iio_priv(indio_dev);
471 const int base_offset = priv->base + 2 * chan->channel + 1;
472 unsigned int mode_cfg;
474 mutex_lock(&priv->lock);
476 mode_cfg = priv->count_mode[chan->channel] << 1;
479 mode_cfg |= (priv->quadrature_scale[chan->channel] + 1) << 3;
481 /* Quadrature scaling only available in quadrature mode */
482 priv->quadrature_scale[chan->channel] = 0;
484 /* Synchronous function not supported in non-quadrature mode */
485 if (priv->synchronous_mode[chan->channel])
486 quad8_set_synchronous_mode(indio_dev, chan, 0);
489 priv->quadrature_mode[chan->channel] = quadrature_mode;
491 /* Load mode configuration to Counter Mode Register */
492 outb(QUAD8_CTR_CMR | mode_cfg, base_offset);
494 mutex_unlock(&priv->lock);
499 static int quad8_get_quadrature_mode(struct iio_dev *indio_dev,
500 const struct iio_chan_spec *chan)
502 const struct quad8_iio *const priv = iio_priv(indio_dev);
504 return priv->quadrature_mode[chan->channel];
507 static const struct iio_enum quad8_quadrature_mode_enum = {
508 .items = quad8_quadrature_modes,
509 .num_items = ARRAY_SIZE(quad8_quadrature_modes),
510 .set = quad8_set_quadrature_mode,
511 .get = quad8_get_quadrature_mode
514 static const char *const quad8_index_polarity_modes[] = {
519 static int quad8_set_index_polarity(struct iio_dev *indio_dev,
520 const struct iio_chan_spec *chan, unsigned int index_polarity)
522 struct quad8_iio *const priv = iio_priv(indio_dev);
523 const int base_offset = priv->base + 2 * chan->channel + 1;
524 unsigned int idr_cfg = index_polarity << 1;
526 mutex_lock(&priv->lock);
528 idr_cfg |= priv->synchronous_mode[chan->channel];
530 priv->index_polarity[chan->channel] = index_polarity;
532 /* Load Index Control configuration to Index Control Register */
533 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
535 mutex_unlock(&priv->lock);
540 static int quad8_get_index_polarity(struct iio_dev *indio_dev,
541 const struct iio_chan_spec *chan)
543 const struct quad8_iio *const priv = iio_priv(indio_dev);
545 return priv->index_polarity[chan->channel];
548 static const struct iio_enum quad8_index_polarity_enum = {
549 .items = quad8_index_polarity_modes,
550 .num_items = ARRAY_SIZE(quad8_index_polarity_modes),
551 .set = quad8_set_index_polarity,
552 .get = quad8_get_index_polarity
555 static const struct iio_chan_spec_ext_info quad8_count_ext_info[] = {
558 .shared = IIO_SEPARATE,
559 .read = quad8_read_preset,
560 .write = quad8_write_preset
563 .name = "set_to_preset_on_index",
564 .shared = IIO_SEPARATE,
565 .read = quad8_read_set_to_preset_on_index,
566 .write = quad8_write_set_to_preset_on_index
568 IIO_ENUM("noise_error", IIO_SEPARATE, &quad8_noise_error_enum),
569 IIO_ENUM_AVAILABLE("noise_error", &quad8_noise_error_enum),
570 IIO_ENUM("count_direction", IIO_SEPARATE, &quad8_count_direction_enum),
571 IIO_ENUM_AVAILABLE("count_direction", &quad8_count_direction_enum),
572 IIO_ENUM("count_mode", IIO_SEPARATE, &quad8_count_mode_enum),
573 IIO_ENUM_AVAILABLE("count_mode", &quad8_count_mode_enum),
574 IIO_ENUM("quadrature_mode", IIO_SEPARATE, &quad8_quadrature_mode_enum),
575 IIO_ENUM_AVAILABLE("quadrature_mode", &quad8_quadrature_mode_enum),
579 static const struct iio_chan_spec_ext_info quad8_index_ext_info[] = {
580 IIO_ENUM("synchronous_mode", IIO_SEPARATE,
581 &quad8_synchronous_mode_enum),
582 IIO_ENUM_AVAILABLE("synchronous_mode", &quad8_synchronous_mode_enum),
583 IIO_ENUM("index_polarity", IIO_SEPARATE, &quad8_index_polarity_enum),
584 IIO_ENUM_AVAILABLE("index_polarity", &quad8_index_polarity_enum),
588 #define QUAD8_COUNT_CHAN(_chan) { \
590 .channel = (_chan), \
591 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
592 BIT(IIO_CHAN_INFO_ENABLE) | BIT(IIO_CHAN_INFO_SCALE), \
593 .ext_info = quad8_count_ext_info, \
597 #define QUAD8_INDEX_CHAN(_chan) { \
599 .channel = (_chan), \
600 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
601 .ext_info = quad8_index_ext_info, \
605 static const struct iio_chan_spec quad8_channels[] = {
606 QUAD8_COUNT_CHAN(0), QUAD8_INDEX_CHAN(0),
607 QUAD8_COUNT_CHAN(1), QUAD8_INDEX_CHAN(1),
608 QUAD8_COUNT_CHAN(2), QUAD8_INDEX_CHAN(2),
609 QUAD8_COUNT_CHAN(3), QUAD8_INDEX_CHAN(3),
610 QUAD8_COUNT_CHAN(4), QUAD8_INDEX_CHAN(4),
611 QUAD8_COUNT_CHAN(5), QUAD8_INDEX_CHAN(5),
612 QUAD8_COUNT_CHAN(6), QUAD8_INDEX_CHAN(6),
613 QUAD8_COUNT_CHAN(7), QUAD8_INDEX_CHAN(7)
616 static int quad8_signal_read(struct counter_device *counter,
617 struct counter_signal *signal, struct counter_signal_read_value *val)
619 const struct quad8_iio *const priv = counter->priv;
621 enum counter_signal_level level;
623 /* Only Index signal levels can be read */
627 state = inb(priv->base + QUAD8_REG_INDEX_INPUT_LEVELS)
628 & BIT(signal->id - 16);
630 level = (state) ? COUNTER_SIGNAL_LEVEL_HIGH : COUNTER_SIGNAL_LEVEL_LOW;
632 counter_signal_read_value_set(val, COUNTER_SIGNAL_LEVEL, &level);
637 static int quad8_count_read(struct counter_device *counter,
638 struct counter_count *count, struct counter_count_read_value *val)
640 struct quad8_iio *const priv = counter->priv;
641 const int base_offset = priv->base + 2 * count->id;
645 unsigned long position;
648 flags = inb(base_offset + 1);
649 borrow = flags & QUAD8_FLAG_BT;
650 carry = !!(flags & QUAD8_FLAG_CT);
652 /* Borrow XOR Carry effectively doubles count range */
653 position = (unsigned long)(borrow ^ carry) << 24;
655 mutex_lock(&priv->lock);
657 /* Reset Byte Pointer; transfer Counter to Output Latch */
658 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_CNTR_OUT,
661 for (i = 0; i < 3; i++)
662 position |= (unsigned long)inb(base_offset) << (8 * i);
664 counter_count_read_value_set(val, COUNTER_COUNT_POSITION, &position);
666 mutex_unlock(&priv->lock);
671 static int quad8_count_write(struct counter_device *counter,
672 struct counter_count *count, struct counter_count_write_value *val)
674 struct quad8_iio *const priv = counter->priv;
675 const int base_offset = priv->base + 2 * count->id;
677 unsigned long position;
680 err = counter_count_write_value_get(&position, COUNTER_COUNT_POSITION,
685 /* Only 24-bit values are supported */
686 if (position > 0xFFFFFF)
689 mutex_lock(&priv->lock);
691 /* Reset Byte Pointer */
692 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
694 /* Counter can only be set via Preset Register */
695 for (i = 0; i < 3; i++)
696 outb(position >> (8 * i), base_offset);
698 /* Transfer Preset Register to Counter */
699 outb(QUAD8_CTR_RLD | QUAD8_RLD_PRESET_CNTR, base_offset + 1);
701 /* Reset Byte Pointer */
702 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
704 /* Set Preset Register back to original value */
705 position = priv->preset[count->id];
706 for (i = 0; i < 3; i++)
707 outb(position >> (8 * i), base_offset);
709 /* Reset Borrow, Carry, Compare, and Sign flags */
710 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_FLAGS, base_offset + 1);
711 /* Reset Error flag */
712 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_E, base_offset + 1);
714 mutex_unlock(&priv->lock);
719 enum quad8_count_function {
720 QUAD8_COUNT_FUNCTION_PULSE_DIRECTION = 0,
721 QUAD8_COUNT_FUNCTION_QUADRATURE_X1,
722 QUAD8_COUNT_FUNCTION_QUADRATURE_X2,
723 QUAD8_COUNT_FUNCTION_QUADRATURE_X4
726 static enum counter_count_function quad8_count_functions_list[] = {
727 [QUAD8_COUNT_FUNCTION_PULSE_DIRECTION] = COUNTER_COUNT_FUNCTION_PULSE_DIRECTION,
728 [QUAD8_COUNT_FUNCTION_QUADRATURE_X1] = COUNTER_COUNT_FUNCTION_QUADRATURE_X1_A,
729 [QUAD8_COUNT_FUNCTION_QUADRATURE_X2] = COUNTER_COUNT_FUNCTION_QUADRATURE_X2_A,
730 [QUAD8_COUNT_FUNCTION_QUADRATURE_X4] = COUNTER_COUNT_FUNCTION_QUADRATURE_X4
733 static int quad8_function_get(struct counter_device *counter,
734 struct counter_count *count, size_t *function)
736 struct quad8_iio *const priv = counter->priv;
737 const int id = count->id;
739 mutex_lock(&priv->lock);
741 if (priv->quadrature_mode[id])
742 switch (priv->quadrature_scale[id]) {
744 *function = QUAD8_COUNT_FUNCTION_QUADRATURE_X1;
747 *function = QUAD8_COUNT_FUNCTION_QUADRATURE_X2;
750 *function = QUAD8_COUNT_FUNCTION_QUADRATURE_X4;
754 *function = QUAD8_COUNT_FUNCTION_PULSE_DIRECTION;
756 mutex_unlock(&priv->lock);
761 static int quad8_function_set(struct counter_device *counter,
762 struct counter_count *count, size_t function)
764 struct quad8_iio *const priv = counter->priv;
765 const int id = count->id;
766 unsigned int *const quadrature_mode = priv->quadrature_mode + id;
767 unsigned int *const scale = priv->quadrature_scale + id;
768 unsigned int *const synchronous_mode = priv->synchronous_mode + id;
769 const int base_offset = priv->base + 2 * id + 1;
770 unsigned int mode_cfg;
771 unsigned int idr_cfg;
773 mutex_lock(&priv->lock);
775 mode_cfg = priv->count_mode[id] << 1;
776 idr_cfg = priv->index_polarity[id] << 1;
778 if (function == QUAD8_COUNT_FUNCTION_PULSE_DIRECTION) {
779 *quadrature_mode = 0;
781 /* Quadrature scaling only available in quadrature mode */
784 /* Synchronous function not supported in non-quadrature mode */
785 if (*synchronous_mode) {
786 *synchronous_mode = 0;
787 /* Disable synchronous function mode */
788 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
791 *quadrature_mode = 1;
794 case QUAD8_COUNT_FUNCTION_QUADRATURE_X1:
796 mode_cfg |= QUAD8_CMR_QUADRATURE_X1;
798 case QUAD8_COUNT_FUNCTION_QUADRATURE_X2:
800 mode_cfg |= QUAD8_CMR_QUADRATURE_X2;
802 case QUAD8_COUNT_FUNCTION_QUADRATURE_X4:
804 mode_cfg |= QUAD8_CMR_QUADRATURE_X4;
809 /* Load mode configuration to Counter Mode Register */
810 outb(QUAD8_CTR_CMR | mode_cfg, base_offset);
812 mutex_unlock(&priv->lock);
817 static void quad8_direction_get(struct counter_device *counter,
818 struct counter_count *count, enum counter_count_direction *direction)
820 const struct quad8_iio *const priv = counter->priv;
821 unsigned int ud_flag;
822 const unsigned int flag_addr = priv->base + 2 * count->id + 1;
824 /* U/D flag: nonzero = up, zero = down */
825 ud_flag = inb(flag_addr) & QUAD8_FLAG_UD;
827 *direction = (ud_flag) ? COUNTER_COUNT_DIRECTION_FORWARD :
828 COUNTER_COUNT_DIRECTION_BACKWARD;
831 enum quad8_synapse_action {
832 QUAD8_SYNAPSE_ACTION_NONE = 0,
833 QUAD8_SYNAPSE_ACTION_RISING_EDGE,
834 QUAD8_SYNAPSE_ACTION_FALLING_EDGE,
835 QUAD8_SYNAPSE_ACTION_BOTH_EDGES
838 static enum counter_synapse_action quad8_index_actions_list[] = {
839 [QUAD8_SYNAPSE_ACTION_NONE] = COUNTER_SYNAPSE_ACTION_NONE,
840 [QUAD8_SYNAPSE_ACTION_RISING_EDGE] = COUNTER_SYNAPSE_ACTION_RISING_EDGE
843 static enum counter_synapse_action quad8_synapse_actions_list[] = {
844 [QUAD8_SYNAPSE_ACTION_NONE] = COUNTER_SYNAPSE_ACTION_NONE,
845 [QUAD8_SYNAPSE_ACTION_RISING_EDGE] = COUNTER_SYNAPSE_ACTION_RISING_EDGE,
846 [QUAD8_SYNAPSE_ACTION_FALLING_EDGE] = COUNTER_SYNAPSE_ACTION_FALLING_EDGE,
847 [QUAD8_SYNAPSE_ACTION_BOTH_EDGES] = COUNTER_SYNAPSE_ACTION_BOTH_EDGES
850 static int quad8_action_get(struct counter_device *counter,
851 struct counter_count *count, struct counter_synapse *synapse,
854 struct quad8_iio *const priv = counter->priv;
857 const size_t signal_a_id = count->synapses[0].signal->id;
858 enum counter_count_direction direction;
860 /* Handle Index signals */
861 if (synapse->signal->id >= 16) {
862 if (priv->preset_enable[count->id])
863 *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
865 *action = QUAD8_SYNAPSE_ACTION_NONE;
870 err = quad8_function_get(counter, count, &function);
874 /* Default action mode */
875 *action = QUAD8_SYNAPSE_ACTION_NONE;
877 /* Determine action mode based on current count function mode */
879 case QUAD8_COUNT_FUNCTION_PULSE_DIRECTION:
880 if (synapse->signal->id == signal_a_id)
881 *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
883 case QUAD8_COUNT_FUNCTION_QUADRATURE_X1:
884 if (synapse->signal->id == signal_a_id) {
885 quad8_direction_get(counter, count, &direction);
887 if (direction == COUNTER_COUNT_DIRECTION_FORWARD)
888 *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
890 *action = QUAD8_SYNAPSE_ACTION_FALLING_EDGE;
893 case QUAD8_COUNT_FUNCTION_QUADRATURE_X2:
894 if (synapse->signal->id == signal_a_id)
895 *action = QUAD8_SYNAPSE_ACTION_BOTH_EDGES;
897 case QUAD8_COUNT_FUNCTION_QUADRATURE_X4:
898 *action = QUAD8_SYNAPSE_ACTION_BOTH_EDGES;
905 static const struct counter_ops quad8_ops = {
906 .signal_read = quad8_signal_read,
907 .count_read = quad8_count_read,
908 .count_write = quad8_count_write,
909 .function_get = quad8_function_get,
910 .function_set = quad8_function_set,
911 .action_get = quad8_action_get
914 static int quad8_index_polarity_get(struct counter_device *counter,
915 struct counter_signal *signal, size_t *index_polarity)
917 const struct quad8_iio *const priv = counter->priv;
918 const size_t channel_id = signal->id - 16;
920 *index_polarity = priv->index_polarity[channel_id];
925 static int quad8_index_polarity_set(struct counter_device *counter,
926 struct counter_signal *signal, size_t index_polarity)
928 struct quad8_iio *const priv = counter->priv;
929 const size_t channel_id = signal->id - 16;
930 const int base_offset = priv->base + 2 * channel_id + 1;
931 unsigned int idr_cfg = index_polarity << 1;
933 mutex_lock(&priv->lock);
935 idr_cfg |= priv->synchronous_mode[channel_id];
937 priv->index_polarity[channel_id] = index_polarity;
939 /* Load Index Control configuration to Index Control Register */
940 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
942 mutex_unlock(&priv->lock);
947 static struct counter_signal_enum_ext quad8_index_pol_enum = {
948 .items = quad8_index_polarity_modes,
949 .num_items = ARRAY_SIZE(quad8_index_polarity_modes),
950 .get = quad8_index_polarity_get,
951 .set = quad8_index_polarity_set
954 static int quad8_synchronous_mode_get(struct counter_device *counter,
955 struct counter_signal *signal, size_t *synchronous_mode)
957 const struct quad8_iio *const priv = counter->priv;
958 const size_t channel_id = signal->id - 16;
960 *synchronous_mode = priv->synchronous_mode[channel_id];
965 static int quad8_synchronous_mode_set(struct counter_device *counter,
966 struct counter_signal *signal, size_t synchronous_mode)
968 struct quad8_iio *const priv = counter->priv;
969 const size_t channel_id = signal->id - 16;
970 const int base_offset = priv->base + 2 * channel_id + 1;
971 unsigned int idr_cfg = synchronous_mode;
973 mutex_lock(&priv->lock);
975 idr_cfg |= priv->index_polarity[channel_id] << 1;
977 /* Index function must be non-synchronous in non-quadrature mode */
978 if (synchronous_mode && !priv->quadrature_mode[channel_id]) {
979 mutex_unlock(&priv->lock);
983 priv->synchronous_mode[channel_id] = synchronous_mode;
985 /* Load Index Control configuration to Index Control Register */
986 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
988 mutex_unlock(&priv->lock);
993 static struct counter_signal_enum_ext quad8_syn_mode_enum = {
994 .items = quad8_synchronous_modes,
995 .num_items = ARRAY_SIZE(quad8_synchronous_modes),
996 .get = quad8_synchronous_mode_get,
997 .set = quad8_synchronous_mode_set
1000 static ssize_t quad8_count_floor_read(struct counter_device *counter,
1001 struct counter_count *count, void *private, char *buf)
1003 /* Only a floor of 0 is supported */
1004 return sprintf(buf, "0\n");
1007 static int quad8_count_mode_get(struct counter_device *counter,
1008 struct counter_count *count, size_t *cnt_mode)
1010 const struct quad8_iio *const priv = counter->priv;
1012 /* Map 104-QUAD-8 count mode to Generic Counter count mode */
1013 switch (priv->count_mode[count->id]) {
1015 *cnt_mode = COUNTER_COUNT_MODE_NORMAL;
1018 *cnt_mode = COUNTER_COUNT_MODE_RANGE_LIMIT;
1021 *cnt_mode = COUNTER_COUNT_MODE_NON_RECYCLE;
1024 *cnt_mode = COUNTER_COUNT_MODE_MODULO_N;
1031 static int quad8_count_mode_set(struct counter_device *counter,
1032 struct counter_count *count, size_t cnt_mode)
1034 struct quad8_iio *const priv = counter->priv;
1035 unsigned int mode_cfg;
1036 const int base_offset = priv->base + 2 * count->id + 1;
1038 /* Map Generic Counter count mode to 104-QUAD-8 count mode */
1040 case COUNTER_COUNT_MODE_NORMAL:
1043 case COUNTER_COUNT_MODE_RANGE_LIMIT:
1046 case COUNTER_COUNT_MODE_NON_RECYCLE:
1049 case COUNTER_COUNT_MODE_MODULO_N:
1054 mutex_lock(&priv->lock);
1056 priv->count_mode[count->id] = cnt_mode;
1058 /* Set count mode configuration value */
1059 mode_cfg = cnt_mode << 1;
1061 /* Add quadrature mode configuration */
1062 if (priv->quadrature_mode[count->id])
1063 mode_cfg |= (priv->quadrature_scale[count->id] + 1) << 3;
1065 /* Load mode configuration to Counter Mode Register */
1066 outb(QUAD8_CTR_CMR | mode_cfg, base_offset);
1068 mutex_unlock(&priv->lock);
1073 static struct counter_count_enum_ext quad8_cnt_mode_enum = {
1074 .items = counter_count_mode_str,
1075 .num_items = ARRAY_SIZE(counter_count_mode_str),
1076 .get = quad8_count_mode_get,
1077 .set = quad8_count_mode_set
1080 static ssize_t quad8_count_direction_read(struct counter_device *counter,
1081 struct counter_count *count, void *priv, char *buf)
1083 enum counter_count_direction dir;
1085 quad8_direction_get(counter, count, &dir);
1087 return sprintf(buf, "%s\n", counter_count_direction_str[dir]);
1090 static ssize_t quad8_count_enable_read(struct counter_device *counter,
1091 struct counter_count *count, void *private, char *buf)
1093 const struct quad8_iio *const priv = counter->priv;
1095 return sprintf(buf, "%u\n", priv->ab_enable[count->id]);
1098 static ssize_t quad8_count_enable_write(struct counter_device *counter,
1099 struct counter_count *count, void *private, const char *buf, size_t len)
1101 struct quad8_iio *const priv = counter->priv;
1102 const int base_offset = priv->base + 2 * count->id;
1105 unsigned int ior_cfg;
1107 err = kstrtobool(buf, &ab_enable);
1111 mutex_lock(&priv->lock);
1113 priv->ab_enable[count->id] = ab_enable;
1115 ior_cfg = ab_enable | priv->preset_enable[count->id] << 1;
1117 /* Load I/O control configuration */
1118 outb(QUAD8_CTR_IOR | ior_cfg, base_offset + 1);
1120 mutex_unlock(&priv->lock);
1125 static int quad8_error_noise_get(struct counter_device *counter,
1126 struct counter_count *count, size_t *noise_error)
1128 const struct quad8_iio *const priv = counter->priv;
1129 const int base_offset = priv->base + 2 * count->id + 1;
1131 *noise_error = !!(inb(base_offset) & QUAD8_FLAG_E);
1136 static struct counter_count_enum_ext quad8_error_noise_enum = {
1137 .items = quad8_noise_error_states,
1138 .num_items = ARRAY_SIZE(quad8_noise_error_states),
1139 .get = quad8_error_noise_get
1142 static ssize_t quad8_count_preset_read(struct counter_device *counter,
1143 struct counter_count *count, void *private, char *buf)
1145 const struct quad8_iio *const priv = counter->priv;
1147 return sprintf(buf, "%u\n", priv->preset[count->id]);
1150 static void quad8_preset_register_set(struct quad8_iio *quad8iio, int id,
1151 unsigned int preset)
1153 const unsigned int base_offset = quad8iio->base + 2 * id;
1156 quad8iio->preset[id] = preset;
1158 /* Reset Byte Pointer */
1159 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
1161 /* Set Preset Register */
1162 for (i = 0; i < 3; i++)
1163 outb(preset >> (8 * i), base_offset);
1166 static ssize_t quad8_count_preset_write(struct counter_device *counter,
1167 struct counter_count *count, void *private, const char *buf, size_t len)
1169 struct quad8_iio *const priv = counter->priv;
1170 unsigned int preset;
1173 ret = kstrtouint(buf, 0, &preset);
1177 /* Only 24-bit values are supported */
1178 if (preset > 0xFFFFFF)
1181 mutex_lock(&priv->lock);
1183 quad8_preset_register_set(priv, count->id, preset);
1185 mutex_unlock(&priv->lock);
1190 static ssize_t quad8_count_ceiling_read(struct counter_device *counter,
1191 struct counter_count *count, void *private, char *buf)
1193 struct quad8_iio *const priv = counter->priv;
1195 mutex_lock(&priv->lock);
1197 /* Range Limit and Modulo-N count modes use preset value as ceiling */
1198 switch (priv->count_mode[count->id]) {
1201 mutex_unlock(&priv->lock);
1202 return sprintf(buf, "%u\n", priv->preset[count->id]);
1205 mutex_unlock(&priv->lock);
1207 /* By default 0x1FFFFFF (25 bits unsigned) is maximum count */
1208 return sprintf(buf, "33554431\n");
1211 static ssize_t quad8_count_ceiling_write(struct counter_device *counter,
1212 struct counter_count *count, void *private, const char *buf, size_t len)
1214 struct quad8_iio *const priv = counter->priv;
1215 unsigned int ceiling;
1218 ret = kstrtouint(buf, 0, &ceiling);
1222 /* Only 24-bit values are supported */
1223 if (ceiling > 0xFFFFFF)
1226 mutex_lock(&priv->lock);
1228 /* Range Limit and Modulo-N count modes use preset value as ceiling */
1229 switch (priv->count_mode[count->id]) {
1232 quad8_preset_register_set(priv, count->id, ceiling);
1236 mutex_unlock(&priv->lock);
1241 static ssize_t quad8_count_preset_enable_read(struct counter_device *counter,
1242 struct counter_count *count, void *private, char *buf)
1244 const struct quad8_iio *const priv = counter->priv;
1246 return sprintf(buf, "%u\n", !priv->preset_enable[count->id]);
1249 static ssize_t quad8_count_preset_enable_write(struct counter_device *counter,
1250 struct counter_count *count, void *private, const char *buf, size_t len)
1252 struct quad8_iio *const priv = counter->priv;
1253 const int base_offset = priv->base + 2 * count->id + 1;
1256 unsigned int ior_cfg;
1258 ret = kstrtobool(buf, &preset_enable);
1262 /* Preset enable is active low in Input/Output Control register */
1263 preset_enable = !preset_enable;
1265 mutex_lock(&priv->lock);
1267 priv->preset_enable[count->id] = preset_enable;
1269 ior_cfg = priv->ab_enable[count->id] | (unsigned int)preset_enable << 1;
1271 /* Load I/O control configuration to Input / Output Control Register */
1272 outb(QUAD8_CTR_IOR | ior_cfg, base_offset);
1274 mutex_unlock(&priv->lock);
1279 static const struct counter_signal_ext quad8_index_ext[] = {
1280 COUNTER_SIGNAL_ENUM("index_polarity", &quad8_index_pol_enum),
1281 COUNTER_SIGNAL_ENUM_AVAILABLE("index_polarity", &quad8_index_pol_enum),
1282 COUNTER_SIGNAL_ENUM("synchronous_mode", &quad8_syn_mode_enum),
1283 COUNTER_SIGNAL_ENUM_AVAILABLE("synchronous_mode", &quad8_syn_mode_enum)
1286 #define QUAD8_QUAD_SIGNAL(_id, _name) { \
1291 #define QUAD8_INDEX_SIGNAL(_id, _name) { \
1294 .ext = quad8_index_ext, \
1295 .num_ext = ARRAY_SIZE(quad8_index_ext) \
1298 static struct counter_signal quad8_signals[] = {
1299 QUAD8_QUAD_SIGNAL(0, "Channel 1 Quadrature A"),
1300 QUAD8_QUAD_SIGNAL(1, "Channel 1 Quadrature B"),
1301 QUAD8_QUAD_SIGNAL(2, "Channel 2 Quadrature A"),
1302 QUAD8_QUAD_SIGNAL(3, "Channel 2 Quadrature B"),
1303 QUAD8_QUAD_SIGNAL(4, "Channel 3 Quadrature A"),
1304 QUAD8_QUAD_SIGNAL(5, "Channel 3 Quadrature B"),
1305 QUAD8_QUAD_SIGNAL(6, "Channel 4 Quadrature A"),
1306 QUAD8_QUAD_SIGNAL(7, "Channel 4 Quadrature B"),
1307 QUAD8_QUAD_SIGNAL(8, "Channel 5 Quadrature A"),
1308 QUAD8_QUAD_SIGNAL(9, "Channel 5 Quadrature B"),
1309 QUAD8_QUAD_SIGNAL(10, "Channel 6 Quadrature A"),
1310 QUAD8_QUAD_SIGNAL(11, "Channel 6 Quadrature B"),
1311 QUAD8_QUAD_SIGNAL(12, "Channel 7 Quadrature A"),
1312 QUAD8_QUAD_SIGNAL(13, "Channel 7 Quadrature B"),
1313 QUAD8_QUAD_SIGNAL(14, "Channel 8 Quadrature A"),
1314 QUAD8_QUAD_SIGNAL(15, "Channel 8 Quadrature B"),
1315 QUAD8_INDEX_SIGNAL(16, "Channel 1 Index"),
1316 QUAD8_INDEX_SIGNAL(17, "Channel 2 Index"),
1317 QUAD8_INDEX_SIGNAL(18, "Channel 3 Index"),
1318 QUAD8_INDEX_SIGNAL(19, "Channel 4 Index"),
1319 QUAD8_INDEX_SIGNAL(20, "Channel 5 Index"),
1320 QUAD8_INDEX_SIGNAL(21, "Channel 6 Index"),
1321 QUAD8_INDEX_SIGNAL(22, "Channel 7 Index"),
1322 QUAD8_INDEX_SIGNAL(23, "Channel 8 Index")
1325 #define QUAD8_COUNT_SYNAPSES(_id) { \
1327 .actions_list = quad8_synapse_actions_list, \
1328 .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
1329 .signal = quad8_signals + 2 * (_id) \
1332 .actions_list = quad8_synapse_actions_list, \
1333 .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
1334 .signal = quad8_signals + 2 * (_id) + 1 \
1337 .actions_list = quad8_index_actions_list, \
1338 .num_actions = ARRAY_SIZE(quad8_index_actions_list), \
1339 .signal = quad8_signals + 2 * (_id) + 16 \
1343 static struct counter_synapse quad8_count_synapses[][3] = {
1344 QUAD8_COUNT_SYNAPSES(0), QUAD8_COUNT_SYNAPSES(1),
1345 QUAD8_COUNT_SYNAPSES(2), QUAD8_COUNT_SYNAPSES(3),
1346 QUAD8_COUNT_SYNAPSES(4), QUAD8_COUNT_SYNAPSES(5),
1347 QUAD8_COUNT_SYNAPSES(6), QUAD8_COUNT_SYNAPSES(7)
1350 static const struct counter_count_ext quad8_count_ext[] = {
1353 .read = quad8_count_ceiling_read,
1354 .write = quad8_count_ceiling_write
1358 .read = quad8_count_floor_read
1360 COUNTER_COUNT_ENUM("count_mode", &quad8_cnt_mode_enum),
1361 COUNTER_COUNT_ENUM_AVAILABLE("count_mode", &quad8_cnt_mode_enum),
1363 .name = "direction",
1364 .read = quad8_count_direction_read
1368 .read = quad8_count_enable_read,
1369 .write = quad8_count_enable_write
1371 COUNTER_COUNT_ENUM("error_noise", &quad8_error_noise_enum),
1372 COUNTER_COUNT_ENUM_AVAILABLE("error_noise", &quad8_error_noise_enum),
1375 .read = quad8_count_preset_read,
1376 .write = quad8_count_preset_write
1379 .name = "preset_enable",
1380 .read = quad8_count_preset_enable_read,
1381 .write = quad8_count_preset_enable_write
1385 #define QUAD8_COUNT(_id, _cntname) { \
1387 .name = (_cntname), \
1388 .functions_list = quad8_count_functions_list, \
1389 .num_functions = ARRAY_SIZE(quad8_count_functions_list), \
1390 .synapses = quad8_count_synapses[(_id)], \
1391 .num_synapses = 2, \
1392 .ext = quad8_count_ext, \
1393 .num_ext = ARRAY_SIZE(quad8_count_ext) \
1396 static struct counter_count quad8_counts[] = {
1397 QUAD8_COUNT(0, "Channel 1 Count"),
1398 QUAD8_COUNT(1, "Channel 2 Count"),
1399 QUAD8_COUNT(2, "Channel 3 Count"),
1400 QUAD8_COUNT(3, "Channel 4 Count"),
1401 QUAD8_COUNT(4, "Channel 5 Count"),
1402 QUAD8_COUNT(5, "Channel 6 Count"),
1403 QUAD8_COUNT(6, "Channel 7 Count"),
1404 QUAD8_COUNT(7, "Channel 8 Count")
1407 static int quad8_probe(struct device *dev, unsigned int id)
1409 struct iio_dev *indio_dev;
1410 struct quad8_iio *quad8iio;
1412 unsigned int base_offset;
1415 if (!devm_request_region(dev, base[id], QUAD8_EXTENT, dev_name(dev))) {
1416 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
1417 base[id], base[id] + QUAD8_EXTENT);
1421 /* Allocate IIO device; this also allocates driver data structure */
1422 indio_dev = devm_iio_device_alloc(dev, sizeof(*quad8iio));
1426 /* Initialize IIO device */
1427 indio_dev->info = &quad8_info;
1428 indio_dev->modes = INDIO_DIRECT_MODE;
1429 indio_dev->num_channels = ARRAY_SIZE(quad8_channels);
1430 indio_dev->channels = quad8_channels;
1431 indio_dev->name = dev_name(dev);
1432 indio_dev->dev.parent = dev;
1434 /* Initialize Counter device and driver data */
1435 quad8iio = iio_priv(indio_dev);
1436 quad8iio->counter.name = dev_name(dev);
1437 quad8iio->counter.parent = dev;
1438 quad8iio->counter.ops = &quad8_ops;
1439 quad8iio->counter.counts = quad8_counts;
1440 quad8iio->counter.num_counts = ARRAY_SIZE(quad8_counts);
1441 quad8iio->counter.signals = quad8_signals;
1442 quad8iio->counter.num_signals = ARRAY_SIZE(quad8_signals);
1443 quad8iio->counter.priv = quad8iio;
1444 quad8iio->base = base[id];
1446 /* Initialize mutex */
1447 mutex_init(&quad8iio->lock);
1449 /* Reset all counters and disable interrupt function */
1450 outb(QUAD8_CHAN_OP_RESET_COUNTERS, base[id] + QUAD8_REG_CHAN_OP);
1451 /* Set initial configuration for all counters */
1452 for (i = 0; i < QUAD8_NUM_COUNTERS; i++) {
1453 base_offset = base[id] + 2 * i;
1454 /* Reset Byte Pointer */
1455 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
1456 /* Reset Preset Register */
1457 for (j = 0; j < 3; j++)
1458 outb(0x00, base_offset);
1459 /* Reset Borrow, Carry, Compare, and Sign flags */
1460 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_FLAGS, base_offset + 1);
1461 /* Reset Error flag */
1462 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_E, base_offset + 1);
1463 /* Binary encoding; Normal count; non-quadrature mode */
1464 outb(QUAD8_CTR_CMR, base_offset + 1);
1465 /* Disable A and B inputs; preset on index; FLG1 as Carry */
1466 outb(QUAD8_CTR_IOR, base_offset + 1);
1467 /* Disable index function; negative index polarity */
1468 outb(QUAD8_CTR_IDR, base_offset + 1);
1470 /* Enable all counters */
1471 outb(QUAD8_CHAN_OP_ENABLE_COUNTERS, base[id] + QUAD8_REG_CHAN_OP);
1473 /* Register IIO device */
1474 err = devm_iio_device_register(dev, indio_dev);
1478 /* Register Counter device */
1479 return devm_counter_register(dev, &quad8iio->counter);
1482 static struct isa_driver quad8_driver = {
1483 .probe = quad8_probe,
1485 .name = "104-quad-8"
1489 module_isa_driver(quad8_driver, num_quad8);
1491 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
1492 MODULE_DESCRIPTION("ACCES 104-QUAD-8 IIO driver");
1493 MODULE_LICENSE("GPL v2");