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 * @fck_prescaler: array of filter clock prescaler configurations
35 * @preset: array of preset values
36 * @count_mode: array of count mode configurations
37 * @quadrature_mode: array of quadrature mode configurations
38 * @quadrature_scale: array of quadrature mode scale configurations
39 * @ab_enable: array of A and B inputs enable configurations
40 * @preset_enable: array of set_to_preset_on_index attribute configurations
41 * @synchronous_mode: array of index function synchronous mode configurations
42 * @index_polarity: array of index function polarity configurations
43 * @cable_fault_enable: differential encoder cable status enable configurations
44 * @base: base port address of the IIO device
48 struct counter_device counter;
49 unsigned int fck_prescaler[QUAD8_NUM_COUNTERS];
50 unsigned int preset[QUAD8_NUM_COUNTERS];
51 unsigned int count_mode[QUAD8_NUM_COUNTERS];
52 unsigned int quadrature_mode[QUAD8_NUM_COUNTERS];
53 unsigned int quadrature_scale[QUAD8_NUM_COUNTERS];
54 unsigned int ab_enable[QUAD8_NUM_COUNTERS];
55 unsigned int preset_enable[QUAD8_NUM_COUNTERS];
56 unsigned int synchronous_mode[QUAD8_NUM_COUNTERS];
57 unsigned int index_polarity[QUAD8_NUM_COUNTERS];
58 unsigned int cable_fault_enable;
62 #define QUAD8_REG_CHAN_OP 0x11
63 #define QUAD8_REG_INDEX_INPUT_LEVELS 0x16
64 #define QUAD8_DIFF_ENCODER_CABLE_STATUS 0x17
66 #define QUAD8_FLAG_E BIT(4)
68 #define QUAD8_FLAG_UD BIT(5)
69 /* Reset and Load Signal Decoders */
70 #define QUAD8_CTR_RLD 0x00
71 /* Counter Mode Register */
72 #define QUAD8_CTR_CMR 0x20
73 /* Input / Output Control Register */
74 #define QUAD8_CTR_IOR 0x40
75 /* Index Control Register */
76 #define QUAD8_CTR_IDR 0x60
77 /* Reset Byte Pointer (three byte data pointer) */
78 #define QUAD8_RLD_RESET_BP 0x01
80 #define QUAD8_RLD_RESET_CNTR 0x02
81 /* Reset Borrow Toggle, Carry Toggle, Compare Toggle, and Sign flags */
82 #define QUAD8_RLD_RESET_FLAGS 0x04
83 /* Reset Error flag */
84 #define QUAD8_RLD_RESET_E 0x06
85 /* Preset Register to Counter */
86 #define QUAD8_RLD_PRESET_CNTR 0x08
87 /* Transfer Counter to Output Latch */
88 #define QUAD8_RLD_CNTR_OUT 0x10
89 /* Transfer Preset Register LSB to FCK Prescaler */
90 #define QUAD8_RLD_PRESET_PSC 0x18
91 #define QUAD8_CHAN_OP_ENABLE_COUNTERS 0x00
92 #define QUAD8_CHAN_OP_RESET_COUNTERS 0x01
93 #define QUAD8_CMR_QUADRATURE_X1 0x08
94 #define QUAD8_CMR_QUADRATURE_X2 0x10
95 #define QUAD8_CMR_QUADRATURE_X4 0x18
98 static int quad8_read_raw(struct iio_dev *indio_dev,
99 struct iio_chan_spec const *chan, int *val, int *val2, long mask)
101 struct quad8_iio *const priv = iio_priv(indio_dev);
102 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));
115 mutex_lock(&priv->lock);
117 /* Reset Byte Pointer; transfer Counter to Output Latch */
118 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_CNTR_OUT,
121 for (i = 0; i < 3; i++)
122 *val |= (unsigned int)inb(base_offset) << (8 * i);
124 mutex_unlock(&priv->lock);
127 case IIO_CHAN_INFO_ENABLE:
128 *val = priv->ab_enable[chan->channel];
130 case IIO_CHAN_INFO_SCALE:
132 *val2 = priv->quadrature_scale[chan->channel];
133 return IIO_VAL_FRACTIONAL_LOG2;
139 static int quad8_write_raw(struct iio_dev *indio_dev,
140 struct iio_chan_spec const *chan, int val, int val2, long mask)
142 struct quad8_iio *const priv = iio_priv(indio_dev);
143 const int base_offset = priv->base + 2 * chan->channel;
145 unsigned int ior_cfg;
148 case IIO_CHAN_INFO_RAW:
149 if (chan->type == IIO_INDEX)
152 /* Only 24-bit values are supported */
153 if ((unsigned int)val > 0xFFFFFF)
156 mutex_lock(&priv->lock);
158 /* Reset Byte Pointer */
159 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
161 /* Counter can only be set via Preset Register */
162 for (i = 0; i < 3; i++)
163 outb(val >> (8 * i), base_offset);
165 /* Transfer Preset Register to Counter */
166 outb(QUAD8_CTR_RLD | QUAD8_RLD_PRESET_CNTR, base_offset + 1);
168 /* Reset Byte Pointer */
169 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
171 /* Set Preset Register back to original value */
172 val = priv->preset[chan->channel];
173 for (i = 0; i < 3; i++)
174 outb(val >> (8 * i), base_offset);
176 /* Reset Borrow, Carry, Compare, and Sign flags */
177 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_FLAGS, base_offset + 1);
178 /* Reset Error flag */
179 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_E, base_offset + 1);
181 mutex_unlock(&priv->lock);
184 case IIO_CHAN_INFO_ENABLE:
185 /* only boolean values accepted */
186 if (val < 0 || val > 1)
189 mutex_lock(&priv->lock);
191 priv->ab_enable[chan->channel] = val;
193 ior_cfg = val | priv->preset_enable[chan->channel] << 1;
195 /* Load I/O control configuration */
196 outb(QUAD8_CTR_IOR | ior_cfg, base_offset + 1);
198 mutex_unlock(&priv->lock);
201 case IIO_CHAN_INFO_SCALE:
202 mutex_lock(&priv->lock);
204 /* Quadrature scaling only available in quadrature mode */
205 if (!priv->quadrature_mode[chan->channel] &&
206 (val2 || val != 1)) {
207 mutex_unlock(&priv->lock);
211 /* Only three gain states (1, 0.5, 0.25) */
212 if (val == 1 && !val2)
213 priv->quadrature_scale[chan->channel] = 0;
217 priv->quadrature_scale[chan->channel] = 1;
220 priv->quadrature_scale[chan->channel] = 2;
223 mutex_unlock(&priv->lock);
227 mutex_unlock(&priv->lock);
231 mutex_unlock(&priv->lock);
238 static const struct iio_info quad8_info = {
239 .read_raw = quad8_read_raw,
240 .write_raw = quad8_write_raw
243 static ssize_t quad8_read_preset(struct iio_dev *indio_dev, uintptr_t private,
244 const struct iio_chan_spec *chan, char *buf)
246 const struct quad8_iio *const priv = iio_priv(indio_dev);
248 return snprintf(buf, PAGE_SIZE, "%u\n", priv->preset[chan->channel]);
251 static ssize_t quad8_write_preset(struct iio_dev *indio_dev, uintptr_t private,
252 const struct iio_chan_spec *chan, const char *buf, size_t len)
254 struct quad8_iio *const priv = iio_priv(indio_dev);
255 const int base_offset = priv->base + 2 * chan->channel;
260 ret = kstrtouint(buf, 0, &preset);
264 /* Only 24-bit values are supported */
265 if (preset > 0xFFFFFF)
268 mutex_lock(&priv->lock);
270 priv->preset[chan->channel] = preset;
272 /* Reset Byte Pointer */
273 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
275 /* Set Preset Register */
276 for (i = 0; i < 3; i++)
277 outb(preset >> (8 * i), base_offset);
279 mutex_unlock(&priv->lock);
284 static ssize_t quad8_read_set_to_preset_on_index(struct iio_dev *indio_dev,
285 uintptr_t private, const struct iio_chan_spec *chan, char *buf)
287 const struct quad8_iio *const priv = iio_priv(indio_dev);
289 return snprintf(buf, PAGE_SIZE, "%u\n",
290 !priv->preset_enable[chan->channel]);
293 static ssize_t quad8_write_set_to_preset_on_index(struct iio_dev *indio_dev,
294 uintptr_t private, const struct iio_chan_spec *chan, const char *buf,
297 struct quad8_iio *const priv = iio_priv(indio_dev);
298 const int base_offset = priv->base + 2 * chan->channel + 1;
301 unsigned int ior_cfg;
303 ret = kstrtobool(buf, &preset_enable);
307 /* Preset enable is active low in Input/Output Control register */
308 preset_enable = !preset_enable;
310 mutex_lock(&priv->lock);
312 priv->preset_enable[chan->channel] = preset_enable;
314 ior_cfg = priv->ab_enable[chan->channel] |
315 (unsigned int)preset_enable << 1;
317 /* Load I/O control configuration to Input / Output Control Register */
318 outb(QUAD8_CTR_IOR | ior_cfg, base_offset);
320 mutex_unlock(&priv->lock);
325 static const char *const quad8_noise_error_states[] = {
326 "No excessive noise is present at the count inputs",
327 "Excessive noise is present at the count inputs"
330 static int quad8_get_noise_error(struct iio_dev *indio_dev,
331 const struct iio_chan_spec *chan)
333 struct quad8_iio *const priv = iio_priv(indio_dev);
334 const int base_offset = priv->base + 2 * chan->channel + 1;
336 return !!(inb(base_offset) & QUAD8_FLAG_E);
339 static const struct iio_enum quad8_noise_error_enum = {
340 .items = quad8_noise_error_states,
341 .num_items = ARRAY_SIZE(quad8_noise_error_states),
342 .get = quad8_get_noise_error
345 static const char *const quad8_count_direction_states[] = {
350 static int quad8_get_count_direction(struct iio_dev *indio_dev,
351 const struct iio_chan_spec *chan)
353 struct quad8_iio *const priv = iio_priv(indio_dev);
354 const int base_offset = priv->base + 2 * chan->channel + 1;
356 return !!(inb(base_offset) & QUAD8_FLAG_UD);
359 static const struct iio_enum quad8_count_direction_enum = {
360 .items = quad8_count_direction_states,
361 .num_items = ARRAY_SIZE(quad8_count_direction_states),
362 .get = quad8_get_count_direction
365 static const char *const quad8_count_modes[] = {
372 static int quad8_set_count_mode(struct iio_dev *indio_dev,
373 const struct iio_chan_spec *chan, unsigned int cnt_mode)
375 struct quad8_iio *const priv = iio_priv(indio_dev);
376 unsigned int mode_cfg = cnt_mode << 1;
377 const int base_offset = priv->base + 2 * chan->channel + 1;
379 mutex_lock(&priv->lock);
381 priv->count_mode[chan->channel] = cnt_mode;
383 /* Add quadrature mode configuration */
384 if (priv->quadrature_mode[chan->channel])
385 mode_cfg |= (priv->quadrature_scale[chan->channel] + 1) << 3;
387 /* Load mode configuration to Counter Mode Register */
388 outb(QUAD8_CTR_CMR | mode_cfg, base_offset);
390 mutex_unlock(&priv->lock);
395 static int quad8_get_count_mode(struct iio_dev *indio_dev,
396 const struct iio_chan_spec *chan)
398 const struct quad8_iio *const priv = iio_priv(indio_dev);
400 return priv->count_mode[chan->channel];
403 static const struct iio_enum quad8_count_mode_enum = {
404 .items = quad8_count_modes,
405 .num_items = ARRAY_SIZE(quad8_count_modes),
406 .set = quad8_set_count_mode,
407 .get = quad8_get_count_mode
410 static const char *const quad8_synchronous_modes[] = {
415 static int quad8_set_synchronous_mode(struct iio_dev *indio_dev,
416 const struct iio_chan_spec *chan, unsigned int synchronous_mode)
418 struct quad8_iio *const priv = iio_priv(indio_dev);
419 const int base_offset = priv->base + 2 * chan->channel + 1;
420 unsigned int idr_cfg = synchronous_mode;
422 mutex_lock(&priv->lock);
424 idr_cfg |= priv->index_polarity[chan->channel] << 1;
426 /* Index function must be non-synchronous in non-quadrature mode */
427 if (synchronous_mode && !priv->quadrature_mode[chan->channel]) {
428 mutex_unlock(&priv->lock);
432 priv->synchronous_mode[chan->channel] = synchronous_mode;
434 /* Load Index Control configuration to Index Control Register */
435 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
437 mutex_unlock(&priv->lock);
442 static int quad8_get_synchronous_mode(struct iio_dev *indio_dev,
443 const struct iio_chan_spec *chan)
445 const struct quad8_iio *const priv = iio_priv(indio_dev);
447 return priv->synchronous_mode[chan->channel];
450 static const struct iio_enum quad8_synchronous_mode_enum = {
451 .items = quad8_synchronous_modes,
452 .num_items = ARRAY_SIZE(quad8_synchronous_modes),
453 .set = quad8_set_synchronous_mode,
454 .get = quad8_get_synchronous_mode
457 static const char *const quad8_quadrature_modes[] = {
462 static int quad8_set_quadrature_mode(struct iio_dev *indio_dev,
463 const struct iio_chan_spec *chan, unsigned int quadrature_mode)
465 struct quad8_iio *const priv = iio_priv(indio_dev);
466 const int base_offset = priv->base + 2 * chan->channel + 1;
467 unsigned int mode_cfg;
469 mutex_lock(&priv->lock);
471 mode_cfg = priv->count_mode[chan->channel] << 1;
474 mode_cfg |= (priv->quadrature_scale[chan->channel] + 1) << 3;
476 /* Quadrature scaling only available in quadrature mode */
477 priv->quadrature_scale[chan->channel] = 0;
479 /* Synchronous function not supported in non-quadrature mode */
480 if (priv->synchronous_mode[chan->channel])
481 quad8_set_synchronous_mode(indio_dev, chan, 0);
484 priv->quadrature_mode[chan->channel] = quadrature_mode;
486 /* Load mode configuration to Counter Mode Register */
487 outb(QUAD8_CTR_CMR | mode_cfg, base_offset);
489 mutex_unlock(&priv->lock);
494 static int quad8_get_quadrature_mode(struct iio_dev *indio_dev,
495 const struct iio_chan_spec *chan)
497 const struct quad8_iio *const priv = iio_priv(indio_dev);
499 return priv->quadrature_mode[chan->channel];
502 static const struct iio_enum quad8_quadrature_mode_enum = {
503 .items = quad8_quadrature_modes,
504 .num_items = ARRAY_SIZE(quad8_quadrature_modes),
505 .set = quad8_set_quadrature_mode,
506 .get = quad8_get_quadrature_mode
509 static const char *const quad8_index_polarity_modes[] = {
514 static int quad8_set_index_polarity(struct iio_dev *indio_dev,
515 const struct iio_chan_spec *chan, unsigned int index_polarity)
517 struct quad8_iio *const priv = iio_priv(indio_dev);
518 const int base_offset = priv->base + 2 * chan->channel + 1;
519 unsigned int idr_cfg = index_polarity << 1;
521 mutex_lock(&priv->lock);
523 idr_cfg |= priv->synchronous_mode[chan->channel];
525 priv->index_polarity[chan->channel] = index_polarity;
527 /* Load Index Control configuration to Index Control Register */
528 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
530 mutex_unlock(&priv->lock);
535 static int quad8_get_index_polarity(struct iio_dev *indio_dev,
536 const struct iio_chan_spec *chan)
538 const struct quad8_iio *const priv = iio_priv(indio_dev);
540 return priv->index_polarity[chan->channel];
543 static const struct iio_enum quad8_index_polarity_enum = {
544 .items = quad8_index_polarity_modes,
545 .num_items = ARRAY_SIZE(quad8_index_polarity_modes),
546 .set = quad8_set_index_polarity,
547 .get = quad8_get_index_polarity
550 static const struct iio_chan_spec_ext_info quad8_count_ext_info[] = {
553 .shared = IIO_SEPARATE,
554 .read = quad8_read_preset,
555 .write = quad8_write_preset
558 .name = "set_to_preset_on_index",
559 .shared = IIO_SEPARATE,
560 .read = quad8_read_set_to_preset_on_index,
561 .write = quad8_write_set_to_preset_on_index
563 IIO_ENUM("noise_error", IIO_SEPARATE, &quad8_noise_error_enum),
564 IIO_ENUM_AVAILABLE("noise_error", &quad8_noise_error_enum),
565 IIO_ENUM("count_direction", IIO_SEPARATE, &quad8_count_direction_enum),
566 IIO_ENUM_AVAILABLE("count_direction", &quad8_count_direction_enum),
567 IIO_ENUM("count_mode", IIO_SEPARATE, &quad8_count_mode_enum),
568 IIO_ENUM_AVAILABLE("count_mode", &quad8_count_mode_enum),
569 IIO_ENUM("quadrature_mode", IIO_SEPARATE, &quad8_quadrature_mode_enum),
570 IIO_ENUM_AVAILABLE("quadrature_mode", &quad8_quadrature_mode_enum),
574 static const struct iio_chan_spec_ext_info quad8_index_ext_info[] = {
575 IIO_ENUM("synchronous_mode", IIO_SEPARATE,
576 &quad8_synchronous_mode_enum),
577 IIO_ENUM_AVAILABLE("synchronous_mode", &quad8_synchronous_mode_enum),
578 IIO_ENUM("index_polarity", IIO_SEPARATE, &quad8_index_polarity_enum),
579 IIO_ENUM_AVAILABLE("index_polarity", &quad8_index_polarity_enum),
583 #define QUAD8_COUNT_CHAN(_chan) { \
585 .channel = (_chan), \
586 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
587 BIT(IIO_CHAN_INFO_ENABLE) | BIT(IIO_CHAN_INFO_SCALE), \
588 .ext_info = quad8_count_ext_info, \
592 #define QUAD8_INDEX_CHAN(_chan) { \
594 .channel = (_chan), \
595 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
596 .ext_info = quad8_index_ext_info, \
600 static const struct iio_chan_spec quad8_channels[] = {
601 QUAD8_COUNT_CHAN(0), QUAD8_INDEX_CHAN(0),
602 QUAD8_COUNT_CHAN(1), QUAD8_INDEX_CHAN(1),
603 QUAD8_COUNT_CHAN(2), QUAD8_INDEX_CHAN(2),
604 QUAD8_COUNT_CHAN(3), QUAD8_INDEX_CHAN(3),
605 QUAD8_COUNT_CHAN(4), QUAD8_INDEX_CHAN(4),
606 QUAD8_COUNT_CHAN(5), QUAD8_INDEX_CHAN(5),
607 QUAD8_COUNT_CHAN(6), QUAD8_INDEX_CHAN(6),
608 QUAD8_COUNT_CHAN(7), QUAD8_INDEX_CHAN(7)
611 static int quad8_signal_read(struct counter_device *counter,
612 struct counter_signal *signal, enum counter_signal_value *val)
614 const struct quad8_iio *const priv = counter->priv;
617 /* Only Index signal levels can be read */
621 state = inb(priv->base + QUAD8_REG_INDEX_INPUT_LEVELS)
622 & BIT(signal->id - 16);
624 *val = (state) ? COUNTER_SIGNAL_HIGH : COUNTER_SIGNAL_LOW;
629 static int quad8_count_read(struct counter_device *counter,
630 struct counter_count *count, unsigned long *val)
632 struct quad8_iio *const priv = counter->priv;
633 const int base_offset = priv->base + 2 * count->id;
638 mutex_lock(&priv->lock);
640 /* Reset Byte Pointer; transfer Counter to Output Latch */
641 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_CNTR_OUT,
644 for (i = 0; i < 3; i++)
645 *val |= (unsigned long)inb(base_offset) << (8 * i);
647 mutex_unlock(&priv->lock);
652 static int quad8_count_write(struct counter_device *counter,
653 struct counter_count *count, unsigned long val)
655 struct quad8_iio *const priv = counter->priv;
656 const int base_offset = priv->base + 2 * count->id;
659 /* Only 24-bit values are supported */
663 mutex_lock(&priv->lock);
665 /* Reset Byte Pointer */
666 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
668 /* Counter can only be set via Preset Register */
669 for (i = 0; i < 3; i++)
670 outb(val >> (8 * i), base_offset);
672 /* Transfer Preset Register to Counter */
673 outb(QUAD8_CTR_RLD | QUAD8_RLD_PRESET_CNTR, base_offset + 1);
675 /* Reset Byte Pointer */
676 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
678 /* Set Preset Register back to original value */
679 val = priv->preset[count->id];
680 for (i = 0; i < 3; i++)
681 outb(val >> (8 * i), base_offset);
683 /* Reset Borrow, Carry, Compare, and Sign flags */
684 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_FLAGS, base_offset + 1);
685 /* Reset Error flag */
686 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_E, base_offset + 1);
688 mutex_unlock(&priv->lock);
693 enum quad8_count_function {
694 QUAD8_COUNT_FUNCTION_PULSE_DIRECTION = 0,
695 QUAD8_COUNT_FUNCTION_QUADRATURE_X1,
696 QUAD8_COUNT_FUNCTION_QUADRATURE_X2,
697 QUAD8_COUNT_FUNCTION_QUADRATURE_X4
700 static enum counter_count_function quad8_count_functions_list[] = {
701 [QUAD8_COUNT_FUNCTION_PULSE_DIRECTION] = COUNTER_COUNT_FUNCTION_PULSE_DIRECTION,
702 [QUAD8_COUNT_FUNCTION_QUADRATURE_X1] = COUNTER_COUNT_FUNCTION_QUADRATURE_X1_A,
703 [QUAD8_COUNT_FUNCTION_QUADRATURE_X2] = COUNTER_COUNT_FUNCTION_QUADRATURE_X2_A,
704 [QUAD8_COUNT_FUNCTION_QUADRATURE_X4] = COUNTER_COUNT_FUNCTION_QUADRATURE_X4
707 static int quad8_function_get(struct counter_device *counter,
708 struct counter_count *count, size_t *function)
710 struct quad8_iio *const priv = counter->priv;
711 const int id = count->id;
713 mutex_lock(&priv->lock);
715 if (priv->quadrature_mode[id])
716 switch (priv->quadrature_scale[id]) {
718 *function = QUAD8_COUNT_FUNCTION_QUADRATURE_X1;
721 *function = QUAD8_COUNT_FUNCTION_QUADRATURE_X2;
724 *function = QUAD8_COUNT_FUNCTION_QUADRATURE_X4;
728 *function = QUAD8_COUNT_FUNCTION_PULSE_DIRECTION;
730 mutex_unlock(&priv->lock);
735 static int quad8_function_set(struct counter_device *counter,
736 struct counter_count *count, size_t function)
738 struct quad8_iio *const priv = counter->priv;
739 const int id = count->id;
740 unsigned int *const quadrature_mode = priv->quadrature_mode + id;
741 unsigned int *const scale = priv->quadrature_scale + id;
742 unsigned int *const synchronous_mode = priv->synchronous_mode + id;
743 const int base_offset = priv->base + 2 * id + 1;
744 unsigned int mode_cfg;
745 unsigned int idr_cfg;
747 mutex_lock(&priv->lock);
749 mode_cfg = priv->count_mode[id] << 1;
750 idr_cfg = priv->index_polarity[id] << 1;
752 if (function == QUAD8_COUNT_FUNCTION_PULSE_DIRECTION) {
753 *quadrature_mode = 0;
755 /* Quadrature scaling only available in quadrature mode */
758 /* Synchronous function not supported in non-quadrature mode */
759 if (*synchronous_mode) {
760 *synchronous_mode = 0;
761 /* Disable synchronous function mode */
762 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
765 *quadrature_mode = 1;
768 case QUAD8_COUNT_FUNCTION_QUADRATURE_X1:
770 mode_cfg |= QUAD8_CMR_QUADRATURE_X1;
772 case QUAD8_COUNT_FUNCTION_QUADRATURE_X2:
774 mode_cfg |= QUAD8_CMR_QUADRATURE_X2;
776 case QUAD8_COUNT_FUNCTION_QUADRATURE_X4:
778 mode_cfg |= QUAD8_CMR_QUADRATURE_X4;
783 /* Load mode configuration to Counter Mode Register */
784 outb(QUAD8_CTR_CMR | mode_cfg, base_offset);
786 mutex_unlock(&priv->lock);
791 static void quad8_direction_get(struct counter_device *counter,
792 struct counter_count *count, enum counter_count_direction *direction)
794 const struct quad8_iio *const priv = counter->priv;
795 unsigned int ud_flag;
796 const unsigned int flag_addr = priv->base + 2 * count->id + 1;
798 /* U/D flag: nonzero = up, zero = down */
799 ud_flag = inb(flag_addr) & QUAD8_FLAG_UD;
801 *direction = (ud_flag) ? COUNTER_COUNT_DIRECTION_FORWARD :
802 COUNTER_COUNT_DIRECTION_BACKWARD;
805 enum quad8_synapse_action {
806 QUAD8_SYNAPSE_ACTION_NONE = 0,
807 QUAD8_SYNAPSE_ACTION_RISING_EDGE,
808 QUAD8_SYNAPSE_ACTION_FALLING_EDGE,
809 QUAD8_SYNAPSE_ACTION_BOTH_EDGES
812 static enum counter_synapse_action quad8_index_actions_list[] = {
813 [QUAD8_SYNAPSE_ACTION_NONE] = COUNTER_SYNAPSE_ACTION_NONE,
814 [QUAD8_SYNAPSE_ACTION_RISING_EDGE] = COUNTER_SYNAPSE_ACTION_RISING_EDGE
817 static enum counter_synapse_action quad8_synapse_actions_list[] = {
818 [QUAD8_SYNAPSE_ACTION_NONE] = COUNTER_SYNAPSE_ACTION_NONE,
819 [QUAD8_SYNAPSE_ACTION_RISING_EDGE] = COUNTER_SYNAPSE_ACTION_RISING_EDGE,
820 [QUAD8_SYNAPSE_ACTION_FALLING_EDGE] = COUNTER_SYNAPSE_ACTION_FALLING_EDGE,
821 [QUAD8_SYNAPSE_ACTION_BOTH_EDGES] = COUNTER_SYNAPSE_ACTION_BOTH_EDGES
824 static int quad8_action_get(struct counter_device *counter,
825 struct counter_count *count, struct counter_synapse *synapse,
828 struct quad8_iio *const priv = counter->priv;
831 const size_t signal_a_id = count->synapses[0].signal->id;
832 enum counter_count_direction direction;
834 /* Handle Index signals */
835 if (synapse->signal->id >= 16) {
836 if (priv->preset_enable[count->id])
837 *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
839 *action = QUAD8_SYNAPSE_ACTION_NONE;
844 err = quad8_function_get(counter, count, &function);
848 /* Default action mode */
849 *action = QUAD8_SYNAPSE_ACTION_NONE;
851 /* Determine action mode based on current count function mode */
853 case QUAD8_COUNT_FUNCTION_PULSE_DIRECTION:
854 if (synapse->signal->id == signal_a_id)
855 *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
857 case QUAD8_COUNT_FUNCTION_QUADRATURE_X1:
858 if (synapse->signal->id == signal_a_id) {
859 quad8_direction_get(counter, count, &direction);
861 if (direction == COUNTER_COUNT_DIRECTION_FORWARD)
862 *action = QUAD8_SYNAPSE_ACTION_RISING_EDGE;
864 *action = QUAD8_SYNAPSE_ACTION_FALLING_EDGE;
867 case QUAD8_COUNT_FUNCTION_QUADRATURE_X2:
868 if (synapse->signal->id == signal_a_id)
869 *action = QUAD8_SYNAPSE_ACTION_BOTH_EDGES;
871 case QUAD8_COUNT_FUNCTION_QUADRATURE_X4:
872 *action = QUAD8_SYNAPSE_ACTION_BOTH_EDGES;
879 static const struct counter_ops quad8_ops = {
880 .signal_read = quad8_signal_read,
881 .count_read = quad8_count_read,
882 .count_write = quad8_count_write,
883 .function_get = quad8_function_get,
884 .function_set = quad8_function_set,
885 .action_get = quad8_action_get
888 static int quad8_index_polarity_get(struct counter_device *counter,
889 struct counter_signal *signal, size_t *index_polarity)
891 const struct quad8_iio *const priv = counter->priv;
892 const size_t channel_id = signal->id - 16;
894 *index_polarity = priv->index_polarity[channel_id];
899 static int quad8_index_polarity_set(struct counter_device *counter,
900 struct counter_signal *signal, size_t index_polarity)
902 struct quad8_iio *const priv = counter->priv;
903 const size_t channel_id = signal->id - 16;
904 const int base_offset = priv->base + 2 * channel_id + 1;
905 unsigned int idr_cfg = index_polarity << 1;
907 mutex_lock(&priv->lock);
909 idr_cfg |= priv->synchronous_mode[channel_id];
911 priv->index_polarity[channel_id] = index_polarity;
913 /* Load Index Control configuration to Index Control Register */
914 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
916 mutex_unlock(&priv->lock);
921 static struct counter_signal_enum_ext quad8_index_pol_enum = {
922 .items = quad8_index_polarity_modes,
923 .num_items = ARRAY_SIZE(quad8_index_polarity_modes),
924 .get = quad8_index_polarity_get,
925 .set = quad8_index_polarity_set
928 static int quad8_synchronous_mode_get(struct counter_device *counter,
929 struct counter_signal *signal, size_t *synchronous_mode)
931 const struct quad8_iio *const priv = counter->priv;
932 const size_t channel_id = signal->id - 16;
934 *synchronous_mode = priv->synchronous_mode[channel_id];
939 static int quad8_synchronous_mode_set(struct counter_device *counter,
940 struct counter_signal *signal, size_t synchronous_mode)
942 struct quad8_iio *const priv = counter->priv;
943 const size_t channel_id = signal->id - 16;
944 const int base_offset = priv->base + 2 * channel_id + 1;
945 unsigned int idr_cfg = synchronous_mode;
947 mutex_lock(&priv->lock);
949 idr_cfg |= priv->index_polarity[channel_id] << 1;
951 /* Index function must be non-synchronous in non-quadrature mode */
952 if (synchronous_mode && !priv->quadrature_mode[channel_id]) {
953 mutex_unlock(&priv->lock);
957 priv->synchronous_mode[channel_id] = synchronous_mode;
959 /* Load Index Control configuration to Index Control Register */
960 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
962 mutex_unlock(&priv->lock);
967 static struct counter_signal_enum_ext quad8_syn_mode_enum = {
968 .items = quad8_synchronous_modes,
969 .num_items = ARRAY_SIZE(quad8_synchronous_modes),
970 .get = quad8_synchronous_mode_get,
971 .set = quad8_synchronous_mode_set
974 static ssize_t quad8_count_floor_read(struct counter_device *counter,
975 struct counter_count *count, void *private, char *buf)
977 /* Only a floor of 0 is supported */
978 return sprintf(buf, "0\n");
981 static int quad8_count_mode_get(struct counter_device *counter,
982 struct counter_count *count, size_t *cnt_mode)
984 const struct quad8_iio *const priv = counter->priv;
986 /* Map 104-QUAD-8 count mode to Generic Counter count mode */
987 switch (priv->count_mode[count->id]) {
989 *cnt_mode = COUNTER_COUNT_MODE_NORMAL;
992 *cnt_mode = COUNTER_COUNT_MODE_RANGE_LIMIT;
995 *cnt_mode = COUNTER_COUNT_MODE_NON_RECYCLE;
998 *cnt_mode = COUNTER_COUNT_MODE_MODULO_N;
1005 static int quad8_count_mode_set(struct counter_device *counter,
1006 struct counter_count *count, size_t cnt_mode)
1008 struct quad8_iio *const priv = counter->priv;
1009 unsigned int mode_cfg;
1010 const int base_offset = priv->base + 2 * count->id + 1;
1012 /* Map Generic Counter count mode to 104-QUAD-8 count mode */
1014 case COUNTER_COUNT_MODE_NORMAL:
1017 case COUNTER_COUNT_MODE_RANGE_LIMIT:
1020 case COUNTER_COUNT_MODE_NON_RECYCLE:
1023 case COUNTER_COUNT_MODE_MODULO_N:
1028 mutex_lock(&priv->lock);
1030 priv->count_mode[count->id] = cnt_mode;
1032 /* Set count mode configuration value */
1033 mode_cfg = cnt_mode << 1;
1035 /* Add quadrature mode configuration */
1036 if (priv->quadrature_mode[count->id])
1037 mode_cfg |= (priv->quadrature_scale[count->id] + 1) << 3;
1039 /* Load mode configuration to Counter Mode Register */
1040 outb(QUAD8_CTR_CMR | mode_cfg, base_offset);
1042 mutex_unlock(&priv->lock);
1047 static struct counter_count_enum_ext quad8_cnt_mode_enum = {
1048 .items = counter_count_mode_str,
1049 .num_items = ARRAY_SIZE(counter_count_mode_str),
1050 .get = quad8_count_mode_get,
1051 .set = quad8_count_mode_set
1054 static ssize_t quad8_count_direction_read(struct counter_device *counter,
1055 struct counter_count *count, void *priv, char *buf)
1057 enum counter_count_direction dir;
1059 quad8_direction_get(counter, count, &dir);
1061 return sprintf(buf, "%s\n", counter_count_direction_str[dir]);
1064 static ssize_t quad8_count_enable_read(struct counter_device *counter,
1065 struct counter_count *count, void *private, char *buf)
1067 const struct quad8_iio *const priv = counter->priv;
1069 return sprintf(buf, "%u\n", priv->ab_enable[count->id]);
1072 static ssize_t quad8_count_enable_write(struct counter_device *counter,
1073 struct counter_count *count, void *private, const char *buf, size_t len)
1075 struct quad8_iio *const priv = counter->priv;
1076 const int base_offset = priv->base + 2 * count->id;
1079 unsigned int ior_cfg;
1081 err = kstrtobool(buf, &ab_enable);
1085 mutex_lock(&priv->lock);
1087 priv->ab_enable[count->id] = ab_enable;
1089 ior_cfg = ab_enable | priv->preset_enable[count->id] << 1;
1091 /* Load I/O control configuration */
1092 outb(QUAD8_CTR_IOR | ior_cfg, base_offset + 1);
1094 mutex_unlock(&priv->lock);
1099 static int quad8_error_noise_get(struct counter_device *counter,
1100 struct counter_count *count, size_t *noise_error)
1102 const struct quad8_iio *const priv = counter->priv;
1103 const int base_offset = priv->base + 2 * count->id + 1;
1105 *noise_error = !!(inb(base_offset) & QUAD8_FLAG_E);
1110 static struct counter_count_enum_ext quad8_error_noise_enum = {
1111 .items = quad8_noise_error_states,
1112 .num_items = ARRAY_SIZE(quad8_noise_error_states),
1113 .get = quad8_error_noise_get
1116 static ssize_t quad8_count_preset_read(struct counter_device *counter,
1117 struct counter_count *count, void *private, char *buf)
1119 const struct quad8_iio *const priv = counter->priv;
1121 return sprintf(buf, "%u\n", priv->preset[count->id]);
1124 static void quad8_preset_register_set(struct quad8_iio *quad8iio, int id,
1125 unsigned int preset)
1127 const unsigned int base_offset = quad8iio->base + 2 * id;
1130 quad8iio->preset[id] = preset;
1132 /* Reset Byte Pointer */
1133 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
1135 /* Set Preset Register */
1136 for (i = 0; i < 3; i++)
1137 outb(preset >> (8 * i), base_offset);
1140 static ssize_t quad8_count_preset_write(struct counter_device *counter,
1141 struct counter_count *count, void *private, const char *buf, size_t len)
1143 struct quad8_iio *const priv = counter->priv;
1144 unsigned int preset;
1147 ret = kstrtouint(buf, 0, &preset);
1151 /* Only 24-bit values are supported */
1152 if (preset > 0xFFFFFF)
1155 mutex_lock(&priv->lock);
1157 quad8_preset_register_set(priv, count->id, preset);
1159 mutex_unlock(&priv->lock);
1164 static ssize_t quad8_count_ceiling_read(struct counter_device *counter,
1165 struct counter_count *count, void *private, char *buf)
1167 struct quad8_iio *const priv = counter->priv;
1169 mutex_lock(&priv->lock);
1171 /* Range Limit and Modulo-N count modes use preset value as ceiling */
1172 switch (priv->count_mode[count->id]) {
1175 mutex_unlock(&priv->lock);
1176 return sprintf(buf, "%u\n", priv->preset[count->id]);
1179 mutex_unlock(&priv->lock);
1181 /* By default 0xFFFFFF (24 bits unsigned) is maximum count */
1182 return sprintf(buf, "16777215\n");
1185 static ssize_t quad8_count_ceiling_write(struct counter_device *counter,
1186 struct counter_count *count, void *private, const char *buf, size_t len)
1188 struct quad8_iio *const priv = counter->priv;
1189 unsigned int ceiling;
1192 ret = kstrtouint(buf, 0, &ceiling);
1196 /* Only 24-bit values are supported */
1197 if (ceiling > 0xFFFFFF)
1200 mutex_lock(&priv->lock);
1202 /* Range Limit and Modulo-N count modes use preset value as ceiling */
1203 switch (priv->count_mode[count->id]) {
1206 quad8_preset_register_set(priv, count->id, ceiling);
1207 mutex_unlock(&priv->lock);
1211 mutex_unlock(&priv->lock);
1216 static ssize_t quad8_count_preset_enable_read(struct counter_device *counter,
1217 struct counter_count *count, void *private, char *buf)
1219 const struct quad8_iio *const priv = counter->priv;
1221 return sprintf(buf, "%u\n", !priv->preset_enable[count->id]);
1224 static ssize_t quad8_count_preset_enable_write(struct counter_device *counter,
1225 struct counter_count *count, void *private, const char *buf, size_t len)
1227 struct quad8_iio *const priv = counter->priv;
1228 const int base_offset = priv->base + 2 * count->id + 1;
1231 unsigned int ior_cfg;
1233 ret = kstrtobool(buf, &preset_enable);
1237 /* Preset enable is active low in Input/Output Control register */
1238 preset_enable = !preset_enable;
1240 mutex_lock(&priv->lock);
1242 priv->preset_enable[count->id] = preset_enable;
1244 ior_cfg = priv->ab_enable[count->id] | (unsigned int)preset_enable << 1;
1246 /* Load I/O control configuration to Input / Output Control Register */
1247 outb(QUAD8_CTR_IOR | ior_cfg, base_offset);
1249 mutex_unlock(&priv->lock);
1254 static ssize_t quad8_signal_cable_fault_read(struct counter_device *counter,
1255 struct counter_signal *signal,
1256 void *private, char *buf)
1258 struct quad8_iio *const priv = counter->priv;
1259 const size_t channel_id = signal->id / 2;
1261 unsigned int status;
1264 mutex_lock(&priv->lock);
1266 disabled = !(priv->cable_fault_enable & BIT(channel_id));
1269 mutex_unlock(&priv->lock);
1273 /* Logic 0 = cable fault */
1274 status = inb(priv->base + QUAD8_DIFF_ENCODER_CABLE_STATUS);
1276 mutex_unlock(&priv->lock);
1278 /* Mask respective channel and invert logic */
1279 fault = !(status & BIT(channel_id));
1281 return sprintf(buf, "%u\n", fault);
1284 static ssize_t quad8_signal_cable_fault_enable_read(
1285 struct counter_device *counter, struct counter_signal *signal,
1286 void *private, char *buf)
1288 const struct quad8_iio *const priv = counter->priv;
1289 const size_t channel_id = signal->id / 2;
1290 const unsigned int enb = !!(priv->cable_fault_enable & BIT(channel_id));
1292 return sprintf(buf, "%u\n", enb);
1295 static ssize_t quad8_signal_cable_fault_enable_write(
1296 struct counter_device *counter, struct counter_signal *signal,
1297 void *private, const char *buf, size_t len)
1299 struct quad8_iio *const priv = counter->priv;
1300 const size_t channel_id = signal->id / 2;
1303 unsigned int cable_fault_enable;
1305 ret = kstrtobool(buf, &enable);
1309 mutex_lock(&priv->lock);
1312 priv->cable_fault_enable |= BIT(channel_id);
1314 priv->cable_fault_enable &= ~BIT(channel_id);
1316 /* Enable is active low in Differential Encoder Cable Status register */
1317 cable_fault_enable = ~priv->cable_fault_enable;
1319 outb(cable_fault_enable, priv->base + QUAD8_DIFF_ENCODER_CABLE_STATUS);
1321 mutex_unlock(&priv->lock);
1326 static ssize_t quad8_signal_fck_prescaler_read(struct counter_device *counter,
1327 struct counter_signal *signal, void *private, char *buf)
1329 const struct quad8_iio *const priv = counter->priv;
1330 const size_t channel_id = signal->id / 2;
1332 return sprintf(buf, "%u\n", priv->fck_prescaler[channel_id]);
1335 static ssize_t quad8_signal_fck_prescaler_write(struct counter_device *counter,
1336 struct counter_signal *signal, void *private, const char *buf,
1339 struct quad8_iio *const priv = counter->priv;
1340 const size_t channel_id = signal->id / 2;
1341 const int base_offset = priv->base + 2 * channel_id;
1345 ret = kstrtou8(buf, 0, &prescaler);
1349 mutex_lock(&priv->lock);
1351 priv->fck_prescaler[channel_id] = prescaler;
1353 /* Reset Byte Pointer */
1354 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
1356 /* Set filter clock factor */
1357 outb(prescaler, base_offset);
1358 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_PRESET_PSC,
1361 mutex_unlock(&priv->lock);
1366 static const struct counter_signal_ext quad8_signal_ext[] = {
1368 .name = "cable_fault",
1369 .read = quad8_signal_cable_fault_read
1372 .name = "cable_fault_enable",
1373 .read = quad8_signal_cable_fault_enable_read,
1374 .write = quad8_signal_cable_fault_enable_write
1377 .name = "filter_clock_prescaler",
1378 .read = quad8_signal_fck_prescaler_read,
1379 .write = quad8_signal_fck_prescaler_write
1383 static const struct counter_signal_ext quad8_index_ext[] = {
1384 COUNTER_SIGNAL_ENUM("index_polarity", &quad8_index_pol_enum),
1385 COUNTER_SIGNAL_ENUM_AVAILABLE("index_polarity", &quad8_index_pol_enum),
1386 COUNTER_SIGNAL_ENUM("synchronous_mode", &quad8_syn_mode_enum),
1387 COUNTER_SIGNAL_ENUM_AVAILABLE("synchronous_mode", &quad8_syn_mode_enum)
1390 #define QUAD8_QUAD_SIGNAL(_id, _name) { \
1393 .ext = quad8_signal_ext, \
1394 .num_ext = ARRAY_SIZE(quad8_signal_ext) \
1397 #define QUAD8_INDEX_SIGNAL(_id, _name) { \
1400 .ext = quad8_index_ext, \
1401 .num_ext = ARRAY_SIZE(quad8_index_ext) \
1404 static struct counter_signal quad8_signals[] = {
1405 QUAD8_QUAD_SIGNAL(0, "Channel 1 Quadrature A"),
1406 QUAD8_QUAD_SIGNAL(1, "Channel 1 Quadrature B"),
1407 QUAD8_QUAD_SIGNAL(2, "Channel 2 Quadrature A"),
1408 QUAD8_QUAD_SIGNAL(3, "Channel 2 Quadrature B"),
1409 QUAD8_QUAD_SIGNAL(4, "Channel 3 Quadrature A"),
1410 QUAD8_QUAD_SIGNAL(5, "Channel 3 Quadrature B"),
1411 QUAD8_QUAD_SIGNAL(6, "Channel 4 Quadrature A"),
1412 QUAD8_QUAD_SIGNAL(7, "Channel 4 Quadrature B"),
1413 QUAD8_QUAD_SIGNAL(8, "Channel 5 Quadrature A"),
1414 QUAD8_QUAD_SIGNAL(9, "Channel 5 Quadrature B"),
1415 QUAD8_QUAD_SIGNAL(10, "Channel 6 Quadrature A"),
1416 QUAD8_QUAD_SIGNAL(11, "Channel 6 Quadrature B"),
1417 QUAD8_QUAD_SIGNAL(12, "Channel 7 Quadrature A"),
1418 QUAD8_QUAD_SIGNAL(13, "Channel 7 Quadrature B"),
1419 QUAD8_QUAD_SIGNAL(14, "Channel 8 Quadrature A"),
1420 QUAD8_QUAD_SIGNAL(15, "Channel 8 Quadrature B"),
1421 QUAD8_INDEX_SIGNAL(16, "Channel 1 Index"),
1422 QUAD8_INDEX_SIGNAL(17, "Channel 2 Index"),
1423 QUAD8_INDEX_SIGNAL(18, "Channel 3 Index"),
1424 QUAD8_INDEX_SIGNAL(19, "Channel 4 Index"),
1425 QUAD8_INDEX_SIGNAL(20, "Channel 5 Index"),
1426 QUAD8_INDEX_SIGNAL(21, "Channel 6 Index"),
1427 QUAD8_INDEX_SIGNAL(22, "Channel 7 Index"),
1428 QUAD8_INDEX_SIGNAL(23, "Channel 8 Index")
1431 #define QUAD8_COUNT_SYNAPSES(_id) { \
1433 .actions_list = quad8_synapse_actions_list, \
1434 .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
1435 .signal = quad8_signals + 2 * (_id) \
1438 .actions_list = quad8_synapse_actions_list, \
1439 .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
1440 .signal = quad8_signals + 2 * (_id) + 1 \
1443 .actions_list = quad8_index_actions_list, \
1444 .num_actions = ARRAY_SIZE(quad8_index_actions_list), \
1445 .signal = quad8_signals + 2 * (_id) + 16 \
1449 static struct counter_synapse quad8_count_synapses[][3] = {
1450 QUAD8_COUNT_SYNAPSES(0), QUAD8_COUNT_SYNAPSES(1),
1451 QUAD8_COUNT_SYNAPSES(2), QUAD8_COUNT_SYNAPSES(3),
1452 QUAD8_COUNT_SYNAPSES(4), QUAD8_COUNT_SYNAPSES(5),
1453 QUAD8_COUNT_SYNAPSES(6), QUAD8_COUNT_SYNAPSES(7)
1456 static const struct counter_count_ext quad8_count_ext[] = {
1459 .read = quad8_count_ceiling_read,
1460 .write = quad8_count_ceiling_write
1464 .read = quad8_count_floor_read
1466 COUNTER_COUNT_ENUM("count_mode", &quad8_cnt_mode_enum),
1467 COUNTER_COUNT_ENUM_AVAILABLE("count_mode", &quad8_cnt_mode_enum),
1469 .name = "direction",
1470 .read = quad8_count_direction_read
1474 .read = quad8_count_enable_read,
1475 .write = quad8_count_enable_write
1477 COUNTER_COUNT_ENUM("error_noise", &quad8_error_noise_enum),
1478 COUNTER_COUNT_ENUM_AVAILABLE("error_noise", &quad8_error_noise_enum),
1481 .read = quad8_count_preset_read,
1482 .write = quad8_count_preset_write
1485 .name = "preset_enable",
1486 .read = quad8_count_preset_enable_read,
1487 .write = quad8_count_preset_enable_write
1491 #define QUAD8_COUNT(_id, _cntname) { \
1493 .name = (_cntname), \
1494 .functions_list = quad8_count_functions_list, \
1495 .num_functions = ARRAY_SIZE(quad8_count_functions_list), \
1496 .synapses = quad8_count_synapses[(_id)], \
1497 .num_synapses = 2, \
1498 .ext = quad8_count_ext, \
1499 .num_ext = ARRAY_SIZE(quad8_count_ext) \
1502 static struct counter_count quad8_counts[] = {
1503 QUAD8_COUNT(0, "Channel 1 Count"),
1504 QUAD8_COUNT(1, "Channel 2 Count"),
1505 QUAD8_COUNT(2, "Channel 3 Count"),
1506 QUAD8_COUNT(3, "Channel 4 Count"),
1507 QUAD8_COUNT(4, "Channel 5 Count"),
1508 QUAD8_COUNT(5, "Channel 6 Count"),
1509 QUAD8_COUNT(6, "Channel 7 Count"),
1510 QUAD8_COUNT(7, "Channel 8 Count")
1513 static int quad8_probe(struct device *dev, unsigned int id)
1515 struct iio_dev *indio_dev;
1516 struct quad8_iio *quad8iio;
1518 unsigned int base_offset;
1521 if (!devm_request_region(dev, base[id], QUAD8_EXTENT, dev_name(dev))) {
1522 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
1523 base[id], base[id] + QUAD8_EXTENT);
1527 /* Allocate IIO device; this also allocates driver data structure */
1528 indio_dev = devm_iio_device_alloc(dev, sizeof(*quad8iio));
1532 /* Initialize IIO device */
1533 indio_dev->info = &quad8_info;
1534 indio_dev->modes = INDIO_DIRECT_MODE;
1535 indio_dev->num_channels = ARRAY_SIZE(quad8_channels);
1536 indio_dev->channels = quad8_channels;
1537 indio_dev->name = dev_name(dev);
1539 /* Initialize Counter device and driver data */
1540 quad8iio = iio_priv(indio_dev);
1541 quad8iio->counter.name = dev_name(dev);
1542 quad8iio->counter.parent = dev;
1543 quad8iio->counter.ops = &quad8_ops;
1544 quad8iio->counter.counts = quad8_counts;
1545 quad8iio->counter.num_counts = ARRAY_SIZE(quad8_counts);
1546 quad8iio->counter.signals = quad8_signals;
1547 quad8iio->counter.num_signals = ARRAY_SIZE(quad8_signals);
1548 quad8iio->counter.priv = quad8iio;
1549 quad8iio->base = base[id];
1551 /* Initialize mutex */
1552 mutex_init(&quad8iio->lock);
1554 /* Reset all counters and disable interrupt function */
1555 outb(QUAD8_CHAN_OP_RESET_COUNTERS, base[id] + QUAD8_REG_CHAN_OP);
1556 /* Set initial configuration for all counters */
1557 for (i = 0; i < QUAD8_NUM_COUNTERS; i++) {
1558 base_offset = base[id] + 2 * i;
1559 /* Reset Byte Pointer */
1560 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
1561 /* Reset filter clock factor */
1562 outb(0, base_offset);
1563 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_PRESET_PSC,
1565 /* Reset Byte Pointer */
1566 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
1567 /* Reset Preset Register */
1568 for (j = 0; j < 3; j++)
1569 outb(0x00, base_offset);
1570 /* Reset Borrow, Carry, Compare, and Sign flags */
1571 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_FLAGS, base_offset + 1);
1572 /* Reset Error flag */
1573 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_E, base_offset + 1);
1574 /* Binary encoding; Normal count; non-quadrature mode */
1575 outb(QUAD8_CTR_CMR, base_offset + 1);
1576 /* Disable A and B inputs; preset on index; FLG1 as Carry */
1577 outb(QUAD8_CTR_IOR, base_offset + 1);
1578 /* Disable index function; negative index polarity */
1579 outb(QUAD8_CTR_IDR, base_offset + 1);
1581 /* Disable Differential Encoder Cable Status for all channels */
1582 outb(0xFF, base[id] + QUAD8_DIFF_ENCODER_CABLE_STATUS);
1583 /* Enable all counters */
1584 outb(QUAD8_CHAN_OP_ENABLE_COUNTERS, base[id] + QUAD8_REG_CHAN_OP);
1586 /* Register IIO device */
1587 err = devm_iio_device_register(dev, indio_dev);
1591 /* Register Counter device */
1592 return devm_counter_register(dev, &quad8iio->counter);
1595 static struct isa_driver quad8_driver = {
1596 .probe = quad8_probe,
1598 .name = "104-quad-8"
1602 module_isa_driver(quad8_driver, num_quad8);
1604 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
1605 MODULE_DESCRIPTION("ACCES 104-QUAD-8 IIO driver");
1606 MODULE_LICENSE("GPL v2");