1.. SPDX-License-Identifier: GPL-2.0 2 3========================= 4Generic Counter Interface 5========================= 6 7Introduction 8============ 9 10Counter devices are prevalent among a diverse spectrum of industries. 11The ubiquitous presence of these devices necessitates a common interface 12and standard of interaction and exposure. This driver API attempts to 13resolve the issue of duplicate code found among existing counter device 14drivers by introducing a generic counter interface for consumption. The 15Generic Counter interface enables drivers to support and expose a common 16set of components and functionality present in counter devices. 17 18Theory 19====== 20 21Counter devices can vary greatly in design, but regardless of whether 22some devices are quadrature encoder counters or tally counters, all 23counter devices consist of a core set of components. This core set of 24components, shared by all counter devices, is what forms the essence of 25the Generic Counter interface. 26 27There are three core components to a counter: 28 29* Signal: 30 Stream of data to be evaluated by the counter. 31 32* Synapse: 33 Association of a Signal, and evaluation trigger, with a Count. 34 35* Count: 36 Accumulation of the effects of connected Synapses. 37 38SIGNAL 39------ 40A Signal represents a stream of data. This is the input data that is 41evaluated by the counter to determine the count data; e.g. a quadrature 42signal output line of a rotary encoder. Not all counter devices provide 43user access to the Signal data, so exposure is optional for drivers. 44 45When the Signal data is available for user access, the Generic Counter 46interface provides the following available signal values: 47 48* SIGNAL_LOW: 49 Signal line is in a low state. 50 51* SIGNAL_HIGH: 52 Signal line is in a high state. 53 54A Signal may be associated with one or more Counts. 55 56SYNAPSE 57------- 58A Synapse represents the association of a Signal with a Count. Signal 59data affects respective Count data, and the Synapse represents this 60relationship. 61 62The Synapse action mode specifies the Signal data condition that 63triggers the respective Count's count function evaluation to update the 64count data. The Generic Counter interface provides the following 65available action modes: 66 67* None: 68 Signal does not trigger the count function. In Pulse-Direction count 69 function mode, this Signal is evaluated as Direction. 70 71* Rising Edge: 72 Low state transitions to high state. 73 74* Falling Edge: 75 High state transitions to low state. 76 77* Both Edges: 78 Any state transition. 79 80A counter is defined as a set of input signals associated with count 81data that are generated by the evaluation of the state of the associated 82input signals as defined by the respective count functions. Within the 83context of the Generic Counter interface, a counter consists of Counts 84each associated with a set of Signals, whose respective Synapse 85instances represent the count function update conditions for the 86associated Counts. 87 88A Synapse associates one Signal with one Count. 89 90COUNT 91----- 92A Count represents the accumulation of the effects of connected 93Synapses; i.e. the count data for a set of Signals. The Generic 94Counter interface represents the count data as a natural number. 95 96A Count has a count function mode which represents the update behavior 97for the count data. The Generic Counter interface provides the following 98available count function modes: 99 100* Increase: 101 Accumulated count is incremented. 102 103* Decrease: 104 Accumulated count is decremented. 105 106* Pulse-Direction: 107 Rising edges on signal A updates the respective count. The input level 108 of signal B determines direction. 109 110* Quadrature: 111 A pair of quadrature encoding signals are evaluated to determine 112 position and direction. The following Quadrature modes are available: 113 114 - x1 A: 115 If direction is forward, rising edges on quadrature pair signal A 116 updates the respective count; if the direction is backward, falling 117 edges on quadrature pair signal A updates the respective count. 118 Quadrature encoding determines the direction. 119 120 - x1 B: 121 If direction is forward, rising edges on quadrature pair signal B 122 updates the respective count; if the direction is backward, falling 123 edges on quadrature pair signal B updates the respective count. 124 Quadrature encoding determines the direction. 125 126 - x2 A: 127 Any state transition on quadrature pair signal A updates the 128 respective count. Quadrature encoding determines the direction. 129 130 - x2 B: 131 Any state transition on quadrature pair signal B updates the 132 respective count. Quadrature encoding determines the direction. 133 134 - x4: 135 Any state transition on either quadrature pair signals updates the 136 respective count. Quadrature encoding determines the direction. 137 138A Count has a set of one or more associated Synapses. 139 140Paradigm 141======== 142 143The most basic counter device may be expressed as a single Count 144associated with a single Signal via a single Synapse. Take for example 145a counter device which simply accumulates a count of rising edges on a 146source input line:: 147 148 Count Synapse Signal 149 ----- ------- ------ 150 +---------------------+ 151 | Data: Count | Rising Edge ________ 152 | Function: Increase | <------------- / Source \ 153 | | ____________ 154 +---------------------+ 155 156In this example, the Signal is a source input line with a pulsing 157voltage, while the Count is a persistent count value which is repeatedly 158incremented. The Signal is associated with the respective Count via a 159Synapse. The increase function is triggered by the Signal data condition 160specified by the Synapse -- in this case a rising edge condition on the 161voltage input line. In summary, the counter device existence and 162behavior is aptly represented by respective Count, Signal, and Synapse 163components: a rising edge condition triggers an increase function on an 164accumulating count datum. 165 166A counter device is not limited to a single Signal; in fact, in theory 167many Signals may be associated with even a single Count. For example, a 168quadrature encoder counter device can keep track of position based on 169the states of two input lines:: 170 171 Count Synapse Signal 172 ----- ------- ------ 173 +-------------------------+ 174 | Data: Position | Both Edges ___ 175 | Function: Quadrature x4 | <------------ / A \ 176 | | _______ 177 | | 178 | | Both Edges ___ 179 | | <------------ / B \ 180 | | _______ 181 +-------------------------+ 182 183In this example, two Signals (quadrature encoder lines A and B) are 184associated with a single Count: a rising or falling edge on either A or 185B triggers the "Quadrature x4" function which determines the direction 186of movement and updates the respective position data. The "Quadrature 187x4" function is likely implemented in the hardware of the quadrature 188encoder counter device; the Count, Signals, and Synapses simply 189represent this hardware behavior and functionality. 190 191Signals associated with the same Count can have differing Synapse action 192mode conditions. For example, a quadrature encoder counter device 193operating in a non-quadrature Pulse-Direction mode could have one input 194line dedicated for movement and a second input line dedicated for 195direction:: 196 197 Count Synapse Signal 198 ----- ------- ------ 199 +---------------------------+ 200 | Data: Position | Rising Edge ___ 201 | Function: Pulse-Direction | <------------- / A \ (Movement) 202 | | _______ 203 | | 204 | | None ___ 205 | | <------------- / B \ (Direction) 206 | | _______ 207 +---------------------------+ 208 209Only Signal A triggers the "Pulse-Direction" update function, but the 210instantaneous state of Signal B is still required in order to know the 211direction so that the position data may be properly updated. Ultimately, 212both Signals are associated with the same Count via two respective 213Synapses, but only one Synapse has an active action mode condition which 214triggers the respective count function while the other is left with a 215"None" condition action mode to indicate its respective Signal's 216availability for state evaluation despite its non-triggering mode. 217 218Keep in mind that the Signal, Synapse, and Count are abstract 219representations which do not need to be closely married to their 220respective physical sources. This allows the user of a counter to 221divorce themselves from the nuances of physical components (such as 222whether an input line is differential or single-ended) and instead focus 223on the core idea of what the data and process represent (e.g. position 224as interpreted from quadrature encoding data). 225 226Userspace Interface 227=================== 228 229Several sysfs attributes are generated by the Generic Counter interface, 230and reside under the /sys/bus/counter/devices/counterX directory, where 231counterX refers to the respective counter device. Please see 232Documentation/ABI/testing/sysfs-bus-counter for detailed 233information on each Generic Counter interface sysfs attribute. 234 235Through these sysfs attributes, programs and scripts may interact with 236the Generic Counter paradigm Counts, Signals, and Synapses of respective 237counter devices. 238 239Driver API 240========== 241 242Driver authors may utilize the Generic Counter interface in their code 243by including the include/linux/counter.h header file. This header file 244provides several core data structures, function prototypes, and macros 245for defining a counter device. 246 247.. kernel-doc:: include/linux/counter.h 248 :internal: 249 250.. kernel-doc:: drivers/counter/counter.c 251 :export: 252 253Implementation 254============== 255 256To support a counter device, a driver must first allocate the available 257Counter Signals via counter_signal structures. These Signals should 258be stored as an array and set to the signals array member of an 259allocated counter_device structure before the Counter is registered to 260the system. 261 262Counter Counts may be allocated via counter_count structures, and 263respective Counter Signal associations (Synapses) made via 264counter_synapse structures. Associated counter_synapse structures are 265stored as an array and set to the the synapses array member of the 266respective counter_count structure. These counter_count structures are 267set to the counts array member of an allocated counter_device structure 268before the Counter is registered to the system. 269 270Driver callbacks should be provided to the counter_device structure via 271a constant counter_ops structure in order to communicate with the 272device: to read and write various Signals and Counts, and to set and get 273the "action mode" and "function mode" for various Synapses and Counts 274respectively. 275 276A defined counter_device structure may be registered to the system by 277passing it to the counter_register function, and unregistered by passing 278it to the counter_unregister function. Similarly, the 279devm_counter_register and devm_counter_unregister functions may be used 280if device memory-managed registration is desired. 281 282Extension sysfs attributes can be created for auxiliary functionality 283and data by passing in defined counter_device_ext, counter_count_ext, 284and counter_signal_ext structures. In these cases, the 285counter_device_ext structure is used for global/miscellaneous exposure 286and configuration of the respective Counter device, while the 287counter_count_ext and counter_signal_ext structures allow for auxiliary 288exposure and configuration of a specific Count or Signal respectively. 289 290Determining the type of extension to create is a matter of scope. 291 292* Signal extensions are attributes that expose information/control 293 specific to a Signal. These types of attributes will exist under a 294 Signal's directory in sysfs. 295 296 For example, if you have an invert feature for a Signal, you can have 297 a Signal extension called "invert" that toggles that feature: 298 /sys/bus/counter/devices/counterX/signalY/invert 299 300* Count extensions are attributes that expose information/control 301 specific to a Count. These type of attributes will exist under a 302 Count's directory in sysfs. 303 304 For example, if you want to pause/unpause a Count from updating, you 305 can have a Count extension called "enable" that toggles such: 306 /sys/bus/counter/devices/counterX/countY/enable 307 308* Device extensions are attributes that expose information/control 309 non-specific to a particular Count or Signal. This is where you would 310 put your global features or other miscellanous functionality. 311 312 For example, if your device has an overtemp sensor, you can report the 313 chip overheated via a device extension called "error_overtemp": 314 /sys/bus/counter/devices/counterX/error_overtemp 315 316Architecture 317============ 318 319When the Generic Counter interface counter module is loaded, the 320counter_init function is called which registers a bus_type named 321"counter" to the system. Subsequently, when the module is unloaded, the 322counter_exit function is called which unregisters the bus_type named 323"counter" from the system. 324 325Counter devices are registered to the system via the counter_register 326function, and later removed via the counter_unregister function. The 327counter_register function establishes a unique ID for the Counter 328device and creates a respective sysfs directory, where X is the 329mentioned unique ID: 330 331 /sys/bus/counter/devices/counterX 332 333Sysfs attributes are created within the counterX directory to expose 334functionality, configurations, and data relating to the Counts, Signals, 335and Synapses of the Counter device, as well as options and information 336for the Counter device itself. 337 338Each Signal has a directory created to house its relevant sysfs 339attributes, where Y is the unique ID of the respective Signal: 340 341 /sys/bus/counter/devices/counterX/signalY 342 343Similarly, each Count has a directory created to house its relevant 344sysfs attributes, where Y is the unique ID of the respective Count: 345 346 /sys/bus/counter/devices/counterX/countY 347 348For a more detailed breakdown of the available Generic Counter interface 349sysfs attributes, please refer to the 350Documentation/ABI/testing/sysfs-bus-counter file. 351 352The Signals and Counts associated with the Counter device are registered 353to the system as well by the counter_register function. The 354signal_read/signal_write driver callbacks are associated with their 355respective Signal attributes, while the count_read/count_write and 356function_get/function_set driver callbacks are associated with their 357respective Count attributes; similarly, the same is true for the 358action_get/action_set driver callbacks and their respective Synapse 359attributes. If a driver callback is left undefined, then the respective 360read/write permission is left disabled for the relevant attributes. 361 362Similarly, extension sysfs attributes are created for the defined 363counter_device_ext, counter_count_ext, and counter_signal_ext 364structures that are passed in. 365