Subject: Re: [PATCH v8 1/6] pwm: extend PWM framework with PWM modes

From: Thierry Reding <[email-masked]>
Date: Wed, 6 Feb 2019 00:01:26 +0100

On Mon, Jan 07, 2019 at 11:10:40PM +0100, Uwe Kleine-König wrote:
> Hello Claudiu,
> 
> On Mon, Jan 07, 2019 at 09:30:55AM +0000, [email-masked] wrote:
> > On 05.01.2019 23:05, Uwe Kleine-König wrote:
> > > On Thu, Jan 03, 2019 at 01:29:44PM +0000, [email-masked] wrote:
> > >> From: Claudiu Beznea <[email-masked]>
> > >>
> > >> Add basic PWM modes: normal and complementary. These modes should
> > >> differentiate the single output PWM channels from two outputs PWM
> > >> channels. These modes could be set as follow:
> > >> 1. PWM channels with one output per channel:
> > >> - normal mode
> > >> 2. PWM channels with two outputs per channel:
> > >> - normal mode
> > >> - complementary mode
> > >> Since users could use a PWM channel with two output as one output PWM
> > >> channel, the PWM normal mode is allowed to be set for PWM channels with
> > >> two outputs; in fact PWM normal mode should be supported by all PWMs.
> > > 
> > > I still think that my suggestion that I sent in reply to your v5 using
> > > .alt_duty_cycle and .alt_offset is the better one as it is more generic.
> > 
> > I like it better my way, I explained myself why.
> 
> I couldn't really follow your argument though. You seemed to acknowledge
> that using .alt_duty_cycle and .alt_offset is more generic. Then you
> wrote that the push-pull mode is hardware generated on Atmel with some
> implementation details. IMHO these implementation details shouldn't be
> part of the PWM API and atmel's .apply should look as follows:
> 
> 	if (state->alt_duty_cycle == 0) {
> 
> 		... configure for normal mode ...
> 
> 	} else if (state->duty_cycle == state->alt_duty_cycle &&
> 	           state->alt_offset == state->period / 2) {
> 
> 		... configure for push pull mode ...
> 
> 	} else if (state->duty_cycle + state->alt_duty_cycle == state->period &&
> 		   state->alt_offset == state->duty_cycle) {
> 
> 		... configure for complementary mode ...
> 
> 	} else {
> 		return -EINVAL;
> 	}
> 
> If it turns out to be a common pattern, we can add helper functions à la
> pwm_is_complementary_mode(state) and
> pwm_set_complementary_mode(state, period, duty_cycle). This allows to
> have a generic way to describe a wide range of wave forms in a uniform
> way in the API (which is good) and each driver implements the parts of
> this range that it can support.

I think this is going to be the rule rather than the exception, so I'd
expect we'll see these helpers used in pretty much all drivers that
support more than just the normal mode.

But I really don't see the point in having consumers jump through hoops
to set one of the standard modes just to have the driver jump through
more hoops to determine which mode was meant.

There are only so many modes and I have never seen hardware that
actually implements the kind of fine-grained control that would be
possible with your proposal.

The goal of an API is to abstract, but .alt_duty_cycle and .alt_offset
would be an inversion of API abstraction. That is, we'd be requiring the
drivers to abstract the inputs of the API, which is the wrong way
around.

> > > I don't repeat what I wrote there assuming you still remember or are
> > > willing to look it up at
> > > e.g. https://www.spinics.net/lists/linux-pwm/msg08174.html (in the 2nd half
> > > of my mail).
> > 
> > Yes, I remember it.
> 
> I expected that, my words were more directed to Thierry than you.
>  
> > > Also I think that if the capabilities function is the way forward adding
> > > support to detect availability of polarity inversion should be
> > > considered. 
> > 
> > Yep, why not. But it should be done in a different patch. It is not related
> > to this series.
> 
> Yes, given that polarity already exists, this would be a good
> opportunity to introduce the capability function for that and only
> afterwards add the new use case with modes. (But having said this, read
> further as I think that this capability function is a bad idea.)

I don't think we need to require this. The series is already big enough
as it is and has been in the works for long enough. There's no harm in
integrating polarity support into the capability function later on.

> > > This would also be an opportunity to split the introduction
> > > of the capabilities function and the introduction of complementary mode.
> > > (But my personal preference would be to just let .apply fail when an
> > > unsupported configuration is requested.)
> > 
> > .apply fails when something wrong is requested.
> 
> If my controller doesn't support a second output is it "wrong" to
> request complementary mode? I'd say yes. So you have to catch that in
> .apply anyhow and there is little benefit to be able to ask the
> controller if it supports it beforehand.
> 
> I don't have a provable statistic at hand, but my feeling is that quite
> some users of the i2c frame work get it wrong to first check the
> capabilities and only then try to use them. This is at least error prone
> and harder to use than the apply function returning an error code.
> And on the driver side the upside is to have all stuff related to which
> wave form can be generated and which cannot is a single place. (Just
> consider "inverted complementary mode". Theoretically this should work
> if your controller supports complementary mode and inverted mode. If you
> now have a driver for a controller that can do both, but not at the same
> time, the separation gets ugly. OK, this is a constructed example, but
> in my experience something like that happens earlier or later.)

I think capabilities are useful in order to be able to implement
fallbacks in consumer drivers. Sure the same thing could be implemented
by trying to apply one state first and then downgrade and retry on
failure and so on, but sometimes it's more convenient to know what's
possible and determine what's the correct solution upfront.

Thierry
_______________________________________________
linux-arm-kernel mailing list
[email-masked]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel