William Breathitt Gray | 09e7d4e | 2019-04-02 15:30:38 +0900 | [diff] [blame] | 1 | .. SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | ========================= |
| 4 | Generic Counter Interface |
| 5 | ========================= |
| 6 | |
| 7 | Introduction |
| 8 | ============ |
| 9 | |
William Breathitt Gray | e58cbfd | 2019-10-06 16:03:10 -0400 | [diff] [blame] | 10 | Counter devices are prevalent among a diverse spectrum of industries. |
William Breathitt Gray | 09e7d4e | 2019-04-02 15:30:38 +0900 | [diff] [blame] | 11 | The ubiquitous presence of these devices necessitates a common interface |
| 12 | and standard of interaction and exposure. This driver API attempts to |
| 13 | resolve the issue of duplicate code found among existing counter device |
| 14 | drivers by introducing a generic counter interface for consumption. The |
| 15 | Generic Counter interface enables drivers to support and expose a common |
| 16 | set of components and functionality present in counter devices. |
| 17 | |
| 18 | Theory |
| 19 | ====== |
| 20 | |
| 21 | Counter devices can vary greatly in design, but regardless of whether |
| 22 | some devices are quadrature encoder counters or tally counters, all |
| 23 | counter devices consist of a core set of components. This core set of |
| 24 | components, shared by all counter devices, is what forms the essence of |
| 25 | the Generic Counter interface. |
| 26 | |
| 27 | There are three core components to a counter: |
| 28 | |
William Breathitt Gray | 09e7d4e | 2019-04-02 15:30:38 +0900 | [diff] [blame] | 29 | * Signal: |
William Breathitt Gray | e58cbfd | 2019-10-06 16:03:10 -0400 | [diff] [blame] | 30 | Stream of data to be evaluated by the counter. |
William Breathitt Gray | 09e7d4e | 2019-04-02 15:30:38 +0900 | [diff] [blame] | 31 | |
| 32 | * Synapse: |
William Breathitt Gray | e58cbfd | 2019-10-06 16:03:10 -0400 | [diff] [blame] | 33 | Association of a Signal, and evaluation trigger, with a Count. |
| 34 | |
| 35 | * Count: |
| 36 | Accumulation of the effects of connected Synapses. |
| 37 | |
| 38 | SIGNAL |
| 39 | ------ |
| 40 | A Signal represents a stream of data. This is the input data that is |
| 41 | evaluated by the counter to determine the count data; e.g. a quadrature |
| 42 | signal output line of a rotary encoder. Not all counter devices provide |
| 43 | user access to the Signal data, so exposure is optional for drivers. |
| 44 | |
| 45 | When the Signal data is available for user access, the Generic Counter |
| 46 | interface 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 | |
| 54 | A Signal may be associated with one or more Counts. |
| 55 | |
| 56 | SYNAPSE |
| 57 | ------- |
| 58 | A Synapse represents the association of a Signal with a Count. Signal |
| 59 | data affects respective Count data, and the Synapse represents this |
| 60 | relationship. |
| 61 | |
| 62 | The Synapse action mode specifies the Signal data condition that |
| 63 | triggers the respective Count's count function evaluation to update the |
| 64 | count data. The Generic Counter interface provides the following |
| 65 | available 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 | |
| 80 | A counter is defined as a set of input signals associated with count |
| 81 | data that are generated by the evaluation of the state of the associated |
| 82 | input signals as defined by the respective count functions. Within the |
| 83 | context of the Generic Counter interface, a counter consists of Counts |
| 84 | each associated with a set of Signals, whose respective Synapse |
| 85 | instances represent the count function update conditions for the |
| 86 | associated Counts. |
| 87 | |
| 88 | A Synapse associates one Signal with one Count. |
William Breathitt Gray | 09e7d4e | 2019-04-02 15:30:38 +0900 | [diff] [blame] | 89 | |
| 90 | COUNT |
| 91 | ----- |
William Breathitt Gray | e58cbfd | 2019-10-06 16:03:10 -0400 | [diff] [blame] | 92 | A Count represents the accumulation of the effects of connected |
| 93 | Synapses; i.e. the count data for a set of Signals. The Generic |
| 94 | Counter interface represents the count data as a natural number. |
William Breathitt Gray | 09e7d4e | 2019-04-02 15:30:38 +0900 | [diff] [blame] | 95 | |
| 96 | A Count has a count function mode which represents the update behavior |
| 97 | for the count data. The Generic Counter interface provides the following |
| 98 | available 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 | |
William Breathitt Gray | e58cbfd | 2019-10-06 16:03:10 -0400 | [diff] [blame] | 138 | A Count has a set of one or more associated Synapses. |
William Breathitt Gray | 09e7d4e | 2019-04-02 15:30:38 +0900 | [diff] [blame] | 139 | |
| 140 | Paradigm |
| 141 | ======== |
| 142 | |
| 143 | The most basic counter device may be expressed as a single Count |
| 144 | associated with a single Signal via a single Synapse. Take for example |
| 145 | a counter device which simply accumulates a count of rising edges on a |
| 146 | source input line:: |
| 147 | |
| 148 | Count Synapse Signal |
| 149 | ----- ------- ------ |
| 150 | +---------------------+ |
| 151 | | Data: Count | Rising Edge ________ |
| 152 | | Function: Increase | <------------- / Source \ |
| 153 | | | ____________ |
| 154 | +---------------------+ |
| 155 | |
| 156 | In this example, the Signal is a source input line with a pulsing |
| 157 | voltage, while the Count is a persistent count value which is repeatedly |
| 158 | incremented. The Signal is associated with the respective Count via a |
| 159 | Synapse. The increase function is triggered by the Signal data condition |
| 160 | specified by the Synapse -- in this case a rising edge condition on the |
| 161 | voltage input line. In summary, the counter device existence and |
| 162 | behavior is aptly represented by respective Count, Signal, and Synapse |
| 163 | components: a rising edge condition triggers an increase function on an |
| 164 | accumulating count datum. |
| 165 | |
| 166 | A counter device is not limited to a single Signal; in fact, in theory |
| 167 | many Signals may be associated with even a single Count. For example, a |
| 168 | quadrature encoder counter device can keep track of position based on |
| 169 | the 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 | |
| 183 | In this example, two Signals (quadrature encoder lines A and B) are |
| 184 | associated with a single Count: a rising or falling edge on either A or |
| 185 | B triggers the "Quadrature x4" function which determines the direction |
| 186 | of movement and updates the respective position data. The "Quadrature |
| 187 | x4" function is likely implemented in the hardware of the quadrature |
| 188 | encoder counter device; the Count, Signals, and Synapses simply |
| 189 | represent this hardware behavior and functionality. |
| 190 | |
| 191 | Signals associated with the same Count can have differing Synapse action |
| 192 | mode conditions. For example, a quadrature encoder counter device |
| 193 | operating in a non-quadrature Pulse-Direction mode could have one input |
| 194 | line dedicated for movement and a second input line dedicated for |
| 195 | direction:: |
| 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 | |
| 209 | Only Signal A triggers the "Pulse-Direction" update function, but the |
| 210 | instantaneous state of Signal B is still required in order to know the |
| 211 | direction so that the position data may be properly updated. Ultimately, |
| 212 | both Signals are associated with the same Count via two respective |
| 213 | Synapses, but only one Synapse has an active action mode condition which |
| 214 | triggers the respective count function while the other is left with a |
| 215 | "None" condition action mode to indicate its respective Signal's |
| 216 | availability for state evaluation despite its non-triggering mode. |
| 217 | |
| 218 | Keep in mind that the Signal, Synapse, and Count are abstract |
| 219 | representations which do not need to be closely married to their |
| 220 | respective physical sources. This allows the user of a counter to |
| 221 | divorce themselves from the nuances of physical components (such as |
| 222 | whether an input line is differential or single-ended) and instead focus |
| 223 | on the core idea of what the data and process represent (e.g. position |
| 224 | as interpreted from quadrature encoding data). |
| 225 | |
| 226 | Userspace Interface |
| 227 | =================== |
| 228 | |
| 229 | Several sysfs attributes are generated by the Generic Counter interface, |
| 230 | and reside under the /sys/bus/counter/devices/counterX directory, where |
| 231 | counterX refers to the respective counter device. Please see |
Marcus Folkesson | 224d5fd | 2019-07-09 09:54:36 +0200 | [diff] [blame] | 232 | Documentation/ABI/testing/sysfs-bus-counter for detailed |
William Breathitt Gray | 09e7d4e | 2019-04-02 15:30:38 +0900 | [diff] [blame] | 233 | information on each Generic Counter interface sysfs attribute. |
| 234 | |
| 235 | Through these sysfs attributes, programs and scripts may interact with |
| 236 | the Generic Counter paradigm Counts, Signals, and Synapses of respective |
| 237 | counter devices. |
| 238 | |
| 239 | Driver API |
| 240 | ========== |
| 241 | |
| 242 | Driver authors may utilize the Generic Counter interface in their code |
| 243 | by including the include/linux/counter.h header file. This header file |
| 244 | provides several core data structures, function prototypes, and macros |
| 245 | for defining a counter device. |
| 246 | |
| 247 | .. kernel-doc:: include/linux/counter.h |
| 248 | :internal: |
| 249 | |
Randy Dunlap | fba3880 | 2019-05-18 21:29:58 -0700 | [diff] [blame] | 250 | .. kernel-doc:: drivers/counter/counter.c |
William Breathitt Gray | 09e7d4e | 2019-04-02 15:30:38 +0900 | [diff] [blame] | 251 | :export: |
| 252 | |
| 253 | Implementation |
| 254 | ============== |
| 255 | |
| 256 | To support a counter device, a driver must first allocate the available |
| 257 | Counter Signals via counter_signal structures. These Signals should |
| 258 | be stored as an array and set to the signals array member of an |
| 259 | allocated counter_device structure before the Counter is registered to |
| 260 | the system. |
| 261 | |
| 262 | Counter Counts may be allocated via counter_count structures, and |
| 263 | respective Counter Signal associations (Synapses) made via |
| 264 | counter_synapse structures. Associated counter_synapse structures are |
Randy Dunlap | 10f3225 | 2020-07-03 20:44:51 -0700 | [diff] [blame] | 265 | stored as an array and set to the synapses array member of the |
William Breathitt Gray | 09e7d4e | 2019-04-02 15:30:38 +0900 | [diff] [blame] | 266 | respective counter_count structure. These counter_count structures are |
| 267 | set to the counts array member of an allocated counter_device structure |
| 268 | before the Counter is registered to the system. |
| 269 | |
| 270 | Driver callbacks should be provided to the counter_device structure via |
| 271 | a constant counter_ops structure in order to communicate with the |
| 272 | device: to read and write various Signals and Counts, and to set and get |
| 273 | the "action mode" and "function mode" for various Synapses and Counts |
| 274 | respectively. |
| 275 | |
| 276 | A defined counter_device structure may be registered to the system by |
| 277 | passing it to the counter_register function, and unregistered by passing |
| 278 | it to the counter_unregister function. Similarly, the |
| 279 | devm_counter_register and devm_counter_unregister functions may be used |
| 280 | if device memory-managed registration is desired. |
| 281 | |
| 282 | Extension sysfs attributes can be created for auxiliary functionality |
| 283 | and data by passing in defined counter_device_ext, counter_count_ext, |
| 284 | and counter_signal_ext structures. In these cases, the |
William Breathitt Gray | e58cbfd | 2019-10-06 16:03:10 -0400 | [diff] [blame] | 285 | counter_device_ext structure is used for global/miscellaneous exposure |
| 286 | and configuration of the respective Counter device, while the |
| 287 | counter_count_ext and counter_signal_ext structures allow for auxiliary |
| 288 | exposure and configuration of a specific Count or Signal respectively. |
| 289 | |
| 290 | Determining 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 |
William Breathitt Gray | c316424 | 2021-06-09 10:31:05 +0900 | [diff] [blame] | 310 | put your global features or other miscellaneous functionality. |
William Breathitt Gray | e58cbfd | 2019-10-06 16:03:10 -0400 | [diff] [blame] | 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 |
William Breathitt Gray | 09e7d4e | 2019-04-02 15:30:38 +0900 | [diff] [blame] | 315 | |
| 316 | Architecture |
| 317 | ============ |
| 318 | |
| 319 | When the Generic Counter interface counter module is loaded, the |
| 320 | counter_init function is called which registers a bus_type named |
| 321 | "counter" to the system. Subsequently, when the module is unloaded, the |
| 322 | counter_exit function is called which unregisters the bus_type named |
| 323 | "counter" from the system. |
| 324 | |
| 325 | Counter devices are registered to the system via the counter_register |
| 326 | function, and later removed via the counter_unregister function. The |
| 327 | counter_register function establishes a unique ID for the Counter |
| 328 | device and creates a respective sysfs directory, where X is the |
| 329 | mentioned unique ID: |
| 330 | |
| 331 | /sys/bus/counter/devices/counterX |
| 332 | |
| 333 | Sysfs attributes are created within the counterX directory to expose |
| 334 | functionality, configurations, and data relating to the Counts, Signals, |
| 335 | and Synapses of the Counter device, as well as options and information |
| 336 | for the Counter device itself. |
| 337 | |
| 338 | Each Signal has a directory created to house its relevant sysfs |
| 339 | attributes, where Y is the unique ID of the respective Signal: |
| 340 | |
| 341 | /sys/bus/counter/devices/counterX/signalY |
| 342 | |
| 343 | Similarly, each Count has a directory created to house its relevant |
| 344 | sysfs attributes, where Y is the unique ID of the respective Count: |
| 345 | |
| 346 | /sys/bus/counter/devices/counterX/countY |
| 347 | |
| 348 | For a more detailed breakdown of the available Generic Counter interface |
| 349 | sysfs attributes, please refer to the |
Marcus Folkesson | 224d5fd | 2019-07-09 09:54:36 +0200 | [diff] [blame] | 350 | Documentation/ABI/testing/sysfs-bus-counter file. |
William Breathitt Gray | 09e7d4e | 2019-04-02 15:30:38 +0900 | [diff] [blame] | 351 | |
| 352 | The Signals and Counts associated with the Counter device are registered |
| 353 | to the system as well by the counter_register function. The |
| 354 | signal_read/signal_write driver callbacks are associated with their |
| 355 | respective Signal attributes, while the count_read/count_write and |
| 356 | function_get/function_set driver callbacks are associated with their |
| 357 | respective Count attributes; similarly, the same is true for the |
| 358 | action_get/action_set driver callbacks and their respective Synapse |
| 359 | attributes. If a driver callback is left undefined, then the respective |
| 360 | read/write permission is left disabled for the relevant attributes. |
| 361 | |
| 362 | Similarly, extension sysfs attributes are created for the defined |
| 363 | counter_device_ext, counter_count_ext, and counter_signal_ext |
| 364 | structures that are passed in. |