0001 .. SPDX-License-Identifier: GPL-2.0
0002
0003 =========================
0004 Generic Counter Interface
0005 =========================
0006
0007 Introduction
0008 ============
0009
0010 Counter devices are prevalent among a diverse spectrum of industries.
0011 The ubiquitous presence of these devices necessitates a common interface
0012 and standard of interaction and exposure. This driver API attempts to
0013 resolve the issue of duplicate code found among existing counter device
0014 drivers by introducing a generic counter interface for consumption. The
0015 Generic Counter interface enables drivers to support and expose a common
0016 set of components and functionality present in counter devices.
0017
0018 Theory
0019 ======
0020
0021 Counter devices can vary greatly in design, but regardless of whether
0022 some devices are quadrature encoder counters or tally counters, all
0023 counter devices consist of a core set of components. This core set of
0024 components, shared by all counter devices, is what forms the essence of
0025 the Generic Counter interface.
0026
0027 There are three core components to a counter:
0028
0029 * Signal:
0030 Stream of data to be evaluated by the counter.
0031
0032 * Synapse:
0033 Association of a Signal, and evaluation trigger, with a Count.
0034
0035 * Count:
0036 Accumulation of the effects of connected Synapses.
0037
0038 SIGNAL
0039 ------
0040 A Signal represents a stream of data. This is the input data that is
0041 evaluated by the counter to determine the count data; e.g. a quadrature
0042 signal output line of a rotary encoder. Not all counter devices provide
0043 user access to the Signal data, so exposure is optional for drivers.
0044
0045 When the Signal data is available for user access, the Generic Counter
0046 interface provides the following available signal values:
0047
0048 * SIGNAL_LOW:
0049 Signal line is in a low state.
0050
0051 * SIGNAL_HIGH:
0052 Signal line is in a high state.
0053
0054 A Signal may be associated with one or more Counts.
0055
0056 SYNAPSE
0057 -------
0058 A Synapse represents the association of a Signal with a Count. Signal
0059 data affects respective Count data, and the Synapse represents this
0060 relationship.
0061
0062 The Synapse action mode specifies the Signal data condition that
0063 triggers the respective Count's count function evaluation to update the
0064 count data. The Generic Counter interface provides the following
0065 available action modes:
0066
0067 * None:
0068 Signal does not trigger the count function. In Pulse-Direction count
0069 function mode, this Signal is evaluated as Direction.
0070
0071 * Rising Edge:
0072 Low state transitions to high state.
0073
0074 * Falling Edge:
0075 High state transitions to low state.
0076
0077 * Both Edges:
0078 Any state transition.
0079
0080 A counter is defined as a set of input signals associated with count
0081 data that are generated by the evaluation of the state of the associated
0082 input signals as defined by the respective count functions. Within the
0083 context of the Generic Counter interface, a counter consists of Counts
0084 each associated with a set of Signals, whose respective Synapse
0085 instances represent the count function update conditions for the
0086 associated Counts.
0087
0088 A Synapse associates one Signal with one Count.
0089
0090 COUNT
0091 -----
0092 A Count represents the accumulation of the effects of connected
0093 Synapses; i.e. the count data for a set of Signals. The Generic
0094 Counter interface represents the count data as a natural number.
0095
0096 A Count has a count function mode which represents the update behavior
0097 for the count data. The Generic Counter interface provides the following
0098 available count function modes:
0099
0100 * Increase:
0101 Accumulated count is incremented.
0102
0103 * Decrease:
0104 Accumulated count is decremented.
0105
0106 * Pulse-Direction:
0107 Rising edges on signal A updates the respective count. The input level
0108 of signal B determines direction.
0109
0110 * Quadrature:
0111 A pair of quadrature encoding signals are evaluated to determine
0112 position and direction. The following Quadrature modes are available:
0113
0114 - x1 A:
0115 If direction is forward, rising edges on quadrature pair signal A
0116 updates the respective count; if the direction is backward, falling
0117 edges on quadrature pair signal A updates the respective count.
0118 Quadrature encoding determines the direction.
0119
0120 - x1 B:
0121 If direction is forward, rising edges on quadrature pair signal B
0122 updates the respective count; if the direction is backward, falling
0123 edges on quadrature pair signal B updates the respective count.
0124 Quadrature encoding determines the direction.
0125
0126 - x2 A:
0127 Any state transition on quadrature pair signal A updates the
0128 respective count. Quadrature encoding determines the direction.
0129
0130 - x2 B:
0131 Any state transition on quadrature pair signal B updates the
0132 respective count. Quadrature encoding determines the direction.
0133
0134 - x4:
0135 Any state transition on either quadrature pair signals updates the
0136 respective count. Quadrature encoding determines the direction.
0137
0138 A Count has a set of one or more associated Synapses.
0139
0140 Paradigm
0141 ========
0142
0143 The most basic counter device may be expressed as a single Count
0144 associated with a single Signal via a single Synapse. Take for example
0145 a counter device which simply accumulates a count of rising edges on a
0146 source input line::
0147
0148 Count Synapse Signal
0149 ----- ------- ------
0150 +---------------------+
0151 | Data: Count | Rising Edge ________
0152 | Function: Increase | <------------- / Source \
0153 | | ____________
0154 +---------------------+
0155
0156 In this example, the Signal is a source input line with a pulsing
0157 voltage, while the Count is a persistent count value which is repeatedly
0158 incremented. The Signal is associated with the respective Count via a
0159 Synapse. The increase function is triggered by the Signal data condition
0160 specified by the Synapse -- in this case a rising edge condition on the
0161 voltage input line. In summary, the counter device existence and
0162 behavior is aptly represented by respective Count, Signal, and Synapse
0163 components: a rising edge condition triggers an increase function on an
0164 accumulating count datum.
0165
0166 A counter device is not limited to a single Signal; in fact, in theory
0167 many Signals may be associated with even a single Count. For example, a
0168 quadrature encoder counter device can keep track of position based on
0169 the states of two input lines::
0170
0171 Count Synapse Signal
0172 ----- ------- ------
0173 +-------------------------+
0174 | Data: Position | Both Edges ___
0175 | Function: Quadrature x4 | <------------ / A \
0176 | | _______
0177 | |
0178 | | Both Edges ___
0179 | | <------------ / B \
0180 | | _______
0181 +-------------------------+
0182
0183 In this example, two Signals (quadrature encoder lines A and B) are
0184 associated with a single Count: a rising or falling edge on either A or
0185 B triggers the "Quadrature x4" function which determines the direction
0186 of movement and updates the respective position data. The "Quadrature
0187 x4" function is likely implemented in the hardware of the quadrature
0188 encoder counter device; the Count, Signals, and Synapses simply
0189 represent this hardware behavior and functionality.
0190
0191 Signals associated with the same Count can have differing Synapse action
0192 mode conditions. For example, a quadrature encoder counter device
0193 operating in a non-quadrature Pulse-Direction mode could have one input
0194 line dedicated for movement and a second input line dedicated for
0195 direction::
0196
0197 Count Synapse Signal
0198 ----- ------- ------
0199 +---------------------------+
0200 | Data: Position | Rising Edge ___
0201 | Function: Pulse-Direction | <------------- / A \ (Movement)
0202 | | _______
0203 | |
0204 | | None ___
0205 | | <------------- / B \ (Direction)
0206 | | _______
0207 +---------------------------+
0208
0209 Only Signal A triggers the "Pulse-Direction" update function, but the
0210 instantaneous state of Signal B is still required in order to know the
0211 direction so that the position data may be properly updated. Ultimately,
0212 both Signals are associated with the same Count via two respective
0213 Synapses, but only one Synapse has an active action mode condition which
0214 triggers the respective count function while the other is left with a
0215 "None" condition action mode to indicate its respective Signal's
0216 availability for state evaluation despite its non-triggering mode.
0217
0218 Keep in mind that the Signal, Synapse, and Count are abstract
0219 representations which do not need to be closely married to their
0220 respective physical sources. This allows the user of a counter to
0221 divorce themselves from the nuances of physical components (such as
0222 whether an input line is differential or single-ended) and instead focus
0223 on the core idea of what the data and process represent (e.g. position
0224 as interpreted from quadrature encoding data).
0225
0226 Driver API
0227 ==========
0228
0229 Driver authors may utilize the Generic Counter interface in their code
0230 by including the include/linux/counter.h header file. This header file
0231 provides several core data structures, function prototypes, and macros
0232 for defining a counter device.
0233
0234 .. kernel-doc:: include/linux/counter.h
0235 :internal:
0236
0237 .. kernel-doc:: drivers/counter/counter-core.c
0238 :export:
0239
0240 .. kernel-doc:: drivers/counter/counter-chrdev.c
0241 :export:
0242
0243 Driver Implementation
0244 =====================
0245
0246 To support a counter device, a driver must first allocate the available
0247 Counter Signals via counter_signal structures. These Signals should
0248 be stored as an array and set to the signals array member of an
0249 allocated counter_device structure before the Counter is registered to
0250 the system.
0251
0252 Counter Counts may be allocated via counter_count structures, and
0253 respective Counter Signal associations (Synapses) made via
0254 counter_synapse structures. Associated counter_synapse structures are
0255 stored as an array and set to the synapses array member of the
0256 respective counter_count structure. These counter_count structures are
0257 set to the counts array member of an allocated counter_device structure
0258 before the Counter is registered to the system.
0259
0260 Driver callbacks must be provided to the counter_device structure in
0261 order to communicate with the device: to read and write various Signals
0262 and Counts, and to set and get the "action mode" and "function mode" for
0263 various Synapses and Counts respectively.
0264
0265 A counter_device structure is allocated using counter_alloc() and then
0266 registered to the system by passing it to the counter_add() function, and
0267 unregistered by passing it to the counter_unregister function. There are
0268 device managed variants of these functions: devm_counter_alloc() and
0269 devm_counter_add().
0270
0271 The struct counter_comp structure is used to define counter extensions
0272 for Signals, Synapses, and Counts.
0273
0274 The "type" member specifies the type of high-level data (e.g. BOOL,
0275 COUNT_DIRECTION, etc.) handled by this extension. The "``*_read``" and
0276 "``*_write``" members can then be set by the counter device driver with
0277 callbacks to handle that data using native C data types (i.e. u8, u64,
0278 etc.).
0279
0280 Convenience macros such as ``COUNTER_COMP_COUNT_U64`` are provided for
0281 use by driver authors. In particular, driver authors are expected to use
0282 the provided macros for standard Counter subsystem attributes in order
0283 to maintain a consistent interface for userspace. For example, a counter
0284 device driver may define several standard attributes like so::
0285
0286 struct counter_comp count_ext[] = {
0287 COUNTER_COMP_DIRECTION(count_direction_read),
0288 COUNTER_COMP_ENABLE(count_enable_read, count_enable_write),
0289 COUNTER_COMP_CEILING(count_ceiling_read, count_ceiling_write),
0290 };
0291
0292 This makes it simple to see, add, and modify the attributes that are
0293 supported by this driver ("direction", "enable", and "ceiling") and to
0294 maintain this code without getting lost in a web of struct braces.
0295
0296 Callbacks must match the function type expected for the respective
0297 component or extension. These function types are defined in the struct
0298 counter_comp structure as the "``*_read``" and "``*_write``" union
0299 members.
0300
0301 The corresponding callback prototypes for the extensions mentioned in
0302 the previous example above would be::
0303
0304 int count_direction_read(struct counter_device *counter,
0305 struct counter_count *count,
0306 enum counter_count_direction *direction);
0307 int count_enable_read(struct counter_device *counter,
0308 struct counter_count *count, u8 *enable);
0309 int count_enable_write(struct counter_device *counter,
0310 struct counter_count *count, u8 enable);
0311 int count_ceiling_read(struct counter_device *counter,
0312 struct counter_count *count, u64 *ceiling);
0313 int count_ceiling_write(struct counter_device *counter,
0314 struct counter_count *count, u64 ceiling);
0315
0316 Determining the type of extension to create is a matter of scope.
0317
0318 * Signal extensions are attributes that expose information/control
0319 specific to a Signal. These types of attributes will exist under a
0320 Signal's directory in sysfs.
0321
0322 For example, if you have an invert feature for a Signal, you can have
0323 a Signal extension called "invert" that toggles that feature:
0324 /sys/bus/counter/devices/counterX/signalY/invert
0325
0326 * Count extensions are attributes that expose information/control
0327 specific to a Count. These type of attributes will exist under a
0328 Count's directory in sysfs.
0329
0330 For example, if you want to pause/unpause a Count from updating, you
0331 can have a Count extension called "enable" that toggles such:
0332 /sys/bus/counter/devices/counterX/countY/enable
0333
0334 * Device extensions are attributes that expose information/control
0335 non-specific to a particular Count or Signal. This is where you would
0336 put your global features or other miscellaneous functionality.
0337
0338 For example, if your device has an overtemp sensor, you can report the
0339 chip overheated via a device extension called "error_overtemp":
0340 /sys/bus/counter/devices/counterX/error_overtemp
0341
0342 Subsystem Architecture
0343 ======================
0344
0345 Counter drivers pass and take data natively (i.e. ``u8``, ``u64``, etc.)
0346 and the shared counter module handles the translation between the sysfs
0347 interface. This guarantees a standard userspace interface for all
0348 counter drivers, and enables a Generic Counter chrdev interface via a
0349 generalized device driver ABI.
0350
0351 A high-level view of how a count value is passed down from a counter
0352 driver is exemplified by the following. The driver callbacks are first
0353 registered to the Counter core component for use by the Counter
0354 userspace interface components::
0355
0356 Driver callbacks registration:
0357 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0358 +----------------------------+
0359 | Counter device driver |
0360 +----------------------------+
0361 | Processes data from device |
0362 +----------------------------+
0363 |
0364 -------------------
0365 / driver callbacks /
0366 -------------------
0367 |
0368 V
0369 +----------------------+
0370 | Counter core |
0371 +----------------------+
0372 | Routes device driver |
0373 | callbacks to the |
0374 | userspace interfaces |
0375 +----------------------+
0376 |
0377 -------------------
0378 / driver callbacks /
0379 -------------------
0380 |
0381 +---------------+---------------+
0382 | |
0383 V V
0384 +--------------------+ +---------------------+
0385 | Counter sysfs | | Counter chrdev |
0386 +--------------------+ +---------------------+
0387 | Translates to the | | Translates to the |
0388 | standard Counter | | standard Counter |
0389 | sysfs output | | character device |
0390 +--------------------+ +---------------------+
0391
0392 Thereafter, data can be transferred directly between the Counter device
0393 driver and Counter userspace interface::
0394
0395 Count data request:
0396 ~~~~~~~~~~~~~~~~~~~
0397 ----------------------
0398 / Counter device \
0399 +----------------------+
0400 | Count register: 0x28 |
0401 +----------------------+
0402 |
0403 -----------------
0404 / raw count data /
0405 -----------------
0406 |
0407 V
0408 +----------------------------+
0409 | Counter device driver |
0410 +----------------------------+
0411 | Processes data from device |
0412 |----------------------------|
0413 | Type: u64 |
0414 | Value: 42 |
0415 +----------------------------+
0416 |
0417 ----------
0418 / u64 /
0419 ----------
0420 |
0421 +---------------+---------------+
0422 | |
0423 V V
0424 +--------------------+ +---------------------+
0425 | Counter sysfs | | Counter chrdev |
0426 +--------------------+ +---------------------+
0427 | Translates to the | | Translates to the |
0428 | standard Counter | | standard Counter |
0429 | sysfs output | | character device |
0430 |--------------------| |---------------------|
0431 | Type: const char * | | Type: u64 |
0432 | Value: "42" | | Value: 42 |
0433 +--------------------+ +---------------------+
0434 | |
0435 --------------- -----------------------
0436 / const char * / / struct counter_event /
0437 --------------- -----------------------
0438 | |
0439 | V
0440 | +-----------+
0441 | | read |
0442 | +-----------+
0443 | \ Count: 42 /
0444 | -----------
0445 |
0446 V
0447 +--------------------------------------------------+
0448 | `/sys/bus/counter/devices/counterX/countY/count` |
0449 +--------------------------------------------------+
0450 \ Count: "42" /
0451 --------------------------------------------------
0452
0453 There are four primary components involved:
0454
0455 Counter device driver
0456 ---------------------
0457 Communicates with the hardware device to read/write data; e.g. counter
0458 drivers for quadrature encoders, timers, etc.
0459
0460 Counter core
0461 ------------
0462 Registers the counter device driver to the system so that the respective
0463 callbacks are called during userspace interaction.
0464
0465 Counter sysfs
0466 -------------
0467 Translates counter data to the standard Counter sysfs interface format
0468 and vice versa.
0469
0470 Please refer to the ``Documentation/ABI/testing/sysfs-bus-counter`` file
0471 for a detailed breakdown of the available Generic Counter interface
0472 sysfs attributes.
0473
0474 Counter chrdev
0475 --------------
0476 Translates Counter events to the standard Counter character device; data
0477 is transferred via standard character device read calls, while Counter
0478 events are configured via ioctl calls.
0479
0480 Sysfs Interface
0481 ===============
0482
0483 Several sysfs attributes are generated by the Generic Counter interface,
0484 and reside under the ``/sys/bus/counter/devices/counterX`` directory,
0485 where ``X`` is to the respective counter device id. Please see
0486 ``Documentation/ABI/testing/sysfs-bus-counter`` for detailed information
0487 on each Generic Counter interface sysfs attribute.
0488
0489 Through these sysfs attributes, programs and scripts may interact with
0490 the Generic Counter paradigm Counts, Signals, and Synapses of respective
0491 counter devices.
0492
0493 Counter Character Device
0494 ========================
0495
0496 Counter character device nodes are created under the ``/dev`` directory
0497 as ``counterX``, where ``X`` is the respective counter device id.
0498 Defines for the standard Counter data types are exposed via the
0499 userspace ``include/uapi/linux/counter.h`` file.
0500
0501 Counter events
0502 --------------
0503 Counter device drivers can support Counter events by utilizing the
0504 ``counter_push_event`` function::
0505
0506 void counter_push_event(struct counter_device *const counter, const u8 event,
0507 const u8 channel);
0508
0509 The event id is specified by the ``event`` parameter; the event channel
0510 id is specified by the ``channel`` parameter. When this function is
0511 called, the Counter data associated with the respective event is
0512 gathered, and a ``struct counter_event`` is generated for each datum and
0513 pushed to userspace.
0514
0515 Counter events can be configured by users to report various Counter
0516 data of interest. This can be conceptualized as a list of Counter
0517 component read calls to perform. For example:
0518
0519 +------------------------+------------------------+
0520 | COUNTER_EVENT_OVERFLOW | COUNTER_EVENT_INDEX |
0521 +========================+========================+
0522 | Channel 0 | Channel 0 |
0523 +------------------------+------------------------+
0524 | * Count 0 | * Signal 0 |
0525 | * Count 1 | * Signal 0 Extension 0 |
0526 | * Signal 3 | * Extension 4 |
0527 | * Count 4 Extension 2 +------------------------+
0528 | * Signal 5 Extension 0 | Channel 1 |
0529 | +------------------------+
0530 | | * Signal 4 |
0531 | | * Signal 4 Extension 0 |
0532 | | * Count 7 |
0533 +------------------------+------------------------+
0534
0535 When ``counter_push_event(counter, COUNTER_EVENT_INDEX, 1)`` is called
0536 for example, it will go down the list for the ``COUNTER_EVENT_INDEX``
0537 event channel 1 and execute the read callbacks for Signal 4, Signal 4
0538 Extension 0, and Count 7 -- the data returned for each is pushed to a
0539 kfifo as a ``struct counter_event``, which userspace can retrieve via a
0540 standard read operation on the respective character device node.
0541
0542 Userspace
0543 ---------
0544 Userspace applications can configure Counter events via ioctl operations
0545 on the Counter character device node. There following ioctl codes are
0546 supported and provided by the ``linux/counter.h`` userspace header file:
0547
0548 * :c:macro:`COUNTER_ADD_WATCH_IOCTL`
0549
0550 * :c:macro:`COUNTER_ENABLE_EVENTS_IOCTL`
0551
0552 * :c:macro:`COUNTER_DISABLE_EVENTS_IOCTL`
0553
0554 To configure events to gather Counter data, users first populate a
0555 ``struct counter_watch`` with the relevant event id, event channel id,
0556 and the information for the desired Counter component from which to
0557 read, and then pass it via the ``COUNTER_ADD_WATCH_IOCTL`` ioctl
0558 command.
0559
0560 Note that an event can be watched without gathering Counter data by
0561 setting the ``component.type`` member equal to
0562 ``COUNTER_COMPONENT_NONE``. With this configuration the Counter
0563 character device will simply populate the event timestamps for those
0564 respective ``struct counter_event`` elements and ignore the component
0565 value.
0566
0567 The ``COUNTER_ADD_WATCH_IOCTL`` command will buffer these Counter
0568 watches. When ready, the ``COUNTER_ENABLE_EVENTS_IOCTL`` ioctl command
0569 may be used to activate these Counter watches.
0570
0571 Userspace applications can then execute a ``read`` operation (optionally
0572 calling ``poll`` first) on the Counter character device node to retrieve
0573 ``struct counter_event`` elements with the desired data.