Re: [RFC PATCH 1/7] drivers: Add motion control subsystem

David Jander
2025-03-10T08:45:59Z

Hi Jonathan,

On Sun, 9 Mar 2025 17:32:50 +0000
Jonathan Cameron <jic23@kernel.org> wrote:

> On Thu, 6 Mar 2025 10:25:40 +0100
> David Jander <david@protonic.nl> wrote:
>
> > On Thu, 6 Mar 2025 00:21:22 +0100
> > Uwe Kleine-König <u.kleine-koenig@baylibre.com> wrote:
> >
> [...]
> > >
> > > Sad, so a userspace process still has to know some internal things about
> > > the motor it drives. :-\
> >
> > Unfortunately that is almost impossible to avoid entirely.
> > You can replace one stepper motor driver with another that might have
> > different micro-stepping subdivision, by looking at struct
> > mot_capabilities.subdiv, but a simple brushed DC motor just isn't able to
> > replace a stepper motor in all but the most trivial applications. I also think
> > that burdening the kernel with all sorts of complicated math to model the
> > mechanical conversion factors involved in anything that's connected to the
> > motor drive shaft is overkill. As well as trying to emulate all missing
> > capabilities from a motion device that is lacking that functionality natively.
> >
> > So just like in IIO you cannot just replace one ADC with any other, in LMC you
> > also cannot replace any device with any other.
> >
> > That's why there is struct mot_capabilities and MOT_IOCTL_GET_CAPA. It enables
> > user-space to optionally support different devices more easily. It is probably
> > best used in conjunction with a LMC user-space library, although I don't want
> > to rely on such a library for being able to use LMC. There is some middle
> > ground here I guess... just like in IIO.
> >
> > One thing I could try to improve though, is to include some additional
> > information in struct mot_capabilities that tells something more about the
> > nature of the used units, just like the speed_conv and accel_conv constants do
> > for time conversion. Something that can be placed in the device tree (possibly
> > in a motor child-node connected to the motor-controller) that contains some
> > conversion constant for distance. That way, if one were to (for example)
> > replace a stepper motor with a BLDC motor + encoder in a new hardware
> > revision, this constant could be used to make the units backwards compatible.
> >
> > As background information: A stepper motor controller counts distance in steps
> > and/or micro-steps. There are mot_capabilities.subdiv micro-steps in each
> > step. The amount of angle the actual motor shaft advances with each whole step
> > depends on the motor construction and is often 200 steps per revolution (1.8
> > degrees), but can vary from 4 to 400 steps per revolution depending on the
> > motor. So it is not only the controller that matters but also the type of
> > motor. This suggests the need of motor sub-nodes in the device-tree if one
> > wanted to extend the hardware knowledge further down from the motor driver.
> > But then there are gear boxes, pulleys, etc... it's basically conversion
> > factors all the way down. How many of them is sensible to bother the kernel
> > with?
>
> I'd have a motor description that is sufficient to be able to swap steppers
> between hardware versions and present sufficient info to userspace to allow
> a library to hide those differences. That description might well be of
> an aggregate device consisting of motor and whatever mechanics to get you
> to the point you care about (actuator motion). Hardest bit will be documenting
> 'where' in the system the DT is describing.

Is it really the purpose of a DT to describe mechanical aspects of a machine?
Aren't we stretching things here?

In case this makes sense, it would need to be optional. Only the electronics
are on the "board", the rest can vary beyond the scope of the DT: one can
connect different motors to a stepper motor controller and often that's the
purpose. Just as one can connect anything to a USB port.

Sometimes the controller is single purpose, just as some USB ports that have a
fixed device connected to it on the board, like a USB-ethernet interface for
example, which also has a certain fixed phy hooked up to it, etc...

If the purpose of the DT is to potentially go beyond the borders of a PCB (or
stack of PCB's) and even beyond the realm of electronic parts into mechanical
parts, wouldn't we need a way to describe a lot of things, like gearboxes,
pulleys, etc...?

Also, in the spirit of the DT, shouldn't that description be complete and
independent of the software (or even OS) involved? If that's the case, I fear
that "...to the point you care about..." is probably not enough?

> It's not that heavily used but we do have analog front ends in IIO that
> provide a not dissimilar thing to the various potential mechanisms here.

AFE's are still part of the electronics and often also part of the same board,
so there it does make more sense. I've used them.

Don't get me wrong, if there is general agreement that information
about mechanical things like a motor, gearbox or other mechanical
transformation of motion should be contained in the DT, I'm fine with it.
Maybe we should create an example to have something to talk about?

Take for example the extruder of a 3D printer:

Complete description of the mechanics, including the stepper motor of type
17HS4401 from a Chinese manufacturer called "songshine" and the extruder,
which consists of a gear reduction and a hobbled shaft pressing the filament
into the hot-end. The stepper motor does one full 360 degree turn per 200
whole steps. The extruder then pushes 2178 micrometer of filament into the
hot-end per whole turn of the motor shaft. We will assume that the extruder
itself is not a standard part that can be ordered and could not have a
standardize compatible string. The motor... could. Or it could be a generic
motor with some parameters in the DT, like this:

&spi2 {
...
stepper-controller3: tmc5240@0 {
compatible = "adi,tmc5240";
reg = <3>;
spi-max-frequency = <10000000>;
interrupts-extended = <&gpiok 6 IRQ_TYPE_LEVEL_LOW>;
clocks = <&clock_tmc5240>;

motor0 {
compatible = "bipolar-stepper-motor";
run-current-ma = <1300>;
hold-current-ma = <260>;
rated-voltage-mv = <3600>;
induction-uh = <2800>;
dc-resistance-mohm = <1500>;
holding-torque-nmm = <400>;
detent-torque-nmm = <22>;
steps-per-turn = <200>;

extruder0 {
compatible = "rotation-to-linear";
distance-per-turn-um = <2178>;
};
};
};
};

Or the motor could be a reusable part with its own entry in some
"bipolar-stepper-motors.c" database under a compatible string:

&spi2 {
...
stepper-controller3: tmc5240@0 {
compatible = "adi,tmc5240";
reg = <3>;
spi-max-frequency = <10000000>;
interrupts-extended = <&gpiok 6 IRQ_TYPE_LEVEL_LOW>;
clocks = <&clock_tmc5240>;

motor0 {
compatible = "songshine,17HS4401";

extruder0 {
compatible = "rotation-to-linear";
distance-per-turn-um = <2178>;
};
};
};
};

Or one could just condense all the information of the motor and mechanics into
one node with the needed properties to obtain the conversion factors the
motion framework needs. But that would make the DT dependent on the software
used, and AFAIK this is not the purpose of a DT, right? It might look like
this:

&spi2 {
...
stepper-controller3: tmc5240@0 {
compatible = "adi,tmc5240";
reg = <3>;
spi-max-frequency = <10000000>;
interrupts-extended = <&gpiok 6 IRQ_TYPE_LEVEL_LOW>;
clocks = <&clock_tmc5240>;

motor0 {
compatible = "stepper-linear-actuator";
run-current-ma = <1300>;
hold-current-ma = <260>;
steps-per-turn = <200>;
...
distance-per-turn-um = <2178>;
};
};
};

I've left out properties like reg, #address-cells and #size-cells here for brevity.

Let me know if you think any of this makes sense.

Best regards,

--
David Jander