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/bitfield.h>
9 #include <linux/bits.h>
10 #include <linux/counter.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
14 #include <linux/ioport.h>
15 #include <linux/interrupt.h>
16 #include <linux/isa.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/regmap.h>
22 #include <linux/spinlock.h>
23 #include <linux/types.h>
25 #include <linux/unaligned.h>
27 #define QUAD8_EXTENT 32
29 static unsigned int base
[max_num_isa_dev(QUAD8_EXTENT
)];
30 static unsigned int num_quad8
;
31 module_param_hw_array(base
, uint
, ioport
, &num_quad8
, 0);
32 MODULE_PARM_DESC(base
, "ACCES 104-QUAD-8 base addresses");
34 static unsigned int irq
[max_num_isa_dev(QUAD8_EXTENT
)];
35 static unsigned int num_irq
;
36 module_param_hw_array(irq
, uint
, irq
, &num_irq
, 0);
37 MODULE_PARM_DESC(irq
, "ACCES 104-QUAD-8 interrupt line numbers");
39 #define QUAD8_NUM_COUNTERS 8
41 #define QUAD8_DATA(_channel) ((_channel) * 2)
42 #define QUAD8_CONTROL(_channel) (QUAD8_DATA(_channel) + 1)
43 #define QUAD8_INTERRUPT_STATUS 0x10
44 #define QUAD8_CHANNEL_OPERATION 0x11
45 #define QUAD8_INDEX_INTERRUPT 0x12
46 #define QUAD8_INDEX_INPUT_LEVELS 0x16
47 #define QUAD8_CABLE_STATUS 0x17
50 * struct quad8 - device private data structure
51 * @lock: lock to prevent clobbering device states during R/W ops
52 * @cmr: array of Counter Mode Register states
53 * @ior: array of Input / Output Control Register states
54 * @idr: array of Index Control Register states
55 * @fck_prescaler: array of filter clock prescaler configurations
56 * @preset: array of preset values
57 * @cable_fault_enable: differential encoder cable status enable configurations
58 * @map: regmap for the device
62 u8 cmr
[QUAD8_NUM_COUNTERS
];
63 u8 ior
[QUAD8_NUM_COUNTERS
];
64 u8 idr
[QUAD8_NUM_COUNTERS
];
65 unsigned int fck_prescaler
[QUAD8_NUM_COUNTERS
];
66 unsigned int preset
[QUAD8_NUM_COUNTERS
];
67 unsigned int cable_fault_enable
;
71 static const struct regmap_range quad8_wr_ranges
[] = {
72 regmap_reg_range(0x0, 0xF), regmap_reg_range(0x11, 0x12), regmap_reg_range(0x17, 0x17),
74 static const struct regmap_range quad8_rd_ranges
[] = {
75 regmap_reg_range(0x0, 0x12), regmap_reg_range(0x16, 0x18),
77 static const struct regmap_access_table quad8_wr_table
= {
78 .yes_ranges
= quad8_wr_ranges
,
79 .n_yes_ranges
= ARRAY_SIZE(quad8_wr_ranges
),
81 static const struct regmap_access_table quad8_rd_table
= {
82 .yes_ranges
= quad8_rd_ranges
,
83 .n_yes_ranges
= ARRAY_SIZE(quad8_rd_ranges
),
85 static const struct regmap_config quad8_regmap_config
= {
90 .wr_table
= &quad8_wr_table
,
91 .rd_table
= &quad8_rd_table
,
97 #define FLAG_UD BIT(5)
101 #define REGISTER_SELECTION GENMASK(6, 5)
103 /* Reset and Load Signal Decoders */
104 #define SELECT_RLD u8_encode_bits(0x0, REGISTER_SELECTION)
105 /* Counter Mode Register */
106 #define SELECT_CMR u8_encode_bits(0x1, REGISTER_SELECTION)
107 /* Input / Output Control Register */
108 #define SELECT_IOR u8_encode_bits(0x2, REGISTER_SELECTION)
109 /* Index Control Register */
110 #define SELECT_IDR u8_encode_bits(0x3, REGISTER_SELECTION)
113 * Reset and Load Signal Decoders
115 #define RESETS GENMASK(2, 1)
116 #define LOADS GENMASK(4, 3)
117 /* Reset Byte Pointer (three byte data pointer) */
118 #define RESET_BP BIT(0)
119 /* Reset Borrow Toggle, Carry toggle, Compare toggle, Sign, and Index flags */
120 #define RESET_BT_CT_CPT_S_IDX u8_encode_bits(0x2, RESETS)
121 /* Reset Error flag */
122 #define RESET_E u8_encode_bits(0x3, RESETS)
123 /* Preset Register to Counter */
124 #define TRANSFER_PR_TO_CNTR u8_encode_bits(0x1, LOADS)
125 /* Transfer Counter to Output Latch */
126 #define TRANSFER_CNTR_TO_OL u8_encode_bits(0x2, LOADS)
127 /* Transfer Preset Register LSB to FCK Prescaler */
128 #define TRANSFER_PR0_TO_PSC u8_encode_bits(0x3, LOADS)
131 * Counter Mode Registers
133 #define COUNT_ENCODING BIT(0)
134 #define COUNT_MODE GENMASK(2, 1)
135 #define QUADRATURE_MODE GENMASK(4, 3)
137 #define BINARY u8_encode_bits(0x0, COUNT_ENCODING)
139 #define NORMAL_COUNT 0x0
141 #define RANGE_LIMIT 0x1
142 /* Non-recycle count */
143 #define NON_RECYCLE_COUNT 0x2
147 #define NON_QUADRATURE 0x0
149 #define QUADRATURE_X1 0x1
151 #define QUADRATURE_X2 0x2
153 #define QUADRATURE_X4 0x3
156 * Input/Output Control Register
158 #define AB_GATE BIT(0)
159 #define LOAD_PIN BIT(1)
160 #define FLG_PINS GENMASK(4, 3)
161 /* Disable inputs A and B */
162 #define DISABLE_AB u8_encode_bits(0x0, AB_GATE)
163 /* Load Counter input */
164 #define LOAD_CNTR 0x0
165 /* FLG1 = CARRY(active low); FLG2 = BORROW(active low) */
166 #define FLG1_CARRY_FLG2_BORROW 0x0
167 /* FLG1 = COMPARE(active low); FLG2 = BORROW(active low) */
168 #define FLG1_COMPARE_FLG2_BORROW 0x1
169 /* FLG1 = Carry(active low)/Borrow(active low); FLG2 = U/D(active low) flag */
170 #define FLG1_CARRYBORROW_FLG2_UD 0x2
171 /* FLG1 = INDX (low pulse at INDEX pin active level); FLG2 = E flag */
172 #define FLG1_INDX_FLG2_E 0x3
175 * INDEX CONTROL REGISTERS
177 #define INDEX_MODE BIT(0)
178 #define INDEX_POLARITY BIT(1)
179 /* Disable Index mode */
180 #define DISABLE_INDEX_MODE 0x0
181 /* Enable Index mode */
182 #define ENABLE_INDEX_MODE 0x1
183 /* Negative Index Polarity */
184 #define NEGATIVE_INDEX_POLARITY 0x0
185 /* Positive Index Polarity */
186 #define POSITIVE_INDEX_POLARITY 0x1
189 * Channel Operation Register
191 #define COUNTERS_OPERATION BIT(0)
192 #define INTERRUPT_FUNCTION BIT(2)
193 /* Enable all Counters */
194 #define ENABLE_COUNTERS u8_encode_bits(0x0, COUNTERS_OPERATION)
195 /* Reset all Counters */
196 #define RESET_COUNTERS u8_encode_bits(0x1, COUNTERS_OPERATION)
197 /* Disable the interrupt function */
198 #define DISABLE_INTERRUPT_FUNCTION u8_encode_bits(0x0, INTERRUPT_FUNCTION)
199 /* Enable the interrupt function */
200 #define ENABLE_INTERRUPT_FUNCTION u8_encode_bits(0x1, INTERRUPT_FUNCTION)
201 /* Any write to the Channel Operation register clears any pending interrupts */
202 #define CLEAR_PENDING_INTERRUPTS (ENABLE_COUNTERS | ENABLE_INTERRUPT_FUNCTION)
204 /* Each Counter is 24 bits wide */
205 #define LS7267_CNTR_MAX GENMASK(23, 0)
207 static __always_inline
int quad8_control_register_update(struct regmap
*const map
, u8
*const buf
,
208 const size_t channel
, const u8 val
,
211 u8p_replace_bits(&buf
[channel
], val
, field
);
212 return regmap_write(map
, QUAD8_CONTROL(channel
), buf
[channel
]);
215 static int quad8_signal_read(struct counter_device
*counter
,
216 struct counter_signal
*signal
,
217 enum counter_signal_level
*level
)
219 const struct quad8
*const priv
= counter_priv(counter
);
222 /* Only Index signal levels can be read */
226 ret
= regmap_test_bits(priv
->map
, QUAD8_INDEX_INPUT_LEVELS
, BIT(signal
->id
- 16));
230 *level
= (ret
) ? COUNTER_SIGNAL_LEVEL_HIGH
: COUNTER_SIGNAL_LEVEL_LOW
;
235 static int quad8_count_read(struct counter_device
*counter
,
236 struct counter_count
*count
, u64
*val
)
238 struct quad8
*const priv
= counter_priv(counter
);
239 unsigned long irqflags
;
243 spin_lock_irqsave(&priv
->lock
, irqflags
);
245 ret
= regmap_write(priv
->map
, QUAD8_CONTROL(count
->id
),
246 SELECT_RLD
| RESET_BP
| TRANSFER_CNTR_TO_OL
);
249 ret
= regmap_noinc_read(priv
->map
, QUAD8_DATA(count
->id
), value
, sizeof(value
));
252 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
254 *val
= get_unaligned_le24(value
);
259 static int quad8_preset_register_set(struct quad8
*const priv
, const size_t id
,
260 const unsigned long preset
)
265 put_unaligned_le24(preset
, value
);
267 ret
= regmap_write(priv
->map
, QUAD8_CONTROL(id
), SELECT_RLD
| RESET_BP
);
270 return regmap_noinc_write(priv
->map
, QUAD8_DATA(id
), value
, sizeof(value
));
273 static int quad8_flag_register_reset(struct quad8
*const priv
, const size_t id
)
277 ret
= regmap_write(priv
->map
, QUAD8_CONTROL(id
), SELECT_RLD
| RESET_BT_CT_CPT_S_IDX
);
280 return regmap_write(priv
->map
, QUAD8_CONTROL(id
), SELECT_RLD
| RESET_E
);
283 static int quad8_count_write(struct counter_device
*counter
,
284 struct counter_count
*count
, u64 val
)
286 struct quad8
*const priv
= counter_priv(counter
);
287 unsigned long irqflags
;
290 if (val
> LS7267_CNTR_MAX
)
293 spin_lock_irqsave(&priv
->lock
, irqflags
);
295 /* Counter can only be set via Preset Register */
296 ret
= quad8_preset_register_set(priv
, count
->id
, val
);
299 ret
= regmap_write(priv
->map
, QUAD8_CONTROL(count
->id
), SELECT_RLD
| TRANSFER_PR_TO_CNTR
);
303 ret
= quad8_flag_register_reset(priv
, count
->id
);
307 /* Set Preset Register back to original value */
308 ret
= quad8_preset_register_set(priv
, count
->id
, priv
->preset
[count
->id
]);
311 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
316 static const enum counter_function quad8_count_functions_list
[] = {
317 COUNTER_FUNCTION_PULSE_DIRECTION
,
318 COUNTER_FUNCTION_QUADRATURE_X1_A
,
319 COUNTER_FUNCTION_QUADRATURE_X2_A
,
320 COUNTER_FUNCTION_QUADRATURE_X4
,
323 static int quad8_function_get(const struct quad8
*const priv
, const size_t id
,
324 enum counter_function
*const function
)
326 switch (u8_get_bits(priv
->cmr
[id
], QUADRATURE_MODE
)) {
328 *function
= COUNTER_FUNCTION_PULSE_DIRECTION
;
331 *function
= COUNTER_FUNCTION_QUADRATURE_X1_A
;
334 *function
= COUNTER_FUNCTION_QUADRATURE_X2_A
;
337 *function
= COUNTER_FUNCTION_QUADRATURE_X4
;
340 /* should never reach this path */
345 static int quad8_function_read(struct counter_device
*counter
,
346 struct counter_count
*count
,
347 enum counter_function
*function
)
349 struct quad8
*const priv
= counter_priv(counter
);
350 unsigned long irqflags
;
353 spin_lock_irqsave(&priv
->lock
, irqflags
);
355 retval
= quad8_function_get(priv
, count
->id
, function
);
357 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
362 static int quad8_function_write(struct counter_device
*counter
,
363 struct counter_count
*count
,
364 enum counter_function function
)
366 struct quad8
*const priv
= counter_priv(counter
);
367 const int id
= count
->id
;
368 unsigned long irqflags
;
369 unsigned int mode_cfg
;
370 bool synchronous_mode
;
374 case COUNTER_FUNCTION_PULSE_DIRECTION
:
375 mode_cfg
= NON_QUADRATURE
;
377 case COUNTER_FUNCTION_QUADRATURE_X1_A
:
378 mode_cfg
= QUADRATURE_X1
;
380 case COUNTER_FUNCTION_QUADRATURE_X2_A
:
381 mode_cfg
= QUADRATURE_X2
;
383 case COUNTER_FUNCTION_QUADRATURE_X4
:
384 mode_cfg
= QUADRATURE_X4
;
387 /* should never reach this path */
391 spin_lock_irqsave(&priv
->lock
, irqflags
);
393 /* Synchronous function not supported in non-quadrature mode */
394 synchronous_mode
= u8_get_bits(priv
->idr
[id
], INDEX_MODE
) == ENABLE_INDEX_MODE
;
395 if (synchronous_mode
&& mode_cfg
== NON_QUADRATURE
) {
396 ret
= quad8_control_register_update(priv
->map
, priv
->idr
, id
, DISABLE_INDEX_MODE
,
402 ret
= quad8_control_register_update(priv
->map
, priv
->cmr
, id
, mode_cfg
, QUADRATURE_MODE
);
405 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
410 static int quad8_direction_read(struct counter_device
*counter
,
411 struct counter_count
*count
,
412 enum counter_count_direction
*direction
)
414 const struct quad8
*const priv
= counter_priv(counter
);
418 ret
= regmap_read(priv
->map
, QUAD8_CONTROL(count
->id
), &flag
);
421 *direction
= (u8_get_bits(flag
, FLAG_UD
) == UP
) ? COUNTER_COUNT_DIRECTION_FORWARD
:
422 COUNTER_COUNT_DIRECTION_BACKWARD
;
427 static const enum counter_synapse_action quad8_index_actions_list
[] = {
428 COUNTER_SYNAPSE_ACTION_NONE
,
429 COUNTER_SYNAPSE_ACTION_RISING_EDGE
,
432 static const enum counter_synapse_action quad8_synapse_actions_list
[] = {
433 COUNTER_SYNAPSE_ACTION_NONE
,
434 COUNTER_SYNAPSE_ACTION_RISING_EDGE
,
435 COUNTER_SYNAPSE_ACTION_FALLING_EDGE
,
436 COUNTER_SYNAPSE_ACTION_BOTH_EDGES
,
439 static int quad8_action_read(struct counter_device
*counter
,
440 struct counter_count
*count
,
441 struct counter_synapse
*synapse
,
442 enum counter_synapse_action
*action
)
444 struct quad8
*const priv
= counter_priv(counter
);
445 unsigned long irqflags
;
447 enum counter_function function
;
448 const size_t signal_a_id
= count
->synapses
[0].signal
->id
;
449 enum counter_count_direction direction
;
451 /* Default action mode */
452 *action
= COUNTER_SYNAPSE_ACTION_NONE
;
454 /* Handle Index signals */
455 if (synapse
->signal
->id
>= 16) {
456 if (u8_get_bits(priv
->ior
[count
->id
], LOAD_PIN
) == LOAD_CNTR
)
457 *action
= COUNTER_SYNAPSE_ACTION_RISING_EDGE
;
461 spin_lock_irqsave(&priv
->lock
, irqflags
);
463 /* Get Count function and direction atomically */
464 err
= quad8_function_get(priv
, count
->id
, &function
);
466 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
469 err
= quad8_direction_read(counter
, count
, &direction
);
471 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
475 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
477 /* Determine action mode based on current count function mode */
479 case COUNTER_FUNCTION_PULSE_DIRECTION
:
480 if (synapse
->signal
->id
== signal_a_id
)
481 *action
= COUNTER_SYNAPSE_ACTION_RISING_EDGE
;
483 case COUNTER_FUNCTION_QUADRATURE_X1_A
:
484 if (synapse
->signal
->id
== signal_a_id
) {
485 if (direction
== COUNTER_COUNT_DIRECTION_FORWARD
)
486 *action
= COUNTER_SYNAPSE_ACTION_RISING_EDGE
;
488 *action
= COUNTER_SYNAPSE_ACTION_FALLING_EDGE
;
491 case COUNTER_FUNCTION_QUADRATURE_X2_A
:
492 if (synapse
->signal
->id
== signal_a_id
)
493 *action
= COUNTER_SYNAPSE_ACTION_BOTH_EDGES
;
495 case COUNTER_FUNCTION_QUADRATURE_X4
:
496 *action
= COUNTER_SYNAPSE_ACTION_BOTH_EDGES
;
499 /* should never reach this path */
504 static int quad8_events_configure(struct counter_device
*counter
)
506 struct quad8
*const priv
= counter_priv(counter
);
507 unsigned long irq_enabled
= 0;
508 unsigned long irqflags
;
509 struct counter_event_node
*event_node
;
513 spin_lock_irqsave(&priv
->lock
, irqflags
);
515 list_for_each_entry(event_node
, &counter
->events_list
, l
) {
516 switch (event_node
->event
) {
517 case COUNTER_EVENT_OVERFLOW
:
518 flg_pins
= FLG1_CARRY_FLG2_BORROW
;
520 case COUNTER_EVENT_THRESHOLD
:
521 flg_pins
= FLG1_COMPARE_FLG2_BORROW
;
523 case COUNTER_EVENT_OVERFLOW_UNDERFLOW
:
524 flg_pins
= FLG1_CARRYBORROW_FLG2_UD
;
526 case COUNTER_EVENT_INDEX
:
527 flg_pins
= FLG1_INDX_FLG2_E
;
530 /* should never reach this path */
535 /* Enable IRQ line */
536 irq_enabled
|= BIT(event_node
->channel
);
538 /* Skip configuration if it is the same as previously set */
539 if (flg_pins
== u8_get_bits(priv
->ior
[event_node
->channel
], FLG_PINS
))
542 /* Save new IRQ function configuration */
543 ret
= quad8_control_register_update(priv
->map
, priv
->ior
, event_node
->channel
,
549 ret
= regmap_write(priv
->map
, QUAD8_INDEX_INTERRUPT
, irq_enabled
);
552 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
557 static int quad8_watch_validate(struct counter_device
*counter
,
558 const struct counter_watch
*watch
)
560 struct counter_event_node
*event_node
;
562 if (watch
->channel
> QUAD8_NUM_COUNTERS
- 1)
565 switch (watch
->event
) {
566 case COUNTER_EVENT_OVERFLOW
:
567 case COUNTER_EVENT_THRESHOLD
:
568 case COUNTER_EVENT_OVERFLOW_UNDERFLOW
:
569 case COUNTER_EVENT_INDEX
:
570 list_for_each_entry(event_node
, &counter
->next_events_list
, l
)
571 if (watch
->channel
== event_node
->channel
&&
572 watch
->event
!= event_node
->event
)
580 static const struct counter_ops quad8_ops
= {
581 .signal_read
= quad8_signal_read
,
582 .count_read
= quad8_count_read
,
583 .count_write
= quad8_count_write
,
584 .function_read
= quad8_function_read
,
585 .function_write
= quad8_function_write
,
586 .action_read
= quad8_action_read
,
587 .events_configure
= quad8_events_configure
,
588 .watch_validate
= quad8_watch_validate
,
591 static const char *const quad8_index_polarity_modes
[] = {
596 static int quad8_index_polarity_get(struct counter_device
*counter
,
597 struct counter_signal
*signal
,
600 const struct quad8
*const priv
= counter_priv(counter
);
601 const size_t channel_id
= signal
->id
- 16;
603 *index_polarity
= u8_get_bits(priv
->idr
[channel_id
], INDEX_POLARITY
);
608 static int quad8_index_polarity_set(struct counter_device
*counter
,
609 struct counter_signal
*signal
,
612 struct quad8
*const priv
= counter_priv(counter
);
613 const size_t channel_id
= signal
->id
- 16;
614 unsigned long irqflags
;
617 spin_lock_irqsave(&priv
->lock
, irqflags
);
619 ret
= quad8_control_register_update(priv
->map
, priv
->idr
, channel_id
, index_polarity
,
622 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
627 static int quad8_polarity_read(struct counter_device
*counter
,
628 struct counter_signal
*signal
,
629 enum counter_signal_polarity
*polarity
)
634 err
= quad8_index_polarity_get(counter
, signal
, &index_polarity
);
638 *polarity
= (index_polarity
== POSITIVE_INDEX_POLARITY
) ? COUNTER_SIGNAL_POLARITY_POSITIVE
:
639 COUNTER_SIGNAL_POLARITY_NEGATIVE
;
644 static int quad8_polarity_write(struct counter_device
*counter
,
645 struct counter_signal
*signal
,
646 enum counter_signal_polarity polarity
)
648 const u32 pol
= (polarity
== COUNTER_SIGNAL_POLARITY_POSITIVE
) ? POSITIVE_INDEX_POLARITY
:
649 NEGATIVE_INDEX_POLARITY
;
651 return quad8_index_polarity_set(counter
, signal
, pol
);
654 static const char *const quad8_synchronous_modes
[] = {
659 static int quad8_synchronous_mode_get(struct counter_device
*counter
,
660 struct counter_signal
*signal
,
661 u32
*synchronous_mode
)
663 const struct quad8
*const priv
= counter_priv(counter
);
664 const size_t channel_id
= signal
->id
- 16;
666 *synchronous_mode
= u8_get_bits(priv
->idr
[channel_id
], INDEX_MODE
);
671 static int quad8_synchronous_mode_set(struct counter_device
*counter
,
672 struct counter_signal
*signal
,
673 u32 synchronous_mode
)
675 struct quad8
*const priv
= counter_priv(counter
);
676 const size_t channel_id
= signal
->id
- 16;
678 unsigned long irqflags
;
681 spin_lock_irqsave(&priv
->lock
, irqflags
);
683 /* Index function must be non-synchronous in non-quadrature mode */
684 quadrature_mode
= u8_get_bits(priv
->idr
[channel_id
], QUADRATURE_MODE
);
685 if (synchronous_mode
&& quadrature_mode
== NON_QUADRATURE
) {
690 ret
= quad8_control_register_update(priv
->map
, priv
->idr
, channel_id
, synchronous_mode
,
694 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
699 static int quad8_count_floor_read(struct counter_device
*counter
,
700 struct counter_count
*count
, u64
*floor
)
702 /* Only a floor of 0 is supported */
708 static int quad8_count_mode_read(struct counter_device
*counter
,
709 struct counter_count
*count
,
710 enum counter_count_mode
*cnt_mode
)
712 const struct quad8
*const priv
= counter_priv(counter
);
714 switch (u8_get_bits(priv
->cmr
[count
->id
], COUNT_MODE
)) {
716 *cnt_mode
= COUNTER_COUNT_MODE_NORMAL
;
719 *cnt_mode
= COUNTER_COUNT_MODE_RANGE_LIMIT
;
721 case NON_RECYCLE_COUNT
:
722 *cnt_mode
= COUNTER_COUNT_MODE_NON_RECYCLE
;
725 *cnt_mode
= COUNTER_COUNT_MODE_MODULO_N
;
732 static int quad8_count_mode_write(struct counter_device
*counter
,
733 struct counter_count
*count
,
734 enum counter_count_mode cnt_mode
)
736 struct quad8
*const priv
= counter_priv(counter
);
737 unsigned int count_mode
;
738 unsigned long irqflags
;
742 case COUNTER_COUNT_MODE_NORMAL
:
743 count_mode
= NORMAL_COUNT
;
745 case COUNTER_COUNT_MODE_RANGE_LIMIT
:
746 count_mode
= RANGE_LIMIT
;
748 case COUNTER_COUNT_MODE_NON_RECYCLE
:
749 count_mode
= NON_RECYCLE_COUNT
;
751 case COUNTER_COUNT_MODE_MODULO_N
:
752 count_mode
= MODULO_N
;
755 /* should never reach this path */
759 spin_lock_irqsave(&priv
->lock
, irqflags
);
761 ret
= quad8_control_register_update(priv
->map
, priv
->cmr
, count
->id
, count_mode
,
764 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
769 static int quad8_count_enable_read(struct counter_device
*counter
,
770 struct counter_count
*count
, u8
*enable
)
772 const struct quad8
*const priv
= counter_priv(counter
);
774 *enable
= u8_get_bits(priv
->ior
[count
->id
], AB_GATE
);
779 static int quad8_count_enable_write(struct counter_device
*counter
,
780 struct counter_count
*count
, u8 enable
)
782 struct quad8
*const priv
= counter_priv(counter
);
783 unsigned long irqflags
;
786 spin_lock_irqsave(&priv
->lock
, irqflags
);
788 ret
= quad8_control_register_update(priv
->map
, priv
->ior
, count
->id
, enable
, AB_GATE
);
790 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
795 static const char *const quad8_noise_error_states
[] = {
796 "No excessive noise is present at the count inputs",
797 "Excessive noise is present at the count inputs"
800 static int quad8_error_noise_get(struct counter_device
*counter
,
801 struct counter_count
*count
, u32
*noise_error
)
803 const struct quad8
*const priv
= counter_priv(counter
);
807 ret
= regmap_read(priv
->map
, QUAD8_CONTROL(count
->id
), &flag
);
810 *noise_error
= u8_get_bits(flag
, FLAG_E
);
815 static int quad8_count_preset_read(struct counter_device
*counter
,
816 struct counter_count
*count
, u64
*preset
)
818 const struct quad8
*const priv
= counter_priv(counter
);
820 *preset
= priv
->preset
[count
->id
];
825 static int quad8_count_preset_write(struct counter_device
*counter
,
826 struct counter_count
*count
, u64 preset
)
828 struct quad8
*const priv
= counter_priv(counter
);
829 unsigned long irqflags
;
832 if (preset
> LS7267_CNTR_MAX
)
835 spin_lock_irqsave(&priv
->lock
, irqflags
);
837 priv
->preset
[count
->id
] = preset
;
838 ret
= quad8_preset_register_set(priv
, count
->id
, preset
);
840 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
845 static int quad8_count_ceiling_read(struct counter_device
*counter
,
846 struct counter_count
*count
, u64
*ceiling
)
848 struct quad8
*const priv
= counter_priv(counter
);
849 unsigned long irqflags
;
851 spin_lock_irqsave(&priv
->lock
, irqflags
);
853 /* Range Limit and Modulo-N count modes use preset value as ceiling */
854 switch (u8_get_bits(priv
->cmr
[count
->id
], COUNT_MODE
)) {
857 *ceiling
= priv
->preset
[count
->id
];
860 *ceiling
= LS7267_CNTR_MAX
;
864 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
869 static int quad8_count_ceiling_write(struct counter_device
*counter
,
870 struct counter_count
*count
, u64 ceiling
)
872 struct quad8
*const priv
= counter_priv(counter
);
873 unsigned long irqflags
;
876 if (ceiling
> LS7267_CNTR_MAX
)
879 spin_lock_irqsave(&priv
->lock
, irqflags
);
881 /* Range Limit and Modulo-N count modes use preset value as ceiling */
882 switch (u8_get_bits(priv
->cmr
[count
->id
], COUNT_MODE
)) {
885 priv
->preset
[count
->id
] = ceiling
;
886 ret
= quad8_preset_register_set(priv
, count
->id
, ceiling
);
893 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
898 static int quad8_count_preset_enable_read(struct counter_device
*counter
,
899 struct counter_count
*count
,
902 const struct quad8
*const priv
= counter_priv(counter
);
904 /* Preset enable is active low in Input/Output Control register */
905 *preset_enable
= !u8_get_bits(priv
->ior
[count
->id
], LOAD_PIN
);
910 static int quad8_count_preset_enable_write(struct counter_device
*counter
,
911 struct counter_count
*count
,
914 struct quad8
*const priv
= counter_priv(counter
);
915 unsigned long irqflags
;
918 spin_lock_irqsave(&priv
->lock
, irqflags
);
920 /* Preset enable is active low in Input/Output Control register */
921 ret
= quad8_control_register_update(priv
->map
, priv
->ior
, count
->id
, !preset_enable
,
924 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
929 static int quad8_signal_cable_fault_read(struct counter_device
*counter
,
930 struct counter_signal
*signal
,
933 struct quad8
*const priv
= counter_priv(counter
);
934 const size_t channel_id
= signal
->id
/ 2;
935 unsigned long irqflags
;
939 spin_lock_irqsave(&priv
->lock
, irqflags
);
941 disabled
= !(priv
->cable_fault_enable
& BIT(channel_id
));
944 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
948 ret
= regmap_test_bits(priv
->map
, QUAD8_CABLE_STATUS
, BIT(channel_id
));
950 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
954 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
956 /* Logic 0 = cable fault */
962 static int quad8_signal_cable_fault_enable_read(struct counter_device
*counter
,
963 struct counter_signal
*signal
,
966 const struct quad8
*const priv
= counter_priv(counter
);
967 const size_t channel_id
= signal
->id
/ 2;
969 *enable
= !!(priv
->cable_fault_enable
& BIT(channel_id
));
974 static int quad8_signal_cable_fault_enable_write(struct counter_device
*counter
,
975 struct counter_signal
*signal
,
978 struct quad8
*const priv
= counter_priv(counter
);
979 const size_t channel_id
= signal
->id
/ 2;
980 unsigned long irqflags
;
981 unsigned int cable_fault_enable
;
984 spin_lock_irqsave(&priv
->lock
, irqflags
);
987 priv
->cable_fault_enable
|= BIT(channel_id
);
989 priv
->cable_fault_enable
&= ~BIT(channel_id
);
991 /* Enable is active low in Differential Encoder Cable Status register */
992 cable_fault_enable
= ~priv
->cable_fault_enable
;
994 ret
= regmap_write(priv
->map
, QUAD8_CABLE_STATUS
, cable_fault_enable
);
996 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
1001 static int quad8_signal_fck_prescaler_read(struct counter_device
*counter
,
1002 struct counter_signal
*signal
,
1005 const struct quad8
*const priv
= counter_priv(counter
);
1007 *prescaler
= priv
->fck_prescaler
[signal
->id
/ 2];
1012 static int quad8_filter_clock_prescaler_set(struct quad8
*const priv
, const size_t id
,
1017 ret
= regmap_write(priv
->map
, QUAD8_CONTROL(id
), SELECT_RLD
| RESET_BP
);
1020 ret
= regmap_write(priv
->map
, QUAD8_DATA(id
), prescaler
);
1023 return regmap_write(priv
->map
, QUAD8_CONTROL(id
), SELECT_RLD
| TRANSFER_PR0_TO_PSC
);
1026 static int quad8_signal_fck_prescaler_write(struct counter_device
*counter
,
1027 struct counter_signal
*signal
,
1030 struct quad8
*const priv
= counter_priv(counter
);
1031 const size_t channel_id
= signal
->id
/ 2;
1032 unsigned long irqflags
;
1035 spin_lock_irqsave(&priv
->lock
, irqflags
);
1037 priv
->fck_prescaler
[channel_id
] = prescaler
;
1038 ret
= quad8_filter_clock_prescaler_set(priv
, channel_id
, prescaler
);
1040 spin_unlock_irqrestore(&priv
->lock
, irqflags
);
1045 static struct counter_comp quad8_signal_ext
[] = {
1046 COUNTER_COMP_SIGNAL_BOOL("cable_fault", quad8_signal_cable_fault_read
,
1048 COUNTER_COMP_SIGNAL_BOOL("cable_fault_enable",
1049 quad8_signal_cable_fault_enable_read
,
1050 quad8_signal_cable_fault_enable_write
),
1051 COUNTER_COMP_SIGNAL_U8("filter_clock_prescaler",
1052 quad8_signal_fck_prescaler_read
,
1053 quad8_signal_fck_prescaler_write
)
1056 static const enum counter_signal_polarity quad8_polarities
[] = {
1057 COUNTER_SIGNAL_POLARITY_POSITIVE
,
1058 COUNTER_SIGNAL_POLARITY_NEGATIVE
,
1061 static DEFINE_COUNTER_AVAILABLE(quad8_polarity_available
, quad8_polarities
);
1063 static DEFINE_COUNTER_ENUM(quad8_index_pol_enum
, quad8_index_polarity_modes
);
1064 static DEFINE_COUNTER_ENUM(quad8_synch_mode_enum
, quad8_synchronous_modes
);
1066 static struct counter_comp quad8_index_ext
[] = {
1067 COUNTER_COMP_SIGNAL_ENUM("index_polarity", quad8_index_polarity_get
,
1068 quad8_index_polarity_set
,
1069 quad8_index_pol_enum
),
1070 COUNTER_COMP_POLARITY(quad8_polarity_read
, quad8_polarity_write
,
1071 quad8_polarity_available
),
1072 COUNTER_COMP_SIGNAL_ENUM("synchronous_mode", quad8_synchronous_mode_get
,
1073 quad8_synchronous_mode_set
,
1074 quad8_synch_mode_enum
),
1077 #define QUAD8_QUAD_SIGNAL(_id, _name) { \
1080 .ext = quad8_signal_ext, \
1081 .num_ext = ARRAY_SIZE(quad8_signal_ext) \
1084 #define QUAD8_INDEX_SIGNAL(_id, _name) { \
1087 .ext = quad8_index_ext, \
1088 .num_ext = ARRAY_SIZE(quad8_index_ext) \
1091 static struct counter_signal quad8_signals
[] = {
1092 QUAD8_QUAD_SIGNAL(0, "Channel 1 Quadrature A"),
1093 QUAD8_QUAD_SIGNAL(1, "Channel 1 Quadrature B"),
1094 QUAD8_QUAD_SIGNAL(2, "Channel 2 Quadrature A"),
1095 QUAD8_QUAD_SIGNAL(3, "Channel 2 Quadrature B"),
1096 QUAD8_QUAD_SIGNAL(4, "Channel 3 Quadrature A"),
1097 QUAD8_QUAD_SIGNAL(5, "Channel 3 Quadrature B"),
1098 QUAD8_QUAD_SIGNAL(6, "Channel 4 Quadrature A"),
1099 QUAD8_QUAD_SIGNAL(7, "Channel 4 Quadrature B"),
1100 QUAD8_QUAD_SIGNAL(8, "Channel 5 Quadrature A"),
1101 QUAD8_QUAD_SIGNAL(9, "Channel 5 Quadrature B"),
1102 QUAD8_QUAD_SIGNAL(10, "Channel 6 Quadrature A"),
1103 QUAD8_QUAD_SIGNAL(11, "Channel 6 Quadrature B"),
1104 QUAD8_QUAD_SIGNAL(12, "Channel 7 Quadrature A"),
1105 QUAD8_QUAD_SIGNAL(13, "Channel 7 Quadrature B"),
1106 QUAD8_QUAD_SIGNAL(14, "Channel 8 Quadrature A"),
1107 QUAD8_QUAD_SIGNAL(15, "Channel 8 Quadrature B"),
1108 QUAD8_INDEX_SIGNAL(16, "Channel 1 Index"),
1109 QUAD8_INDEX_SIGNAL(17, "Channel 2 Index"),
1110 QUAD8_INDEX_SIGNAL(18, "Channel 3 Index"),
1111 QUAD8_INDEX_SIGNAL(19, "Channel 4 Index"),
1112 QUAD8_INDEX_SIGNAL(20, "Channel 5 Index"),
1113 QUAD8_INDEX_SIGNAL(21, "Channel 6 Index"),
1114 QUAD8_INDEX_SIGNAL(22, "Channel 7 Index"),
1115 QUAD8_INDEX_SIGNAL(23, "Channel 8 Index")
1118 #define QUAD8_COUNT_SYNAPSES(_id) { \
1120 .actions_list = quad8_synapse_actions_list, \
1121 .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
1122 .signal = quad8_signals + 2 * (_id) \
1125 .actions_list = quad8_synapse_actions_list, \
1126 .num_actions = ARRAY_SIZE(quad8_synapse_actions_list), \
1127 .signal = quad8_signals + 2 * (_id) + 1 \
1130 .actions_list = quad8_index_actions_list, \
1131 .num_actions = ARRAY_SIZE(quad8_index_actions_list), \
1132 .signal = quad8_signals + 2 * (_id) + 16 \
1136 static struct counter_synapse quad8_count_synapses
[][3] = {
1137 QUAD8_COUNT_SYNAPSES(0), QUAD8_COUNT_SYNAPSES(1),
1138 QUAD8_COUNT_SYNAPSES(2), QUAD8_COUNT_SYNAPSES(3),
1139 QUAD8_COUNT_SYNAPSES(4), QUAD8_COUNT_SYNAPSES(5),
1140 QUAD8_COUNT_SYNAPSES(6), QUAD8_COUNT_SYNAPSES(7)
1143 static const enum counter_count_mode quad8_cnt_modes
[] = {
1144 COUNTER_COUNT_MODE_NORMAL
,
1145 COUNTER_COUNT_MODE_RANGE_LIMIT
,
1146 COUNTER_COUNT_MODE_NON_RECYCLE
,
1147 COUNTER_COUNT_MODE_MODULO_N
,
1150 static DEFINE_COUNTER_AVAILABLE(quad8_count_mode_available
, quad8_cnt_modes
);
1152 static DEFINE_COUNTER_ENUM(quad8_error_noise_enum
, quad8_noise_error_states
);
1154 static struct counter_comp quad8_count_ext
[] = {
1155 COUNTER_COMP_CEILING(quad8_count_ceiling_read
,
1156 quad8_count_ceiling_write
),
1157 COUNTER_COMP_FLOOR(quad8_count_floor_read
, NULL
),
1158 COUNTER_COMP_COUNT_MODE(quad8_count_mode_read
, quad8_count_mode_write
,
1159 quad8_count_mode_available
),
1160 COUNTER_COMP_DIRECTION(quad8_direction_read
),
1161 COUNTER_COMP_ENABLE(quad8_count_enable_read
, quad8_count_enable_write
),
1162 COUNTER_COMP_COUNT_ENUM("error_noise", quad8_error_noise_get
, NULL
,
1163 quad8_error_noise_enum
),
1164 COUNTER_COMP_PRESET(quad8_count_preset_read
, quad8_count_preset_write
),
1165 COUNTER_COMP_PRESET_ENABLE(quad8_count_preset_enable_read
,
1166 quad8_count_preset_enable_write
),
1169 #define QUAD8_COUNT(_id, _cntname) { \
1171 .name = (_cntname), \
1172 .functions_list = quad8_count_functions_list, \
1173 .num_functions = ARRAY_SIZE(quad8_count_functions_list), \
1174 .synapses = quad8_count_synapses[(_id)], \
1175 .num_synapses = 2, \
1176 .ext = quad8_count_ext, \
1177 .num_ext = ARRAY_SIZE(quad8_count_ext) \
1180 static struct counter_count quad8_counts
[] = {
1181 QUAD8_COUNT(0, "Channel 1 Count"),
1182 QUAD8_COUNT(1, "Channel 2 Count"),
1183 QUAD8_COUNT(2, "Channel 3 Count"),
1184 QUAD8_COUNT(3, "Channel 4 Count"),
1185 QUAD8_COUNT(4, "Channel 5 Count"),
1186 QUAD8_COUNT(5, "Channel 6 Count"),
1187 QUAD8_COUNT(6, "Channel 7 Count"),
1188 QUAD8_COUNT(7, "Channel 8 Count")
1191 static irqreturn_t
quad8_irq_handler(int irq
, void *private)
1193 struct counter_device
*counter
= private;
1194 struct quad8
*const priv
= counter_priv(counter
);
1195 unsigned int status
;
1196 unsigned long irq_status
;
1197 unsigned long channel
;
1198 unsigned int flg_pins
;
1202 ret
= regmap_read(priv
->map
, QUAD8_INTERRUPT_STATUS
, &status
);
1208 irq_status
= status
;
1209 for_each_set_bit(channel
, &irq_status
, QUAD8_NUM_COUNTERS
) {
1210 flg_pins
= u8_get_bits(priv
->ior
[channel
], FLG_PINS
);
1212 case FLG1_CARRY_FLG2_BORROW
:
1213 event
= COUNTER_EVENT_OVERFLOW
;
1215 case FLG1_COMPARE_FLG2_BORROW
:
1216 event
= COUNTER_EVENT_THRESHOLD
;
1218 case FLG1_CARRYBORROW_FLG2_UD
:
1219 event
= COUNTER_EVENT_OVERFLOW_UNDERFLOW
;
1221 case FLG1_INDX_FLG2_E
:
1222 event
= COUNTER_EVENT_INDEX
;
1225 /* should never reach this path */
1226 WARN_ONCE(true, "invalid interrupt trigger function %u configured for channel %lu\n",
1231 counter_push_event(counter
, event
, channel
);
1234 ret
= regmap_write(priv
->map
, QUAD8_CHANNEL_OPERATION
, CLEAR_PENDING_INTERRUPTS
);
1241 static int quad8_init_counter(struct quad8
*const priv
, const size_t channel
)
1245 ret
= quad8_filter_clock_prescaler_set(priv
, channel
, 0);
1248 ret
= quad8_preset_register_set(priv
, channel
, 0);
1251 ret
= quad8_flag_register_reset(priv
, channel
);
1255 /* Binary encoding; Normal count; non-quadrature mode */
1256 priv
->cmr
[channel
] = SELECT_CMR
| BINARY
| u8_encode_bits(NORMAL_COUNT
, COUNT_MODE
) |
1257 u8_encode_bits(NON_QUADRATURE
, QUADRATURE_MODE
);
1258 ret
= regmap_write(priv
->map
, QUAD8_CONTROL(channel
), priv
->cmr
[channel
]);
1262 /* Disable A and B inputs; preset on index; FLG1 as Carry */
1263 priv
->ior
[channel
] = SELECT_IOR
| DISABLE_AB
| u8_encode_bits(LOAD_CNTR
, LOAD_PIN
) |
1264 u8_encode_bits(FLG1_CARRY_FLG2_BORROW
, FLG_PINS
);
1265 ret
= regmap_write(priv
->map
, QUAD8_CONTROL(channel
), priv
->ior
[channel
]);
1269 /* Disable index function; negative index polarity */
1270 priv
->idr
[channel
] = SELECT_IDR
| u8_encode_bits(DISABLE_INDEX_MODE
, INDEX_MODE
) |
1271 u8_encode_bits(NEGATIVE_INDEX_POLARITY
, INDEX_POLARITY
);
1272 return regmap_write(priv
->map
, QUAD8_CONTROL(channel
), priv
->idr
[channel
]);
1275 static int quad8_probe(struct device
*dev
, unsigned int id
)
1277 struct counter_device
*counter
;
1283 if (!devm_request_region(dev
, base
[id
], QUAD8_EXTENT
, dev_name(dev
))) {
1284 dev_err(dev
, "Unable to lock port addresses (0x%X-0x%X)\n",
1285 base
[id
], base
[id
] + QUAD8_EXTENT
);
1289 counter
= devm_counter_alloc(dev
, sizeof(*priv
));
1292 priv
= counter_priv(counter
);
1294 regs
= devm_ioport_map(dev
, base
[id
], QUAD8_EXTENT
);
1298 priv
->map
= devm_regmap_init_mmio(dev
, regs
, &quad8_regmap_config
);
1299 if (IS_ERR(priv
->map
))
1300 return dev_err_probe(dev
, PTR_ERR(priv
->map
),
1301 "Unable to initialize register map\n");
1303 /* Initialize Counter device and driver data */
1304 counter
->name
= dev_name(dev
);
1305 counter
->parent
= dev
;
1306 counter
->ops
= &quad8_ops
;
1307 counter
->counts
= quad8_counts
;
1308 counter
->num_counts
= ARRAY_SIZE(quad8_counts
);
1309 counter
->signals
= quad8_signals
;
1310 counter
->num_signals
= ARRAY_SIZE(quad8_signals
);
1312 spin_lock_init(&priv
->lock
);
1314 /* Reset Index/Interrupt Register */
1315 ret
= regmap_write(priv
->map
, QUAD8_INDEX_INTERRUPT
, 0x00);
1318 /* Reset all counters and disable interrupt function */
1319 ret
= regmap_write(priv
->map
, QUAD8_CHANNEL_OPERATION
,
1320 RESET_COUNTERS
| DISABLE_INTERRUPT_FUNCTION
);
1323 /* Set initial configuration for all counters */
1324 for (i
= 0; i
< QUAD8_NUM_COUNTERS
; i
++) {
1325 ret
= quad8_init_counter(priv
, i
);
1329 /* Disable Differential Encoder Cable Status for all channels */
1330 ret
= regmap_write(priv
->map
, QUAD8_CABLE_STATUS
, GENMASK(7, 0));
1333 /* Enable all counters and enable interrupt function */
1334 ret
= regmap_write(priv
->map
, QUAD8_CHANNEL_OPERATION
,
1335 ENABLE_COUNTERS
| ENABLE_INTERRUPT_FUNCTION
);
1339 ret
= devm_request_irq(&counter
->dev
, irq
[id
], quad8_irq_handler
,
1340 IRQF_SHARED
, counter
->name
, counter
);
1344 ret
= devm_counter_add(dev
, counter
);
1346 return dev_err_probe(dev
, ret
, "Failed to add counter\n");
1351 static struct isa_driver quad8_driver
= {
1352 .probe
= quad8_probe
,
1354 .name
= "104-quad-8"
1358 module_isa_driver_with_irq(quad8_driver
, num_quad8
, num_irq
);
1360 MODULE_AUTHOR("William Breathitt Gray <vilhelm.gray@gmail.com>");
1361 MODULE_DESCRIPTION("ACCES 104-QUAD-8 driver");
1362 MODULE_LICENSE("GPL v2");
1363 MODULE_IMPORT_NS(COUNTER
);