On 1/21/25 9:19 AM, Csókás Bence wrote: > Hi all, > > we want to measure the frequency and duty cycle of a signal (relating to power consumption) using a hardware timer in our SoC (Microchip SAMA5D2). The hardware is capable of taking a snapshot of the timer value into another dedicated register pair (RA, RB) on the rising/falling edges, and a small `devmem`-based userspace utility was created as a working PoC. Now we want to move to a "proper" kernelspace solution. > > However, none of the existing drivers seem to be able to do this; the closest was `drivers/counter/microchip-tcb-capture.c`, but that only seems to count one type of edge (rising/falling), and cannot give us the time between them, which would be needed for duty cycle calculation. The only other driver I could find was `drivers/clocksource/timer-atmel-tcb.c`, which again seems incapable of such measurements. Therefore, a new module will probably be needed; the question then becomes: which API to implement. > > As `microchip-tcb-capture.c` uses the Generic Counter Interface, that was obviously a first stop. However, from what I could see, you can only represent (1) the number of times an edge has been encountered, and (2) rotary encodings (quadrature and direction-step decoders); and not the time between edges. You might find some interesting reading at [1]. A few years ago, I started adding support for an "edge capture unit" and timer on the TI EQEP quadrature encoder driver in the counter subsystem. I never found time to keep working on that to get it merged, but I've actually been looking at it again just last week. It sounds quite similar, but I didn't consider the possibility of looking at the duty cycle, only the rate. [1]: https://lore.kernel.org/linux-iio/20211017013343.3385923-1-david@xxxxxxxxxxxxxx/ > > IIO_ALTVOLTAGE and IIO_CHAN_INFO_FREQUENCY/_PHASE also seemed promising (although the lack of IIO_CHAN_INFO_DUTY_CYCLE already posed a problem), until I saw that all current drivers are frequency *generators*, and not measurers, the latter seems to be completely unimplemented. A similar case came up somewhat recently [2] where it was suggested to add IIO_FREQUENCY to be able to measure a clock frequency [3]. [2]: https://lore.kernel.org/linux-iio/20240624173105.909554-1-jbrunet@xxxxxxxxxxxx/ [3]: https://lore.kernel.org/linux-iio/20240629204025.683b1b69@jic23-huawei/ > > The only other contender I could find was the Hardware Timestamping Engine (HTE), but again, it's not clear whether (1) the API is even capable of relaying duty cycle information to userspace and (2) if it is, how would one go about implementing it. > > It is also entirely possible I missed a driver or API that could handle this better, if so, please don't keep it to yourselves. In the PWM subsystem, there is pwm_capture() which allows measuring period and duty cycle, so could be exactly what you are looking for. > > So, how could one go about implementing such a driver? > > Bence > >