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>
13 #include <linux/ioport.h>
14 #include <linux/interrupt.h>
15 #include <linux/isa.h>
16 #include <linux/kernel.h>
17 #include <linux/list.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/types.h>
21 #include <linux/spinlock.h>
23 #define QUAD8_EXTENT 32
25 static unsigned int base[max_num_isa_dev(QUAD8_EXTENT)];
26 static unsigned int num_quad8;
27 module_param_hw_array(base, uint, ioport, &num_quad8, 0);
28 MODULE_PARM_DESC(base, "ACCES 104-QUAD-8 base addresses");
30 static unsigned int irq[max_num_isa_dev(QUAD8_EXTENT)];
31 module_param_hw_array(irq, uint, irq, NULL, 0);
32 MODULE_PARM_DESC(irq, "ACCES 104-QUAD-8 interrupt line numbers");
34 #define QUAD8_NUM_COUNTERS 8
37 * struct quad8 - device private data structure
38 * @lock: lock to prevent clobbering device states during R/W ops
39 * @counter: instance of the counter_device
40 * @fck_prescaler: array of filter clock prescaler configurations
41 * @preset: array of preset values
42 * @count_mode: array of count mode configurations
43 * @quadrature_mode: array of quadrature mode configurations
44 * @quadrature_scale: array of quadrature mode scale configurations
45 * @ab_enable: array of A and B inputs enable configurations
46 * @preset_enable: array of set_to_preset_on_index attribute configurations
47 * @irq_trigger: array of current IRQ trigger function configurations
48 * @synchronous_mode: array of index function synchronous mode configurations
49 * @index_polarity: array of index function polarity configurations
50 * @cable_fault_enable: differential encoder cable status enable configurations
51 * @base: base port address of the device
55 unsigned int fck_prescaler[QUAD8_NUM_COUNTERS];
56 unsigned int preset[QUAD8_NUM_COUNTERS];
57 unsigned int count_mode[QUAD8_NUM_COUNTERS];
58 unsigned int quadrature_mode[QUAD8_NUM_COUNTERS];
59 unsigned int quadrature_scale[QUAD8_NUM_COUNTERS];
60 unsigned int ab_enable[QUAD8_NUM_COUNTERS];
61 unsigned int preset_enable[QUAD8_NUM_COUNTERS];
62 unsigned int irq_trigger[QUAD8_NUM_COUNTERS];
63 unsigned int synchronous_mode[QUAD8_NUM_COUNTERS];
64 unsigned int index_polarity[QUAD8_NUM_COUNTERS];
65 unsigned int cable_fault_enable;
69 #define QUAD8_REG_INTERRUPT_STATUS 0x10
70 #define QUAD8_REG_CHAN_OP 0x11
71 #define QUAD8_REG_INDEX_INTERRUPT 0x12
72 #define QUAD8_REG_INDEX_INPUT_LEVELS 0x16
73 #define QUAD8_DIFF_ENCODER_CABLE_STATUS 0x17
74 /* Borrow Toggle flip-flop */
75 #define QUAD8_FLAG_BT BIT(0)
76 /* Carry Toggle flip-flop */
77 #define QUAD8_FLAG_CT BIT(1)
79 #define QUAD8_FLAG_E BIT(4)
81 #define QUAD8_FLAG_UD BIT(5)
82 /* Reset and Load Signal Decoders */
83 #define QUAD8_CTR_RLD 0x00
84 /* Counter Mode Register */
85 #define QUAD8_CTR_CMR 0x20
86 /* Input / Output Control Register */
87 #define QUAD8_CTR_IOR 0x40
88 /* Index Control Register */
89 #define QUAD8_CTR_IDR 0x60
90 /* Reset Byte Pointer (three byte data pointer) */
91 #define QUAD8_RLD_RESET_BP 0x01
93 #define QUAD8_RLD_RESET_CNTR 0x02
94 /* Reset Borrow Toggle, Carry Toggle, Compare Toggle, and Sign flags */
95 #define QUAD8_RLD_RESET_FLAGS 0x04
96 /* Reset Error flag */
97 #define QUAD8_RLD_RESET_E 0x06
98 /* Preset Register to Counter */
99 #define QUAD8_RLD_PRESET_CNTR 0x08
100 /* Transfer Counter to Output Latch */
101 #define QUAD8_RLD_CNTR_OUT 0x10
102 /* Transfer Preset Register LSB to FCK Prescaler */
103 #define QUAD8_RLD_PRESET_PSC 0x18
104 #define QUAD8_CHAN_OP_RESET_COUNTERS 0x01
105 #define QUAD8_CHAN_OP_ENABLE_INTERRUPT_FUNC 0x04
106 #define QUAD8_CMR_QUADRATURE_X1 0x08
107 #define QUAD8_CMR_QUADRATURE_X2 0x10
108 #define QUAD8_CMR_QUADRATURE_X4 0x18
110 static int quad8_signal_read(struct counter_device *counter,
111 struct counter_signal *signal,
112 enum counter_signal_level *level)
114 const struct quad8 *const priv = counter_priv(counter);
117 /* Only Index signal levels can be read */
121 state = inb(priv->base + QUAD8_REG_INDEX_INPUT_LEVELS)
122 & BIT(signal->id - 16);
124 *level = (state) ? COUNTER_SIGNAL_LEVEL_HIGH : COUNTER_SIGNAL_LEVEL_LOW;
129 static int quad8_count_read(struct counter_device *counter,
130 struct counter_count *count, u64 *val)
132 struct quad8 *const priv = counter_priv(counter);
133 const int base_offset = priv->base + 2 * count->id;
137 unsigned long irqflags;
140 flags = inb(base_offset + 1);
141 borrow = flags & QUAD8_FLAG_BT;
142 carry = !!(flags & QUAD8_FLAG_CT);
144 /* Borrow XOR Carry effectively doubles count range */
145 *val = (unsigned long)(borrow ^ carry) << 24;
147 spin_lock_irqsave(&priv->lock, irqflags);
149 /* Reset Byte Pointer; transfer Counter to Output Latch */
150 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_CNTR_OUT,
153 for (i = 0; i < 3; i++)
154 *val |= (unsigned long)inb(base_offset) << (8 * i);
156 spin_unlock_irqrestore(&priv->lock, irqflags);
161 static int quad8_count_write(struct counter_device *counter,
162 struct counter_count *count, u64 val)
164 struct quad8 *const priv = counter_priv(counter);
165 const int base_offset = priv->base + 2 * count->id;
166 unsigned long irqflags;
169 /* Only 24-bit values are supported */
173 spin_lock_irqsave(&priv->lock, irqflags);
175 /* Reset Byte Pointer */
176 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
178 /* Counter can only be set via Preset Register */
179 for (i = 0; i < 3; i++)
180 outb(val >> (8 * i), base_offset);
182 /* Transfer Preset Register to Counter */
183 outb(QUAD8_CTR_RLD | QUAD8_RLD_PRESET_CNTR, base_offset + 1);
185 /* Reset Byte Pointer */
186 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
188 /* Set Preset Register back to original value */
189 val = priv->preset[count->id];
190 for (i = 0; i < 3; i++)
191 outb(val >> (8 * i), base_offset);
193 /* Reset Borrow, Carry, Compare, and Sign flags */
194 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_FLAGS, base_offset + 1);
195 /* Reset Error flag */
196 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_E, base_offset + 1);
198 spin_unlock_irqrestore(&priv->lock, irqflags);
203 static const enum counter_function quad8_count_functions_list[] = {
204 COUNTER_FUNCTION_PULSE_DIRECTION,
205 COUNTER_FUNCTION_QUADRATURE_X1_A,
206 COUNTER_FUNCTION_QUADRATURE_X2_A,
207 COUNTER_FUNCTION_QUADRATURE_X4,
210 static int quad8_function_read(struct counter_device *counter,
211 struct counter_count *count,
212 enum counter_function *function)
214 struct quad8 *const priv = counter_priv(counter);
215 const int id = count->id;
216 unsigned long irqflags;
218 spin_lock_irqsave(&priv->lock, irqflags);
220 if (priv->quadrature_mode[id])
221 switch (priv->quadrature_scale[id]) {
223 *function = COUNTER_FUNCTION_QUADRATURE_X1_A;
226 *function = COUNTER_FUNCTION_QUADRATURE_X2_A;
229 *function = COUNTER_FUNCTION_QUADRATURE_X4;
233 *function = COUNTER_FUNCTION_PULSE_DIRECTION;
235 spin_unlock_irqrestore(&priv->lock, irqflags);
240 static int quad8_function_write(struct counter_device *counter,
241 struct counter_count *count,
242 enum counter_function function)
244 struct quad8 *const priv = counter_priv(counter);
245 const int id = count->id;
246 unsigned int *const quadrature_mode = priv->quadrature_mode + id;
247 unsigned int *const scale = priv->quadrature_scale + id;
248 unsigned int *const synchronous_mode = priv->synchronous_mode + id;
249 const int base_offset = priv->base + 2 * id + 1;
250 unsigned long irqflags;
251 unsigned int mode_cfg;
252 unsigned int idr_cfg;
254 spin_lock_irqsave(&priv->lock, irqflags);
256 mode_cfg = priv->count_mode[id] << 1;
257 idr_cfg = priv->index_polarity[id] << 1;
259 if (function == COUNTER_FUNCTION_PULSE_DIRECTION) {
260 *quadrature_mode = 0;
262 /* Quadrature scaling only available in quadrature mode */
265 /* Synchronous function not supported in non-quadrature mode */
266 if (*synchronous_mode) {
267 *synchronous_mode = 0;
268 /* Disable synchronous function mode */
269 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
272 *quadrature_mode = 1;
275 case COUNTER_FUNCTION_QUADRATURE_X1_A:
277 mode_cfg |= QUAD8_CMR_QUADRATURE_X1;
279 case COUNTER_FUNCTION_QUADRATURE_X2_A:
281 mode_cfg |= QUAD8_CMR_QUADRATURE_X2;
283 case COUNTER_FUNCTION_QUADRATURE_X4:
285 mode_cfg |= QUAD8_CMR_QUADRATURE_X4;
288 /* should never reach this path */
289 spin_unlock_irqrestore(&priv->lock, irqflags);
294 /* Load mode configuration to Counter Mode Register */
295 outb(QUAD8_CTR_CMR | mode_cfg, base_offset);
297 spin_unlock_irqrestore(&priv->lock, irqflags);
302 static int quad8_direction_read(struct counter_device *counter,
303 struct counter_count *count,
304 enum counter_count_direction *direction)
306 const struct quad8 *const priv = counter_priv(counter);
307 unsigned int ud_flag;
308 const unsigned int flag_addr = priv->base + 2 * count->id + 1;
310 /* U/D flag: nonzero = up, zero = down */
311 ud_flag = inb(flag_addr) & QUAD8_FLAG_UD;
313 *direction = (ud_flag) ? COUNTER_COUNT_DIRECTION_FORWARD :
314 COUNTER_COUNT_DIRECTION_BACKWARD;
319 static const enum counter_synapse_action quad8_index_actions_list[] = {
320 COUNTER_SYNAPSE_ACTION_NONE,
321 COUNTER_SYNAPSE_ACTION_RISING_EDGE,
324 static const enum counter_synapse_action quad8_synapse_actions_list[] = {
325 COUNTER_SYNAPSE_ACTION_NONE,
326 COUNTER_SYNAPSE_ACTION_RISING_EDGE,
327 COUNTER_SYNAPSE_ACTION_FALLING_EDGE,
328 COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
331 static int quad8_action_read(struct counter_device *counter,
332 struct counter_count *count,
333 struct counter_synapse *synapse,
334 enum counter_synapse_action *action)
336 struct quad8 *const priv = counter_priv(counter);
338 enum counter_function function;
339 const size_t signal_a_id = count->synapses[0].signal->id;
340 enum counter_count_direction direction;
342 /* Handle Index signals */
343 if (synapse->signal->id >= 16) {
344 if (priv->preset_enable[count->id])
345 *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
347 *action = COUNTER_SYNAPSE_ACTION_NONE;
352 err = quad8_function_read(counter, count, &function);
356 /* Default action mode */
357 *action = COUNTER_SYNAPSE_ACTION_NONE;
359 /* Determine action mode based on current count function mode */
361 case COUNTER_FUNCTION_PULSE_DIRECTION:
362 if (synapse->signal->id == signal_a_id)
363 *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
365 case COUNTER_FUNCTION_QUADRATURE_X1_A:
366 if (synapse->signal->id == signal_a_id) {
367 err = quad8_direction_read(counter, count, &direction);
371 if (direction == COUNTER_COUNT_DIRECTION_FORWARD)
372 *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
374 *action = COUNTER_SYNAPSE_ACTION_FALLING_EDGE;
377 case COUNTER_FUNCTION_QUADRATURE_X2_A:
378 if (synapse->signal->id == signal_a_id)
379 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
381 case COUNTER_FUNCTION_QUADRATURE_X4:
382 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
385 /* should never reach this path */
391 QUAD8_EVENT_CARRY = 0,
392 QUAD8_EVENT_COMPARE = 1,
393 QUAD8_EVENT_CARRY_BORROW = 2,
394 QUAD8_EVENT_INDEX = 3,
397 static int quad8_events_configure(struct counter_device *counter)
399 struct quad8 *const priv = counter_priv(counter);
400 unsigned long irq_enabled = 0;
401 unsigned long irqflags;
402 struct counter_event_node *event_node;
403 unsigned int next_irq_trigger;
404 unsigned long ior_cfg;
405 unsigned long base_offset;
407 spin_lock_irqsave(&priv->lock, irqflags);
409 list_for_each_entry(event_node, &counter->events_list, l) {
410 switch (event_node->event) {
411 case COUNTER_EVENT_OVERFLOW:
412 next_irq_trigger = QUAD8_EVENT_CARRY;
414 case COUNTER_EVENT_THRESHOLD:
415 next_irq_trigger = QUAD8_EVENT_COMPARE;
417 case COUNTER_EVENT_OVERFLOW_UNDERFLOW:
418 next_irq_trigger = QUAD8_EVENT_CARRY_BORROW;
420 case COUNTER_EVENT_INDEX:
421 next_irq_trigger = QUAD8_EVENT_INDEX;
424 /* should never reach this path */
425 spin_unlock_irqrestore(&priv->lock, irqflags);
429 /* Skip configuration if it is the same as previously set */
430 if (priv->irq_trigger[event_node->channel] == next_irq_trigger)
433 /* Save new IRQ function configuration */
434 priv->irq_trigger[event_node->channel] = next_irq_trigger;
436 /* Load configuration to I/O Control Register */
437 ior_cfg = priv->ab_enable[event_node->channel] |
438 priv->preset_enable[event_node->channel] << 1 |
439 priv->irq_trigger[event_node->channel] << 3;
440 base_offset = priv->base + 2 * event_node->channel + 1;
441 outb(QUAD8_CTR_IOR | ior_cfg, base_offset);
443 /* Enable IRQ line */
444 irq_enabled |= BIT(event_node->channel);
447 outb(irq_enabled, priv->base + QUAD8_REG_INDEX_INTERRUPT);
449 spin_unlock_irqrestore(&priv->lock, irqflags);
454 static int quad8_watch_validate(struct counter_device *counter,
455 const struct counter_watch *watch)
457 struct counter_event_node *event_node;
459 if (watch->channel > QUAD8_NUM_COUNTERS - 1)
462 switch (watch->event) {
463 case COUNTER_EVENT_OVERFLOW:
464 case COUNTER_EVENT_THRESHOLD:
465 case COUNTER_EVENT_OVERFLOW_UNDERFLOW:
466 case COUNTER_EVENT_INDEX:
467 list_for_each_entry(event_node, &counter->next_events_list, l)
468 if (watch->channel == event_node->channel &&
469 watch->event != event_node->event)
477 static const struct counter_ops quad8_ops = {
478 .signal_read = quad8_signal_read,
479 .count_read = quad8_count_read,
480 .count_write = quad8_count_write,
481 .function_read = quad8_function_read,
482 .function_write = quad8_function_write,
483 .action_read = quad8_action_read,
484 .events_configure = quad8_events_configure,
485 .watch_validate = quad8_watch_validate,
488 static const char *const quad8_index_polarity_modes[] = {
493 static int quad8_index_polarity_get(struct counter_device *counter,
494 struct counter_signal *signal,
497 const struct quad8 *const priv = counter_priv(counter);
498 const size_t channel_id = signal->id - 16;
500 *index_polarity = priv->index_polarity[channel_id];
505 static int quad8_index_polarity_set(struct counter_device *counter,
506 struct counter_signal *signal,
509 struct quad8 *const priv = counter_priv(counter);
510 const size_t channel_id = signal->id - 16;
511 const int base_offset = priv->base + 2 * channel_id + 1;
512 unsigned long irqflags;
513 unsigned int idr_cfg = index_polarity << 1;
515 spin_lock_irqsave(&priv->lock, irqflags);
517 idr_cfg |= priv->synchronous_mode[channel_id];
519 priv->index_polarity[channel_id] = index_polarity;
521 /* Load Index Control configuration to Index Control Register */
522 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
524 spin_unlock_irqrestore(&priv->lock, irqflags);
529 static const char *const quad8_synchronous_modes[] = {
534 static int quad8_synchronous_mode_get(struct counter_device *counter,
535 struct counter_signal *signal,
536 u32 *synchronous_mode)
538 const struct quad8 *const priv = counter_priv(counter);
539 const size_t channel_id = signal->id - 16;
541 *synchronous_mode = priv->synchronous_mode[channel_id];
546 static int quad8_synchronous_mode_set(struct counter_device *counter,
547 struct counter_signal *signal,
548 u32 synchronous_mode)
550 struct quad8 *const priv = counter_priv(counter);
551 const size_t channel_id = signal->id - 16;
552 const int base_offset = priv->base + 2 * channel_id + 1;
553 unsigned long irqflags;
554 unsigned int idr_cfg = synchronous_mode;
556 spin_lock_irqsave(&priv->lock, irqflags);
558 idr_cfg |= priv->index_polarity[channel_id] << 1;
560 /* Index function must be non-synchronous in non-quadrature mode */
561 if (synchronous_mode && !priv->quadrature_mode[channel_id]) {
562 spin_unlock_irqrestore(&priv->lock, irqflags);
566 priv->synchronous_mode[channel_id] = synchronous_mode;
568 /* Load Index Control configuration to Index Control Register */
569 outb(QUAD8_CTR_IDR | idr_cfg, base_offset);
571 spin_unlock_irqrestore(&priv->lock, irqflags);
576 static int quad8_count_floor_read(struct counter_device *counter,
577 struct counter_count *count, u64 *floor)
579 /* Only a floor of 0 is supported */
585 static int quad8_count_mode_read(struct counter_device *counter,
586 struct counter_count *count,
587 enum counter_count_mode *cnt_mode)
589 const struct quad8 *const priv = counter_priv(counter);
591 /* Map 104-QUAD-8 count mode to Generic Counter count mode */
592 switch (priv->count_mode[count->id]) {
594 *cnt_mode = COUNTER_COUNT_MODE_NORMAL;
597 *cnt_mode = COUNTER_COUNT_MODE_RANGE_LIMIT;
600 *cnt_mode = COUNTER_COUNT_MODE_NON_RECYCLE;
603 *cnt_mode = COUNTER_COUNT_MODE_MODULO_N;
610 static int quad8_count_mode_write(struct counter_device *counter,
611 struct counter_count *count,
612 enum counter_count_mode cnt_mode)
614 struct quad8 *const priv = counter_priv(counter);
615 unsigned int count_mode;
616 unsigned int mode_cfg;
617 const int base_offset = priv->base + 2 * count->id + 1;
618 unsigned long irqflags;
620 /* Map Generic Counter count mode to 104-QUAD-8 count mode */
622 case COUNTER_COUNT_MODE_NORMAL:
625 case COUNTER_COUNT_MODE_RANGE_LIMIT:
628 case COUNTER_COUNT_MODE_NON_RECYCLE:
631 case COUNTER_COUNT_MODE_MODULO_N:
635 /* should never reach this path */
639 spin_lock_irqsave(&priv->lock, irqflags);
641 priv->count_mode[count->id] = count_mode;
643 /* Set count mode configuration value */
644 mode_cfg = count_mode << 1;
646 /* Add quadrature mode configuration */
647 if (priv->quadrature_mode[count->id])
648 mode_cfg |= (priv->quadrature_scale[count->id] + 1) << 3;
650 /* Load mode configuration to Counter Mode Register */
651 outb(QUAD8_CTR_CMR | mode_cfg, base_offset);
653 spin_unlock_irqrestore(&priv->lock, irqflags);
658 static int quad8_count_enable_read(struct counter_device *counter,
659 struct counter_count *count, u8 *enable)
661 const struct quad8 *const priv = counter_priv(counter);
663 *enable = priv->ab_enable[count->id];
668 static int quad8_count_enable_write(struct counter_device *counter,
669 struct counter_count *count, u8 enable)
671 struct quad8 *const priv = counter_priv(counter);
672 const int base_offset = priv->base + 2 * count->id;
673 unsigned long irqflags;
674 unsigned int ior_cfg;
676 spin_lock_irqsave(&priv->lock, irqflags);
678 priv->ab_enable[count->id] = enable;
680 ior_cfg = enable | priv->preset_enable[count->id] << 1 |
681 priv->irq_trigger[count->id] << 3;
683 /* Load I/O control configuration */
684 outb(QUAD8_CTR_IOR | ior_cfg, base_offset + 1);
686 spin_unlock_irqrestore(&priv->lock, irqflags);
691 static const char *const quad8_noise_error_states[] = {
692 "No excessive noise is present at the count inputs",
693 "Excessive noise is present at the count inputs"
696 static int quad8_error_noise_get(struct counter_device *counter,
697 struct counter_count *count, u32 *noise_error)
699 const struct quad8 *const priv = counter_priv(counter);
700 const int base_offset = priv->base + 2 * count->id + 1;
702 *noise_error = !!(inb(base_offset) & QUAD8_FLAG_E);
707 static int quad8_count_preset_read(struct counter_device *counter,
708 struct counter_count *count, u64 *preset)
710 const struct quad8 *const priv = counter_priv(counter);
712 *preset = priv->preset[count->id];
717 static void quad8_preset_register_set(struct quad8 *const priv, const int id,
718 const unsigned int preset)
720 const unsigned int base_offset = priv->base + 2 * id;
723 priv->preset[id] = preset;
725 /* Reset Byte Pointer */
726 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
728 /* Set Preset Register */
729 for (i = 0; i < 3; i++)
730 outb(preset >> (8 * i), base_offset);
733 static int quad8_count_preset_write(struct counter_device *counter,
734 struct counter_count *count, u64 preset)
736 struct quad8 *const priv = counter_priv(counter);
737 unsigned long irqflags;
739 /* Only 24-bit values are supported */
740 if (preset > 0xFFFFFF)
743 spin_lock_irqsave(&priv->lock, irqflags);
745 quad8_preset_register_set(priv, count->id, preset);
747 spin_unlock_irqrestore(&priv->lock, irqflags);
752 static int quad8_count_ceiling_read(struct counter_device *counter,
753 struct counter_count *count, u64 *ceiling)
755 struct quad8 *const priv = counter_priv(counter);
756 unsigned long irqflags;
758 spin_lock_irqsave(&priv->lock, irqflags);
760 /* Range Limit and Modulo-N count modes use preset value as ceiling */
761 switch (priv->count_mode[count->id]) {
764 *ceiling = priv->preset[count->id];
767 /* By default 0x1FFFFFF (25 bits unsigned) is maximum count */
768 *ceiling = 0x1FFFFFF;
772 spin_unlock_irqrestore(&priv->lock, irqflags);
777 static int quad8_count_ceiling_write(struct counter_device *counter,
778 struct counter_count *count, u64 ceiling)
780 struct quad8 *const priv = counter_priv(counter);
781 unsigned long irqflags;
783 /* Only 24-bit values are supported */
784 if (ceiling > 0xFFFFFF)
787 spin_lock_irqsave(&priv->lock, irqflags);
789 /* Range Limit and Modulo-N count modes use preset value as ceiling */
790 switch (priv->count_mode[count->id]) {
793 quad8_preset_register_set(priv, count->id, ceiling);
794 spin_unlock_irqrestore(&priv->lock, irqflags);
798 spin_unlock_irqrestore(&priv->lock, irqflags);
803 static int quad8_count_preset_enable_read(struct counter_device *counter,
804 struct counter_count *count,
807 const struct quad8 *const priv = counter_priv(counter);
809 *preset_enable = !priv->preset_enable[count->id];
814 static int quad8_count_preset_enable_write(struct counter_device *counter,
815 struct counter_count *count,
818 struct quad8 *const priv = counter_priv(counter);
819 const int base_offset = priv->base + 2 * count->id + 1;
820 unsigned long irqflags;
821 unsigned int ior_cfg;
823 /* Preset enable is active low in Input/Output Control register */
824 preset_enable = !preset_enable;
826 spin_lock_irqsave(&priv->lock, irqflags);
828 priv->preset_enable[count->id] = preset_enable;
830 ior_cfg = priv->ab_enable[count->id] | preset_enable << 1 |
831 priv->irq_trigger[count->id] << 3;
833 /* Load I/O control configuration to Input / Output Control Register */
834 outb(QUAD8_CTR_IOR | ior_cfg, base_offset);
836 spin_unlock_irqrestore(&priv->lock, irqflags);
841 static int quad8_signal_cable_fault_read(struct counter_device *counter,
842 struct counter_signal *signal,
845 struct quad8 *const priv = counter_priv(counter);
846 const size_t channel_id = signal->id / 2;
847 unsigned long irqflags;
851 spin_lock_irqsave(&priv->lock, irqflags);
853 disabled = !(priv->cable_fault_enable & BIT(channel_id));
856 spin_unlock_irqrestore(&priv->lock, irqflags);
860 /* Logic 0 = cable fault */
861 status = inb(priv->base + QUAD8_DIFF_ENCODER_CABLE_STATUS);
863 spin_unlock_irqrestore(&priv->lock, irqflags);
865 /* Mask respective channel and invert logic */
866 *cable_fault = !(status & BIT(channel_id));
871 static int quad8_signal_cable_fault_enable_read(struct counter_device *counter,
872 struct counter_signal *signal,
875 const struct quad8 *const priv = counter_priv(counter);
876 const size_t channel_id = signal->id / 2;
878 *enable = !!(priv->cable_fault_enable & BIT(channel_id));
883 static int quad8_signal_cable_fault_enable_write(struct counter_device *counter,
884 struct counter_signal *signal,
887 struct quad8 *const priv = counter_priv(counter);
888 const size_t channel_id = signal->id / 2;
889 unsigned long irqflags;
890 unsigned int cable_fault_enable;
892 spin_lock_irqsave(&priv->lock, irqflags);
895 priv->cable_fault_enable |= BIT(channel_id);
897 priv->cable_fault_enable &= ~BIT(channel_id);
899 /* Enable is active low in Differential Encoder Cable Status register */
900 cable_fault_enable = ~priv->cable_fault_enable;
902 outb(cable_fault_enable, priv->base + QUAD8_DIFF_ENCODER_CABLE_STATUS);
904 spin_unlock_irqrestore(&priv->lock, irqflags);
909 static int quad8_signal_fck_prescaler_read(struct counter_device *counter,
910 struct counter_signal *signal,
913 const struct quad8 *const priv = counter_priv(counter);
915 *prescaler = priv->fck_prescaler[signal->id / 2];
920 static int quad8_signal_fck_prescaler_write(struct counter_device *counter,
921 struct counter_signal *signal,
924 struct quad8 *const priv = counter_priv(counter);
925 const size_t channel_id = signal->id / 2;
926 const int base_offset = priv->base + 2 * channel_id;
927 unsigned long irqflags;
929 spin_lock_irqsave(&priv->lock, irqflags);
931 priv->fck_prescaler[channel_id] = prescaler;
933 /* Reset Byte Pointer */
934 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
936 /* Set filter clock factor */
937 outb(prescaler, base_offset);
938 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_PRESET_PSC,
941 spin_unlock_irqrestore(&priv->lock, irqflags);
946 static struct counter_comp quad8_signal_ext[] = {
947 COUNTER_COMP_SIGNAL_BOOL("cable_fault", quad8_signal_cable_fault_read,
949 COUNTER_COMP_SIGNAL_BOOL("cable_fault_enable",
950 quad8_signal_cable_fault_enable_read,
951 quad8_signal_cable_fault_enable_write),
952 COUNTER_COMP_SIGNAL_U8("filter_clock_prescaler",
953 quad8_signal_fck_prescaler_read,
954 quad8_signal_fck_prescaler_write)
957 static DEFINE_COUNTER_ENUM(quad8_index_pol_enum, quad8_index_polarity_modes);
958 static DEFINE_COUNTER_ENUM(quad8_synch_mode_enum, quad8_synchronous_modes);
960 static struct counter_comp quad8_index_ext[] = {
961 COUNTER_COMP_SIGNAL_ENUM("index_polarity", quad8_index_polarity_get,
962 quad8_index_polarity_set,
963 quad8_index_pol_enum),
964 COUNTER_COMP_SIGNAL_ENUM("synchronous_mode", quad8_synchronous_mode_get,
965 quad8_synchronous_mode_set,
966 quad8_synch_mode_enum),
969 #define QUAD8_QUAD_SIGNAL(_id, _name) { \
972 .ext = quad8_signal_ext, \
973 .num_ext = ARRAY_SIZE(quad8_signal_ext) \
976 #define QUAD8_INDEX_SIGNAL(_id, _name) { \
979 .ext = quad8_index_ext, \
980 .num_ext = ARRAY_SIZE(quad8_index_ext) \
983 static struct counter_signal quad8_signals[] = {
984 QUAD8_QUAD_SIGNAL(0, "Channel 1 Quadrature A"),
985 QUAD8_QUAD_SIGNAL(1, "Channel 1 Quadrature B"),
986 QUAD8_QUAD_SIGNAL(2, "Channel 2 Quadrature A"),
987 QUAD8_QUAD_SIGNAL(3, "Channel 2 Quadrature B"),
988 QUAD8_QUAD_SIGNAL(4, "Channel 3 Quadrature A"),
989 QUAD8_QUAD_SIGNAL(5, "Channel 3 Quadrature B"),
990 QUAD8_QUAD_SIGNAL(6, "Channel 4 Quadrature A"),
991 QUAD8_QUAD_SIGNAL(7, "Channel 4 Quadrature B"),
992 QUAD8_QUAD_SIGNAL(8, "Channel 5 Quadrature A"),
993 QUAD8_QUAD_SIGNAL(9, "Channel 5 Quadrature B"),
994 QUAD8_QUAD_SIGNAL(10, "Channel 6 Quadrature A"),
995 QUAD8_QUAD_SIGNAL(11, "Channel 6 Quadrature B"),
996 QUAD8_QUAD_SIGNAL(12, "Channel 7 Quadrature A"),
997 QUAD8_QUAD_SIGNAL(13, "Channel 7 Quadrature B"),
998 QUAD8_QUAD_SIGNAL(14, "Channel 8 Quadrature A"),
999 QUAD8_QUAD_SIGNAL(15, "Channel 8 Quadrature B"),
1000 QUAD8_INDEX_SIGNAL(16, "Channel 1 Index"),
1001 QUAD8_INDEX_SIGNAL(17, "Channel 2 Index"),
1002 QUAD8_INDEX_SIGNAL(18, "Channel 3 Index"),
1003 QUAD8_INDEX_SIGNAL(19, "Channel 4 Index"),
1004 QUAD8_INDEX_SIGNAL(20, "Channel 5 Index"),
1005 QUAD8_INDEX_SIGNAL(21, "Channel 6 Index"),
1006 QUAD8_INDEX_SIGNAL(22, "Channel 7 Index"),
1007 QUAD8_INDEX_SIGNAL(23, "Channel 8 Index")
1010 #define QUAD8_COUNT_SYNAPSES(_id) { \
1012 .actions_list = quad8_synapse_actions_list, \
1013 .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
1014 .signal = quad8_signals + 2 * (_id) \
1017 .actions_list = quad8_synapse_actions_list, \
1018 .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
1019 .signal = quad8_signals + 2 * (_id) + 1 \
1022 .actions_list = quad8_index_actions_list, \
1023 .num_actions = ARRAY_SIZE(quad8_index_actions_list), \
1024 .signal = quad8_signals + 2 * (_id) + 16 \
1028 static struct counter_synapse quad8_count_synapses[][3] = {
1029 QUAD8_COUNT_SYNAPSES(0), QUAD8_COUNT_SYNAPSES(1),
1030 QUAD8_COUNT_SYNAPSES(2), QUAD8_COUNT_SYNAPSES(3),
1031 QUAD8_COUNT_SYNAPSES(4), QUAD8_COUNT_SYNAPSES(5),
1032 QUAD8_COUNT_SYNAPSES(6), QUAD8_COUNT_SYNAPSES(7)
1035 static const enum counter_count_mode quad8_cnt_modes[] = {
1036 COUNTER_COUNT_MODE_NORMAL,
1037 COUNTER_COUNT_MODE_RANGE_LIMIT,
1038 COUNTER_COUNT_MODE_NON_RECYCLE,
1039 COUNTER_COUNT_MODE_MODULO_N,
1042 static DEFINE_COUNTER_AVAILABLE(quad8_count_mode_available, quad8_cnt_modes);
1044 static DEFINE_COUNTER_ENUM(quad8_error_noise_enum, quad8_noise_error_states);
1046 static struct counter_comp quad8_count_ext[] = {
1047 COUNTER_COMP_CEILING(quad8_count_ceiling_read,
1048 quad8_count_ceiling_write),
1049 COUNTER_COMP_FLOOR(quad8_count_floor_read, NULL),
1050 COUNTER_COMP_COUNT_MODE(quad8_count_mode_read, quad8_count_mode_write,
1051 quad8_count_mode_available),
1052 COUNTER_COMP_DIRECTION(quad8_direction_read),
1053 COUNTER_COMP_ENABLE(quad8_count_enable_read, quad8_count_enable_write),
1054 COUNTER_COMP_COUNT_ENUM("error_noise", quad8_error_noise_get, NULL,
1055 quad8_error_noise_enum),
1056 COUNTER_COMP_PRESET(quad8_count_preset_read, quad8_count_preset_write),
1057 COUNTER_COMP_PRESET_ENABLE(quad8_count_preset_enable_read,
1058 quad8_count_preset_enable_write),
1061 #define QUAD8_COUNT(_id, _cntname) { \
1063 .name = (_cntname), \
1064 .functions_list = quad8_count_functions_list, \
1065 .num_functions = ARRAY_SIZE(quad8_count_functions_list), \
1066 .synapses = quad8_count_synapses[(_id)], \
1067 .num_synapses = 2, \
1068 .ext = quad8_count_ext, \
1069 .num_ext = ARRAY_SIZE(quad8_count_ext) \
1072 static struct counter_count quad8_counts[] = {
1073 QUAD8_COUNT(0, "Channel 1 Count"),
1074 QUAD8_COUNT(1, "Channel 2 Count"),
1075 QUAD8_COUNT(2, "Channel 3 Count"),
1076 QUAD8_COUNT(3, "Channel 4 Count"),
1077 QUAD8_COUNT(4, "Channel 5 Count"),
1078 QUAD8_COUNT(5, "Channel 6 Count"),
1079 QUAD8_COUNT(6, "Channel 7 Count"),
1080 QUAD8_COUNT(7, "Channel 8 Count")
1083 static irqreturn_t quad8_irq_handler(int irq, void *private)
1085 struct counter_device *counter = private;
1086 struct quad8 *const priv = counter_priv(counter);
1087 const unsigned long base = priv->base;
1088 unsigned long irq_status;
1089 unsigned long channel;
1092 irq_status = inb(base + QUAD8_REG_INTERRUPT_STATUS);
1096 for_each_set_bit(channel, &irq_status, QUAD8_NUM_COUNTERS) {
1097 switch (priv->irq_trigger[channel]) {
1098 case QUAD8_EVENT_CARRY:
1099 event = COUNTER_EVENT_OVERFLOW;
1101 case QUAD8_EVENT_COMPARE:
1102 event = COUNTER_EVENT_THRESHOLD;
1104 case QUAD8_EVENT_CARRY_BORROW:
1105 event = COUNTER_EVENT_OVERFLOW_UNDERFLOW;
1107 case QUAD8_EVENT_INDEX:
1108 event = COUNTER_EVENT_INDEX;
1111 /* should never reach this path */
1112 WARN_ONCE(true, "invalid interrupt trigger function %u configured for channel %lu\n",
1113 priv->irq_trigger[channel], channel);
1117 counter_push_event(counter, event, channel);
1120 /* Clear pending interrupts on device */
1121 outb(QUAD8_CHAN_OP_ENABLE_INTERRUPT_FUNC, base + QUAD8_REG_CHAN_OP);
1126 static int quad8_probe(struct device *dev, unsigned int id)
1128 struct counter_device *counter;
1131 unsigned int base_offset;
1134 if (!devm_request_region(dev, base[id], QUAD8_EXTENT, dev_name(dev))) {
1135 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
1136 base[id], base[id] + QUAD8_EXTENT);
1140 counter = devm_counter_alloc(dev, sizeof(*priv));
1143 priv = counter_priv(counter);
1145 /* Initialize Counter device and driver data */
1146 counter->name = dev_name(dev);
1147 counter->parent = dev;
1148 counter->ops = &quad8_ops;
1149 counter->counts = quad8_counts;
1150 counter->num_counts = ARRAY_SIZE(quad8_counts);
1151 counter->signals = quad8_signals;
1152 counter->num_signals = ARRAY_SIZE(quad8_signals);
1153 priv->base = base[id];
1155 spin_lock_init(&priv->lock);
1157 /* Reset Index/Interrupt Register */
1158 outb(0x00, base[id] + QUAD8_REG_INDEX_INTERRUPT);
1159 /* Reset all counters and disable interrupt function */
1160 outb(QUAD8_CHAN_OP_RESET_COUNTERS, base[id] + QUAD8_REG_CHAN_OP);
1161 /* Set initial configuration for all counters */
1162 for (i = 0; i < QUAD8_NUM_COUNTERS; i++) {
1163 base_offset = base[id] + 2 * i;
1164 /* Reset Byte Pointer */
1165 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
1166 /* Reset filter clock factor */
1167 outb(0, base_offset);
1168 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_PRESET_PSC,
1170 /* Reset Byte Pointer */
1171 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, base_offset + 1);
1172 /* Reset Preset Register */
1173 for (j = 0; j < 3; j++)
1174 outb(0x00, base_offset);
1175 /* Reset Borrow, Carry, Compare, and Sign flags */
1176 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_FLAGS, base_offset + 1);
1177 /* Reset Error flag */
1178 outb(QUAD8_CTR_RLD | QUAD8_RLD_RESET_E, base_offset + 1);
1179 /* Binary encoding; Normal count; non-quadrature mode */
1180 outb(QUAD8_CTR_CMR, base_offset + 1);
1181 /* Disable A and B inputs; preset on index; FLG1 as Carry */
1182 outb(QUAD8_CTR_IOR, base_offset + 1);
1183 /* Disable index function; negative index polarity */
1184 outb(QUAD8_CTR_IDR, base_offset + 1);
1186 /* Disable Differential Encoder Cable Status for all channels */
1187 outb(0xFF, base[id] + QUAD8_DIFF_ENCODER_CABLE_STATUS);
1188 /* Enable all counters and enable interrupt function */
1189 outb(QUAD8_CHAN_OP_ENABLE_INTERRUPT_FUNC, base[id] + QUAD8_REG_CHAN_OP);
1191 err = devm_request_irq(&counter->dev, irq[id], quad8_irq_handler,
1192 IRQF_SHARED, counter->name, counter);
1196 err = devm_counter_add(dev, counter);
1198 return dev_err_probe(dev, err, "Failed to add counter\n");
1203 static struct isa_driver quad8_driver = {
1204 .probe = quad8_probe,
1206 .name = "104-quad-8"
1210 module_isa_driver(quad8_driver, num_quad8);
1212 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
1213 MODULE_DESCRIPTION("ACCES 104-QUAD-8 driver");
1214 MODULE_LICENSE("GPL v2");