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 static unsigned int num_irq;
32 module_param_hw_array(irq, uint, irq, &num_irq, 0);
33 MODULE_PARM_DESC(irq, "ACCES 104-QUAD-8 interrupt line numbers");
35 #define QUAD8_NUM_COUNTERS 8
38 * struct channel_reg - channel register structure
40 * @control: Channel flags and control
48 * struct quad8_reg - device register structure
49 * @channel: quadrature counter data and control
50 * @interrupt_status: channel interrupt status
51 * @channel_oper: enable/reset counters and interrupt functions
52 * @index_interrupt: enable channel interrupts
53 * @reserved: reserved for Factory Use
54 * @index_input_levels: index signal logical input level
55 * @cable_status: differential encoder cable status
58 struct channel_reg channel[QUAD8_NUM_COUNTERS];
63 u8 index_input_levels;
68 * struct quad8 - device private data structure
69 * @lock: lock to prevent clobbering device states during R/W ops
70 * @counter: instance of the counter_device
71 * @fck_prescaler: array of filter clock prescaler configurations
72 * @preset: array of preset values
73 * @count_mode: array of count mode configurations
74 * @quadrature_mode: array of quadrature mode configurations
75 * @quadrature_scale: array of quadrature mode scale configurations
76 * @ab_enable: array of A and B inputs enable configurations
77 * @preset_enable: array of set_to_preset_on_index attribute configurations
78 * @irq_trigger: array of current IRQ trigger function configurations
79 * @synchronous_mode: array of index function synchronous mode configurations
80 * @index_polarity: array of index function polarity configurations
81 * @cable_fault_enable: differential encoder cable status enable configurations
82 * @reg: I/O address offset for the device registers
86 unsigned int fck_prescaler[QUAD8_NUM_COUNTERS];
87 unsigned int preset[QUAD8_NUM_COUNTERS];
88 unsigned int count_mode[QUAD8_NUM_COUNTERS];
89 unsigned int quadrature_mode[QUAD8_NUM_COUNTERS];
90 unsigned int quadrature_scale[QUAD8_NUM_COUNTERS];
91 unsigned int ab_enable[QUAD8_NUM_COUNTERS];
92 unsigned int preset_enable[QUAD8_NUM_COUNTERS];
93 unsigned int irq_trigger[QUAD8_NUM_COUNTERS];
94 unsigned int synchronous_mode[QUAD8_NUM_COUNTERS];
95 unsigned int index_polarity[QUAD8_NUM_COUNTERS];
96 unsigned int cable_fault_enable;
97 struct quad8_reg __iomem *reg;
100 /* Borrow Toggle flip-flop */
101 #define QUAD8_FLAG_BT BIT(0)
102 /* Carry Toggle flip-flop */
103 #define QUAD8_FLAG_CT BIT(1)
105 #define QUAD8_FLAG_E BIT(4)
107 #define QUAD8_FLAG_UD BIT(5)
108 /* Reset and Load Signal Decoders */
109 #define QUAD8_CTR_RLD 0x00
110 /* Counter Mode Register */
111 #define QUAD8_CTR_CMR 0x20
112 /* Input / Output Control Register */
113 #define QUAD8_CTR_IOR 0x40
114 /* Index Control Register */
115 #define QUAD8_CTR_IDR 0x60
116 /* Reset Byte Pointer (three byte data pointer) */
117 #define QUAD8_RLD_RESET_BP 0x01
119 #define QUAD8_RLD_RESET_CNTR 0x02
120 /* Reset Borrow Toggle, Carry Toggle, Compare Toggle, and Sign flags */
121 #define QUAD8_RLD_RESET_FLAGS 0x04
122 /* Reset Error flag */
123 #define QUAD8_RLD_RESET_E 0x06
124 /* Preset Register to Counter */
125 #define QUAD8_RLD_PRESET_CNTR 0x08
126 /* Transfer Counter to Output Latch */
127 #define QUAD8_RLD_CNTR_OUT 0x10
128 /* Transfer Preset Register LSB to FCK Prescaler */
129 #define QUAD8_RLD_PRESET_PSC 0x18
130 #define QUAD8_CHAN_OP_RESET_COUNTERS 0x01
131 #define QUAD8_CHAN_OP_ENABLE_INTERRUPT_FUNC 0x04
132 #define QUAD8_CMR_QUADRATURE_X1 0x08
133 #define QUAD8_CMR_QUADRATURE_X2 0x10
134 #define QUAD8_CMR_QUADRATURE_X4 0x18
136 static int quad8_signal_read(struct counter_device *counter,
137 struct counter_signal *signal,
138 enum counter_signal_level *level)
140 const struct quad8 *const priv = counter_priv(counter);
143 /* Only Index signal levels can be read */
147 state = ioread8(&priv->reg->index_input_levels) & BIT(signal->id - 16);
149 *level = (state) ? COUNTER_SIGNAL_LEVEL_HIGH : COUNTER_SIGNAL_LEVEL_LOW;
154 static int quad8_count_read(struct counter_device *counter,
155 struct counter_count *count, u64 *val)
157 struct quad8 *const priv = counter_priv(counter);
158 struct channel_reg __iomem *const chan = priv->reg->channel + count->id;
162 unsigned long irqflags;
165 flags = ioread8(&chan->control);
166 borrow = flags & QUAD8_FLAG_BT;
167 carry = !!(flags & QUAD8_FLAG_CT);
169 /* Borrow XOR Carry effectively doubles count range */
170 *val = (unsigned long)(borrow ^ carry) << 24;
172 spin_lock_irqsave(&priv->lock, irqflags);
174 /* Reset Byte Pointer; transfer Counter to Output Latch */
175 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_CNTR_OUT,
178 for (i = 0; i < 3; i++)
179 *val |= (unsigned long)ioread8(&chan->data) << (8 * i);
181 spin_unlock_irqrestore(&priv->lock, irqflags);
186 static int quad8_count_write(struct counter_device *counter,
187 struct counter_count *count, u64 val)
189 struct quad8 *const priv = counter_priv(counter);
190 struct channel_reg __iomem *const chan = priv->reg->channel + count->id;
191 unsigned long irqflags;
194 /* Only 24-bit values are supported */
198 spin_lock_irqsave(&priv->lock, irqflags);
200 /* Reset Byte Pointer */
201 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, &chan->control);
203 /* Counter can only be set via Preset Register */
204 for (i = 0; i < 3; i++)
205 iowrite8(val >> (8 * i), &chan->data);
207 /* Transfer Preset Register to Counter */
208 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_PRESET_CNTR, &chan->control);
210 /* Reset Byte Pointer */
211 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, &chan->control);
213 /* Set Preset Register back to original value */
214 val = priv->preset[count->id];
215 for (i = 0; i < 3; i++)
216 iowrite8(val >> (8 * i), &chan->data);
218 /* Reset Borrow, Carry, Compare, and Sign flags */
219 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_FLAGS, &chan->control);
220 /* Reset Error flag */
221 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_E, &chan->control);
223 spin_unlock_irqrestore(&priv->lock, irqflags);
228 static const enum counter_function quad8_count_functions_list[] = {
229 COUNTER_FUNCTION_PULSE_DIRECTION,
230 COUNTER_FUNCTION_QUADRATURE_X1_A,
231 COUNTER_FUNCTION_QUADRATURE_X2_A,
232 COUNTER_FUNCTION_QUADRATURE_X4,
235 static int quad8_function_get(const struct quad8 *const priv, const size_t id,
236 enum counter_function *const function)
238 if (!priv->quadrature_mode[id]) {
239 *function = COUNTER_FUNCTION_PULSE_DIRECTION;
243 switch (priv->quadrature_scale[id]) {
245 *function = COUNTER_FUNCTION_QUADRATURE_X1_A;
248 *function = COUNTER_FUNCTION_QUADRATURE_X2_A;
251 *function = COUNTER_FUNCTION_QUADRATURE_X4;
254 /* should never reach this path */
259 static int quad8_function_read(struct counter_device *counter,
260 struct counter_count *count,
261 enum counter_function *function)
263 struct quad8 *const priv = counter_priv(counter);
264 unsigned long irqflags;
267 spin_lock_irqsave(&priv->lock, irqflags);
269 retval = quad8_function_get(priv, count->id, function);
271 spin_unlock_irqrestore(&priv->lock, irqflags);
276 static int quad8_function_write(struct counter_device *counter,
277 struct counter_count *count,
278 enum counter_function function)
280 struct quad8 *const priv = counter_priv(counter);
281 const int id = count->id;
282 unsigned int *const quadrature_mode = priv->quadrature_mode + id;
283 unsigned int *const scale = priv->quadrature_scale + id;
284 unsigned int *const synchronous_mode = priv->synchronous_mode + id;
285 u8 __iomem *const control = &priv->reg->channel[id].control;
286 unsigned long irqflags;
287 unsigned int mode_cfg;
288 unsigned int idr_cfg;
290 spin_lock_irqsave(&priv->lock, irqflags);
292 mode_cfg = priv->count_mode[id] << 1;
293 idr_cfg = priv->index_polarity[id] << 1;
295 if (function == COUNTER_FUNCTION_PULSE_DIRECTION) {
296 *quadrature_mode = 0;
298 /* Quadrature scaling only available in quadrature mode */
301 /* Synchronous function not supported in non-quadrature mode */
302 if (*synchronous_mode) {
303 *synchronous_mode = 0;
304 /* Disable synchronous function mode */
305 iowrite8(QUAD8_CTR_IDR | idr_cfg, control);
308 *quadrature_mode = 1;
311 case COUNTER_FUNCTION_QUADRATURE_X1_A:
313 mode_cfg |= QUAD8_CMR_QUADRATURE_X1;
315 case COUNTER_FUNCTION_QUADRATURE_X2_A:
317 mode_cfg |= QUAD8_CMR_QUADRATURE_X2;
319 case COUNTER_FUNCTION_QUADRATURE_X4:
321 mode_cfg |= QUAD8_CMR_QUADRATURE_X4;
324 /* should never reach this path */
325 spin_unlock_irqrestore(&priv->lock, irqflags);
330 /* Load mode configuration to Counter Mode Register */
331 iowrite8(QUAD8_CTR_CMR | mode_cfg, control);
333 spin_unlock_irqrestore(&priv->lock, irqflags);
338 static int quad8_direction_read(struct counter_device *counter,
339 struct counter_count *count,
340 enum counter_count_direction *direction)
342 const struct quad8 *const priv = counter_priv(counter);
343 unsigned int ud_flag;
344 u8 __iomem *const flag_addr = &priv->reg->channel[count->id].control;
346 /* U/D flag: nonzero = up, zero = down */
347 ud_flag = ioread8(flag_addr) & QUAD8_FLAG_UD;
349 *direction = (ud_flag) ? COUNTER_COUNT_DIRECTION_FORWARD :
350 COUNTER_COUNT_DIRECTION_BACKWARD;
355 static const enum counter_synapse_action quad8_index_actions_list[] = {
356 COUNTER_SYNAPSE_ACTION_NONE,
357 COUNTER_SYNAPSE_ACTION_RISING_EDGE,
360 static const enum counter_synapse_action quad8_synapse_actions_list[] = {
361 COUNTER_SYNAPSE_ACTION_NONE,
362 COUNTER_SYNAPSE_ACTION_RISING_EDGE,
363 COUNTER_SYNAPSE_ACTION_FALLING_EDGE,
364 COUNTER_SYNAPSE_ACTION_BOTH_EDGES,
367 static int quad8_action_read(struct counter_device *counter,
368 struct counter_count *count,
369 struct counter_synapse *synapse,
370 enum counter_synapse_action *action)
372 struct quad8 *const priv = counter_priv(counter);
373 unsigned long irqflags;
375 enum counter_function function;
376 const size_t signal_a_id = count->synapses[0].signal->id;
377 enum counter_count_direction direction;
379 /* Handle Index signals */
380 if (synapse->signal->id >= 16) {
381 if (priv->preset_enable[count->id])
382 *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
384 *action = COUNTER_SYNAPSE_ACTION_NONE;
389 spin_lock_irqsave(&priv->lock, irqflags);
391 /* Get Count function and direction atomically */
392 err = quad8_function_get(priv, count->id, &function);
394 spin_unlock_irqrestore(&priv->lock, irqflags);
397 err = quad8_direction_read(counter, count, &direction);
399 spin_unlock_irqrestore(&priv->lock, irqflags);
403 spin_unlock_irqrestore(&priv->lock, irqflags);
405 /* Default action mode */
406 *action = COUNTER_SYNAPSE_ACTION_NONE;
408 /* Determine action mode based on current count function mode */
410 case COUNTER_FUNCTION_PULSE_DIRECTION:
411 if (synapse->signal->id == signal_a_id)
412 *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
414 case COUNTER_FUNCTION_QUADRATURE_X1_A:
415 if (synapse->signal->id == signal_a_id) {
416 if (direction == COUNTER_COUNT_DIRECTION_FORWARD)
417 *action = COUNTER_SYNAPSE_ACTION_RISING_EDGE;
419 *action = COUNTER_SYNAPSE_ACTION_FALLING_EDGE;
422 case COUNTER_FUNCTION_QUADRATURE_X2_A:
423 if (synapse->signal->id == signal_a_id)
424 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
426 case COUNTER_FUNCTION_QUADRATURE_X4:
427 *action = COUNTER_SYNAPSE_ACTION_BOTH_EDGES;
430 /* should never reach this path */
436 QUAD8_EVENT_CARRY = 0,
437 QUAD8_EVENT_COMPARE = 1,
438 QUAD8_EVENT_CARRY_BORROW = 2,
439 QUAD8_EVENT_INDEX = 3,
442 static int quad8_events_configure(struct counter_device *counter)
444 struct quad8 *const priv = counter_priv(counter);
445 unsigned long irq_enabled = 0;
446 unsigned long irqflags;
447 struct counter_event_node *event_node;
448 unsigned int next_irq_trigger;
449 unsigned long ior_cfg;
451 spin_lock_irqsave(&priv->lock, irqflags);
453 list_for_each_entry(event_node, &counter->events_list, l) {
454 switch (event_node->event) {
455 case COUNTER_EVENT_OVERFLOW:
456 next_irq_trigger = QUAD8_EVENT_CARRY;
458 case COUNTER_EVENT_THRESHOLD:
459 next_irq_trigger = QUAD8_EVENT_COMPARE;
461 case COUNTER_EVENT_OVERFLOW_UNDERFLOW:
462 next_irq_trigger = QUAD8_EVENT_CARRY_BORROW;
464 case COUNTER_EVENT_INDEX:
465 next_irq_trigger = QUAD8_EVENT_INDEX;
468 /* should never reach this path */
469 spin_unlock_irqrestore(&priv->lock, irqflags);
473 /* Enable IRQ line */
474 irq_enabled |= BIT(event_node->channel);
476 /* Skip configuration if it is the same as previously set */
477 if (priv->irq_trigger[event_node->channel] == next_irq_trigger)
480 /* Save new IRQ function configuration */
481 priv->irq_trigger[event_node->channel] = next_irq_trigger;
483 /* Load configuration to I/O Control Register */
484 ior_cfg = priv->ab_enable[event_node->channel] |
485 priv->preset_enable[event_node->channel] << 1 |
486 priv->irq_trigger[event_node->channel] << 3;
487 iowrite8(QUAD8_CTR_IOR | ior_cfg,
488 &priv->reg->channel[event_node->channel].control);
491 iowrite8(irq_enabled, &priv->reg->index_interrupt);
493 spin_unlock_irqrestore(&priv->lock, irqflags);
498 static int quad8_watch_validate(struct counter_device *counter,
499 const struct counter_watch *watch)
501 struct counter_event_node *event_node;
503 if (watch->channel > QUAD8_NUM_COUNTERS - 1)
506 switch (watch->event) {
507 case COUNTER_EVENT_OVERFLOW:
508 case COUNTER_EVENT_THRESHOLD:
509 case COUNTER_EVENT_OVERFLOW_UNDERFLOW:
510 case COUNTER_EVENT_INDEX:
511 list_for_each_entry(event_node, &counter->next_events_list, l)
512 if (watch->channel == event_node->channel &&
513 watch->event != event_node->event)
521 static const struct counter_ops quad8_ops = {
522 .signal_read = quad8_signal_read,
523 .count_read = quad8_count_read,
524 .count_write = quad8_count_write,
525 .function_read = quad8_function_read,
526 .function_write = quad8_function_write,
527 .action_read = quad8_action_read,
528 .events_configure = quad8_events_configure,
529 .watch_validate = quad8_watch_validate,
532 static const char *const quad8_index_polarity_modes[] = {
537 static int quad8_index_polarity_get(struct counter_device *counter,
538 struct counter_signal *signal,
541 const struct quad8 *const priv = counter_priv(counter);
542 const size_t channel_id = signal->id - 16;
544 *index_polarity = priv->index_polarity[channel_id];
549 static int quad8_index_polarity_set(struct counter_device *counter,
550 struct counter_signal *signal,
553 struct quad8 *const priv = counter_priv(counter);
554 const size_t channel_id = signal->id - 16;
555 u8 __iomem *const control = &priv->reg->channel[channel_id].control;
556 unsigned long irqflags;
557 unsigned int idr_cfg = index_polarity << 1;
559 spin_lock_irqsave(&priv->lock, irqflags);
561 idr_cfg |= priv->synchronous_mode[channel_id];
563 priv->index_polarity[channel_id] = index_polarity;
565 /* Load Index Control configuration to Index Control Register */
566 iowrite8(QUAD8_CTR_IDR | idr_cfg, control);
568 spin_unlock_irqrestore(&priv->lock, irqflags);
573 static int quad8_polarity_read(struct counter_device *counter,
574 struct counter_signal *signal,
575 enum counter_signal_polarity *polarity)
580 err = quad8_index_polarity_get(counter, signal, &index_polarity);
584 *polarity = (index_polarity) ? COUNTER_SIGNAL_POLARITY_POSITIVE :
585 COUNTER_SIGNAL_POLARITY_NEGATIVE;
590 static int quad8_polarity_write(struct counter_device *counter,
591 struct counter_signal *signal,
592 enum counter_signal_polarity polarity)
594 const u32 pol = (polarity == COUNTER_SIGNAL_POLARITY_POSITIVE) ? 1 : 0;
596 return quad8_index_polarity_set(counter, signal, pol);
599 static const char *const quad8_synchronous_modes[] = {
604 static int quad8_synchronous_mode_get(struct counter_device *counter,
605 struct counter_signal *signal,
606 u32 *synchronous_mode)
608 const struct quad8 *const priv = counter_priv(counter);
609 const size_t channel_id = signal->id - 16;
611 *synchronous_mode = priv->synchronous_mode[channel_id];
616 static int quad8_synchronous_mode_set(struct counter_device *counter,
617 struct counter_signal *signal,
618 u32 synchronous_mode)
620 struct quad8 *const priv = counter_priv(counter);
621 const size_t channel_id = signal->id - 16;
622 u8 __iomem *const control = &priv->reg->channel[channel_id].control;
623 unsigned long irqflags;
624 unsigned int idr_cfg = synchronous_mode;
626 spin_lock_irqsave(&priv->lock, irqflags);
628 idr_cfg |= priv->index_polarity[channel_id] << 1;
630 /* Index function must be non-synchronous in non-quadrature mode */
631 if (synchronous_mode && !priv->quadrature_mode[channel_id]) {
632 spin_unlock_irqrestore(&priv->lock, irqflags);
636 priv->synchronous_mode[channel_id] = synchronous_mode;
638 /* Load Index Control configuration to Index Control Register */
639 iowrite8(QUAD8_CTR_IDR | idr_cfg, control);
641 spin_unlock_irqrestore(&priv->lock, irqflags);
646 static int quad8_count_floor_read(struct counter_device *counter,
647 struct counter_count *count, u64 *floor)
649 /* Only a floor of 0 is supported */
655 static int quad8_count_mode_read(struct counter_device *counter,
656 struct counter_count *count,
657 enum counter_count_mode *cnt_mode)
659 const struct quad8 *const priv = counter_priv(counter);
661 /* Map 104-QUAD-8 count mode to Generic Counter count mode */
662 switch (priv->count_mode[count->id]) {
664 *cnt_mode = COUNTER_COUNT_MODE_NORMAL;
667 *cnt_mode = COUNTER_COUNT_MODE_RANGE_LIMIT;
670 *cnt_mode = COUNTER_COUNT_MODE_NON_RECYCLE;
673 *cnt_mode = COUNTER_COUNT_MODE_MODULO_N;
680 static int quad8_count_mode_write(struct counter_device *counter,
681 struct counter_count *count,
682 enum counter_count_mode cnt_mode)
684 struct quad8 *const priv = counter_priv(counter);
685 unsigned int count_mode;
686 unsigned int mode_cfg;
687 u8 __iomem *const control = &priv->reg->channel[count->id].control;
688 unsigned long irqflags;
690 /* Map Generic Counter count mode to 104-QUAD-8 count mode */
692 case COUNTER_COUNT_MODE_NORMAL:
695 case COUNTER_COUNT_MODE_RANGE_LIMIT:
698 case COUNTER_COUNT_MODE_NON_RECYCLE:
701 case COUNTER_COUNT_MODE_MODULO_N:
705 /* should never reach this path */
709 spin_lock_irqsave(&priv->lock, irqflags);
711 priv->count_mode[count->id] = count_mode;
713 /* Set count mode configuration value */
714 mode_cfg = count_mode << 1;
716 /* Add quadrature mode configuration */
717 if (priv->quadrature_mode[count->id])
718 mode_cfg |= (priv->quadrature_scale[count->id] + 1) << 3;
720 /* Load mode configuration to Counter Mode Register */
721 iowrite8(QUAD8_CTR_CMR | mode_cfg, control);
723 spin_unlock_irqrestore(&priv->lock, irqflags);
728 static int quad8_count_enable_read(struct counter_device *counter,
729 struct counter_count *count, u8 *enable)
731 const struct quad8 *const priv = counter_priv(counter);
733 *enable = priv->ab_enable[count->id];
738 static int quad8_count_enable_write(struct counter_device *counter,
739 struct counter_count *count, u8 enable)
741 struct quad8 *const priv = counter_priv(counter);
742 u8 __iomem *const control = &priv->reg->channel[count->id].control;
743 unsigned long irqflags;
744 unsigned int ior_cfg;
746 spin_lock_irqsave(&priv->lock, irqflags);
748 priv->ab_enable[count->id] = enable;
750 ior_cfg = enable | priv->preset_enable[count->id] << 1 |
751 priv->irq_trigger[count->id] << 3;
753 /* Load I/O control configuration */
754 iowrite8(QUAD8_CTR_IOR | ior_cfg, control);
756 spin_unlock_irqrestore(&priv->lock, irqflags);
761 static const char *const quad8_noise_error_states[] = {
762 "No excessive noise is present at the count inputs",
763 "Excessive noise is present at the count inputs"
766 static int quad8_error_noise_get(struct counter_device *counter,
767 struct counter_count *count, u32 *noise_error)
769 const struct quad8 *const priv = counter_priv(counter);
770 u8 __iomem *const flag_addr = &priv->reg->channel[count->id].control;
772 *noise_error = !!(ioread8(flag_addr) & QUAD8_FLAG_E);
777 static int quad8_count_preset_read(struct counter_device *counter,
778 struct counter_count *count, u64 *preset)
780 const struct quad8 *const priv = counter_priv(counter);
782 *preset = priv->preset[count->id];
787 static void quad8_preset_register_set(struct quad8 *const priv, const int id,
788 const unsigned int preset)
790 struct channel_reg __iomem *const chan = priv->reg->channel + id;
793 priv->preset[id] = preset;
795 /* Reset Byte Pointer */
796 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, &chan->control);
798 /* Set Preset Register */
799 for (i = 0; i < 3; i++)
800 iowrite8(preset >> (8 * i), &chan->data);
803 static int quad8_count_preset_write(struct counter_device *counter,
804 struct counter_count *count, u64 preset)
806 struct quad8 *const priv = counter_priv(counter);
807 unsigned long irqflags;
809 /* Only 24-bit values are supported */
810 if (preset > 0xFFFFFF)
813 spin_lock_irqsave(&priv->lock, irqflags);
815 quad8_preset_register_set(priv, count->id, preset);
817 spin_unlock_irqrestore(&priv->lock, irqflags);
822 static int quad8_count_ceiling_read(struct counter_device *counter,
823 struct counter_count *count, u64 *ceiling)
825 struct quad8 *const priv = counter_priv(counter);
826 unsigned long irqflags;
828 spin_lock_irqsave(&priv->lock, irqflags);
830 /* Range Limit and Modulo-N count modes use preset value as ceiling */
831 switch (priv->count_mode[count->id]) {
834 *ceiling = priv->preset[count->id];
837 /* By default 0x1FFFFFF (25 bits unsigned) is maximum count */
838 *ceiling = 0x1FFFFFF;
842 spin_unlock_irqrestore(&priv->lock, irqflags);
847 static int quad8_count_ceiling_write(struct counter_device *counter,
848 struct counter_count *count, u64 ceiling)
850 struct quad8 *const priv = counter_priv(counter);
851 unsigned long irqflags;
853 /* Only 24-bit values are supported */
854 if (ceiling > 0xFFFFFF)
857 spin_lock_irqsave(&priv->lock, irqflags);
859 /* Range Limit and Modulo-N count modes use preset value as ceiling */
860 switch (priv->count_mode[count->id]) {
863 quad8_preset_register_set(priv, count->id, ceiling);
864 spin_unlock_irqrestore(&priv->lock, irqflags);
868 spin_unlock_irqrestore(&priv->lock, irqflags);
873 static int quad8_count_preset_enable_read(struct counter_device *counter,
874 struct counter_count *count,
877 const struct quad8 *const priv = counter_priv(counter);
879 *preset_enable = !priv->preset_enable[count->id];
884 static int quad8_count_preset_enable_write(struct counter_device *counter,
885 struct counter_count *count,
888 struct quad8 *const priv = counter_priv(counter);
889 u8 __iomem *const control = &priv->reg->channel[count->id].control;
890 unsigned long irqflags;
891 unsigned int ior_cfg;
893 /* Preset enable is active low in Input/Output Control register */
894 preset_enable = !preset_enable;
896 spin_lock_irqsave(&priv->lock, irqflags);
898 priv->preset_enable[count->id] = preset_enable;
900 ior_cfg = priv->ab_enable[count->id] | preset_enable << 1 |
901 priv->irq_trigger[count->id] << 3;
903 /* Load I/O control configuration to Input / Output Control Register */
904 iowrite8(QUAD8_CTR_IOR | ior_cfg, control);
906 spin_unlock_irqrestore(&priv->lock, irqflags);
911 static int quad8_signal_cable_fault_read(struct counter_device *counter,
912 struct counter_signal *signal,
915 struct quad8 *const priv = counter_priv(counter);
916 const size_t channel_id = signal->id / 2;
917 unsigned long irqflags;
921 spin_lock_irqsave(&priv->lock, irqflags);
923 disabled = !(priv->cable_fault_enable & BIT(channel_id));
926 spin_unlock_irqrestore(&priv->lock, irqflags);
930 /* Logic 0 = cable fault */
931 status = ioread8(&priv->reg->cable_status);
933 spin_unlock_irqrestore(&priv->lock, irqflags);
935 /* Mask respective channel and invert logic */
936 *cable_fault = !(status & BIT(channel_id));
941 static int quad8_signal_cable_fault_enable_read(struct counter_device *counter,
942 struct counter_signal *signal,
945 const struct quad8 *const priv = counter_priv(counter);
946 const size_t channel_id = signal->id / 2;
948 *enable = !!(priv->cable_fault_enable & BIT(channel_id));
953 static int quad8_signal_cable_fault_enable_write(struct counter_device *counter,
954 struct counter_signal *signal,
957 struct quad8 *const priv = counter_priv(counter);
958 const size_t channel_id = signal->id / 2;
959 unsigned long irqflags;
960 unsigned int cable_fault_enable;
962 spin_lock_irqsave(&priv->lock, irqflags);
965 priv->cable_fault_enable |= BIT(channel_id);
967 priv->cable_fault_enable &= ~BIT(channel_id);
969 /* Enable is active low in Differential Encoder Cable Status register */
970 cable_fault_enable = ~priv->cable_fault_enable;
972 iowrite8(cable_fault_enable, &priv->reg->cable_status);
974 spin_unlock_irqrestore(&priv->lock, irqflags);
979 static int quad8_signal_fck_prescaler_read(struct counter_device *counter,
980 struct counter_signal *signal,
983 const struct quad8 *const priv = counter_priv(counter);
985 *prescaler = priv->fck_prescaler[signal->id / 2];
990 static int quad8_signal_fck_prescaler_write(struct counter_device *counter,
991 struct counter_signal *signal,
994 struct quad8 *const priv = counter_priv(counter);
995 const size_t channel_id = signal->id / 2;
996 struct channel_reg __iomem *const chan = priv->reg->channel + channel_id;
997 unsigned long irqflags;
999 spin_lock_irqsave(&priv->lock, irqflags);
1001 priv->fck_prescaler[channel_id] = prescaler;
1003 /* Reset Byte Pointer */
1004 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, &chan->control);
1006 /* Set filter clock factor */
1007 iowrite8(prescaler, &chan->data);
1008 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_PRESET_PSC,
1011 spin_unlock_irqrestore(&priv->lock, irqflags);
1016 static struct counter_comp quad8_signal_ext[] = {
1017 COUNTER_COMP_SIGNAL_BOOL("cable_fault", quad8_signal_cable_fault_read,
1019 COUNTER_COMP_SIGNAL_BOOL("cable_fault_enable",
1020 quad8_signal_cable_fault_enable_read,
1021 quad8_signal_cable_fault_enable_write),
1022 COUNTER_COMP_SIGNAL_U8("filter_clock_prescaler",
1023 quad8_signal_fck_prescaler_read,
1024 quad8_signal_fck_prescaler_write)
1027 static const enum counter_signal_polarity quad8_polarities[] = {
1028 COUNTER_SIGNAL_POLARITY_POSITIVE,
1029 COUNTER_SIGNAL_POLARITY_NEGATIVE,
1032 static DEFINE_COUNTER_AVAILABLE(quad8_polarity_available, quad8_polarities);
1034 static DEFINE_COUNTER_ENUM(quad8_index_pol_enum, quad8_index_polarity_modes);
1035 static DEFINE_COUNTER_ENUM(quad8_synch_mode_enum, quad8_synchronous_modes);
1037 static struct counter_comp quad8_index_ext[] = {
1038 COUNTER_COMP_SIGNAL_ENUM("index_polarity", quad8_index_polarity_get,
1039 quad8_index_polarity_set,
1040 quad8_index_pol_enum),
1041 COUNTER_COMP_POLARITY(quad8_polarity_read, quad8_polarity_write,
1042 quad8_polarity_available),
1043 COUNTER_COMP_SIGNAL_ENUM("synchronous_mode", quad8_synchronous_mode_get,
1044 quad8_synchronous_mode_set,
1045 quad8_synch_mode_enum),
1048 #define QUAD8_QUAD_SIGNAL(_id, _name) { \
1051 .ext = quad8_signal_ext, \
1052 .num_ext = ARRAY_SIZE(quad8_signal_ext) \
1055 #define QUAD8_INDEX_SIGNAL(_id, _name) { \
1058 .ext = quad8_index_ext, \
1059 .num_ext = ARRAY_SIZE(quad8_index_ext) \
1062 static struct counter_signal quad8_signals[] = {
1063 QUAD8_QUAD_SIGNAL(0, "Channel 1 Quadrature A"),
1064 QUAD8_QUAD_SIGNAL(1, "Channel 1 Quadrature B"),
1065 QUAD8_QUAD_SIGNAL(2, "Channel 2 Quadrature A"),
1066 QUAD8_QUAD_SIGNAL(3, "Channel 2 Quadrature B"),
1067 QUAD8_QUAD_SIGNAL(4, "Channel 3 Quadrature A"),
1068 QUAD8_QUAD_SIGNAL(5, "Channel 3 Quadrature B"),
1069 QUAD8_QUAD_SIGNAL(6, "Channel 4 Quadrature A"),
1070 QUAD8_QUAD_SIGNAL(7, "Channel 4 Quadrature B"),
1071 QUAD8_QUAD_SIGNAL(8, "Channel 5 Quadrature A"),
1072 QUAD8_QUAD_SIGNAL(9, "Channel 5 Quadrature B"),
1073 QUAD8_QUAD_SIGNAL(10, "Channel 6 Quadrature A"),
1074 QUAD8_QUAD_SIGNAL(11, "Channel 6 Quadrature B"),
1075 QUAD8_QUAD_SIGNAL(12, "Channel 7 Quadrature A"),
1076 QUAD8_QUAD_SIGNAL(13, "Channel 7 Quadrature B"),
1077 QUAD8_QUAD_SIGNAL(14, "Channel 8 Quadrature A"),
1078 QUAD8_QUAD_SIGNAL(15, "Channel 8 Quadrature B"),
1079 QUAD8_INDEX_SIGNAL(16, "Channel 1 Index"),
1080 QUAD8_INDEX_SIGNAL(17, "Channel 2 Index"),
1081 QUAD8_INDEX_SIGNAL(18, "Channel 3 Index"),
1082 QUAD8_INDEX_SIGNAL(19, "Channel 4 Index"),
1083 QUAD8_INDEX_SIGNAL(20, "Channel 5 Index"),
1084 QUAD8_INDEX_SIGNAL(21, "Channel 6 Index"),
1085 QUAD8_INDEX_SIGNAL(22, "Channel 7 Index"),
1086 QUAD8_INDEX_SIGNAL(23, "Channel 8 Index")
1089 #define QUAD8_COUNT_SYNAPSES(_id) { \
1091 .actions_list = quad8_synapse_actions_list, \
1092 .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
1093 .signal = quad8_signals + 2 * (_id) \
1096 .actions_list = quad8_synapse_actions_list, \
1097 .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
1098 .signal = quad8_signals + 2 * (_id) + 1 \
1101 .actions_list = quad8_index_actions_list, \
1102 .num_actions = ARRAY_SIZE(quad8_index_actions_list), \
1103 .signal = quad8_signals + 2 * (_id) + 16 \
1107 static struct counter_synapse quad8_count_synapses[][3] = {
1108 QUAD8_COUNT_SYNAPSES(0), QUAD8_COUNT_SYNAPSES(1),
1109 QUAD8_COUNT_SYNAPSES(2), QUAD8_COUNT_SYNAPSES(3),
1110 QUAD8_COUNT_SYNAPSES(4), QUAD8_COUNT_SYNAPSES(5),
1111 QUAD8_COUNT_SYNAPSES(6), QUAD8_COUNT_SYNAPSES(7)
1114 static const enum counter_count_mode quad8_cnt_modes[] = {
1115 COUNTER_COUNT_MODE_NORMAL,
1116 COUNTER_COUNT_MODE_RANGE_LIMIT,
1117 COUNTER_COUNT_MODE_NON_RECYCLE,
1118 COUNTER_COUNT_MODE_MODULO_N,
1121 static DEFINE_COUNTER_AVAILABLE(quad8_count_mode_available, quad8_cnt_modes);
1123 static DEFINE_COUNTER_ENUM(quad8_error_noise_enum, quad8_noise_error_states);
1125 static struct counter_comp quad8_count_ext[] = {
1126 COUNTER_COMP_CEILING(quad8_count_ceiling_read,
1127 quad8_count_ceiling_write),
1128 COUNTER_COMP_FLOOR(quad8_count_floor_read, NULL),
1129 COUNTER_COMP_COUNT_MODE(quad8_count_mode_read, quad8_count_mode_write,
1130 quad8_count_mode_available),
1131 COUNTER_COMP_DIRECTION(quad8_direction_read),
1132 COUNTER_COMP_ENABLE(quad8_count_enable_read, quad8_count_enable_write),
1133 COUNTER_COMP_COUNT_ENUM("error_noise", quad8_error_noise_get, NULL,
1134 quad8_error_noise_enum),
1135 COUNTER_COMP_PRESET(quad8_count_preset_read, quad8_count_preset_write),
1136 COUNTER_COMP_PRESET_ENABLE(quad8_count_preset_enable_read,
1137 quad8_count_preset_enable_write),
1140 #define QUAD8_COUNT(_id, _cntname) { \
1142 .name = (_cntname), \
1143 .functions_list = quad8_count_functions_list, \
1144 .num_functions = ARRAY_SIZE(quad8_count_functions_list), \
1145 .synapses = quad8_count_synapses[(_id)], \
1146 .num_synapses = 2, \
1147 .ext = quad8_count_ext, \
1148 .num_ext = ARRAY_SIZE(quad8_count_ext) \
1151 static struct counter_count quad8_counts[] = {
1152 QUAD8_COUNT(0, "Channel 1 Count"),
1153 QUAD8_COUNT(1, "Channel 2 Count"),
1154 QUAD8_COUNT(2, "Channel 3 Count"),
1155 QUAD8_COUNT(3, "Channel 4 Count"),
1156 QUAD8_COUNT(4, "Channel 5 Count"),
1157 QUAD8_COUNT(5, "Channel 6 Count"),
1158 QUAD8_COUNT(6, "Channel 7 Count"),
1159 QUAD8_COUNT(7, "Channel 8 Count")
1162 static irqreturn_t quad8_irq_handler(int irq, void *private)
1164 struct counter_device *counter = private;
1165 struct quad8 *const priv = counter_priv(counter);
1166 unsigned long irq_status;
1167 unsigned long channel;
1170 irq_status = ioread8(&priv->reg->interrupt_status);
1174 for_each_set_bit(channel, &irq_status, QUAD8_NUM_COUNTERS) {
1175 switch (priv->irq_trigger[channel]) {
1176 case QUAD8_EVENT_CARRY:
1177 event = COUNTER_EVENT_OVERFLOW;
1179 case QUAD8_EVENT_COMPARE:
1180 event = COUNTER_EVENT_THRESHOLD;
1182 case QUAD8_EVENT_CARRY_BORROW:
1183 event = COUNTER_EVENT_OVERFLOW_UNDERFLOW;
1185 case QUAD8_EVENT_INDEX:
1186 event = COUNTER_EVENT_INDEX;
1189 /* should never reach this path */
1190 WARN_ONCE(true, "invalid interrupt trigger function %u configured for channel %lu\n",
1191 priv->irq_trigger[channel], channel);
1195 counter_push_event(counter, event, channel);
1198 /* Clear pending interrupts on device */
1199 iowrite8(QUAD8_CHAN_OP_ENABLE_INTERRUPT_FUNC, &priv->reg->channel_oper);
1204 static void quad8_init_counter(struct channel_reg __iomem *const chan)
1208 /* Reset Byte Pointer */
1209 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, &chan->control);
1210 /* Reset filter clock factor */
1211 iowrite8(0, &chan->data);
1212 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP | QUAD8_RLD_PRESET_PSC,
1214 /* Reset Byte Pointer */
1215 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_BP, &chan->control);
1216 /* Reset Preset Register */
1217 for (i = 0; i < 3; i++)
1218 iowrite8(0x00, &chan->data);
1219 /* Reset Borrow, Carry, Compare, and Sign flags */
1220 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_FLAGS, &chan->control);
1221 /* Reset Error flag */
1222 iowrite8(QUAD8_CTR_RLD | QUAD8_RLD_RESET_E, &chan->control);
1223 /* Binary encoding; Normal count; non-quadrature mode */
1224 iowrite8(QUAD8_CTR_CMR, &chan->control);
1225 /* Disable A and B inputs; preset on index; FLG1 as Carry */
1226 iowrite8(QUAD8_CTR_IOR, &chan->control);
1227 /* Disable index function; negative index polarity */
1228 iowrite8(QUAD8_CTR_IDR, &chan->control);
1231 static int quad8_probe(struct device *dev, unsigned int id)
1233 struct counter_device *counter;
1238 if (!devm_request_region(dev, base[id], QUAD8_EXTENT, dev_name(dev))) {
1239 dev_err(dev, "Unable to lock port addresses (0x%X-0x%X)\n",
1240 base[id], base[id] + QUAD8_EXTENT);
1244 counter = devm_counter_alloc(dev, sizeof(*priv));
1247 priv = counter_priv(counter);
1249 priv->reg = devm_ioport_map(dev, base[id], QUAD8_EXTENT);
1253 /* Initialize Counter device and driver data */
1254 counter->name = dev_name(dev);
1255 counter->parent = dev;
1256 counter->ops = &quad8_ops;
1257 counter->counts = quad8_counts;
1258 counter->num_counts = ARRAY_SIZE(quad8_counts);
1259 counter->signals = quad8_signals;
1260 counter->num_signals = ARRAY_SIZE(quad8_signals);
1262 spin_lock_init(&priv->lock);
1264 /* Reset Index/Interrupt Register */
1265 iowrite8(0x00, &priv->reg->index_interrupt);
1266 /* Reset all counters and disable interrupt function */
1267 iowrite8(QUAD8_CHAN_OP_RESET_COUNTERS, &priv->reg->channel_oper);
1268 /* Set initial configuration for all counters */
1269 for (i = 0; i < QUAD8_NUM_COUNTERS; i++)
1270 quad8_init_counter(priv->reg->channel + i);
1271 /* Disable Differential Encoder Cable Status for all channels */
1272 iowrite8(0xFF, &priv->reg->cable_status);
1273 /* Enable all counters and enable interrupt function */
1274 iowrite8(QUAD8_CHAN_OP_ENABLE_INTERRUPT_FUNC, &priv->reg->channel_oper);
1276 err = devm_request_irq(&counter->dev, irq[id], quad8_irq_handler,
1277 IRQF_SHARED, counter->name, counter);
1281 err = devm_counter_add(dev, counter);
1283 return dev_err_probe(dev, err, "Failed to add counter\n");
1288 static struct isa_driver quad8_driver = {
1289 .probe = quad8_probe,
1291 .name = "104-quad-8"
1295 module_isa_driver_with_irq(quad8_driver, num_quad8, num_irq);
1297 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
1298 MODULE_DESCRIPTION("ACCES 104-QUAD-8 driver");
1299 MODULE_LICENSE("GPL v2");
1300 MODULE_IMPORT_NS(COUNTER);