Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-26 Thread Vokáč Michal
On 26.11.2018 14:34, Thierry Reding wrote:
> On Mon, Nov 26, 2018 at 01:23:16PM +0100, Lothar Waßmann wrote:
>> Thierry Reding  wrote:
>>
>>> On Fri, Nov 23, 2018 at 03:15:11PM +, Vokáč Michal wrote:
 On 22.11.2018 20:03, Uwe Kleine-König wrote:
> On Thu, Nov 22, 2018 at 04:46:39PM +, Vokáč Michal wrote:
>> On 22.11.2018 17:23, Uwe Kleine-König wrote:
>>> On Thu, Nov 22, 2018 at 03:42:14PM +, Vokáč Michal wrote:
 On 16.11.2018 09:25, Uwe Kleine-König wrote:
> On Fri, Nov 16, 2018 at 08:34:30AM +0100, Lothar Waßmann wrote:
>> No. You can disable the output driver via pinctrl, so that only the
>> selected pull-up/down is relevant. The pin function and GPIO register
>> settings don't matter at all in this case.

 Lothar, please can you be more specific how would you do that? IFAIK 
 the
 pull-up/down internal resistors have effect only if the pin is 
 configured
 as GPIO *input* (on i.MX6 at least). See the TRM, 29.4.2.2 Output 
 driver:

  "Internal pull-up, pull-down resistors, and pad keeper are 
 disabled in
   output mode."
>
> This would mean you'd have to rely on an external pull up for your use
> case. I wouldn't be surprised however if DSE=0 wouldn't count as "output
> mode". Given the reliability of NXP documentation I wouldn't bet neither
> on one nor the other possibility.

 Yeah, the NXP documentation sometimes does not really match reality.
 My use case is based on the fact that I configure the pin as input in
 the driver. Then it works just fine.

>>> So I'd expect this to really work on i.MX6 but not the earlier SoCs
>>> without a gpio specifier.
>>
>> Maybe you would expect it to work but I already tested and measured
>> that weeks ago ;) It did not work.
>
> Which pin/gpio do we talk about? Which i.MX6 variant did you test this
> on? (Assuming i.MX6D or i.MX6Q and PAD_DISP0_DATA09, did you try setting
>
>   IOMUXC_SW_MUX_CTL_PAD_DISP0_DATA09 (0x020E0194) = 0x0005
>   IOMUXC_SW_PAD_CTL_PAD_DISP0_DATA09 (0x020E04A8) = 0xb080
>
> and then play with GPIO 4.30 direction and output value?)

 My test setup is as follows:
 - SoC is i.MX6DL or i.MX6S - I have three board variants in total.
 - Pin used for PWM/GPIO is PAD_GPIO9.
 - The pin is not connected to any circuit. Just a test point.
 - pinctrl setup in DT:
 - for "pwm":
   - fsl,pins = 
   - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0004
   - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0x0008

 - for "gpio":
  - fsl,pins = 
  - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0005
  - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0xb000
>>>
>>> Does it help if you additionally set the ODE bit (bit 11) here?
>>>
>> That only helps to NOT actively pulling the pin HIGH, but the opposite
>> is what is needed here.
> 
>  From the description in the reference manual it sounded like the ODE
> would avoid the pin from actively being driven anywhere if configured as
> output. So I was hoping that in conjunction with the pull-up it would
> actually do the right thing.

Not exactly. I tested/measured this and the ODE setting works like
Lothar said.

If ODE=1 and GPIO is in output mode:

- The internal pull resistors are enabled even if PKE=0 (pull/keeper
   is disabled). That's news to me but it actually matches the GPIO pad
   functional diagram. See Figure 29-3 in RM.

- If the GPIO output is set LOW, the output is actively pulled down by
   the low side transistor.

- If the GPIO output is set HIGH the output is passively pulled up by
   the internal pull-up resistor. The value of the resistor can be
   selected by PUS and I am able to measure it.

That does not solve the issue that the pin may me configured as output
and set LOW from bootloader though.

Michal


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-26 Thread Thierry Reding
On Mon, Nov 26, 2018 at 01:23:16PM +0100, Lothar Waßmann wrote:
> Thierry Reding  wrote:
> 
> > On Fri, Nov 23, 2018 at 03:15:11PM +, Vokáč Michal wrote:
> > > On 22.11.2018 20:03, Uwe Kleine-König wrote:  
> > > > On Thu, Nov 22, 2018 at 04:46:39PM +, Vokáč Michal wrote:  
> > > >> On 22.11.2018 17:23, Uwe Kleine-König wrote:  
> > > >>> On Thu, Nov 22, 2018 at 03:42:14PM +, Vokáč Michal wrote:  
> > >  On 16.11.2018 09:25, Uwe Kleine-König wrote:  
> > > > On Fri, Nov 16, 2018 at 08:34:30AM +0100, Lothar Waßmann wrote:  
> > > >> No. You can disable the output driver via pinctrl, so that only the
> > > >> selected pull-up/down is relevant. The pin function and GPIO 
> > > >> register
> > > >> settings don't matter at all in this case.  
> > > 
> > >  Lothar, please can you be more specific how would you do that? IFAIK 
> > >  the
> > >  pull-up/down internal resistors have effect only if the pin is 
> > >  configured
> > >  as GPIO *input* (on i.MX6 at least). See the TRM, 29.4.2.2 Output 
> > >  driver:
> > > 
> > >  "Internal pull-up, pull-down resistors, and pad keeper are 
> > >  disabled in
> > >   output mode."  
> > > > 
> > > > This would mean you'd have to rely on an external pull up for your use
> > > > case. I wouldn't be surprised however if DSE=0 wouldn't count as "output
> > > > mode". Given the reliability of NXP documentation I wouldn't bet neither
> > > > on one nor the other possibility.  
> > > 
> > > Yeah, the NXP documentation sometimes does not really match reality.
> > > My use case is based on the fact that I configure the pin as input in
> > > the driver. Then it works just fine.
> > >   
> > > >>> So I'd expect this to really work on i.MX6 but not the earlier SoCs
> > > >>> without a gpio specifier.  
> > > >>
> > > >> Maybe you would expect it to work but I already tested and measured
> > > >> that weeks ago ;) It did not work.  
> > > > 
> > > > Which pin/gpio do we talk about? Which i.MX6 variant did you test this
> > > > on? (Assuming i.MX6D or i.MX6Q and PAD_DISP0_DATA09, did you try setting
> > > > 
> > > > IOMUXC_SW_MUX_CTL_PAD_DISP0_DATA09 (0x020E0194) = 0x0005
> > > > IOMUXC_SW_PAD_CTL_PAD_DISP0_DATA09 (0x020E04A8) = 0xb080
> > > > 
> > > > and then play with GPIO 4.30 direction and output value?)  
> > > 
> > > My test setup is as follows:
> > > - SoC is i.MX6DL or i.MX6S - I have three board variants in total.
> > > - Pin used for PWM/GPIO is PAD_GPIO9.
> > > - The pin is not connected to any circuit. Just a test point.
> > > - pinctrl setup in DT:
> > >- for "pwm":
> > >  - fsl,pins = 
> > >  - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0004
> > >  - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0x0008
> > > 
> > >- for "gpio":
> > > - fsl,pins = 
> > > - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0005
> > > - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0xb000  
> > 
> > Does it help if you additionally set the ODE bit (bit 11) here?
> > 
> That only helps to NOT actively pulling the pin HIGH, but the opposite
> is what is needed here.

From the description in the reference manual it sounded like the ODE
would avoid the pin from actively being driven anywhere if configured as
output. So I was hoping that in conjunction with the pull-up it would
actually do the right thing.

Thierry


signature.asc
Description: PGP signature


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-26 Thread Lothar Waßmann
Thierry Reding  wrote:

> On Fri, Nov 23, 2018 at 03:15:11PM +, Vokáč Michal wrote:
> > On 22.11.2018 20:03, Uwe Kleine-König wrote:  
> > > On Thu, Nov 22, 2018 at 04:46:39PM +, Vokáč Michal wrote:  
> > >> On 22.11.2018 17:23, Uwe Kleine-König wrote:  
> > >>> On Thu, Nov 22, 2018 at 03:42:14PM +, Vokáč Michal wrote:  
> >  On 16.11.2018 09:25, Uwe Kleine-König wrote:  
> > > On Fri, Nov 16, 2018 at 08:34:30AM +0100, Lothar Waßmann wrote:  
> > >> No. You can disable the output driver via pinctrl, so that only the
> > >> selected pull-up/down is relevant. The pin function and GPIO register
> > >> settings don't matter at all in this case.  
> > 
> >  Lothar, please can you be more specific how would you do that? IFAIK 
> >  the
> >  pull-up/down internal resistors have effect only if the pin is 
> >  configured
> >  as GPIO *input* (on i.MX6 at least). See the TRM, 29.4.2.2 Output 
> >  driver:
> > 
> >  "Internal pull-up, pull-down resistors, and pad keeper are 
> >  disabled in
> >   output mode."  
> > > 
> > > This would mean you'd have to rely on an external pull up for your use
> > > case. I wouldn't be surprised however if DSE=0 wouldn't count as "output
> > > mode". Given the reliability of NXP documentation I wouldn't bet neither
> > > on one nor the other possibility.  
> > 
> > Yeah, the NXP documentation sometimes does not really match reality.
> > My use case is based on the fact that I configure the pin as input in
> > the driver. Then it works just fine.
> >   
> > >>> So I'd expect this to really work on i.MX6 but not the earlier SoCs
> > >>> without a gpio specifier.  
> > >>
> > >> Maybe you would expect it to work but I already tested and measured
> > >> that weeks ago ;) It did not work.  
> > > 
> > > Which pin/gpio do we talk about? Which i.MX6 variant did you test this
> > > on? (Assuming i.MX6D or i.MX6Q and PAD_DISP0_DATA09, did you try setting
> > > 
> > >   IOMUXC_SW_MUX_CTL_PAD_DISP0_DATA09 (0x020E0194) = 0x0005
> > >   IOMUXC_SW_PAD_CTL_PAD_DISP0_DATA09 (0x020E04A8) = 0xb080
> > > 
> > > and then play with GPIO 4.30 direction and output value?)  
> > 
> > My test setup is as follows:
> > - SoC is i.MX6DL or i.MX6S - I have three board variants in total.
> > - Pin used for PWM/GPIO is PAD_GPIO9.
> > - The pin is not connected to any circuit. Just a test point.
> > - pinctrl setup in DT:
> >- for "pwm":
> >  - fsl,pins = 
> >  - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0004
> >  - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0x0008
> > 
> >- for "gpio":
> > - fsl,pins = 
> > - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0005
> > - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0xb000  
> 
> Does it help if you additionally set the ODE bit (bit 11) here?
> 
That only helps to NOT actively pulling the pin HIGH, but the opposite
is what is needed here.


Lothar Waßmann
-- 
___

Ka-Ro electronics GmbH | Pascalstraße 22 | D - 52076 Aachen
Phone: +49 2408 1402-0 | Fax: +49 2408 1402-10
Geschäftsführer: Matthias Kaussen
Handelsregistereintrag: Amtsgericht Aachen, HRB 4996

www.karo-electronics.de | i...@karo-electronics.de
___


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-26 Thread Thierry Reding
On Fri, Nov 23, 2018 at 03:15:11PM +, Vokáč Michal wrote:
> On 22.11.2018 20:03, Uwe Kleine-König wrote:
> > On Thu, Nov 22, 2018 at 04:46:39PM +, Vokáč Michal wrote:
> >> On 22.11.2018 17:23, Uwe Kleine-König wrote:
> >>> On Thu, Nov 22, 2018 at 03:42:14PM +, Vokáč Michal wrote:
>  On 16.11.2018 09:25, Uwe Kleine-König wrote:
> > On Fri, Nov 16, 2018 at 08:34:30AM +0100, Lothar Waßmann wrote:
> >> No. You can disable the output driver via pinctrl, so that only the
> >> selected pull-up/down is relevant. The pin function and GPIO register
> >> settings don't matter at all in this case.
> 
>  Lothar, please can you be more specific how would you do that? IFAIK the
>  pull-up/down internal resistors have effect only if the pin is configured
>  as GPIO *input* (on i.MX6 at least). See the TRM, 29.4.2.2 Output driver:
> 
>  "Internal pull-up, pull-down resistors, and pad keeper are disabled 
>  in
>   output mode."
> > 
> > This would mean you'd have to rely on an external pull up for your use
> > case. I wouldn't be surprised however if DSE=0 wouldn't count as "output
> > mode". Given the reliability of NXP documentation I wouldn't bet neither
> > on one nor the other possibility.
> 
> Yeah, the NXP documentation sometimes does not really match reality.
> My use case is based on the fact that I configure the pin as input in
> the driver. Then it works just fine.
> 
> >>> So I'd expect this to really work on i.MX6 but not the earlier SoCs
> >>> without a gpio specifier.
> >>
> >> Maybe you would expect it to work but I already tested and measured
> >> that weeks ago ;) It did not work.
> > 
> > Which pin/gpio do we talk about? Which i.MX6 variant did you test this
> > on? (Assuming i.MX6D or i.MX6Q and PAD_DISP0_DATA09, did you try setting
> > 
> > IOMUXC_SW_MUX_CTL_PAD_DISP0_DATA09 (0x020E0194) = 0x0005
> > IOMUXC_SW_PAD_CTL_PAD_DISP0_DATA09 (0x020E04A8) = 0xb080
> > 
> > and then play with GPIO 4.30 direction and output value?)
> 
> My test setup is as follows:
> - SoC is i.MX6DL or i.MX6S - I have three board variants in total.
> - Pin used for PWM/GPIO is PAD_GPIO9.
> - The pin is not connected to any circuit. Just a test point.
> - pinctrl setup in DT:
>- for "pwm":
>  - fsl,pins = 
>  - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0004
>  - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0x0008
> 
>- for "gpio":
> - fsl,pins = 
> - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0005
> - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0xb000

Does it help if you additionally set the ODE bit (bit 11) here?

Thierry


signature.asc
Description: PGP signature


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-26 Thread Lothar Waßmann
Uwe Kleine-König  wrote:

> Hallo Lothar,
> 
> On Mon, Nov 26, 2018 at 10:11:16AM +0100, Lothar Waßmann wrote:
> > Uwe Kleine-König  wrote:  
> > > @Lothar: if Michal did something different than you expected, please
> > > tell us with a few more details.  
> >
> > No, Michal's findings are in sync with what I checked here.  
> 
> so this is a confirmation that the gpio handling cannot be dropped,
> right?
> 
It seems so.


Lothar Waßmann
-- 
___

Ka-Ro electronics GmbH | Pascalstraße 22 | D - 52076 Aachen
Phone: +49 2408 1402-0 | Fax: +49 2408 1402-10
Geschäftsführer: Matthias Kaussen
Handelsregistereintrag: Amtsgericht Aachen, HRB 4996

www.karo-electronics.de | i...@karo-electronics.de
___


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-26 Thread Uwe Kleine-König
Hallo Lothar,

On Mon, Nov 26, 2018 at 10:11:16AM +0100, Lothar Waßmann wrote:
> Uwe Kleine-König  wrote:
> > @Lothar: if Michal did something different than you expected, please
> > tell us with a few more details.
>
> No, Michal's findings are in sync with what I checked here.

so this is a confirmation that the gpio handling cannot be dropped,
right?

Best regards
Uwe

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-26 Thread Lothar Waßmann
Uwe Kleine-König  wrote:

> Hello Michal,
> 
> On Fri, Nov 23, 2018 at 03:15:11PM +, Vokáč Michal wrote:
> > On 22.11.2018 20:03, Uwe Kleine-König wrote:  
> > > On Thu, Nov 22, 2018 at 04:46:39PM +, Vokáč Michal wrote:  
> > >> On 22.11.2018 17:23, Uwe Kleine-König wrote:  
> > >>> So I'd expect this to really work on i.MX6 but not the earlier SoCs
> > >>> without a gpio specifier.  
> > >>
> > >> Maybe you would expect it to work but I already tested and measured
> > >> that weeks ago ;) It did not work.  
> > > 
> > > Which pin/gpio do we talk about? Which i.MX6 variant did you test this
> > > on? (Assuming i.MX6D or i.MX6Q and PAD_DISP0_DATA09, did you try setting
> > > 
> > >   IOMUXC_SW_MUX_CTL_PAD_DISP0_DATA09 (0x020E0194) = 0x0005
> > >   IOMUXC_SW_PAD_CTL_PAD_DISP0_DATA09 (0x020E04A8) = 0xb080
> > > 
> > > and then play with GPIO 4.30 direction and output value?)  
> > 
> > My test setup is as follows:
> > - SoC is i.MX6DL or i.MX6S - I have three board variants in total.
> > - Pin used for PWM/GPIO is PAD_GPIO9.
> > - The pin is not connected to any circuit. Just a test point.
> > - pinctrl setup in DT:
> >- for "pwm":
> >  - fsl,pins = 
> >  - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0004
> >  - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0x0008
> > 
> >- for "gpio":
> > - fsl,pins = 
> > - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0005
> > - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0xb000  
> 
> FTR: I suggested 0xb080 here, but 0xb000 is just fine.
> 
> > Test scenario:
> > 
> > - In bootloader configure the pin as GPIO output, set it LOW (0V).
> >I set it LOW to start in the oposite state than I want to end up in.
> > - Boot Linux
> > - Use sysfs to configure the PWM to produce some signal.
> > - In the PWM driver select the "pwm" pinctrl state.
> > - Measure the output - expected waveform on the scope.
> > - Use sysfs to stop the PWM "echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable°
> > - In the PWM driver select the "gpio" pinctrl state.
> > - Then in the PWM driver:
> > 
> > a) non-working variant
> > - Do not request the GPIO - so do not touch its pad control reg.
> >It is set as output from the bootloader, data register = 0.
> > - Measure voltage on the output
> >- against GND = 0V
> >- against VCC = 0V
> > - Measure resistance against VCC: Hi-Z
> > 
> > The pin is in Hi-Z mode. That is fine but that does not satisfy any
> > logic level.
> > 
> > b) working variant
> > - Request the GPIO, configure it as input.
> > - Measure voltage on the output
> >- against GND = VCC (3.3V)
> >- against VCC = 0V
> > - Measure resistance against VCC = 91.1kOhm
> > 
> > The pull-up is active.  
> 
> Thanks for your detailed test. So DSE = 0 doesn't work as I (and I think
> Lothar) expected.
> 
> @Lothar: if Michal did something different than you expected, please
> tell us with a few more details.
> 
No, Michal's findings are in sync with what I checked here.


Lothar Waßmann
-- 
___

Ka-Ro electronics GmbH | Pascalstraße 22 | D - 52076 Aachen
Phone: +49 2408 1402-0 | Fax: +49 2408 1402-10
Geschäftsführer: Matthias Kaussen
Handelsregistereintrag: Amtsgericht Aachen, HRB 4996

www.karo-electronics.de | i...@karo-electronics.de
___


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-25 Thread Uwe Kleine-König
Hello Michal,

On Fri, Nov 23, 2018 at 03:15:11PM +, Vokáč Michal wrote:
> On 22.11.2018 20:03, Uwe Kleine-König wrote:
> > On Thu, Nov 22, 2018 at 04:46:39PM +, Vokáč Michal wrote:
> >> On 22.11.2018 17:23, Uwe Kleine-König wrote:
> >>> So I'd expect this to really work on i.MX6 but not the earlier SoCs
> >>> without a gpio specifier.
> >>
> >> Maybe you would expect it to work but I already tested and measured
> >> that weeks ago ;) It did not work.
> > 
> > Which pin/gpio do we talk about? Which i.MX6 variant did you test this
> > on? (Assuming i.MX6D or i.MX6Q and PAD_DISP0_DATA09, did you try setting
> > 
> > IOMUXC_SW_MUX_CTL_PAD_DISP0_DATA09 (0x020E0194) = 0x0005
> > IOMUXC_SW_PAD_CTL_PAD_DISP0_DATA09 (0x020E04A8) = 0xb080
> > 
> > and then play with GPIO 4.30 direction and output value?)
> 
> My test setup is as follows:
> - SoC is i.MX6DL or i.MX6S - I have three board variants in total.
> - Pin used for PWM/GPIO is PAD_GPIO9.
> - The pin is not connected to any circuit. Just a test point.
> - pinctrl setup in DT:
>- for "pwm":
>  - fsl,pins = 
>  - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0004
>  - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0x0008
> 
>- for "gpio":
> - fsl,pins = 
> - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0005
> - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0xb000

FTR: I suggested 0xb080 here, but 0xb000 is just fine.

> Test scenario:
> 
> - In bootloader configure the pin as GPIO output, set it LOW (0V).
>I set it LOW to start in the oposite state than I want to end up in.
> - Boot Linux
> - Use sysfs to configure the PWM to produce some signal.
> - In the PWM driver select the "pwm" pinctrl state.
> - Measure the output - expected waveform on the scope.
> - Use sysfs to stop the PWM "echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable°
> - In the PWM driver select the "gpio" pinctrl state.
> - Then in the PWM driver:
> 
> a) non-working variant
> - Do not request the GPIO - so do not touch its pad control reg.
>It is set as output from the bootloader, data register = 0.
> - Measure voltage on the output
>- against GND = 0V
>- against VCC = 0V
> - Measure resistance against VCC: Hi-Z
> 
> The pin is in Hi-Z mode. That is fine but that does not satisfy any
> logic level.
> 
> b) working variant
> - Request the GPIO, configure it as input.
> - Measure voltage on the output
>- against GND = VCC (3.3V)
>- against VCC = 0V
> - Measure resistance against VCC = 91.1kOhm
> 
> The pull-up is active.

Thanks for your detailed test. So DSE = 0 doesn't work as I (and I think
Lothar) expected.

@Lothar: if Michal did something different than you expected, please
tell us with a few more details.

> I can repeat the results on both i.MX6DL and i.MX6S on pwm1 and pwm4.
> All pull-up and pull-down combinations gave me the expected results
> only if the pin is configured as input. So if I repeat the test and
> do not touch the pin in bootloader at all, it works as well even if
> I do not request/configure the GPIO in the PWM driver.

OK.

Best regards
Uwe

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-23 Thread Vokáč Michal
On 22.11.2018 20:03, Uwe Kleine-König wrote:
> On Thu, Nov 22, 2018 at 04:46:39PM +, Vokáč Michal wrote:
>> On 22.11.2018 17:23, Uwe Kleine-König wrote:
>>> On Thu, Nov 22, 2018 at 03:42:14PM +, Vokáč Michal wrote:
 On 16.11.2018 09:25, Uwe Kleine-König wrote:
> On Fri, Nov 16, 2018 at 08:34:30AM +0100, Lothar Waßmann wrote:
>> No. You can disable the output driver via pinctrl, so that only the
>> selected pull-up/down is relevant. The pin function and GPIO register
>> settings don't matter at all in this case.

 Lothar, please can you be more specific how would you do that? IFAIK the
 pull-up/down internal resistors have effect only if the pin is configured
 as GPIO *input* (on i.MX6 at least). See the TRM, 29.4.2.2 Output driver:

 "Internal pull-up, pull-down resistors, and pad keeper are disabled in
  output mode."
> 
> This would mean you'd have to rely on an external pull up for your use
> case. I wouldn't be surprised however if DSE=0 wouldn't count as "output
> mode". Given the reliability of NXP documentation I wouldn't bet neither
> on one nor the other possibility.

Yeah, the NXP documentation sometimes does not really match reality.
My use case is based on the fact that I configure the pin as input in
the driver. Then it works just fine.

>>> So I'd expect this to really work on i.MX6 but not the earlier SoCs
>>> without a gpio specifier.
>>
>> Maybe you would expect it to work but I already tested and measured
>> that weeks ago ;) It did not work.
> 
> Which pin/gpio do we talk about? Which i.MX6 variant did you test this
> on? (Assuming i.MX6D or i.MX6Q and PAD_DISP0_DATA09, did you try setting
> 
>   IOMUXC_SW_MUX_CTL_PAD_DISP0_DATA09 (0x020E0194) = 0x0005
>   IOMUXC_SW_PAD_CTL_PAD_DISP0_DATA09 (0x020E04A8) = 0xb080
> 
> and then play with GPIO 4.30 direction and output value?)

My test setup is as follows:
- SoC is i.MX6DL or i.MX6S - I have three board variants in total.
- Pin used for PWM/GPIO is PAD_GPIO9.
- The pin is not connected to any circuit. Just a test point.
- pinctrl setup in DT:
   - for "pwm":
 - fsl,pins = 
 - IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0004
 - IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0x0008

   - for "gpio":
- fsl,pins = 
- IOMUXC_SW_MUX_CTL_PAD_GPIO09 = 0x0005
- IOMUXC_SW_PAD_CTL_PAD_GPIO09 = 0xb000

Test scenario:

- In bootloader configure the pin as GPIO output, set it LOW (0V).
   I set it LOW to start in the oposite state than I want to end up in.
- Boot Linux
- Use sysfs to configure the PWM to produce some signal.
- In the PWM driver select the "pwm" pinctrl state.
- Measure the output - expected waveform on the scope.
- Use sysfs to stop the PWM "echo 0 > /sys/class/pwm/pwmchip0/pwm0/enable°
- In the PWM driver select the "gpio" pinctrl state.
- Then in the PWM driver:

a) non-working variant
- Do not request the GPIO - so do not touch its pad control reg.
   It is set as output from the bootloader, data register = 0.
- Measure voltage on the output
   - against GND = 0V
   - against VCC = 0V
- Measure resistance against VCC: Hi-Z

The pin is in Hi-Z mode. That is fine but that does not satisfy any
logic level.

b) working variant
- Request the GPIO, configure it as input.
- Measure voltage on the output
   - against GND = VCC (3.3V)
   - against VCC = 0V
- Measure resistance against VCC = 91.1kOhm

The pull-up is active.

I can repeat the results on both i.MX6DL and i.MX6S on pwm1 and pwm4.
All pull-up and pull-down combinations gave me the expected results
only if the pin is configured as input. So if I repeat the test and
do not touch the pin in bootloader at all, it works as well even if
I do not request/configure the GPIO in the PWM driver.

But we can not rely on the fact that the pin is correctly configured
from bootloader or that it is in the after-reset state. It should
just work regardless of what state it was left in.

Best regards,
Michal


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-22 Thread Uwe Kleine-König
Hello Michal,

On Thu, Nov 22, 2018 at 04:46:39PM +, Vokáč Michal wrote:
> On 22.11.2018 17:23, Uwe Kleine-König wrote:
> > On Thu, Nov 22, 2018 at 03:42:14PM +, Vokáč Michal wrote:
> >> On 16.11.2018 09:25, Uwe Kleine-König wrote:
> >>> On Fri, Nov 16, 2018 at 08:34:30AM +0100, Lothar Waßmann wrote:
>  No. You can disable the output driver via pinctrl, so that only the
>  selected pull-up/down is relevant. The pin function and GPIO register
>  settings don't matter at all in this case.
> >>
> >> Lothar, please can you be more specific how would you do that? IFAIK the
> >> pull-up/down internal resistors have effect only if the pin is configured
> >> as GPIO *input* (on i.MX6 at least). See the TRM, 29.4.2.2 Output driver:
> >>
> >>"Internal pull-up, pull-down resistors, and pad keeper are disabled in
> >> output mode."

This would mean you'd have to rely on an external pull up for your use
case. I wouldn't be surprised however if DSE=0 wouldn't count as "output
mode". Given the reliability of NXP documentation I wouldn't bet neither
on one nor the other possibility.

> > So I'd expect this to really work on i.MX6 but not the earlier SoCs
> > without a gpio specifier.
> 
> Maybe you would expect it to work but I already tested and measured
> that weeks ago ;) It did not work.

Which pin/gpio do we talk about? Which i.MX6 variant did you test this
on? (Assuming i.MX6D or i.MX6Q and PAD_DISP0_DATA09, did you try setting

IOMUXC_SW_MUX_CTL_PAD_DISP0_DATA09 (0x020E0194) = 0x0005
IOMUXC_SW_PAD_CTL_PAD_DISP0_DATA09 (0x020E04A8) = 0xb080

and then play with GPIO 4.30 direction and output value?)

Best regards
Uwe

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-22 Thread Vokáč Michal
On 22.11.2018 17:23, Uwe Kleine-König wrote:
> Hello Michal,
> 
> On Thu, Nov 22, 2018 at 03:42:14PM +, Vokáč Michal wrote:
>> On 16.11.2018 09:25, Uwe Kleine-König wrote:
>>> On Fri, Nov 16, 2018 at 08:34:30AM +0100, Lothar Waßmann wrote:
 No. You can disable the output driver via pinctrl, so that only the
 selected pull-up/down is relevant. The pin function and GPIO register
 settings don't matter at all in this case.
>>
>> Lothar, please can you be more specific how would you do that? IFAIK the
>> pull-up/down internal resistors have effect only if the pin is configured
>> as GPIO *input* (on i.MX6 at least). See the TRM, 29.4.2.2 Output driver:
>>
>>"Internal pull-up, pull-down resistors, and pad keeper are disabled in
>> output mode."
>>
>>> Is this about DSE=HI-Z in the pad control register? I didn't know about
>>> this. I see this exists on i.MX6 but not on i.MX25. Also i.MX51 doesn't
>>> seem to have this feature.
>>
>> Uwe, I also did not find the DSE option on other SoCs in their respective
>> manuals and source code. But even if it would be available on all SoCs
>> I still think it is not enough.
> 
> If I understand Lothar correctly and this is about the pad control
> register, there is no need for software support. You just specify the
> value you want to have written to the pad register in the device tree.

I agree. But the pad control register is not the one that controls if
the pad is configured as input or output. So if the pin happens to be
configured as output the pull/keeper setting simply has no effect.

> So I'd expect this to really work on i.MX6 but not the earlier SoCs
> without a gpio specifier.

Maybe you would expect it to work but I already tested and measured
that weeks ago ;) It did not work.

Michal


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-22 Thread Uwe Kleine-König
Hello Michal,

On Thu, Nov 22, 2018 at 03:42:14PM +, Vokáč Michal wrote:
> On 16.11.2018 09:25, Uwe Kleine-König wrote:
> > On Fri, Nov 16, 2018 at 08:34:30AM +0100, Lothar Waßmann wrote:
> > > No. You can disable the output driver via pinctrl, so that only the
> > > selected pull-up/down is relevant. The pin function and GPIO register
> > > settings don't matter at all in this case.
> 
> Lothar, please can you be more specific how would you do that? IFAIK the
> pull-up/down internal resistors have effect only if the pin is configured
> as GPIO *input* (on i.MX6 at least). See the TRM, 29.4.2.2 Output driver:
> 
>   "Internal pull-up, pull-down resistors, and pad keeper are disabled in
>output mode."
> 
> > Is this about DSE=HI-Z in the pad control register? I didn't know about
> > this. I see this exists on i.MX6 but not on i.MX25. Also i.MX51 doesn't
> > seem to have this feature.
> 
> Uwe, I also did not find the DSE option on other SoCs in their respective
> manuals and source code. But even if it would be available on all SoCs
> I still think it is not enough.

If I understand Lothar correctly and this is about the pad control
register, there is no need for software support. You just specify the
value you want to have written to the pad register in the device tree.

So I'd expect this to really work on i.MX6 but not the earlier SoCs
without a gpio specifier.

Best regards
Uwe

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-22 Thread Uwe Kleine-König
Hello Thierry,

On Thu, Nov 22, 2018 at 04:03:38PM +0100, Thierry Reding wrote:
> On Sun, Nov 18, 2018 at 09:08:15PM +0100, Uwe Kleine-König wrote:
> > Thinking a bit about this it doesn't really matter for the consumer if
> > the pin stays in the idle level because there is a pull into the right
> > direction and the PWM is high-Z or if the PWM pulls actively in the
> > right direction. Also for pwm_config(pwm, 0, 100) the PWM could disable
> > its output in the presence of a pull, so the property says true that the
> > effects of pwm_config(pwm, 0, 100) and pwm_disable(pwm) should be the
> > same. And so my claim that pwm_disable is a part of the API that
> > doesn't give any value stays true.
> 
> I still think there's a slight difference there. Granted the effect on
> the consumer is the same whether you disable or set the duty-cycle to
> zero because the power output is the same. pwm_disable() is still more
> explicit, though, so it may involve more than just setting the duty-
> cycle.

If pwm_disable() vs. duty-cycle=0 doesn't make a difference to the
consumer, who do we do a favour by providing pwm_disable()?

> > > But again, why should PWMs be special? You turn off all other resources
> > > when you no longer need them, right? If you power your panel with a
> > > regulator, then when the panel is disabled you want to disable the
> > > regulator, right? Similarily if you don't use your I2C controller you
> > > want to turn off the clock that drives it, right? This is the same for
> > > any resource in your system: if you no longer need it, disable it. The
> > > fact that "disabling" the PWM is not straightforward on i.MX doesn't
> > > mean that we should simply ignore it.
> > 
> > I don't say we should ignore it. I say we shouldn't disable the hardware
> > if the consumer calls pwm_disable() if disabling the hardware results in
> > a state that shouldn't happen on pwm_disable().
> 
> That's backwards. If disabling the hardware results in a state that
> shouldn't happen when you disable the hardware, that just means that
> you're doing something wrong. When you do something wrong you fix it.

For you pwm_disable must imply disabling the PWM hardware. As you cannot
have both (disable the hardware and keep the pin on the idle level
without involving pinmux and GPIO) on i.MX in my eyes it is sensible to
drop one requirement from the framework's expectations to make it
possible for i.MX to fulfill them.

As there is no advantage for anyone to force .disable() to actually
disable the hardware (apart from a little power saving that probably
isn't even measurable) that's IMHO the one to drop.

> If the pin goes to the wrong level when you disable the hardware, the
> natural fix for the problem is to make sure the pin stays at the right
> level.

Ack. And you can do this either in an easy variant or in a more
complicated one.

Best regards
Uwe

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-22 Thread Vokáč Michal
On 16.11.2018 09:25, Uwe Kleine-König wrote:
> On Fri, Nov 16, 2018 at 08:34:30AM +0100, Lothar Waßmann wrote:
>> Uwe Kleine-König  wrote:
>>> On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:
 On Wed, Nov 14, 2018 at 10:51:20PM +0100, Uwe Kleine-König wrote:
> On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:
>> [...]
> Also dts writes don't need to lookup the needed GPIO numbers and pinctrl.

 Just to clarify: I don't think that we need to get the GPIO number
 involved in this case, because we don't have to reconfigure the pin as
 GPIO to make this work. The only reason that Michal's proposal did that
 is because that was believed to be necessary. But if the pin can just be
 configured with a 100k pull-up, that's enough to pull the pin high when
 we need it.

Thierry, I have to disagree here. I do not really know how to activate
the internal pull-up without requesting the GPIO and configuring the pin
as *input*. And to do it the GPIO number needs to be specified in DT.

>>> Unless the gpio happens to be configured as output at the wrong value.
>>> Further I'm not sure if the pwm in disabled state actively pulls to 0
>>> and if in this state the PU of the pin is good enough to ensure a one
>>> here. That would need verification first.
>>
>> No. You can disable the output driver via pinctrl, so that only the
>> selected pull-up/down is relevant. The pin function and GPIO register
>> settings don't matter at all in this case.

Lothar, please can you be more specific how would you do that? IFAIK the
pull-up/down internal resistors have effect only if the pin is configured
as GPIO *input* (on i.MX6 at least). See the TRM, 29.4.2.2 Output driver:

  "Internal pull-up, pull-down resistors, and pad keeper are disabled in
   output mode."

> Is this about DSE=HI-Z in the pad control register? I didn't know about
> this. I see this exists on i.MX6 but not on i.MX25. Also i.MX51 doesn't
> seem to have this feature.

Uwe, I also did not find the DSE option on other SoCs in their respective
manuals and source code. But even if it would be available on all SoCs
I still think it is not enough.

Best regards,
Michal


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-22 Thread Thierry Reding
On Sun, Nov 18, 2018 at 09:08:15PM +0100, Uwe Kleine-König wrote:
> Hello Thierry,
> 
> On Fri, Nov 16, 2018 at 01:24:45PM +0100, Thierry Reding wrote:
> > On Fri, Nov 16, 2018 at 11:39:29AM +0100, Uwe Kleine-König wrote:
> > > On Fri, Nov 16, 2018 at 10:51:24AM +0100, Thierry Reding wrote:
> > > > On Thu, Nov 15, 2018 at 09:37:33PM +0100, Uwe Kleine-König wrote:
> > > > > On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:
> > > > > > On Wed, Nov 14, 2018 at 10:51:20PM +0100, Uwe Kleine-König wrote:
> > > > > > > On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:
> > > > > > > > On Fri, Nov 09, 2018 at 05:55:55PM +0100, Uwe Kleine-König 
> > > > > > > > wrote:
> > > > > > > > > On Fri, Nov 09, 2018 at 02:24:42PM +, Vokáč Michal wrote:
> > > > > > > > > > On 8.11.2018 20:18, Uwe Kleine-König wrote:
> > > > > > > > > > > Taking your example with the backlight device you specify 
> > > > > > > > > > > an "init" and
> > > > > > > > > > > a "default" pinctrl and only "default" contains the 
> > > > > > > > > > > muxing for the PWM
> > > > > > > > > > > pin everything should be as smooth as necessary: The pwm 
> > > > > > > > > > > is only muxed
> > > > > > > > > > > when the backlight device is successfully bound.
> > > > > > > > > > 
> > > > > > > > > > Have you tried that Uwe? The bad news is I tested that 
> > > > > > > > > > before and now
> > > > > > > > > > again and it does not work like that. We already discussed 
> > > > > > > > > > that earlier.
> > > > > > > > > 
> > > > > > > > > The key is that the pinmux setting for the PWM pin should be 
> > > > > > > > > part of the
> > > > > > > > > bl pinctrl, not the pwm pinctrl. Then "default" is only setup 
> > > > > > > > > when the
> > > > > > > > > bl device is successfully bound which is after the bl's 
> > > > > > > > > .probe callback
> > > > > > > > > called pwm_apply().
> > > > > > > > 
> > > > > > > > No, that's not at all correct. Pinmux settings should reside 
> > > > > > > > with the
> > > > > > > > consumer of the pin. In this case, the PWM is the consumer of 
> > > > > > > > the pin,
> > > > > > > > whereas the backlight is the consumer of the *PWM*.
> > > > > > > 
> > > > > > > This is news to me. Adding Linus W. to Cc, maybe he can comment?!
> > > > > > > 
> > > > > > > Grepping through the arm device trees it really seems common to 
> > > > > > > put the
> > > > > > > pinctrl for the pwm pin into the pwm device. I didn't search in 
> > > > > > > depth,
> > > > > > > but I didn't find a counter example.
> > > > > > > 
> > > > > > > For GPIOs it is common that the pinmuxing is included in the 
> > > > > > > GPIO's
> > > > > > > consumer pinctrl. Ditto for mdio busses whose pinctrl is included 
> > > > > > > in the
> > > > > > > ethernet pinctrl.
> > > > > > 
> > > > > > GPIO is different from PWM in that the GPIO is already the pin 
> > > > > > itself
> > > > > > and is otherwise generic. So typically you put the pinmuxing options
> > > > > > into the device tree node for the consumer of the GPIO, because it 
> > > > > > is
> > > > > > only when the consumer uses the GPIO that you need to configure that
> > > > > > pin as GPIO.
> > > > > > 
> > > > > > For PWM, however, the PWM consumer is only the consumer of the PWM, 
> > > > > > but
> > > > > > the PWM device itself is the real consumer of the pin that outputs 
> > > > > > the
> > > > > > PWM signal. So the PWM determines when the pinmux states need to be
> > > > > > applied, whereas the consumer of the PWM only deals with the PWM.
> > > > > > 
> > > > > > For MDIO busses, I think they are usually part of, and driven by, 
> > > > > > the
> > > > > > ethernet controller, so again it makes sense to put the pinmux into 
> > > > > > the
> > > > > > node of the ethernet controller, because the ethernet controller is 
> > > > > > the
> > > > > > user of the pins.
> > > > > 
> > > > > Maybe it was a bad idea to broaden the discussion to talk about gpios
> > > > > and ethernet stuff here. I'd still consider it a valid construct to 
> > > > > put
> > > > > the pwm pin into the backlight's pinctrl unless Linux W. disagrees.
> > > > 
> > > > But why? The backlight doesn't care about the specific pinmuxing of the
> > > > PWM pin. All it cares about is the PWM signal. That's the level of
> > > > abstraction that the PWM consumer expects, anything lower level belongs
> > > > in the PWM driver.
> > > 
> > > The backlight driver cares about the PWM pin muxing because if it's
> > > wrongly muxed the backlight doesn't work as intended.
> > 
> > It shouldn't care about that. It should only care about the PWM and the
> > PWM should make sure that the pin is correctly muxed because otherwise
> > it can't work as expected.
> > 
> > > > > > > > The problem with making the PWM mode the "default" pinctrl 
> > > > > > > > state is that
> > > > > > > > the default state will be applied before the driver is even 
> > > > > > > > probed. That
> > > > > > > > makes it unsuitable for this c

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-22 Thread Vokáč Michal
Ahoj Uwe,

On 20.11.2018 17:54, Uwe Kleine-König wrote:
> On Tue, Nov 20, 2018 at 01:14:33PM +, Vokáč Michal wrote:
>> On 16.11.2018 10:51, Thierry Reding wrote:
>>> On Thu, Nov 15, 2018 at 09:37:33PM +0100, Uwe Kleine-König wrote:
 On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:
>>> My impression was that he was trying to find concensus. But the way I
>>> read it he was still arguing that the pinctrl solution was still the
>>> most complete, and would therefore still prefer it.
>>
>> It is like you said Thierry. I am only trying to remain open to test
>> Uwe's ideas. After what I have tested so far I still prefer a imx-specific
>> pinctrl solution.
> 
> Is that because you consider it right to be imx-specific or because
> that's what you already have? Is there a flaw in my explanation that
> the problem isn't imx-specific per se and so the solution shouldn't be
> that either?

I consider it right to be imx-specific because of the current state of
awareness and interest in this problem from other SoC's communities.
I spent quite some time looking for a solution and my impression is that
only imx users perceive this problem and are the only one that already
did some attempts to solve it.

I think this is in line with what Thierry said - if other users have
the same problem, they should speak-up. In my eyes as he is the pwm
subsystem maintainer he knows the best if others have the same problem
or not. My understanding of his arguments is such that they do not.

I understand that having a more generic solution may be better. I am not
really sure if it is valid in this case.

> Is the problem with the "tests so far" that it doesn't work yet, or is
> there something conceptually wrong in your eyes?

It does not work yet. And it would not solve all the cases. Namely the
sysfs use-case. And Thierry confirmed sysfs is a supported interface.

I do not dare to speculate whether your idea is conceptually wrong or
not. I only can say that putting the pinctrl of the PWM pin into the
PWM consumer seems very unnatural to me. I have not seen any example
of such usage yet. That does not mean it is wrong of course.

On the other hand, you said that we are inventing new pinctrl names.
But I see quite a lot of usages of non-["init", "default", "sleep"]
pinctrl names in the DTS files. Most of them are actually imx users.

Still, the main problem is it is not possible to turn-off the backlight
or cooling fan or whoever is the PWM consumer if inverted PWM is used.
Not that it is not possible to keep it turned-off until it is turned-on
by the user. So for me, any solution that at least allows that is better
than the current state. The pinctrl solution just solves all at once.

I fully agree the pinctrl solution has drawbacks. It needs to be well
documented and DT authors need to know how to use it and in what cases.

Best regards,
Michal



Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-20 Thread Uwe Kleine-König
Hello Michal,

On Tue, Nov 20, 2018 at 01:14:33PM +, Vokáč Michal wrote:
> On 16.11.2018 10:51, Thierry Reding wrote:
> > On Thu, Nov 15, 2018 at 09:37:33PM +0100, Uwe Kleine-König wrote:
> >> On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:
> > My impression was that he was trying to find concensus. But the way I
> > read it he was still arguing that the pinctrl solution was still the
> > most complete, and would therefore still prefer it.
> 
> It is like you said Thierry. I am only trying to remain open to test
> Uwe's ideas. After what I have tested so far I still prefer a imx-specific
> pinctrl solution.

Is that because you consider it right to be imx-specific or because
that's what you already have? Is there a flaw in my explanation that
the problem isn't imx-specific per se and so the solution shouldn't be
that either?

Is the problem with the "tests so far" that it doesn't work yet, or is
there something conceptually wrong in your eyes?

Best regards
Uwe

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-20 Thread Vokáč Michal
Hi,
sorry for the delay, I was out of office last week.
My comments below are just to clarify my attitude to the topic as my
name was mentioned few times while I was offline.

On 16.11.2018 10:51, Thierry Reding wrote:
> On Thu, Nov 15, 2018 at 09:37:33PM +0100, Uwe Kleine-König wrote:
>> On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:
>>> On Wed, Nov 14, 2018 at 10:51:20PM +0100, Uwe Kleine-König wrote:
 On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:

>> Maybe it was a bad idea to broaden the discussion to talk about gpios
>> and ethernet stuff here. I'd still consider it a valid construct to put
>> the pwm pin into the backlight's pinctrl unless Linux W. disagrees.
> 
> But why? The backlight doesn't care about the specific pinmuxing of the
> PWM pin. All it cares about is the PWM signal. That's the level of
> abstraction that the PWM consumer expects, anything lower level belongs
> in the PWM driver.

I also do not like the idea of putting the pinmuxing of the PWM pin into
the PWM consumer. That is why it never came to my mind to do it when
I tested what Uwe was suggesting.

 Other than that my approach looks more elegant to me (which obviously is
 subjective). It works in all cases apart from sysfs (which is special
 because it's not integrated into the device model) and there is no need
 to teach the pwm framework about pinmuxing and invent new pinctrl modes
 for it.
>>>
>>> Elegance is useless if what you have doesn't work consistently and
>>> reliably.
>>>
>>> I don't understand your resistance to the pinctrl work. It's not rocket
>>> science. Michal already posted patches showing how it can be done and
>>> they're not very complicated. Also we're not doing anything out of the
>>> ordinary here. This is exactly the purpose of the pinctrl framework, so
>>> let's use the best tool at hand.
>>
>> In my last exchange with Michal I had the impression that he liked
>> my approach, too.
> 
> My impression was that he was trying to find concensus. But the way I
> read it he was still arguing that the pinctrl solution was still the
> most complete, and would therefore still prefer it.

It is like you said Thierry. I am only trying to remain open to test
Uwe's ideas. After what I have tested so far I still prefer a imx-specific
pinctrl solution.

Michal


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-20 Thread Uwe Kleine-König
Hello,

On Tue, Nov 20, 2018 at 09:35:47AM +0100, Linus Walleij wrote:
> On Mon, Nov 19, 2018 at 9:32 AM Uwe Kleine-König
>  wrote:
> 
> > To sumarize: When the pwm driver probes it is not yet clear if the idle
> > state of the output pin is high or low. Even when the pinctrl device has
> > an "init" and a "default" pinctrl, it is not yet fixed when its
> > "default" is configured.
> >
> > The way Thierry wants to fix that is by disabling the output driver
> > until the pwm is in use und rely on a pull-up or pull-down in hardware.
> >
> > The way I want to fix this is to only configure the pwm pin as part of
> > the consumer. This is late enough that the consumer already requested
> > and configured the pwm such that the idle level is known.
> > Thierry's and Lothar's claim was that putting the pin setup of the pwm
> > pin into the pwm consumer's pinctrl was forbidden. That's why I asked
> > you as pinctrl maintainer if there is a requirement that I don't know
> > of.
> 
> I think we need to be pragmatic and listen most to whoever has
> the hardware and need to get it to work. The system needs
> to come up in some reasonable way, preferably so that vendors
> doing products with it do not have to apply a fat patch stack
> to get that backlight working in an acceptable way. Else the
> end result is out-of-tree code to paper over the issue and that
> IMO is worse than some minor ugliness in the kernel.
> 
> However the whole ordeal points to a problem that is with the
> pin control system and Thierry's and Lothar's intuition about this
> is right in a way: if the pin control system could read out the
> state of the hardware at boot (as we nowadays do with GPIO,
> which also has a consumer flag cleverly named GPIOD_ASIS)
> the whole thing would be no problem.

Well, on the problematic machine the setup currently is:

The bootloader configures the brightness pin as GPIO high output to
disable the backlight, the PWM isn't touched and so is off and
uninverted.

The next relevant event is that the PWM driver is probed.
The PWM registers are not touched, so keeps being off and outputs
a 0. If in this state the pin is muxed as PWM the output becomes 0 which
should be prevented, so this must not be part of a "default" or "init"
pinctrl for the PWM node.

The next event is that the backlight driver is probed. The probe
function grabs the PWM, tells it to be inverted and off. So the output
of the PWM is a 1 and only after that the PWM pin can be safely muxed as
PWM.

So the options are:

 a) In the bootloader setup the PWM as inverted and configure the pin as
PWM.

 b) Make the PWM aware that it is off and let it mux an "idle" setting
that depending on the SoC might involve muxing the pin as GPIO and
setting the GPIO as input.

 c) setup the PWM pin as PWM only when the backlight is probed.

a) has the downside that configuring a PWM is a tad more complex then
setting up a GPIO. Also it's a bit ugly to let Linux impose a limitation
on the bootloader about how to disable the backlight there.

b) has the downside that the dts author has to specify a GPIO and the
idle level and two instead of only one pinctrl setting. Conceptually
this is not so nice, because normally the PWM doesn't know if it is used
inverted or not. With the GPIO specification this information is
implicitly added. (Also there might be additional hurdles, for example
on i.MX31 (IIRC) there are pins that cannot be muxed as GPIO but are in
use as CS signal for spi. I didn't check if there is a similar
limitation for PWM pins.)

With c) the pwm is setup only when the backlight driver requests it. If
the backlight driver fails to probe or is disabled nothing happens to
the PWM pin. If the backlight driver was loaded successfully the PWM pin
is muxed just after it was set up with the right polarity. Similar as
with b) there is no limitation about how the bootloader disabled the
backlight. The beauty I see in c) is this is a solution that doesn't
have to care about GPIOs and muxing pins depending on the PWM state. And
given that there is such a way, I don't see why we should impose on dts
authors to specify these things that are not technically necessary.

Both b) and c) is about delaying muxing the pin as PWM late enough.
Suggestion b) is about doing this explicitly somewhere below driver/pwm,
c) only uses pinctrl as available automatically for all devices.

Probably its subjective if you consider b) or c) as the better/prettier
solution.

Note that the solution b) is independent of the imx uglyness[1] and
applies to all PWMs that don't disable its output driver on disable.
(And for PWMs that disable the output driver, the solution might be
needed anyhow because there is "broken" hardware that doesn't have the
right pullups assembled to ensure the right idle level when the pin
doesn't drive.)

So the sensible options are in my eyes:

 - Implement b) in the PWM core (and not the pwm-imx driver); or
 - just use c) accepting that the PWM is muxed no

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-20 Thread Viresh Kumar
On 20-11-18, 09:35, Linus Walleij wrote:
> The whole issue with splash screens and different hardware
> turned over to Linux in running state is a bit imperfect I would
> say, I think Viresh was working on boot constraints to get
> handover of different systems components into some kind
> of shape but maybe that stopped short because of the
> complexities involved.

It got stuck mostly due to lack of use-cases where that can be useful.
All I could find until now is LCD and earlycon stuff.

Though things may change soon after the interconnect subsystem gets
merged, as we will have bootloader requirements for each and every
device in that case which the boot loader enables and passes on to
kernel.

-- 
viresh


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-20 Thread Linus Walleij
On Mon, Nov 19, 2018 at 9:32 AM Uwe Kleine-König
 wrote:

> To sumarize: When the pwm driver probes it is not yet clear if the idle
> state of the output pin is high or low. Even when the pinctrl device has
> an "init" and a "default" pinctrl, it is not yet fixed when its
> "default" is configured.
>
> The way Thierry wants to fix that is by disabling the output driver
> until the pwm is in use und rely on a pull-up or pull-down in hardware.
>
> The way I want to fix this is to only configure the pwm pin as part of
> the consumer. This is late enough that the consumer already requested
> and configured the pwm such that the idle level is known.
> Thierry's and Lothar's claim was that putting the pin setup of the pwm
> pin into the pwm consumer's pinctrl was forbidden. That's why I asked
> you as pinctrl maintainer if there is a requirement that I don't know
> of.

I think we need to be pragmatic and listen most to whoever has
the hardware and need to get it to work. The system needs
to come up in some reasonable way, preferably so that vendors
doing products with it do not have to apply a fat patch stack
to get that backlight working in an acceptable way. Else the
end result is out-of-tree code to paper over the issue and that
IMO is worse than some minor ugliness in the kernel.

However the whole ordeal points to a problem that is with the
pin control system and Thierry's and Lothar's intuition about this
is right in a way: if the pin control system could read out the
state of the hardware at boot (as we nowadays do with GPIO,
which also has a consumer flag cleverly named GPIOD_ASIS)
the whole thing would be no problem.

The same kind of goes for PWM itself in this case I guess.

The whole issue with splash screens and different hardware
turned over to Linux in running state is a bit imperfect I would
say, I think Viresh was working on boot constraints to get
handover of different systems components into some kind
of shape but maybe that stopped short because of the
complexities involved. Isn't that the real problem here?
https://lkml.org/lkml/2017/8/1/191

Yours,
Linus Walleij


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-19 Thread Uwe Kleine-König
Hello,

On Sun, Nov 18, 2018 at 09:08:15PM +0100, Uwe Kleine-König wrote:
> On Fri, Nov 16, 2018 at 01:24:45PM +0100, Thierry Reding wrote:
> > On Fri, Nov 16, 2018 at 11:39:29AM +0100, Uwe Kleine-König wrote:
> > > Also note that you don't include the poor souls where there is no
> > > hardware pullup into the right direction.
> > 
> > The poor souls should speak up and then we can look into finding a good
> > solution for them. I'm pretty sure there must be some equivalent that
> > can be used for other users.
> 
> I think one of the boards that I'm working with has an enabled backlight
> at power on until the bootloader actively drives the related pwm to
> the right level (either using the pwm or the gpio function of the pin).
> I will double check that but assuming that is right, just disabling the
> output won't work here.

I just confirmed this. On the board I have in mind the backlight really
goes on during powerup. One of the first things the bootloader does is
to ensure the backlight goes off again by actively driving the
respective pin.

I fully agree with you this is unfortunate and the hardware team could
have made the situation smoother if there were a pull up. I guess asking
them to go back and fix that won't be accepted[1]. So please whatever
solution you force through, please consider it should work without a
sane pull and also with the imx SoCs before i.MX6 which cannot disable
the output driver via pinmux.

Best regards
Uwe

[1] Additionally this is a bought display+backlight+touch combo, so the
right place to put the pull would be in this unit, because the board
designer rightfully says: I won't put a pull up on my board because
for the next batch we're maybe switching to the similar display
combo that needs an active high signal for the backlight and I won't
create another factor 2 for the board variants for stuff that can be
fixed in software.

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-19 Thread Uwe Kleine-König
Hello Linus,

On Mon, Nov 19, 2018 at 08:44:38AM +0100, Linus Walleij wrote:
> There is quite a discussion you folks have going on here. I tried to
> grasp it but I can't. I can try to answer the above question specifically.

To sumarize: When the pwm driver probes it is not yet clear if the idle
state of the output pin is high or low. Even when the pinctrl device has
an "init" and a "default" pinctrl, it is not yet fixed when its
"default" is configured.

The way Thierry wants to fix that is by disabling the output driver
until the pwm is in use und rely on a pull-up or pull-down in hardware.

The way I want to fix this is to only configure the pwm pin as part of
the consumer. This is late enough that the consumer already requested
and configured the pwm such that the idle level is known.
Thierry's and Lothar's claim was that putting the pin setup of the pwm
pin into the pwm consumer's pinctrl was forbidden. That's why I asked
you as pinctrl maintainer if there is a requirement that I don't know
of.

> So compared to things like that (that we still have to support
> forever) whatever you are doing here you're doing great as long
> as it is consumer controlled.
> 
> Whether that consumer is the previous driver thingie in the
> daisy-chain of consumers or the final end user consumer of
> the pin doesn't really matter to pin control, as long as it is a
> consumer. I would tend to say it is up to the subsystem,
> and the old IETF motto "rough consensus and running code".

So I understand that from the pinctrl side there is no hard requirement
that the PWM pin must be muxed as part of the pwm device. And if things
work better when the pin is only configured as part of its consumer this
is legitimate.

Thanks for your feedback.

Best regards
Uwe

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-18 Thread Linus Walleij
On Wed, Nov 14, 2018 at 10:51 PM Uwe Kleine-König
 wrote:
> On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:
> > On Fri, Nov 09, 2018 at 05:55:55PM +0100, Uwe Kleine-König wrote:
> > > On Fri, Nov 09, 2018 at 02:24:42PM +, Vokáč Michal wrote:
> > > > On 8.11.2018 20:18, Uwe Kleine-König wrote:
> > > > > Taking your example with the backlight device you specify an "init" 
> > > > > and
> > > > > a "default" pinctrl and only "default" contains the muxing for the PWM
> > > > > pin everything should be as smooth as necessary: The pwm is only muxed
> > > > > when the backlight device is successfully bound.
> > > >
> > > > Have you tried that Uwe? The bad news is I tested that before and now
> > > > again and it does not work like that. We already discussed that earlier.
> > >
> > > The key is that the pinmux setting for the PWM pin should be part of the
> > > bl pinctrl, not the pwm pinctrl. Then "default" is only setup when the
> > > bl device is successfully bound which is after the bl's .probe callback
> > > called pwm_apply().
> >
> > No, that's not at all correct. Pinmux settings should reside with the
> > consumer of the pin. In this case, the PWM is the consumer of the pin,
> > whereas the backlight is the consumer of the *PWM*.
>
> This is news to me. Adding Linus W. to Cc, maybe he can comment?!
>
> Grepping through the arm device trees it really seems common to put the
> pinctrl for the pwm pin into the pwm device. I didn't search in depth,
> but I didn't find a counter example.
>
> For GPIOs it is common that the pinmuxing is included in the GPIO's
> consumer pinctrl. Ditto for mdio busses whose pinctrl is included in the
> ethernet pinctrl.

There is quite a discussion you folks have going on here. I tried to
grasp it but I can't. I can try to answer the above question specifically.

For pin control it is mainly paramount that the state is associated
with *a* consumer. The problem we were facing when fleshing out
the subsystem can be seen in horrific solutions such as in
Documentation/devicetree/bindings/gpio/gpio-twl4030.txt
where you see that pull-ups and pull-downs are set on the
PRODUCER side, which just make everything a complete
mess.

So compared to things like that (that we still have to support
forever) whatever you are doing here you're doing great as long
as it is consumer controlled.

Whether that consumer is the previous driver thingie in the
daisy-chain of consumers or the final end user consumer of
the pin doesn't really matter to pin control, as long as it is a
consumer. I would tend to say it is up to the subsystem,
and the old IETF motto "rough consensus and running code".

It seems in the current discussion the "rough consensus"
part is the problem, and I'm afraid I can't fix that.

Yours,
Linus Walleij


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-18 Thread Uwe Kleine-König
Hello Thierry,

On Fri, Nov 16, 2018 at 01:24:45PM +0100, Thierry Reding wrote:
> On Fri, Nov 16, 2018 at 11:39:29AM +0100, Uwe Kleine-König wrote:
> > On Fri, Nov 16, 2018 at 10:51:24AM +0100, Thierry Reding wrote:
> > > On Thu, Nov 15, 2018 at 09:37:33PM +0100, Uwe Kleine-König wrote:
> > > > On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:
> > > > > On Wed, Nov 14, 2018 at 10:51:20PM +0100, Uwe Kleine-König wrote:
> > > > > > On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:
> > > > > > > On Fri, Nov 09, 2018 at 05:55:55PM +0100, Uwe Kleine-König wrote:
> > > > > > > > On Fri, Nov 09, 2018 at 02:24:42PM +, Vokáč Michal wrote:
> > > > > > > > > On 8.11.2018 20:18, Uwe Kleine-König wrote:
> > > > > > > > > > Taking your example with the backlight device you specify 
> > > > > > > > > > an "init" and
> > > > > > > > > > a "default" pinctrl and only "default" contains the muxing 
> > > > > > > > > > for the PWM
> > > > > > > > > > pin everything should be as smooth as necessary: The pwm is 
> > > > > > > > > > only muxed
> > > > > > > > > > when the backlight device is successfully bound.
> > > > > > > > > 
> > > > > > > > > Have you tried that Uwe? The bad news is I tested that before 
> > > > > > > > > and now
> > > > > > > > > again and it does not work like that. We already discussed 
> > > > > > > > > that earlier.
> > > > > > > > 
> > > > > > > > The key is that the pinmux setting for the PWM pin should be 
> > > > > > > > part of the
> > > > > > > > bl pinctrl, not the pwm pinctrl. Then "default" is only setup 
> > > > > > > > when the
> > > > > > > > bl device is successfully bound which is after the bl's .probe 
> > > > > > > > callback
> > > > > > > > called pwm_apply().
> > > > > > > 
> > > > > > > No, that's not at all correct. Pinmux settings should reside with 
> > > > > > > the
> > > > > > > consumer of the pin. In this case, the PWM is the consumer of the 
> > > > > > > pin,
> > > > > > > whereas the backlight is the consumer of the *PWM*.
> > > > > > 
> > > > > > This is news to me. Adding Linus W. to Cc, maybe he can comment?!
> > > > > > 
> > > > > > Grepping through the arm device trees it really seems common to put 
> > > > > > the
> > > > > > pinctrl for the pwm pin into the pwm device. I didn't search in 
> > > > > > depth,
> > > > > > but I didn't find a counter example.
> > > > > > 
> > > > > > For GPIOs it is common that the pinmuxing is included in the GPIO's
> > > > > > consumer pinctrl. Ditto for mdio busses whose pinctrl is included 
> > > > > > in the
> > > > > > ethernet pinctrl.
> > > > > 
> > > > > GPIO is different from PWM in that the GPIO is already the pin itself
> > > > > and is otherwise generic. So typically you put the pinmuxing options
> > > > > into the device tree node for the consumer of the GPIO, because it is
> > > > > only when the consumer uses the GPIO that you need to configure that
> > > > > pin as GPIO.
> > > > > 
> > > > > For PWM, however, the PWM consumer is only the consumer of the PWM, 
> > > > > but
> > > > > the PWM device itself is the real consumer of the pin that outputs the
> > > > > PWM signal. So the PWM determines when the pinmux states need to be
> > > > > applied, whereas the consumer of the PWM only deals with the PWM.
> > > > > 
> > > > > For MDIO busses, I think they are usually part of, and driven by, the
> > > > > ethernet controller, so again it makes sense to put the pinmux into 
> > > > > the
> > > > > node of the ethernet controller, because the ethernet controller is 
> > > > > the
> > > > > user of the pins.
> > > > 
> > > > Maybe it was a bad idea to broaden the discussion to talk about gpios
> > > > and ethernet stuff here. I'd still consider it a valid construct to put
> > > > the pwm pin into the backlight's pinctrl unless Linux W. disagrees.
> > > 
> > > But why? The backlight doesn't care about the specific pinmuxing of the
> > > PWM pin. All it cares about is the PWM signal. That's the level of
> > > abstraction that the PWM consumer expects, anything lower level belongs
> > > in the PWM driver.
> > 
> > The backlight driver cares about the PWM pin muxing because if it's
> > wrongly muxed the backlight doesn't work as intended.
> 
> It shouldn't care about that. It should only care about the PWM and the
> PWM should make sure that the pin is correctly muxed because otherwise
> it can't work as expected.
> 
> > > > > > > The problem with making the PWM mode the "default" pinctrl state 
> > > > > > > is that
> > > > > > > the default state will be applied before the driver is even 
> > > > > > > probed. That
> > > > > > > makes it unsuitable for this case. I think what we really want 
> > > > > > > here is
> > > > > > > explicitly "active" and "inactive" states for pinctrl where the 
> > > > > > > PWM
> > > > > > > driver controls when exactly each state is applied.
> > > > > > 
> > > > > > Note that this problem goes away nicely if the pwm pin is attached 
> > > > > > to
> > > > > > the ba

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-18 Thread Uwe Kleine-König
Hello Lothar,

On Fri, Nov 16, 2018 at 12:56:33PM +0100, Lothar Waßmann wrote:
> Uwe Kleine-König  wrote:
> > On Fri, Nov 16, 2018 at 10:51:24AM +0100, Thierry Reding wrote:
> > > On Thu, Nov 15, 2018 at 09:37:33PM +0100, Uwe Kleine-König wrote:  
> > > > On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:  
> [...]
> > > But why? The backlight doesn't care about the specific pinmuxing of the
> > > PWM pin. All it cares about is the PWM signal. That's the level of
> > > abstraction that the PWM consumer expects, anything lower level belongs
> > > in the PWM driver.  
> > 
> > The backlight driver cares about the PWM pin muxing because if it's
> > wrongly muxed the backlight doesn't work as intended.
>
> With this argumentation you would also have to define the clocks needed
> for the PWM in the backlight (or whatever pwm consumer) driver, because
> if the clocks are not set up correctly the backlight won't work as
> expected...

The reason why I think it is sensible to put the PWM pin into the
consumer's pinctrl is that only after the consumer grabbed the PWM it is
fixed (in software) what the idle level is. So the pwm alone isn't
complete and it's not clear what to do with this pin. High-Z is probably
a good choice for most setups, but this relies on a PU or PD into the
right direction that isn't always present. (I'm currently working with
such a machine, where such a PD/PU is missing. Not entirely sure this is
a problem because maybe the display unit has a pull. Didn't take the
time yet to verify.) Also this needs some care because the PWM might
already running on purpose (e.g. to show a splash screen) then switching
to High-Z should be avoided.

So IMHO it is sensible to delay setting up the pinmux until the boundary
conditions are fixed. This is also the general idea with the involvement
of pinctrl stuff, but given that even without handling pinctrl specially
it already works just fine when the pwm is only muxed as part of the
consumer.
 
Best regards
Uwe

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-16 Thread Lothar Waßmann
Uwe Kleine-König  wrote:

> Hello Thierry,
> 
> On Fri, Nov 16, 2018 at 10:51:24AM +0100, Thierry Reding wrote:
> > On Thu, Nov 15, 2018 at 09:37:33PM +0100, Uwe Kleine-König wrote:  
> > > On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:  
[...]
> > But why? The backlight doesn't care about the specific pinmuxing of the
> > PWM pin. All it cares about is the PWM signal. That's the level of
> > abstraction that the PWM consumer expects, anything lower level belongs
> > in the PWM driver.  
> 
> The backlight driver cares about the PWM pin muxing because if it's
> wrongly muxed the backlight doesn't work as intended.
> 
With this argumentation you would also have to define the clocks needed
for the PWM in the backlight (or whatever pwm consumer) driver, because
if the clocks are not set up correctly the backlight won't work as
expected...

The PWM driver is driving the pins, so that is where the pinctrls
belong.


Lothar Waßmann
-- 
___

Ka-Ro electronics GmbH | Pascalstraße 22 | D - 52076 Aachen
Phone: +49 2408 1402-0 | Fax: +49 2408 1402-10
Geschäftsführer: Matthias Kaussen
Handelsregistereintrag: Amtsgericht Aachen, HRB 4996

www.karo-electronics.de | i...@karo-electronics.de
___


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-16 Thread Thierry Reding
On Fri, Nov 16, 2018 at 11:39:29AM +0100, Uwe Kleine-König wrote:
> Hello Thierry,
> 
> On Fri, Nov 16, 2018 at 10:51:24AM +0100, Thierry Reding wrote:
> > On Thu, Nov 15, 2018 at 09:37:33PM +0100, Uwe Kleine-König wrote:
> > > On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:
> > > > On Wed, Nov 14, 2018 at 10:51:20PM +0100, Uwe Kleine-König wrote:
> > > > > On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:
> > > > > > On Fri, Nov 09, 2018 at 05:55:55PM +0100, Uwe Kleine-König wrote:
> > > > > > > On Fri, Nov 09, 2018 at 02:24:42PM +, Vokáč Michal wrote:
> > > > > > > > On 8.11.2018 20:18, Uwe Kleine-König wrote:
> > > > > > > > > Taking your example with the backlight device you specify an 
> > > > > > > > > "init" and
> > > > > > > > > a "default" pinctrl and only "default" contains the muxing 
> > > > > > > > > for the PWM
> > > > > > > > > pin everything should be as smooth as necessary: The pwm is 
> > > > > > > > > only muxed
> > > > > > > > > when the backlight device is successfully bound.
> > > > > > > > 
> > > > > > > > Have you tried that Uwe? The bad news is I tested that before 
> > > > > > > > and now
> > > > > > > > again and it does not work like that. We already discussed that 
> > > > > > > > earlier.
> > > > > > > 
> > > > > > > The key is that the pinmux setting for the PWM pin should be part 
> > > > > > > of the
> > > > > > > bl pinctrl, not the pwm pinctrl. Then "default" is only setup 
> > > > > > > when the
> > > > > > > bl device is successfully bound which is after the bl's .probe 
> > > > > > > callback
> > > > > > > called pwm_apply().
> > > > > > 
> > > > > > No, that's not at all correct. Pinmux settings should reside with 
> > > > > > the
> > > > > > consumer of the pin. In this case, the PWM is the consumer of the 
> > > > > > pin,
> > > > > > whereas the backlight is the consumer of the *PWM*.
> > > > > 
> > > > > This is news to me. Adding Linus W. to Cc, maybe he can comment?!
> > > > > 
> > > > > Grepping through the arm device trees it really seems common to put 
> > > > > the
> > > > > pinctrl for the pwm pin into the pwm device. I didn't search in depth,
> > > > > but I didn't find a counter example.
> > > > > 
> > > > > For GPIOs it is common that the pinmuxing is included in the GPIO's
> > > > > consumer pinctrl. Ditto for mdio busses whose pinctrl is included in 
> > > > > the
> > > > > ethernet pinctrl.
> > > > 
> > > > GPIO is different from PWM in that the GPIO is already the pin itself
> > > > and is otherwise generic. So typically you put the pinmuxing options
> > > > into the device tree node for the consumer of the GPIO, because it is
> > > > only when the consumer uses the GPIO that you need to configure that
> > > > pin as GPIO.
> > > > 
> > > > For PWM, however, the PWM consumer is only the consumer of the PWM, but
> > > > the PWM device itself is the real consumer of the pin that outputs the
> > > > PWM signal. So the PWM determines when the pinmux states need to be
> > > > applied, whereas the consumer of the PWM only deals with the PWM.
> > > > 
> > > > For MDIO busses, I think they are usually part of, and driven by, the
> > > > ethernet controller, so again it makes sense to put the pinmux into the
> > > > node of the ethernet controller, because the ethernet controller is the
> > > > user of the pins.
> > > 
> > > Maybe it was a bad idea to broaden the discussion to talk about gpios
> > > and ethernet stuff here. I'd still consider it a valid construct to put
> > > the pwm pin into the backlight's pinctrl unless Linux W. disagrees.
> > 
> > But why? The backlight doesn't care about the specific pinmuxing of the
> > PWM pin. All it cares about is the PWM signal. That's the level of
> > abstraction that the PWM consumer expects, anything lower level belongs
> > in the PWM driver.
> 
> The backlight driver cares about the PWM pin muxing because if it's
> wrongly muxed the backlight doesn't work as intended.

It shouldn't care about that. It should only care about the PWM and the
PWM should make sure that the pin is correctly muxed because otherwise
it can't work as expected.

> > > > > > The problem with making the PWM mode the "default" pinctrl state is 
> > > > > > that
> > > > > > the default state will be applied before the driver is even probed. 
> > > > > > That
> > > > > > makes it unsuitable for this case. I think what we really want here 
> > > > > > is
> > > > > > explicitly "active" and "inactive" states for pinctrl where the PWM
> > > > > > driver controls when exactly each state is applied.
> > > > > 
> > > > > Note that this problem goes away nicely if the pwm pin is attached to
> > > > > the backlight. Because it's the backlight's driver that "knows" when 
> > > > > the
> > > > > pwm is configured correctly and so the already existing mechanisms 
> > > > > that
> > > > > setup the mux when the bl is correctly probed do the right thing at 
> > > > > the
> > > > > right time.
> > > > 
> > > > Actually that

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-16 Thread Uwe Kleine-König
Hello Thierry,

On Fri, Nov 16, 2018 at 10:51:24AM +0100, Thierry Reding wrote:
> On Thu, Nov 15, 2018 at 09:37:33PM +0100, Uwe Kleine-König wrote:
> > On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:
> > > On Wed, Nov 14, 2018 at 10:51:20PM +0100, Uwe Kleine-König wrote:
> > > > On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:
> > > > > On Fri, Nov 09, 2018 at 05:55:55PM +0100, Uwe Kleine-König wrote:
> > > > > > On Fri, Nov 09, 2018 at 02:24:42PM +, Vokáč Michal wrote:
> > > > > > > On 8.11.2018 20:18, Uwe Kleine-König wrote:
> > > > > > > > Taking your example with the backlight device you specify an 
> > > > > > > > "init" and
> > > > > > > > a "default" pinctrl and only "default" contains the muxing for 
> > > > > > > > the PWM
> > > > > > > > pin everything should be as smooth as necessary: The pwm is 
> > > > > > > > only muxed
> > > > > > > > when the backlight device is successfully bound.
> > > > > > > 
> > > > > > > Have you tried that Uwe? The bad news is I tested that before and 
> > > > > > > now
> > > > > > > again and it does not work like that. We already discussed that 
> > > > > > > earlier.
> > > > > > 
> > > > > > The key is that the pinmux setting for the PWM pin should be part 
> > > > > > of the
> > > > > > bl pinctrl, not the pwm pinctrl. Then "default" is only setup when 
> > > > > > the
> > > > > > bl device is successfully bound which is after the bl's .probe 
> > > > > > callback
> > > > > > called pwm_apply().
> > > > > 
> > > > > No, that's not at all correct. Pinmux settings should reside with the
> > > > > consumer of the pin. In this case, the PWM is the consumer of the pin,
> > > > > whereas the backlight is the consumer of the *PWM*.
> > > > 
> > > > This is news to me. Adding Linus W. to Cc, maybe he can comment?!
> > > > 
> > > > Grepping through the arm device trees it really seems common to put the
> > > > pinctrl for the pwm pin into the pwm device. I didn't search in depth,
> > > > but I didn't find a counter example.
> > > > 
> > > > For GPIOs it is common that the pinmuxing is included in the GPIO's
> > > > consumer pinctrl. Ditto for mdio busses whose pinctrl is included in the
> > > > ethernet pinctrl.
> > > 
> > > GPIO is different from PWM in that the GPIO is already the pin itself
> > > and is otherwise generic. So typically you put the pinmuxing options
> > > into the device tree node for the consumer of the GPIO, because it is
> > > only when the consumer uses the GPIO that you need to configure that
> > > pin as GPIO.
> > > 
> > > For PWM, however, the PWM consumer is only the consumer of the PWM, but
> > > the PWM device itself is the real consumer of the pin that outputs the
> > > PWM signal. So the PWM determines when the pinmux states need to be
> > > applied, whereas the consumer of the PWM only deals with the PWM.
> > > 
> > > For MDIO busses, I think they are usually part of, and driven by, the
> > > ethernet controller, so again it makes sense to put the pinmux into the
> > > node of the ethernet controller, because the ethernet controller is the
> > > user of the pins.
> > 
> > Maybe it was a bad idea to broaden the discussion to talk about gpios
> > and ethernet stuff here. I'd still consider it a valid construct to put
> > the pwm pin into the backlight's pinctrl unless Linux W. disagrees.
> 
> But why? The backlight doesn't care about the specific pinmuxing of the
> PWM pin. All it cares about is the PWM signal. That's the level of
> abstraction that the PWM consumer expects, anything lower level belongs
> in the PWM driver.

The backlight driver cares about the PWM pin muxing because if it's
wrongly muxed the backlight doesn't work as intended.

> > > > > The problem with making the PWM mode the "default" pinctrl state is 
> > > > > that
> > > > > the default state will be applied before the driver is even probed. 
> > > > > That
> > > > > makes it unsuitable for this case. I think what we really want here is
> > > > > explicitly "active" and "inactive" states for pinctrl where the PWM
> > > > > driver controls when exactly each state is applied.
> > > > 
> > > > Note that this problem goes away nicely if the pwm pin is attached to
> > > > the backlight. Because it's the backlight's driver that "knows" when the
> > > > pwm is configured correctly and so the already existing mechanisms that
> > > > setup the mux when the bl is correctly probed do the right thing at the
> > > > right time.
> > > 
> > > Actually that's not exactly true. The default pinctrl state will be
> > > applied before the driver's ->probe() implementation, so the pinctrl
> > > state will be active some time before even the backlight driver gets
> > > around to setting up the PWM. If you look at drivers/base/dd.c you'll
> > > see that really_probe() calls pinctrl_bind_pins() before calling the
> > > driver's ->probe() and will select the default state (unless there's
> > > also an "init" state defined, in which case that will get applied an

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-16 Thread Thierry Reding
On Thu, Nov 15, 2018 at 09:37:33PM +0100, Uwe Kleine-König wrote:
> Hello Thierry,
> 
> On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:
> > On Wed, Nov 14, 2018 at 10:51:20PM +0100, Uwe Kleine-König wrote:
> > > On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:
> > > > On Fri, Nov 09, 2018 at 05:55:55PM +0100, Uwe Kleine-König wrote:
> > > > > On Fri, Nov 09, 2018 at 02:24:42PM +, Vokáč Michal wrote:
> > > > > > On 8.11.2018 20:18, Uwe Kleine-König wrote:
> > > > > > > Taking your example with the backlight device you specify an 
> > > > > > > "init" and
> > > > > > > a "default" pinctrl and only "default" contains the muxing for 
> > > > > > > the PWM
> > > > > > > pin everything should be as smooth as necessary: The pwm is only 
> > > > > > > muxed
> > > > > > > when the backlight device is successfully bound.
> > > > > > 
> > > > > > Have you tried that Uwe? The bad news is I tested that before and 
> > > > > > now
> > > > > > again and it does not work like that. We already discussed that 
> > > > > > earlier.
> > > > > 
> > > > > The key is that the pinmux setting for the PWM pin should be part of 
> > > > > the
> > > > > bl pinctrl, not the pwm pinctrl. Then "default" is only setup when the
> > > > > bl device is successfully bound which is after the bl's .probe 
> > > > > callback
> > > > > called pwm_apply().
> > > > 
> > > > No, that's not at all correct. Pinmux settings should reside with the
> > > > consumer of the pin. In this case, the PWM is the consumer of the pin,
> > > > whereas the backlight is the consumer of the *PWM*.
> > > 
> > > This is news to me. Adding Linus W. to Cc, maybe he can comment?!
> > > 
> > > Grepping through the arm device trees it really seems common to put the
> > > pinctrl for the pwm pin into the pwm device. I didn't search in depth,
> > > but I didn't find a counter example.
> > > 
> > > For GPIOs it is common that the pinmuxing is included in the GPIO's
> > > consumer pinctrl. Ditto for mdio busses whose pinctrl is included in the
> > > ethernet pinctrl.
> > 
> > GPIO is different from PWM in that the GPIO is already the pin itself
> > and is otherwise generic. So typically you put the pinmuxing options
> > into the device tree node for the consumer of the GPIO, because it is
> > only when the consumer uses the GPIO that you need to configure that
> > pin as GPIO.
> > 
> > For PWM, however, the PWM consumer is only the consumer of the PWM, but
> > the PWM device itself is the real consumer of the pin that outputs the
> > PWM signal. So the PWM determines when the pinmux states need to be
> > applied, whereas the consumer of the PWM only deals with the PWM.
> > 
> > For MDIO busses, I think they are usually part of, and driven by, the
> > ethernet controller, so again it makes sense to put the pinmux into the
> > node of the ethernet controller, because the ethernet controller is the
> > user of the pins.
> 
> Maybe it was a bad idea to broaden the discussion to talk about gpios
> and ethernet stuff here. I'd still consider it a valid construct to put
> the pwm pin into the backlight's pinctrl unless Linux W. disagrees.

But why? The backlight doesn't care about the specific pinmuxing of the
PWM pin. All it cares about is the PWM signal. That's the level of
abstraction that the PWM consumer expects, anything lower level belongs
in the PWM driver.

> > > > The problem with making the PWM mode the "default" pinctrl state is that
> > > > the default state will be applied before the driver is even probed. That
> > > > makes it unsuitable for this case. I think what we really want here is
> > > > explicitly "active" and "inactive" states for pinctrl where the PWM
> > > > driver controls when exactly each state is applied.
> > > 
> > > Note that this problem goes away nicely if the pwm pin is attached to
> > > the backlight. Because it's the backlight's driver that "knows" when the
> > > pwm is configured correctly and so the already existing mechanisms that
> > > setup the mux when the bl is correctly probed do the right thing at the
> > > right time.
> > 
> > Actually that's not exactly true. The default pinctrl state will be
> > applied before the driver's ->probe() implementation, so the pinctrl
> > state will be active some time before even the backlight driver gets
> > around to setting up the PWM. If you look at drivers/base/dd.c you'll
> > see that really_probe() calls pinctrl_bind_pins() before calling the
> > driver's ->probe() and will select the default state (unless there's
> > also an "init" state defined, in which case that will get applied and
> > only after successful probe will the default state be selected).
> > 
> > So if you use only a default state, then you could even get into a
> > situation where ->probe() return -EPROBE_DEFER and it would potentially
> > take several seconds before the driver is reprobed, during which time
> > the pinmux will already be set up but the PWM not configured properly
> > and potenti

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-16 Thread Uwe Kleine-König
On Fri, Nov 16, 2018 at 08:34:30AM +0100, Lothar Waßmann wrote:
> Uwe Kleine-König  wrote:
> 
> > Hello Thierry,
> > 
> > On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:
> > > On Wed, Nov 14, 2018 at 10:51:20PM +0100, Uwe Kleine-König wrote:  
> > > > On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:  
> [...]
> > > > Also dts writes don't need to lookup the needed GPIO numbers and 
> > > > pinctrl.  
> > > 
> > > Just to clarify: I don't think that we need to get the GPIO number
> > > involved in this case, because we don't have to reconfigure the pin as
> > > GPIO to make this work. The only reason that Michal's proposal did that
> > > is because that was believed to be necessary. But if the pin can just be
> > > configured with a 100k pull-up, that's enough to pull the pin high when
> > > we need it.  
> > 
> > Unless the gpio happens to be configured as output at the wrong value.
> > Further I'm not sure if the pwm in disabled state actively pulls to 0
> > and if in this state the PU of the pin is good enough to ensure a one
> > here. That would need verification first.
>
> No. You can disable the output driver via pinctrl, so that only the
> selected pull-up/down is relevant. The pin function and GPIO register
> settings don't matter at all in this case.

Is this about DSE=HI-Z in the pad control register? I didn't know about
this. I see this exists on i.MX6 but not on i.MX25. Also i.MX51 doesn't
seem to have this feature.

Best regards
Uwe

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-16 Thread Lothar Waßmann
Uwe Kleine-König  wrote:

> Hello Thierry,
> 
> On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:
> > On Wed, Nov 14, 2018 at 10:51:20PM +0100, Uwe Kleine-König wrote:  
> > > On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:  
[...]
> > > Also dts writes don't need to lookup the needed GPIO numbers and pinctrl. 
> > >  
> > 
> > Just to clarify: I don't think that we need to get the GPIO number
> > involved in this case, because we don't have to reconfigure the pin as
> > GPIO to make this work. The only reason that Michal's proposal did that
> > is because that was believed to be necessary. But if the pin can just be
> > configured with a 100k pull-up, that's enough to pull the pin high when
> > we need it.  
> 
> Unless the gpio happens to be configured as output at the wrong value.
> Further I'm not sure if the pwm in disabled state actively pulls to 0
> and if in this state the PU of the pin is good enough to ensure a one
> here. That would need verification first.
> 
No. You can disable the output driver via pinctrl, so that only the
selected pull-up/down is relevant. The pin function and GPIO register
settings don't matter at all in this case.


Lothar Waßmann
-- 
___

Ka-Ro electronics GmbH | Pascalstraße 22 | D - 52076 Aachen
Phone: +49 2408 1402-0 | Fax: +49 2408 1402-10
Geschäftsführer: Matthias Kaussen
Handelsregistereintrag: Amtsgericht Aachen, HRB 4996

www.karo-electronics.de | i...@karo-electronics.de
___


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-15 Thread Uwe Kleine-König
Hello Thierry,

On Thu, Nov 15, 2018 at 04:25:45PM +0100, Thierry Reding wrote:
> On Wed, Nov 14, 2018 at 10:51:20PM +0100, Uwe Kleine-König wrote:
> > On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:
> > > On Fri, Nov 09, 2018 at 05:55:55PM +0100, Uwe Kleine-König wrote:
> > > > On Fri, Nov 09, 2018 at 02:24:42PM +, Vokáč Michal wrote:
> > > > > On 8.11.2018 20:18, Uwe Kleine-König wrote:
> > > > > > Taking your example with the backlight device you specify an "init" 
> > > > > > and
> > > > > > a "default" pinctrl and only "default" contains the muxing for the 
> > > > > > PWM
> > > > > > pin everything should be as smooth as necessary: The pwm is only 
> > > > > > muxed
> > > > > > when the backlight device is successfully bound.
> > > > > 
> > > > > Have you tried that Uwe? The bad news is I tested that before and now
> > > > > again and it does not work like that. We already discussed that 
> > > > > earlier.
> > > > 
> > > > The key is that the pinmux setting for the PWM pin should be part of the
> > > > bl pinctrl, not the pwm pinctrl. Then "default" is only setup when the
> > > > bl device is successfully bound which is after the bl's .probe callback
> > > > called pwm_apply().
> > > 
> > > No, that's not at all correct. Pinmux settings should reside with the
> > > consumer of the pin. In this case, the PWM is the consumer of the pin,
> > > whereas the backlight is the consumer of the *PWM*.
> > 
> > This is news to me. Adding Linus W. to Cc, maybe he can comment?!
> > 
> > Grepping through the arm device trees it really seems common to put the
> > pinctrl for the pwm pin into the pwm device. I didn't search in depth,
> > but I didn't find a counter example.
> > 
> > For GPIOs it is common that the pinmuxing is included in the GPIO's
> > consumer pinctrl. Ditto for mdio busses whose pinctrl is included in the
> > ethernet pinctrl.
> 
> GPIO is different from PWM in that the GPIO is already the pin itself
> and is otherwise generic. So typically you put the pinmuxing options
> into the device tree node for the consumer of the GPIO, because it is
> only when the consumer uses the GPIO that you need to configure that
> pin as GPIO.
> 
> For PWM, however, the PWM consumer is only the consumer of the PWM, but
> the PWM device itself is the real consumer of the pin that outputs the
> PWM signal. So the PWM determines when the pinmux states need to be
> applied, whereas the consumer of the PWM only deals with the PWM.
> 
> For MDIO busses, I think they are usually part of, and driven by, the
> ethernet controller, so again it makes sense to put the pinmux into the
> node of the ethernet controller, because the ethernet controller is the
> user of the pins.

Maybe it was a bad idea to broaden the discussion to talk about gpios
and ethernet stuff here. I'd still consider it a valid construct to put
the pwm pin into the backlight's pinctrl unless Linux W. disagrees.

> > > The problem with making the PWM mode the "default" pinctrl state is that
> > > the default state will be applied before the driver is even probed. That
> > > makes it unsuitable for this case. I think what we really want here is
> > > explicitly "active" and "inactive" states for pinctrl where the PWM
> > > driver controls when exactly each state is applied.
> > 
> > Note that this problem goes away nicely if the pwm pin is attached to
> > the backlight. Because it's the backlight's driver that "knows" when the
> > pwm is configured correctly and so the already existing mechanisms that
> > setup the mux when the bl is correctly probed do the right thing at the
> > right time.
> 
> Actually that's not exactly true. The default pinctrl state will be
> applied before the driver's ->probe() implementation, so the pinctrl
> state will be active some time before even the backlight driver gets
> around to setting up the PWM. If you look at drivers/base/dd.c you'll
> see that really_probe() calls pinctrl_bind_pins() before calling the
> driver's ->probe() and will select the default state (unless there's
> also an "init" state defined, in which case that will get applied and
> only after successful probe will the default state be selected).
> 
> So if you use only a default state, then you could even get into a
> situation where ->probe() return -EPROBE_DEFER and it would potentially
> take several seconds before the driver is reprobed, during which time
> the pinmux will already be set up but the PWM not configured properly
> and potentially outputting the wrong level.

If you reread my suggestion to Michal completely you will notice I got
that right.

> > > This solves the problem quite nicely because by default the pinctrl
> > > state isn't touched. For the case where the bootloader didn't initialize
> > > the PWM pin at all, the driver core won't do anything and keep it at the
> > > 100k pull-up default.
> > 
> > Ditto if the pwm pinctrl is attached to the consumer without having to
> > introduce new pwm-specific stuff.
> 
>

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-15 Thread Thierry Reding
On Wed, Nov 14, 2018 at 10:51:20PM +0100, Uwe Kleine-König wrote:
> Hello Thierry,
> 
> On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:
> > On Fri, Nov 09, 2018 at 05:55:55PM +0100, Uwe Kleine-König wrote:
> > > On Fri, Nov 09, 2018 at 02:24:42PM +, Vokáč Michal wrote:
> > > > On 8.11.2018 20:18, Uwe Kleine-König wrote:
> > > > > Taking your example with the backlight device you specify an "init" 
> > > > > and
> > > > > a "default" pinctrl and only "default" contains the muxing for the PWM
> > > > > pin everything should be as smooth as necessary: The pwm is only muxed
> > > > > when the backlight device is successfully bound.
> > > > 
> > > > Have you tried that Uwe? The bad news is I tested that before and now
> > > > again and it does not work like that. We already discussed that earlier.
> > > 
> > > The key is that the pinmux setting for the PWM pin should be part of the
> > > bl pinctrl, not the pwm pinctrl. Then "default" is only setup when the
> > > bl device is successfully bound which is after the bl's .probe callback
> > > called pwm_apply().
> > 
> > No, that's not at all correct. Pinmux settings should reside with the
> > consumer of the pin. In this case, the PWM is the consumer of the pin,
> > whereas the backlight is the consumer of the *PWM*.
> 
> This is news to me. Adding Linus W. to Cc, maybe he can comment?!
> 
> Grepping through the arm device trees it really seems common to put the
> pinctrl for the pwm pin into the pwm device. I didn't search in depth,
> but I didn't find a counter example.
> 
> For GPIOs it is common that the pinmuxing is included in the GPIO's
> consumer pinctrl. Ditto for mdio busses whose pinctrl is included in the
> ethernet pinctrl.

GPIO is different from PWM in that the GPIO is already the pin itself
and is otherwise generic. So typically you put the pinmuxing options
into the device tree node for the consumer of the GPIO, because it is
only when the consumer uses the GPIO that you need to configure that
pin as GPIO.

For PWM, however, the PWM consumer is only the consumer of the PWM, but
the PWM device itself is the real consumer of the pin that outputs the
PWM signal. So the PWM determines when the pinmux states need to be
applied, whereas the consumer of the PWM only deals with the PWM.

For MDIO busses, I think they are usually part of, and driven by, the
ethernet controller, so again it makes sense to put the pinmux into the
node of the ethernet controller, because the ethernet controller is the
user of the pins.

> > The problem with making the PWM mode the "default" pinctrl state is that
> > the default state will be applied before the driver is even probed. That
> > makes it unsuitable for this case. I think what we really want here is
> > explicitly "active" and "inactive" states for pinctrl where the PWM
> > driver controls when exactly each state is applied.
> 
> Note that this problem goes away nicely if the pwm pin is attached to
> the backlight. Because it's the backlight's driver that "knows" when the
> pwm is configured correctly and so the already existing mechanisms that
> setup the mux when the bl is correctly probed do the right thing at the
> right time.

Actually that's not exactly true. The default pinctrl state will be
applied before the driver's ->probe() implementation, so the pinctrl
state will be active some time before even the backlight driver gets
around to setting up the PWM. If you look at drivers/base/dd.c you'll
see that really_probe() calls pinctrl_bind_pins() before calling the
driver's ->probe() and will select the default state (unless there's
also an "init" state defined, in which case that will get applied and
only after successful probe will the default state be selected).

So if you use only a default state, then you could even get into a
situation where ->probe() return -EPROBE_DEFER and it would potentially
take several seconds before the driver is reprobed, during which time
the pinmux will already be set up but the PWM not configured properly
and potentially outputting the wrong level.

> > This solves the problem quite nicely because by default the pinctrl
> > state isn't touched. For the case where the bootloader didn't initialize
> > the PWM pin at all, the driver core won't do anything and keep it at the
> > 100k pull-up default.
> 
> Ditto if the pwm pinctrl is attached to the consumer without having to
> introduce new pwm-specific stuff.

Well yes, but you'd obviously also have to avoid using the "default"
state, otherwise you'd run into the issues that I described above.

> > > > > No I meant the pwm. Well, it's as easy as that: Whenever with your
> > > > > approach you configure the pin as GPIO with the output set to low,
> > > > > instead configure the pwm with duty_cycle to zero (or disable it).
> > > > > Whenever with your approach you configure the pin as GPIO with the
> > > > > output set to high, configure the pwm with duty_cycle to 100%. 
> > > > > (Keeping
> > > > > out inver

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-14 Thread Uwe Kleine-König
Hello Thierry,

On Wed, Nov 14, 2018 at 12:34:49PM +0100, Thierry Reding wrote:
> On Fri, Nov 09, 2018 at 05:55:55PM +0100, Uwe Kleine-König wrote:
> > On Fri, Nov 09, 2018 at 02:24:42PM +, Vokáč Michal wrote:
> > > On 8.11.2018 20:18, Uwe Kleine-König wrote:
> > > > Taking your example with the backlight device you specify an "init" and
> > > > a "default" pinctrl and only "default" contains the muxing for the PWM
> > > > pin everything should be as smooth as necessary: The pwm is only muxed
> > > > when the backlight device is successfully bound.
> > > 
> > > Have you tried that Uwe? The bad news is I tested that before and now
> > > again and it does not work like that. We already discussed that earlier.
> > 
> > The key is that the pinmux setting for the PWM pin should be part of the
> > bl pinctrl, not the pwm pinctrl. Then "default" is only setup when the
> > bl device is successfully bound which is after the bl's .probe callback
> > called pwm_apply().
> 
> No, that's not at all correct. Pinmux settings should reside with the
> consumer of the pin. In this case, the PWM is the consumer of the pin,
> whereas the backlight is the consumer of the *PWM*.

This is news to me. Adding Linus W. to Cc, maybe he can comment?!

Grepping through the arm device trees it really seems common to put the
pinctrl for the pwm pin into the pwm device. I didn't search in depth,
but I didn't find a counter example.

For GPIOs it is common that the pinmuxing is included in the GPIO's
consumer pinctrl. Ditto for mdio busses whose pinctrl is included in the
ethernet pinctrl.

> The problem with making the PWM mode the "default" pinctrl state is that
> the default state will be applied before the driver is even probed. That
> makes it unsuitable for this case. I think what we really want here is
> explicitly "active" and "inactive" states for pinctrl where the PWM
> driver controls when exactly each state is applied.

Note that this problem goes away nicely if the pwm pin is attached to
the backlight. Because it's the backlight's driver that "knows" when the
pwm is configured correctly and so the already existing mechanisms that
setup the mux when the bl is correctly probed do the right thing at the
right time.

> This solves the problem quite nicely because by default the pinctrl
> state isn't touched. For the case where the bootloader didn't initialize
> the PWM pin at all, the driver core won't do anything and keep it at the
> 100k pull-up default.

Ditto if the pwm pinctrl is attached to the consumer without having to
introduce new pwm-specific stuff.

> Only when the PWM is actually taken into active use will the "active"
> state (i.e. PWM mode) be applied, so that the PWM can drive it with
> the right configuration to give the desired result. If the bootloader
> did initialize the PWM it would also work because then at PWM driver
> probe time we don't do anything either, keeping the pin in the PWM
> mode as configured by the bootloader and not touching the PWM
> configuration either.

Ditto if the pwm pinctrl is attached to the consumer without having to
introduce new pwm-specific stuff.

> If hardware readout is implemented, we can load
> the exact state that we're in and seamlessly take over in the kernel
> driver. Enabling the PWM in this case would apply the current setting,
> which should be a no-op, or at least idempotent.

Ditto if the pwm pinctrl is attached to the consumer without having to
introduce new pwm-specific stuff.

> > > > No I meant the pwm. Well, it's as easy as that: Whenever with your
> > > > approach you configure the pin as GPIO with the output set to low,
> > > > instead configure the pwm with duty_cycle to zero (or disable it).
> > > > Whenever with your approach you configure the pin as GPIO with the
> > > > output set to high, configure the pwm with duty_cycle to 100%. (Keeping
> > > > out inverted PWMs for the ease of discussion, but the procedure can be
> > > > adapted accordingly.) The only difference then is that with your
> > > > approach you already "know" in pwm-imx's .probe the idle level and can
> > > > configure the GPIO accordingly. With my approach you just have to wait
> > > > until the first pwm_apply which (as described above) works just as well.
> > > 
> > > While here I am quite confident you are talking about kernel code, right?
> > > If yes, then your approach is clear to me.
> > > 
> > > The problem is I am quite sure your approach does not solve the cases
> > > the pinctrl solution does. And according to my tests so far it does not
> > > work at all because the "init" and "default" states does not work as you
> > > are saying.
> > 
> > That's as pointed out above, because you're looking at the pwm's pinctrl
> > and I at the pwm-consumer's pinctrl.
> > 
> > Note that a sysfs consumer cannot be operated smoothly here, because
> > there is no pinctrl node to add the PWM mode to that only gets active
> > after the first configuration. This however is something that shou

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-14 Thread Thierry Reding
On Fri, Nov 09, 2018 at 05:55:55PM +0100, Uwe Kleine-König wrote:
> On Fri, Nov 09, 2018 at 02:24:42PM +, Vokáč Michal wrote:
> > On 8.11.2018 20:18, Uwe Kleine-König wrote:
> > > On Thu, Nov 08, 2018 at 03:21:44PM +, Vokáč Michal wrote:
> > >> Hi Uwe,
> > >>
> > >> On 7.11.2018 16:01, Uwe Kleine-König wrote:
> >  Interesting idea. I just wonder why nobody else did not come up with 
> >  such
> >  a simple solution before.
> > >>>
> > >>> I think I mentioned it already in this thread, but it went unnoticed :-)
> > >>
> > >> I meant it like "How happened this was not invented years ago, when 
> > >> people
> > >> first noticed the issue with using inverted PWM for backlight on i.MX6."
> > >> In our project, this issue dates back to 2015 :(
> > >>
> > >>> Then the patch isn't correct yet. The idea is always keep the hardware
> > >>> running and only disable it if it's uninverted.
> > >>
> > >> OK, I got the point.
> > >>
> > >>> In imx_pwm_probe it's not yet known what the polarity is supposed to be,
> > >>> right?
> > >>
> > >> Not really. It can already be known but currently there is no way how to
> > >> pass the information to the probe function. I, as a creator of the device
> > >> (and author of a DTS file) know that the circuit needs inverted PWM 
> > >> signal.
> > >> And I know that the circuit needs to be disabled until I say it can be
> > >> enabled. How I say that can warry. It may be default brightness level > 0
> > >> in DTS file or from a userspace program using PWM sysfs interface.
> > >>
> > >>>   So the right thing to do there is to not touch the configuration
> > >>> of the pwm. I think all states that are problematic then are also
> > >>> problematic with the gpio/pinmux approach.
> > >>
> > >> I think my use-case I already presented before is an example where
> > >> involving pinctrl solves the problem while the "leave PWM enabled
> > >> for inverted users" does not. That is all the time between
> > >> imx_pwm_probe() and imx_pwm_apply_v2().
> > > 
> > > You're doing in probe:
> > > 
> > >if (pwm_is_running()):
> > >  mux(pin, function=pwm)
> > >else:
> > >  gpio_set_value(gpio, 0)
> > >  mux(pin, function=gpio)
> > > 
> > > This gives you the right level assuming the gpio specification uses the
> > > right flag (GPIO_ACTIVE_HIGH or GPIO_ACTIVE_LOW).
> > 
> > Agree.
> > 
> > > Taking your example with the backlight device you specify an "init" and
> > > a "default" pinctrl and only "default" contains the muxing for the PWM
> > > pin everything should be as smooth as necessary: The pwm is only muxed
> > > when the backlight device is successfully bound.
> > 
> > Have you tried that Uwe? The bad news is I tested that before and now
> > again and it does not work like that. We already discussed that earlier.
> 
> The key is that the pinmux setting for the PWM pin should be part of the
> bl pinctrl, not the pwm pinctrl. Then "default" is only setup when the
> bl device is successfully bound which is after the bl's .probe callback
> called pwm_apply().

No, that's not at all correct. Pinmux settings should reside with the
consumer of the pin. In this case, the PWM is the consumer of the pin,
whereas the backlight is the consumer of the *PWM*.

The problem with making the PWM mode the "default" pinctrl state is that
the default state will be applied before the driver is even probed. That
makes it unsuitable for this case. I think what we really want here is
explicitly "active" and "inactive" states for pinctrl where the PWM
driver controls when exactly each state is applied.

This solves the problem quite nicely because by default the pinctrl
state isn't touched. For the case where the bootloader didn't initialize
the PWM pin at all, the driver core won't do anything and keep it at the
100k pull-up default. Only when the PWM is actually taken into active
use will the "active" state (i.e. PWM mode) be applied, so that the PWM
can drive it with the right configuration to give the desired result. If
the bootloader did initialize the PWM it would also work because then at
PWM driver probe time we don't do anything either, keeping the pin in
the PWM mode as configured by the bootloader and not touching the PWM
configuration either. If hardware readout is implemented, we can load
the exact state that we're in and seamlessly take over in the kernel
driver. Enabling the PWM in this case would apply the current setting,
which should be a no-op, or at least idempotent.

> > > No I meant the pwm. Well, it's as easy as that: Whenever with your
> > > approach you configure the pin as GPIO with the output set to low,
> > > instead configure the pwm with duty_cycle to zero (or disable it).
> > > Whenever with your approach you configure the pin as GPIO with the
> > > output set to high, configure the pwm with duty_cycle to 100%. (Keeping
> > > out inverted PWMs for the ease of discussion, but the procedure can be
> > > adapted accordingly.) The only difference then is 

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-14 Thread Thierry Reding
On Thu, Nov 08, 2018 at 03:21:44PM +, Vokáč Michal wrote:
> On 7.11.2018 16:01, Uwe Kleine-König wrote:
[...]
> > For both the solution is to let the bootloader enable the pwm with
> > the right output level. Am I missing something?
> 
> Bootloader is only a small part of the whole solution I think. And I
> suppose you meant: "enable the *GPIO* with the right output level".
> 
>   - Even if you use GPIO in bootloader to set the required level the
> time frame from imx_pwm_probe to imx_pwm_apply is not covered.
> 
>   - Currently there is no support in Linux pwm-imx driver to detect
> the PWM chip is already enabled at probe time. I actually send
> patches for this a month ago [1]. No response yet.
> 
>   - Inverted PWM does not work in U-Boot (on imx at least). And it
> does not seam like it can be fixed easily. I do not know what is
> the situation in other bootloaders.
> 
> So my current bootloader solution is one of:
>   - Set the pin to the appropriate (HIGH) level using GPIO.
>   - Do not touch the pin at all, it has 100k pull-up by default.

First of all, I don't think we should rely on any bootloader setting up
things properly. I already said elsewhere that the reset defaults will
likely already be such that the PWM outputs zero power (i.e. high for
inversed PWM). Michal's comments above seem to suggest that this is
indeed the case. If the pin is 100k pull-up by default (I assume that
means on reset), then that's exactly what I would expect for this kind
of pin.

And that's also an excellent pin state for the kernel to use when it no
longer needs to actively drive the PWM. That means on pwm_disable() we
can simply revert to the 100k pull-up default and let the PWM pin be in
the very same state that it is on reset. Can't get any better than that.

So really, I think the only proper solution to this problem is to get
pinctrl involved and configure the pin as PWM when it is actively used,
and change it back to 100k pull-up otherwise.

And, yes, I understand that this slightly complicates the driver, but
it's really the right thing to do and it fixes all known issues, and to
me that's clear evidence that it is the right solution and therefore
definitely worth the added complexity.

Thierry


signature.asc
Description: PGP signature


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-14 Thread Uwe Kleine-König
Hello Michal,

On Fri, Nov 09, 2018 at 05:55:55PM +0100, Uwe Kleine-König wrote:
> On Fri, Nov 09, 2018 at 02:24:42PM +, Vokáč Michal wrote:
> > On 8.11.2018 20:18, Uwe Kleine-König wrote:
> > > On Thu, Nov 08, 2018 at 03:21:44PM +, Vokáč Michal wrote:
> > >> Hi Uwe,
> > >>
> > >> On 7.11.2018 16:01, Uwe Kleine-König wrote:
> >  Interesting idea. I just wonder why nobody else did not come up with 
> >  such
> >  a simple solution before.
> > >>>
> > >>> I think I mentioned it already in this thread, but it went unnoticed :-)
> > >>
> > >> I meant it like "How happened this was not invented years ago, when 
> > >> people
> > >> first noticed the issue with using inverted PWM for backlight on i.MX6."
> > >> In our project, this issue dates back to 2015 :(
> > >>
> > >>> Then the patch isn't correct yet. The idea is always keep the hardware
> > >>> running and only disable it if it's uninverted.
> > >>
> > >> OK, I got the point.
> > >>
> > >>> In imx_pwm_probe it's not yet known what the polarity is supposed to be,
> > >>> right?
> > >>
> > >> Not really. It can already be known but currently there is no way how to
> > >> pass the information to the probe function. I, as a creator of the device
> > >> (and author of a DTS file) know that the circuit needs inverted PWM 
> > >> signal.
> > >> And I know that the circuit needs to be disabled until I say it can be
> > >> enabled. How I say that can warry. It may be default brightness level > 0
> > >> in DTS file or from a userspace program using PWM sysfs interface.
> > >>
> > >>>   So the right thing to do there is to not touch the configuration
> > >>> of the pwm. I think all states that are problematic then are also
> > >>> problematic with the gpio/pinmux approach.
> > >>
> > >> I think my use-case I already presented before is an example where
> > >> involving pinctrl solves the problem while the "leave PWM enabled
> > >> for inverted users" does not. That is all the time between
> > >> imx_pwm_probe() and imx_pwm_apply_v2().
> > > 
> > > You're doing in probe:
> > > 
> > >if (pwm_is_running()):
> > >  mux(pin, function=pwm)
> > >else:
> > >  gpio_set_value(gpio, 0)
> > >  mux(pin, function=gpio)
> > > 
> > > This gives you the right level assuming the gpio specification uses the
> > > right flag (GPIO_ACTIVE_HIGH or GPIO_ACTIVE_LOW).
> > 
> > Agree.
> > 
> > > Taking your example with the backlight device you specify an "init" and
> > > a "default" pinctrl and only "default" contains the muxing for the PWM
> > > pin everything should be as smooth as necessary: The pwm is only muxed
> > > when the backlight device is successfully bound.
> > 
> > Have you tried that Uwe? The bad news is I tested that before and now
> > again and it does not work like that. We already discussed that earlier.
> 
> The key is that the pinmux setting for the PWM pin should be part of the
> bl pinctrl, not the pwm pinctrl. Then "default" is only setup when the
> bl device is successfully bound which is after the bl's .probe callback
> called pwm_apply().

Did my mail clear up my suggestion? Do you agree it should work like
this (or even did you test that, as I did not)?

Best regards
Uwe

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-09 Thread Uwe Kleine-König
On Fri, Nov 09, 2018 at 02:24:42PM +, Vokáč Michal wrote:
> On 8.11.2018 20:18, Uwe Kleine-König wrote:
> > On Thu, Nov 08, 2018 at 03:21:44PM +, Vokáč Michal wrote:
> >> Hi Uwe,
> >>
> >> On 7.11.2018 16:01, Uwe Kleine-König wrote:
>  Interesting idea. I just wonder why nobody else did not come up with such
>  a simple solution before.
> >>>
> >>> I think I mentioned it already in this thread, but it went unnoticed :-)
> >>
> >> I meant it like "How happened this was not invented years ago, when people
> >> first noticed the issue with using inverted PWM for backlight on i.MX6."
> >> In our project, this issue dates back to 2015 :(
> >>
> >>> Then the patch isn't correct yet. The idea is always keep the hardware
> >>> running and only disable it if it's uninverted.
> >>
> >> OK, I got the point.
> >>
> >>> In imx_pwm_probe it's not yet known what the polarity is supposed to be,
> >>> right?
> >>
> >> Not really. It can already be known but currently there is no way how to
> >> pass the information to the probe function. I, as a creator of the device
> >> (and author of a DTS file) know that the circuit needs inverted PWM signal.
> >> And I know that the circuit needs to be disabled until I say it can be
> >> enabled. How I say that can warry. It may be default brightness level > 0
> >> in DTS file or from a userspace program using PWM sysfs interface.
> >>
> >>>   So the right thing to do there is to not touch the configuration
> >>> of the pwm. I think all states that are problematic then are also
> >>> problematic with the gpio/pinmux approach.
> >>
> >> I think my use-case I already presented before is an example where
> >> involving pinctrl solves the problem while the "leave PWM enabled
> >> for inverted users" does not. That is all the time between
> >> imx_pwm_probe() and imx_pwm_apply_v2().
> > 
> > You're doing in probe:
> > 
> >if (pwm_is_running()):
> >  mux(pin, function=pwm)
> >else:
> >  gpio_set_value(gpio, 0)
> >  mux(pin, function=gpio)
> > 
> > This gives you the right level assuming the gpio specification uses the
> > right flag (GPIO_ACTIVE_HIGH or GPIO_ACTIVE_LOW).
> 
> Agree.
> 
> > Taking your example with the backlight device you specify an "init" and
> > a "default" pinctrl and only "default" contains the muxing for the PWM
> > pin everything should be as smooth as necessary: The pwm is only muxed
> > when the backlight device is successfully bound.
> 
> Have you tried that Uwe? The bad news is I tested that before and now
> again and it does not work like that. We already discussed that earlier.

The key is that the pinmux setting for the PWM pin should be part of the
bl pinctrl, not the pwm pinctrl. Then "default" is only setup when the
bl device is successfully bound which is after the bl's .probe callback
called pwm_apply().

> The "default" state is selected by the pinctrl core driver when imx-pwm's
> driver probe is finished. And it does not matter if the imx-pwm driver
> has some in-kernel users or not. See:
> 
> https://elixir.bootlin.com/linux/v4.20-rc1/source/drivers/pinctrl/core.c#L1497
> 
> It is possible that I made some mistake. So this is what I have in DT:
> 
>   pinctrl_pwm: pwm1grp {
>   // PWM out
>   fsl,pins = ;
>   }:
> 
>   pinctrl_pwm_gpio: pwm1gpiogrp {
>   // GPIO, 100k pull-up
>   fsl,pins = ;
>   };
> 
>   &pwm1 {
>   #pwm-cells = <3>;
>   pinctrl-names = "init", "default";
>   pinctrl-0 = <&pinctrl_pwm_gpio>;
>   pinctrl-1 = <&pinctrl_pwm>;
>   status = "okay";
>   };

With this dt snippet the effect is as you describe. But that's not what
I meant. (Also "init" should/could be empty.)

> > The probe function (of
> > the backlight) should have initialized the PWM with the right polarity.
> 
> Agree.
> 
> > Until then nothing happens on the pin (either because it's not muxed as
> > PWM or because the PWM is already initialized by the bootloader).
> 
> I do not agree. My tests repeatedly show it does not work like that.

See above.

> > The only thing that is (maybe) needed on top of my change is that the
> > pwm isn't stopped in pwm-imx's .probe.
> > 
>  In probe you do not have any users yet. So you do not know the requested
>  output polarity. With "default" pinctrl the PWM output would be muxed to
>  the selected pin and since the PWM chip is most probably disabled
>  (unless you enabled it in bootloader) you would get low level on the pin.
>  That means your backlight is fully enabled until the first call to
>  imx_pwm_apply_v2(). On my system this is 2 seconds.
> >>>
> >>> With the gpio/pinmux approach you don't know the intended polarity
> >>> either and maybe enable the display, too.
> >>
> >> You know it because the pinctrl solution is optional. So if you use it,
> >> you use it on purpose to override the default PWM output level in PWM
> >> disabled state. 

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-09 Thread Vokáč Michal
On 8.11.2018 20:18, Uwe Kleine-König wrote:
> On Thu, Nov 08, 2018 at 03:21:44PM +, Vokáč Michal wrote:
>> Hi Uwe,
>>
>> On 7.11.2018 16:01, Uwe Kleine-König wrote:
 Interesting idea. I just wonder why nobody else did not come up with such
 a simple solution before.
>>>
>>> I think I mentioned it already in this thread, but it went unnoticed :-)
>>
>> I meant it like "How happened this was not invented years ago, when people
>> first noticed the issue with using inverted PWM for backlight on i.MX6."
>> In our project, this issue dates back to 2015 :(
>>
>>> Then the patch isn't correct yet. The idea is always keep the hardware
>>> running and only disable it if it's uninverted.
>>
>> OK, I got the point.
>>
>>> In imx_pwm_probe it's not yet known what the polarity is supposed to be,
>>> right?
>>
>> Not really. It can already be known but currently there is no way how to
>> pass the information to the probe function. I, as a creator of the device
>> (and author of a DTS file) know that the circuit needs inverted PWM signal.
>> And I know that the circuit needs to be disabled until I say it can be
>> enabled. How I say that can warry. It may be default brightness level > 0
>> in DTS file or from a userspace program using PWM sysfs interface.
>>
>>>   So the right thing to do there is to not touch the configuration
>>> of the pwm. I think all states that are problematic then are also
>>> problematic with the gpio/pinmux approach.
>>
>> I think my use-case I already presented before is an example where
>> involving pinctrl solves the problem while the "leave PWM enabled
>> for inverted users" does not. That is all the time between
>> imx_pwm_probe() and imx_pwm_apply_v2().
> 
> You're doing in probe:
> 
>if (pwm_is_running()):
>  mux(pin, function=pwm)
>else:
>  gpio_set_value(gpio, 0)
>  mux(pin, function=gpio)
> 
> This gives you the right level assuming the gpio specification uses the
> right flag (GPIO_ACTIVE_HIGH or GPIO_ACTIVE_LOW).

Agree.

> Taking your example with the backlight device you specify an "init" and
> a "default" pinctrl and only "default" contains the muxing for the PWM
> pin everything should be as smooth as necessary: The pwm is only muxed
> when the backlight device is successfully bound.

Have you tried that Uwe? The bad news is I tested that before and now
again and it does not work like that. We already discussed that earlier.
The "default" state is selected by the pinctrl core driver when imx-pwm's
driver probe is finished. And it does not matter if the imx-pwm driver
has some in-kernel users or not. See:

https://elixir.bootlin.com/linux/v4.20-rc1/source/drivers/pinctrl/core.c#L1497

It is possible that I made some mistake. So this is what I have in DT:

pinctrl_pwm: pwm1grp {
// PWM out
fsl,pins = ;
}:

pinctrl_pwm_gpio: pwm1gpiogrp {
// GPIO, 100k pull-up
fsl,pins = ;
};

&pwm1 {
#pwm-cells = <3>;
pinctrl-names = "init", "default";
pinctrl-0 = <&pinctrl_pwm_gpio>;
pinctrl-1 = <&pinctrl_pwm>;
status = "okay";
};

The PWM output pin is not connected anywhere. It is flying in the breeze.
I do not touch the pin configuration from bootloader. So the default is
input, 100k pull-up. There is no user of the pwm1 anywhere in my DT.
If what you say is correct I should see a straight HIGH level line on
a scope from power-up to userspace. Unfortunately I do not. The output
goes low as soon as the imx-pwm probe is finished.

> The probe function (of
> the backlight) should have initialized the PWM with the right polarity.

Agree.

> Until then nothing happens on the pin (either because it's not muxed as
> PWM or because the PWM is already initialized by the bootloader).

I do not agree. My tests repeatedly show it does not work like that.

> The only thing that is (maybe) needed on top of my change is that the
> pwm isn't stopped in pwm-imx's .probe.
> 
 In probe you do not have any users yet. So you do not know the requested
 output polarity. With "default" pinctrl the PWM output would be muxed to
 the selected pin and since the PWM chip is most probably disabled
 (unless you enabled it in bootloader) you would get low level on the pin.
 That means your backlight is fully enabled until the first call to
 imx_pwm_apply_v2(). On my system this is 2 seconds.
>>>
>>> With the gpio/pinmux approach you don't know the intended polarity
>>> either and maybe enable the display, too.
>>
>> You know it because the pinctrl solution is optional. So if you use it,
>> you use it on purpose to override the default PWM output level in PWM
>> disabled state. It is very useful in cases where you need inverted and
>> disabled PWM signal from power-up to userspace. Or until some kernel
>> driver (backlight, led, fan..) enables it. For this it is the only
>> solution 

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-08 Thread Uwe Kleine-König
On Thu, Nov 08, 2018 at 03:21:44PM +, Vokáč Michal wrote:
> Hi Uwe,
> 
> On 7.11.2018 16:01, Uwe Kleine-König wrote:
> >> Interesting idea. I just wonder why nobody else did not come up with such
> >> a simple solution before.
> > 
> > I think I mentioned it already in this thread, but it went unnoticed :-)
> 
> I meant it like "How happened this was not invented years ago, when people
> first noticed the issue with using inverted PWM for backlight on i.MX6."
> In our project, this issue dates back to 2015 :(
> 
> > Then the patch isn't correct yet. The idea is always keep the hardware
> > running and only disable it if it's uninverted.
> 
> OK, I got the point.
> 
> > In imx_pwm_probe it's not yet known what the polarity is supposed to be,
> > right?
> 
> Not really. It can already be known but currently there is no way how to
> pass the information to the probe function. I, as a creator of the device
> (and author of a DTS file) know that the circuit needs inverted PWM signal.
> And I know that the circuit needs to be disabled until I say it can be
> enabled. How I say that can warry. It may be default brightness level > 0
> in DTS file or from a userspace program using PWM sysfs interface.
> 
> > So the right thing to do there is to not touch the configuration
> > of the pwm. I think all states that are problematic then are also
> > problematic with the gpio/pinmux approach.
> 
> I think my use-case I already presented before is an example where
> involving pinctrl solves the problem while the "leave PWM enabled
> for inverted users" does not. That is all the time between
> imx_pwm_probe() and imx_pwm_apply_v2().

You're doing in probe:

  if (pwm_is_running()):
mux(pin, function=pwm)
  else:
gpio_set_value(gpio, 0)
mux(pin, function=gpio)

This gives you the right level assuming the gpio specification uses the
right flag (GPIO_ACTIVE_HIGH or GPIO_ACTIVE_LOW).

Taking your example with the backlight device you specify an "init" and
a "default" pinctrl and only "default" contains the muxing for the PWM
pin everything should be as smooth as necessary: The pwm is only muxed
when the backlight device is successfully bound. The probe function (of
the backlight) should have initialized the PWM with the right polarity.
Until then nothing happens on the pin (either because it's not muxed as
PWM or because the PWM is already initialized by the bootloader).

The only thing that is (maybe) needed on top of my change is that the
pwm isn't stopped in pwm-imx's .probe.

> >> In probe you do not have any users yet. So you do not know the requested
> >> output polarity. With "default" pinctrl the PWM output would be muxed to
> >> the selected pin and since the PWM chip is most probably disabled
> >> (unless you enabled it in bootloader) you would get low level on the pin.
> >> That means your backlight is fully enabled until the first call to
> >> imx_pwm_apply_v2(). On my system this is 2 seconds.
> > 
> > With the gpio/pinmux approach you don't know the intended polarity
> > either and maybe enable the display, too.
> 
> You know it because the pinctrl solution is optional. So if you use it,
> you use it on purpose to override the default PWM output level in PWM
> disabled state. It is very useful in cases where you need inverted and
> disabled PWM signal from power-up to userspace. Or until some kernel
> driver (backlight, led, fan..) enables it. For this it is the only
> solution I think.
> 
> It allows you to boot with disabled PWM that has normal polarity set
> by default. Later on from your userspace program you configure the PWM
> to desired period/duty, set PWM output to inversed and enable it.
> Until this point the circuit is disabled with my solution.
> 
> > For both the solution is to let the bootloader enable the pwm with
> > the right output level. Am I missing something?
> 
> Bootloader is only a small part of the whole solution I think. And I
> suppose you meant: "enable the *GPIO* with the right output level".

No I meant the pwm. Well, it's as easy as that: Whenever with your
approach you configure the pin as GPIO with the output set to low,
instead configure the pwm with duty_cycle to zero (or disable it).
Whenever with your approach you configure the pin as GPIO with the
output set to high, configure the pwm with duty_cycle to 100%. (Keeping
out inverted PWMs for the ease of discussion, but the procedure can be
adapted accordingly.) The only difference then is that with your
approach you already "know" in pwm-imx's .probe the idle level and can
configure the GPIO accordingly. With my approach you just have to wait
until the first pwm_apply which (as described above) works just as well.
 
>   - Even if you use GPIO in bootloader to set the required level the
> time frame from imx_pwm_probe to imx_pwm_apply is not covered.
> 
>   - Currently there is no support in Linux pwm-imx driver to detect
> the PWM chip is already enabled at probe time. I actually send
> patches for 

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-08 Thread Vokáč Michal
Hi Uwe,

On 7.11.2018 16:01, Uwe Kleine-König wrote:
>> Interesting idea. I just wonder why nobody else did not come up with such
>> a simple solution before.
> 
> I think I mentioned it already in this thread, but it went unnoticed :-)

I meant it like "How happened this was not invented years ago, when people
first noticed the issue with using inverted PWM for backlight on i.MX6."
In our project, this issue dates back to 2015 :(

> Then the patch isn't correct yet. The idea is always keep the hardware
> running and only disable it if it's uninverted.

OK, I got the point.

> In imx_pwm_probe it's not yet known what the polarity is supposed to be,
> right?

Not really. It can already be known but currently there is no way how to
pass the information to the probe function. I, as a creator of the device
(and author of a DTS file) know that the circuit needs inverted PWM signal.
And I know that the circuit needs to be disabled until I say it can be
enabled. How I say that can warry. It may be default brightness level > 0
in DTS file or from a userspace program using PWM sysfs interface.

> So the right thing to do there is to not touch the configuration
> of the pwm. I think all states that are problematic then are also
> problematic with the gpio/pinmux approach.

I think my use-case I already presented before is an example where
involving pinctrl solves the problem while the "leave PWM enabled
for inverted users" does not. That is all the time between
imx_pwm_probe() and imx_pwm_apply_v2().
  
>> In probe you do not have any users yet. So you do not know the requested
>> output polarity. With "default" pinctrl the PWM output would be muxed to
>> the selected pin and since the PWM chip is most probably disabled
>> (unless you enabled it in bootloader) you would get low level on the pin.
>> That means your backlight is fully enabled until the first call to
>> imx_pwm_apply_v2(). On my system this is 2 seconds.
> 
> With the gpio/pinmux approach you don't know the intended polarity
> either and maybe enable the display, too.

You know it because the pinctrl solution is optional. So if you use it,
you use it on purpose to override the default PWM output level in PWM
disabled state. It is very useful in cases where you need inverted and
disabled PWM signal from power-up to userspace. Or until some kernel
driver (backlight, led, fan..) enables it. For this it is the only
solution I think.

It allows you to boot with disabled PWM that has normal polarity set
by default. Later on from your userspace program you configure the PWM
to desired period/duty, set PWM output to inversed and enable it.
Until this point the circuit is disabled with my solution.

> For both the solution is to let the bootloader enable the pwm with
> the right output level. Am I missing something?

Bootloader is only a small part of the whole solution I think. And I
suppose you meant: "enable the *GPIO* with the right output level".

  - Even if you use GPIO in bootloader to set the required level the
time frame from imx_pwm_probe to imx_pwm_apply is not covered.

  - Currently there is no support in Linux pwm-imx driver to detect
the PWM chip is already enabled at probe time. I actually send
patches for this a month ago [1]. No response yet.

  - Inverted PWM does not work in U-Boot (on imx at least). And it
does not seam like it can be fixed easily. I do not know what is
the situation in other bootloaders.

So my current bootloader solution is one of:
  - Set the pin to the appropriate (HIGH) level using GPIO.
  - Do not touch the pin at all, it has 100k pull-up by default.

>> The other thing is I would prefer to make the change optional. With your
>> approach you are changing the behavior for all current users of inverted
>> PWM. I do not think all imx6 users are aware of the problem so they might
>> not be OK with the sudden change in the behavior.
> 
> Isn't my change an improvement for all users? What state do you have in
> mind that make things worse than they are now?

Lets say that the user:
- Needs inverted PWM signal.
- Needs it to be disabled all the time unless he enable it.

Current situation:
H|
L|\_
  +---++-+-+-+
  | reset | bootloader | default pinctrl | PWM enable 100% | PWM disable |

What you propose (for all users of inverted PWM):
H| _
L|\___/
  +---++-+-+-+
  | reset | bootloader | default pinctrl | PWM enable 100% | PWM disable |

My solution (for those who want it):
H|__   _
L|  \_/
  +---++-+-+-+
  | 

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-07 Thread Uwe Kleine-König
Hello Michal,

On Wed, Nov 07, 2018 at 01:32:10PM +, Vokáč Michal wrote:
> On 7.11.2018 10:33, Uwe Kleine-König wrote:
> > Hello Michal,
> > 
> > just to state it more explicitly, I think the following patch (not even
> > compile tested) is much preferable over your approach:
> 
> Interesting idea. I just wonder why nobody else did not come up with such
> a simple solution before.

I think I mentioned it already in this thread, but it went unnoticed :-)

> > diff --git a/drivers/pwm/pwm-imx.c b/drivers/pwm/pwm-imx.c
> > index 1d5242c9cde0..af88644b5efb 100644
> > --- a/drivers/pwm/pwm-imx.c
> > +++ b/drivers/pwm/pwm-imx.c
> > @@ -216,7 +216,14 @@ static int imx_pwm_apply_v2(struct pwm_chip *chip, 
> > struct pwm_device *pwm,
> > cr |= MX3_PWMCR_POUTC;
> >   
> > writel(cr, imx->mmio_base + MX3_PWMCR);
> > -   } else if (cstate.enabled) {
> > +   } else if (cstate.enabled && state->polarity == PWM_POLARITY_NORMAL) {
> > +   /*
> > +* When disabled in hardware the output pin goes to 0
> > +* independant of the polarity setting. The expectation of some
> > +* people however is that after disabling the pin goes to the
> > +* inactive level which isn't given for an inversed pwm, so
> > +* only disable for normal polarity.
> > +*/
> > writel(0, imx->mmio_base + MX3_PWMCR);
> >   
> > clk_disable_unprepare(imx->clk_per);
> 
> I tested your patch. It does not work as you expected.
> 
> In v4.20-rc1 the pwm-backlight driver has been converted to atomic API.
> So the pwm_apply_v2 function is called only once to set new period/duty
> and state. With your patch that means that "echo 0 > brightness" has no
> visible effect. It leaves the PWM chip enabled with period/duty set to
> however it was. But the core thinks it was reconfigured:

Then the patch isn't correct yet. The idea is always keep the hardware
running and only disable it if it's uninverted.
> 
> # cat /sys/class/backlight/backight/brightness
> 0
> 
> # cat /sys/kernel/debug/pwm
> platform/208.pwm, 1 PWM device
>   pwm-0   (backlight   ): requested period: 50 ns duty: 0 ns 
> polarity: inverse
> 
> > I think it solves most if not all problems you want to address with the
> > pinctrl stuff.
> 
> Unfortunately not. I also tested your patch on v4.19. It works as you
> probably intended - it is possible to disable backlight without the PWM
> chip being disabled. But it does not solve the time frame between
> imx_pwm_probe() and imx_pwm_apply_v2().

In imx_pwm_probe it's not yet known what the polarity is supposed to be,
right? So the right thing to do there is to not touch the configuration
of the pwm. I think all states that are problematic then are also
problematic with the gpio/pinmux approach. (I might be wrong here, so
feel free to invest some brain cycles to prove me wrong. But I'd be
surprised if there are problems that are relevant.)
 
> In probe you do not have any users yet. So you do not know the requested
> output polarity. With "default" pinctrl the PWM output would be muxed to
> the selected pin and since the PWM chip is most probably disabled
> (unless you enabled it in bootloader) you would get low level on the pin.
> That means your backlight is fully enabled until the first call to
> imx_pwm_apply_v2(). On my system this is 2 seconds.

With the gpio/pinmux approach you don't know the intended polarity
either and maybe enable the display, too. For both the solution is to
let the bootloader enable the pwm with the right output level.
Am I missing something?

> It might not be a big issue for backlight but for motor control it is
> not the right thing to do.
> 
> The other thing is I would prefer to make the change optional. With your
> approach you are changing the behavior for all current users of inverted
> PWM. I do not think all imx6 users are aware of the problem so they might
> not be OK with the sudden change in the behavior.

Isn't my change an improvement for all users? What state do you have in
mind that make things worse than they are now?

Best regards
Uwe

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-07 Thread Vokáč Michal
Hi Uwe,

On 7.11.2018 10:33, Uwe Kleine-König wrote:
> Hello Michal,
> 
> just to state it more explicitly, I think the following patch (not even
> compile tested) is much preferable over your approach:

Interesting idea. I just wonder why nobody else did not come up with such
a simple solution before.

> diff --git a/drivers/pwm/pwm-imx.c b/drivers/pwm/pwm-imx.c
> index 1d5242c9cde0..af88644b5efb 100644
> --- a/drivers/pwm/pwm-imx.c
> +++ b/drivers/pwm/pwm-imx.c
> @@ -216,7 +216,14 @@ static int imx_pwm_apply_v2(struct pwm_chip *chip, 
> struct pwm_device *pwm,
>   cr |= MX3_PWMCR_POUTC;
>   
>   writel(cr, imx->mmio_base + MX3_PWMCR);
> - } else if (cstate.enabled) {
> + } else if (cstate.enabled && state->polarity == PWM_POLARITY_NORMAL) {
> + /*
> +  * When disabled in hardware the output pin goes to 0
> +  * independant of the polarity setting. The expectation of some
> +  * people however is that after disabling the pin goes to the
> +  * inactive level which isn't given for an inversed pwm, so
> +  * only disable for normal polarity.
> +  */
>   writel(0, imx->mmio_base + MX3_PWMCR);
>   
>   clk_disable_unprepare(imx->clk_per);

I tested your patch. It does not work as you expected.

In v4.20-rc1 the pwm-backlight driver has been converted to atomic API.
So the pwm_apply_v2 function is called only once to set new period/duty
and state. With your patch that means that "echo 0 > brightness" has no
visible effect. It leaves the PWM chip enabled with period/duty set to
however it was. But the core thinks it was reconfigured:

# cat /sys/class/backlight/backight/brightness
0

# cat /sys/kernel/debug/pwm
platform/208.pwm, 1 PWM device
  pwm-0   (backlight   ): requested period: 50 ns duty: 0 ns 
polarity: inverse

> I think it solves most if not all problems you want to address with the
> pinctrl stuff.

Unfortunately not. I also tested your patch on v4.19. It works as you
probably intended - it is possible to disable backlight without the PWM
chip being disabled. But it does not solve the time frame between
imx_pwm_probe() and imx_pwm_apply_v2().

In probe you do not have any users yet. So you do not know the requested
output polarity. With "default" pinctrl the PWM output would be muxed to
the selected pin and since the PWM chip is most probably disabled
(unless you enabled it in bootloader) you would get low level on the pin.
That means your backlight is fully enabled until the first call to
imx_pwm_apply_v2(). On my system this is 2 seconds.

It might not be a big issue for backlight but for motor control it is
not the right thing to do.

The other thing is I would prefer to make the change optional. With your
approach you are changing the behavior for all current users of inverted
PWM. I do not think all imx6 users are aware of the problem so they might
not be OK with the sudden change in the behavior.

I agree it would be nice if such a simple change as you proposed would
solve the problem. Still, I do not see any other solution than pinctrl
to deal with all the problems I would like to address.

Anyway, thank you for the idea!

All the best,
Michal


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-11-07 Thread Uwe Kleine-König
Hello Michal,

just to state it more explicitly, I think the following patch (not even
compile tested) is much preferable over your approach:

diff --git a/drivers/pwm/pwm-imx.c b/drivers/pwm/pwm-imx.c
index 1d5242c9cde0..af88644b5efb 100644
--- a/drivers/pwm/pwm-imx.c
+++ b/drivers/pwm/pwm-imx.c
@@ -216,7 +216,14 @@ static int imx_pwm_apply_v2(struct pwm_chip *chip, struct 
pwm_device *pwm,
cr |= MX3_PWMCR_POUTC;
 
writel(cr, imx->mmio_base + MX3_PWMCR);
-   } else if (cstate.enabled) {
+   } else if (cstate.enabled && state->polarity == PWM_POLARITY_NORMAL) {
+   /*
+* When disabled in hardware the output pin goes to 0
+* independant of the polarity setting. The expectation of some
+* people however is that after disabling the pin goes to the
+* inactive level which isn't given for an inversed pwm, so
+* only disable for normal polarity.
+*/
writel(0, imx->mmio_base + MX3_PWMCR);
 
clk_disable_unprepare(imx->clk_per);

I think it solves most if not all problems you want to address with the
pinctrl stuff.

Best regards
Uwe

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-10-29 Thread Vokáč Michal
On 15.10.2018 10:45, Thierry Reding wrote:
> On Sun, Oct 14, 2018 at 10:24:57PM +0200, Uwe Kleine-König wrote:
>> Hello,
>>
>> On Fri, Oct 12, 2018 at 06:08:54PM +0200, Uwe Kleine-König wrote:
>> +if (PTR_ERR(imx_chip->pwm_gpiod) == -EPROBE_DEFER) {
>
> You must not use PTR_ERR on a value that might not contain an error
> pointer.

 OK, thank you for valuable info.
 So it seems like the I2C folks are in troubles as well:


 https://elixir.bootlin.com/linux/latest/source/drivers/i2c/busses/i2c-imx.c#L996
>>>
>>> correct. I cannot find this documented though.
>>
>> I found in LDD3[1], chapter 11 on page 295:
>>
>>  If you need the actual error code, it can be extracted with:
>>
>>  long PTR_ERR(const void *ptr);
>>
>>  You should use PTR_ERR only on a value for which IS_ERR returns a true
>>  value; any other value is a valid pointer.
>>
>> That is probably where I have my claim from. There is no further
>> explanation though, so I'll post a patch adding a comment to the
>> definition of PTR_ERR to find out if there is a relevant reason.

Thanks for mentioning the LDD3 Uwe. I almost forgot it exist.
I will dust-off my electronic copy ;)

> Michal's code above does an implicit IS_ERR() by comparing to an actual
> error code. It's certainly true that PTR_ERR() on any pointer and then
> using that value can be risky because it may not actually be an error.
> So if you go and unconditionally print that error code even if it isn't
> an error but a valid pointer, you've leaked a kernel address.
> 
> However, it's perfectly safe to use PTR_ERR(ptr) == -EPROBE_DEFER (or
> for any other error code for that matter).

OK, I will leave that as is. And thanks for all the details.
I always learn something new from the comments!

Michal


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-10-29 Thread Vokáč Michal
On 12.10.2018 18:08, Uwe Kleine-König wrote:
> Hello,
> 
> On Fri, Oct 12, 2018 at 03:04:48PM +, Vokáč Michal wrote:
>> On 12.10.2018 10:57, Uwe Kleine-König wrote:
>>> On Wed, Oct 10, 2018 at 09:33:26AM +, Vokáč Michal wrote:
 Normally the PWM output is held LOW when PWM is disabled. This can cause
 problems when inverted PWM signal polarity is needed. With this behavior
 the connected circuit is fed by 100% duty cycle instead of being shut-off.

 Allow users to define a "gpio" and a "pwm" pinctrl states. The pwm pinctrl
 state is then selected when PWM is enabled and the gpio pinctrl state is
 selected when PWM is disabled. Also add a new pwm-gpios GPIO that is used
 to drive the output in the gpio state.

 If all the pinctrl states and the pwm-gpios are not correctly specified
 in DT the logic will work as before.

 As an example, with this patch a PWM controlled backlight with inversed
 signal polarity can be used in full brightness range. Without this patch
 the backlight can not be turned off as brightness = 0 disables the PWM
 and that in turn set PWM output LOW, that is full brightness.

 Output of the PWM with "default" pinctrl and with "pwm"+"gpio" pinctrl:

 +--++---+---+
 | After reset  | Bootloader | Linux pinctrl | User (sysfs, backlight..) |
 | 100k pull-up | (not used) |   |   enable|   disable   |
 +--++---+---+
___default_   _   _
   |_| |_| |_| |_|_

  pwm + gpio
___   _   _   _   _
   |_| |_| |_| |_|
>>>
>>> I was made aware of this patch by Thierry while discussion about a patch
>>> opportunity. I already pointed out some stuff I don't like about this
>>> patch in the repective thread, but I repeat it here to have it at the
>>> right place.
>>
>> Thank you for taking time to comment on this one as well Uwe.
>>
 Signed-off-by: Michal Vokáč 
 ---
 Changes in v2:
- Utilize the "pwm" and "gpio" pinctrl states.
- Use the pwm-gpios signal to drive the output in "gpio" pinctrl state.
- Select the right pinctrl state in probe.

drivers/pwm/pwm-imx.c | 86 
 +++
1 file changed, 86 insertions(+)

 diff --git a/drivers/pwm/pwm-imx.c b/drivers/pwm/pwm-imx.c
 index 6cd3b72..3502123 100644
 --- a/drivers/pwm/pwm-imx.c
 +++ b/drivers/pwm/pwm-imx.c
 @@ -10,11 +10,13 @@
#include 
#include 
#include 
 +#include 
#include 
#include 
#include 
#include 
#include 
 +#include 
#include 
#include 
#include 
 @@ -92,10 +94,45 @@ struct imx_chip {
void __iomem*mmio_base;

struct pwm_chip chip;
 +
 +  struct pinctrl *pinctrl;
 +  struct pinctrl_state *pinctrl_pins_gpio;
 +  struct pinctrl_state *pinctrl_pins_pwm;
 +  struct gpio_desc *pwm_gpiod;
>>>
>>> The pinctrl framework already knows about "init" and "default". These
>>> should be enough. i.e. "init" configures as gpio and "default" als pwm.
>>
>> That is totally new information for me. I've never seen any usage of
>> "init" pinctrl state before. The total number of users is 6 (rockchip)
>> so that explains a lot.
>>
>> I think that it would not work though. The basic idea behind my
>> solution is that the pinctrl driver does not know what is the correct
>> pin configuration at probe whereas the pwm-imx driver does.
>>
>> With the "init" and "default" states the pinctrl driver will always
>> select the "default" if it find the pins in "init" state.
>>
>>  
>> https://elixir.bootlin.com/linux/latest/source/drivers/pinctrl/core.c#L1469
>>
>> But we want the pins to stay in the "init" GPIO state unless a client
>> enables the PWM.
> 
> Note I don't know this very well either. What should work then is to
> have "init" as GPIO and "active" as PWM. Well, though this breaks if you
> intend to probe a running pwm without stopping it.

I'm not sure what you mean by "active". I tried to find more info about
that and it looks like it is not a natively supported pinctrl state.
Only "init", "default" and "sleep" are, right? I could not find any current
users of "active". Few TI dra7 boards pretend to use it for dcan1 pinctrl
but I did not find any code handling that state.

Or do you propose to rename what I call "pwm" state to "active"?

Anyway, as you said - this still does not work as I would like to have
an option not to stop PWM that is already running.

As a side note - while greping the "ac

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-10-15 Thread Thierry Reding
On Sun, Oct 14, 2018 at 10:24:57PM +0200, Uwe Kleine-König wrote:
> Hello,
> 
> On Fri, Oct 12, 2018 at 06:08:54PM +0200, Uwe Kleine-König wrote:
> > > >> +  if (PTR_ERR(imx_chip->pwm_gpiod) == -EPROBE_DEFER) {
> > > > 
> > > > You must not use PTR_ERR on a value that might not contain an error
> > > > pointer.
> > > 
> > > OK, thank you for valuable info.
> > > So it seems like the I2C folks are in troubles as well:
> > > 
> > >   
> > > https://elixir.bootlin.com/linux/latest/source/drivers/i2c/busses/i2c-imx.c#L996
> > 
> > correct. I cannot find this documented though.
> 
> I found in LDD3[1], chapter 11 on page 295:
> 
>   If you need the actual error code, it can be extracted with:
> 
>   long PTR_ERR(const void *ptr);
> 
>   You should use PTR_ERR only on a value for which IS_ERR returns a true
>   value; any other value is a valid pointer.
> 
> That is probably where I have my claim from. There is no further
> explanation though, so I'll post a patch adding a comment to the
> definition of PTR_ERR to find out if there is a relevant reason.

Michal's code above does an implicit IS_ERR() by comparing to an actual
error code. It's certainly true that PTR_ERR() on any pointer and then
using that value can be risky because it may not actually be an error.
So if you go and unconditionally print that error code even if it isn't
an error but a valid pointer, you've leaked a kernel address.

However, it's perfectly safe to use PTR_ERR(ptr) == -EPROBE_DEFER (or
for any other error code for that matter).

Thierry


signature.asc
Description: PGP signature


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-10-14 Thread Uwe Kleine-König
Hello,

On Fri, Oct 12, 2018 at 06:08:54PM +0200, Uwe Kleine-König wrote:
> > >> +if (PTR_ERR(imx_chip->pwm_gpiod) == -EPROBE_DEFER) {
> > > 
> > > You must not use PTR_ERR on a value that might not contain an error
> > > pointer.
> > 
> > OK, thank you for valuable info.
> > So it seems like the I2C folks are in troubles as well:
> > 
> > 
> > https://elixir.bootlin.com/linux/latest/source/drivers/i2c/busses/i2c-imx.c#L996
> 
> correct. I cannot find this documented though.

I found in LDD3[1], chapter 11 on page 295:

If you need the actual error code, it can be extracted with:

long PTR_ERR(const void *ptr);

You should use PTR_ERR only on a value for which IS_ERR returns a true
value; any other value is a valid pointer.

That is probably where I have my claim from. There is no further
explanation though, so I'll post a patch adding a comment to the
definition of PTR_ERR to find out if there is a relevant reason.

Best regards
Uwe

[1] https://lwn.net/Kernel/LDD3/

-- 
Pengutronix e.K.   | Uwe Kleine-König|
Industrial Linux Solutions | http://www.pengutronix.de/  |


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-10-12 Thread Uwe Kleine-König
Hello,

On Fri, Oct 12, 2018 at 03:04:48PM +, Vokáč Michal wrote:
> On 12.10.2018 10:57, Uwe Kleine-König wrote:
> > On Wed, Oct 10, 2018 at 09:33:26AM +, Vokáč Michal wrote:
> >> Normally the PWM output is held LOW when PWM is disabled. This can cause
> >> problems when inverted PWM signal polarity is needed. With this behavior
> >> the connected circuit is fed by 100% duty cycle instead of being shut-off.
> >>
> >> Allow users to define a "gpio" and a "pwm" pinctrl states. The pwm pinctrl
> >> state is then selected when PWM is enabled and the gpio pinctrl state is
> >> selected when PWM is disabled. Also add a new pwm-gpios GPIO that is used
> >> to drive the output in the gpio state.
> >>
> >> If all the pinctrl states and the pwm-gpios are not correctly specified
> >> in DT the logic will work as before.
> >>
> >> As an example, with this patch a PWM controlled backlight with inversed
> >> signal polarity can be used in full brightness range. Without this patch
> >> the backlight can not be turned off as brightness = 0 disables the PWM
> >> and that in turn set PWM output LOW, that is full brightness.
> >>
> >> Output of the PWM with "default" pinctrl and with "pwm"+"gpio" pinctrl:
> >>
> >> +--++---+---+
> >> | After reset  | Bootloader | Linux pinctrl | User (sysfs, backlight..) |
> >> | 100k pull-up | (not used) |   |   enable|   disable   |
> >> +--++---+---+
> >>   ___default_   _   _
> >>  |_| |_| |_| |_|_
> >>
> >> pwm + gpio
> >>   ___   _   _   _   _
> >>  |_| |_| |_| |_|
> > 
> > I was made aware of this patch by Thierry while discussion about a patch
> > opportunity. I already pointed out some stuff I don't like about this
> > patch in the repective thread, but I repeat it here to have it at the
> > right place.
> 
> Thank you for taking time to comment on this one as well Uwe.
> 
> >> Signed-off-by: Michal Vokáč 
> >> ---
> >> Changes in v2:
> >>   - Utilize the "pwm" and "gpio" pinctrl states.
> >>   - Use the pwm-gpios signal to drive the output in "gpio" pinctrl state.
> >>   - Select the right pinctrl state in probe.
> >>
> >>   drivers/pwm/pwm-imx.c | 86 
> >> +++
> >>   1 file changed, 86 insertions(+)
> >>
> >> diff --git a/drivers/pwm/pwm-imx.c b/drivers/pwm/pwm-imx.c
> >> index 6cd3b72..3502123 100644
> >> --- a/drivers/pwm/pwm-imx.c
> >> +++ b/drivers/pwm/pwm-imx.c
> >> @@ -10,11 +10,13 @@
> >>   #include 
> >>   #include 
> >>   #include 
> >> +#include 
> >>   #include 
> >>   #include 
> >>   #include 
> >>   #include 
> >>   #include 
> >> +#include 
> >>   #include 
> >>   #include 
> >>   #include 
> >> @@ -92,10 +94,45 @@ struct imx_chip {
> >>void __iomem*mmio_base;
> >>   
> >>struct pwm_chip chip;
> >> +
> >> +  struct pinctrl *pinctrl;
> >> +  struct pinctrl_state *pinctrl_pins_gpio;
> >> +  struct pinctrl_state *pinctrl_pins_pwm;
> >> +  struct gpio_desc *pwm_gpiod;
> > 
> > The pinctrl framework already knows about "init" and "default". These
> > should be enough. i.e. "init" configures as gpio and "default" als pwm.
> 
> That is totally new information for me. I've never seen any usage of
> "init" pinctrl state before. The total number of users is 6 (rockchip)
> so that explains a lot.
> 
> I think that it would not work though. The basic idea behind my
> solution is that the pinctrl driver does not know what is the correct
> pin configuration at probe whereas the pwm-imx driver does.
> 
> With the "init" and "default" states the pinctrl driver will always
> select the "default" if it find the pins in "init" state.
> 
>   
> https://elixir.bootlin.com/linux/latest/source/drivers/pinctrl/core.c#L1469
> 
> But we want the pins to stay in the "init" GPIO state unless a client
> enables the PWM.

Note I don't know this very well either. What should work then is to
have "init" as GPIO and "active" as PWM. Well, though this breaks if you
intend to probe a running pwm without stopping it.
 
> >>   };
> >>   
> >> +
> >>   #define to_imx_chip(chip)container_of(chip, struct imx_chip, 
> >> chip)
> >>   
> >> +static int imx_pwm_init_pinctrl_info(struct imx_chip *imx_chip,
> >> +  struct platform_device *pdev)
> >> +{
> >> +  imx_chip->pinctrl = devm_pinctrl_get(&pdev->dev);
> >> +  if (!imx_chip->pinctrl || IS_ERR(imx_chip->pinctrl)) {
> >> +  dev_info(&pdev->dev, "can not get pinctrl\n");
> >> +  return PTR_ERR(imx_chip->pinctrl);
> >> +  }
> >> +
> >> +  imx_chip->pinctrl_pins_pwm = pinctrl_lookup_state(imx_chip->pinctrl,
> >> +  "pwm");
> >> +  imx_chip->pinctrl_pins_gpio = pinctrl_lookup_state(imx_chip->

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-10-12 Thread Thierry Reding
On Fri, Oct 12, 2018 at 03:04:48PM +, Vokáč Michal wrote:
> On 12.10.2018 10:57, Uwe Kleine-König wrote:
> > On Wed, Oct 10, 2018 at 09:33:26AM +, Vokáč Michal wrote:
[...]
> >> +static int imx_pwm_init_pinctrl_info(struct imx_chip *imx_chip,
> >> +  struct platform_device *pdev)
> >> +{
> >> +  imx_chip->pinctrl = devm_pinctrl_get(&pdev->dev);
> >> +  if (!imx_chip->pinctrl || IS_ERR(imx_chip->pinctrl)) {
> >> +  dev_info(&pdev->dev, "can not get pinctrl\n");
> >> +  return PTR_ERR(imx_chip->pinctrl);
> >> +  }
> >> +
> >> +  imx_chip->pinctrl_pins_pwm = pinctrl_lookup_state(imx_chip->pinctrl,
> >> +  "pwm");
> >> +  imx_chip->pinctrl_pins_gpio = pinctrl_lookup_state(imx_chip->pinctrl,
> >> +  "gpio");
> >> +  imx_chip->pwm_gpiod = devm_gpiod_get_optional(&pdev->dev, "pwm",
> >> +  GPIOD_IN);
> >> +
> >> +  if (PTR_ERR(imx_chip->pwm_gpiod) == -EPROBE_DEFER) {
> > 
> > You must not use PTR_ERR on a value that might not contain an error
> > pointer.
> 
> OK, thank you for valuable info.
> So it seems like the I2C folks are in troubles as well:
> 
>   
> https://elixir.bootlin.com/linux/latest/source/drivers/i2c/busses/i2c-imx.c#L996

There's nothing inherently wrong with doing it like the above, just
maybe slightly unusual. PTR_ERR() is really just casting from a pointer
to an integer, so if the pointer happens to contain the value
-EPROBE_DEFER, then the above will be true. If it contains a valid
pointer, the above will be false, so it does exactly what you want.

Perhaps a more idiomatic way to write this would be:

if (IS_ERR(imx_chip->pwm_gpiod)) {
if (PTR_ERR(imx_chip->pwm_gpiod) == -EPROBE_DEFER)
return -EPROBE_DEFER;
}

But that's not much clearer than what you have, so feel free to keep it
that way.

Thierry


signature.asc
Description: PGP signature


Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-10-12 Thread Vokáč Michal
On 12.10.2018 10:57, Uwe Kleine-König wrote:
> Hello,
> 
> On Wed, Oct 10, 2018 at 09:33:26AM +, Vokáč Michal wrote:
>> Normally the PWM output is held LOW when PWM is disabled. This can cause
>> problems when inverted PWM signal polarity is needed. With this behavior
>> the connected circuit is fed by 100% duty cycle instead of being shut-off.
>>
>> Allow users to define a "gpio" and a "pwm" pinctrl states. The pwm pinctrl
>> state is then selected when PWM is enabled and the gpio pinctrl state is
>> selected when PWM is disabled. Also add a new pwm-gpios GPIO that is used
>> to drive the output in the gpio state.
>>
>> If all the pinctrl states and the pwm-gpios are not correctly specified
>> in DT the logic will work as before.
>>
>> As an example, with this patch a PWM controlled backlight with inversed
>> signal polarity can be used in full brightness range. Without this patch
>> the backlight can not be turned off as brightness = 0 disables the PWM
>> and that in turn set PWM output LOW, that is full brightness.
>>
>> Output of the PWM with "default" pinctrl and with "pwm"+"gpio" pinctrl:
>>
>> +--++---+---+
>> | After reset  | Bootloader | Linux pinctrl | User (sysfs, backlight..) |
>> | 100k pull-up | (not used) |   |   enable|   disable   |
>> +--++---+---+
>>   ___default_   _   _
>>  |_| |_| |_| |_|_
>>
>> pwm + gpio
>>   ___   _   _   _   _
>>  |_| |_| |_| |_|
> 
> I was made aware of this patch by Thierry while discussion about a patch
> opportunity. I already pointed out some stuff I don't like about this
> patch in the repective thread, but I repeat it here to have it at the
> right place.

Thank you for taking time to comment on this one as well Uwe.

>> Signed-off-by: Michal Vokáč 
>> ---
>> Changes in v2:
>>   - Utilize the "pwm" and "gpio" pinctrl states.
>>   - Use the pwm-gpios signal to drive the output in "gpio" pinctrl state.
>>   - Select the right pinctrl state in probe.
>>
>>   drivers/pwm/pwm-imx.c | 86 
>> +++
>>   1 file changed, 86 insertions(+)
>>
>> diff --git a/drivers/pwm/pwm-imx.c b/drivers/pwm/pwm-imx.c
>> index 6cd3b72..3502123 100644
>> --- a/drivers/pwm/pwm-imx.c
>> +++ b/drivers/pwm/pwm-imx.c
>> @@ -10,11 +10,13 @@
>>   #include 
>>   #include 
>>   #include 
>> +#include 
>>   #include 
>>   #include 
>>   #include 
>>   #include 
>>   #include 
>> +#include 
>>   #include 
>>   #include 
>>   #include 
>> @@ -92,10 +94,45 @@ struct imx_chip {
>>  void __iomem*mmio_base;
>>   
>>  struct pwm_chip chip;
>> +
>> +struct pinctrl *pinctrl;
>> +struct pinctrl_state *pinctrl_pins_gpio;
>> +struct pinctrl_state *pinctrl_pins_pwm;
>> +struct gpio_desc *pwm_gpiod;
> 
> The pinctrl framework already knows about "init" and "default". These
> should be enough. i.e. "init" configures as gpio and "default" als pwm.

That is totally new information for me. I've never seen any usage of
"init" pinctrl state before. The total number of users is 6 (rockchip)
so that explains a lot.

I think that it would not work though. The basic idea behind my
solution is that the pinctrl driver does not know what is the correct
pin configuration at probe whereas the pwm-imx driver does.

With the "init" and "default" states the pinctrl driver will always
select the "default" if it find the pins in "init" state.


https://elixir.bootlin.com/linux/latest/source/drivers/pinctrl/core.c#L1469

But we want the pins to stay in the "init" GPIO state unless a client
enables the PWM.

>>   };
>>   
>> +
>>   #define to_imx_chip(chip)  container_of(chip, struct imx_chip, chip)
>>   
>> +static int imx_pwm_init_pinctrl_info(struct imx_chip *imx_chip,
>> +struct platform_device *pdev)
>> +{
>> +imx_chip->pinctrl = devm_pinctrl_get(&pdev->dev);
>> +if (!imx_chip->pinctrl || IS_ERR(imx_chip->pinctrl)) {
>> +dev_info(&pdev->dev, "can not get pinctrl\n");
>> +return PTR_ERR(imx_chip->pinctrl);
>> +}
>> +
>> +imx_chip->pinctrl_pins_pwm = pinctrl_lookup_state(imx_chip->pinctrl,
>> +"pwm");
>> +imx_chip->pinctrl_pins_gpio = pinctrl_lookup_state(imx_chip->pinctrl,
>> +"gpio");
>> +imx_chip->pwm_gpiod = devm_gpiod_get_optional(&pdev->dev, "pwm",
>> +GPIOD_IN);
>> +
>> +if (PTR_ERR(imx_chip->pwm_gpiod) == -EPROBE_DEFER) {
> 
> You must not use PTR_ERR on a value that might not contain an error
> pointer.

OK, thank you for valuable info.
So it seems like the I2C folks are in troubles as well:


https://elixir.bootlin.com/linux/latest/source/drivers/i2c/bu

Re: [RCF PATCH,v2,2/2] pwm: imx: Configure output to GPIO in disabled state

2018-10-12 Thread Uwe Kleine-König
Hello,

On Wed, Oct 10, 2018 at 09:33:26AM +, Vokáč Michal wrote:
> Normally the PWM output is held LOW when PWM is disabled. This can cause
> problems when inverted PWM signal polarity is needed. With this behavior
> the connected circuit is fed by 100% duty cycle instead of being shut-off.
> 
> Allow users to define a "gpio" and a "pwm" pinctrl states. The pwm pinctrl
> state is then selected when PWM is enabled and the gpio pinctrl state is
> selected when PWM is disabled. Also add a new pwm-gpios GPIO that is used
> to drive the output in the gpio state.
> 
> If all the pinctrl states and the pwm-gpios are not correctly specified
> in DT the logic will work as before.
> 
> As an example, with this patch a PWM controlled backlight with inversed
> signal polarity can be used in full brightness range. Without this patch
> the backlight can not be turned off as brightness = 0 disables the PWM
> and that in turn set PWM output LOW, that is full brightness.
> 
> Output of the PWM with "default" pinctrl and with "pwm"+"gpio" pinctrl:
> 
> +--++---+---+
> | After reset  | Bootloader | Linux pinctrl | User (sysfs, backlight..) |
> | 100k pull-up | (not used) |   |   enable|   disable   |
> +--++---+---+
>  ___default_   _   _
> |_| |_| |_| |_|_
> 
>pwm + gpio
>  ___   _   _   _   _
> |_| |_| |_| |_|

I was made aware of this patch by Thierry while discussion about a patch
opportunity. I already pointed out some stuff I don't like about this
patch in the repective thread, but I repeat it here to have it at the
right place.

> Signed-off-by: Michal Vokáč 
> ---
> Changes in v2:
>  - Utilize the "pwm" and "gpio" pinctrl states.
>  - Use the pwm-gpios signal to drive the output in "gpio" pinctrl state.
>  - Select the right pinctrl state in probe. 
> 
>  drivers/pwm/pwm-imx.c | 86 
> +++
>  1 file changed, 86 insertions(+)
> 
> diff --git a/drivers/pwm/pwm-imx.c b/drivers/pwm/pwm-imx.c
> index 6cd3b72..3502123 100644
> --- a/drivers/pwm/pwm-imx.c
> +++ b/drivers/pwm/pwm-imx.c
> @@ -10,11 +10,13 @@
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
>  #include 
>  #include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -92,10 +94,45 @@ struct imx_chip {
>   void __iomem*mmio_base;
>  
>   struct pwm_chip chip;
> +
> + struct pinctrl *pinctrl;
> + struct pinctrl_state *pinctrl_pins_gpio;
> + struct pinctrl_state *pinctrl_pins_pwm;
> + struct gpio_desc *pwm_gpiod;

The pinctrl framework already knows about "init" and "default". These
should be enough. i.e. "init" configures as gpio and "default" als pwm.

>  };
>  
> +
>  #define to_imx_chip(chip)container_of(chip, struct imx_chip, chip)
>  
> +static int imx_pwm_init_pinctrl_info(struct imx_chip *imx_chip,
> + struct platform_device *pdev)
> +{
> + imx_chip->pinctrl = devm_pinctrl_get(&pdev->dev);
> + if (!imx_chip->pinctrl || IS_ERR(imx_chip->pinctrl)) {
> + dev_info(&pdev->dev, "can not get pinctrl\n");
> + return PTR_ERR(imx_chip->pinctrl);
> + }
> +
> + imx_chip->pinctrl_pins_pwm = pinctrl_lookup_state(imx_chip->pinctrl,
> + "pwm");
> + imx_chip->pinctrl_pins_gpio = pinctrl_lookup_state(imx_chip->pinctrl,
> + "gpio");
> + imx_chip->pwm_gpiod = devm_gpiod_get_optional(&pdev->dev, "pwm",
> + GPIOD_IN);
> +
> + if (PTR_ERR(imx_chip->pwm_gpiod) == -EPROBE_DEFER) {

You must not use PTR_ERR on a value that might not contain an error
pointer.

> + return -EPROBE_DEFER;
> + } else if (IS_ERR(imx_chip->pwm_gpiod) ||
> +IS_ERR(imx_chip->pinctrl_pins_pwm) ||
> +IS_ERR(imx_chip->pinctrl_pins_gpio)) {

Would it be more correct to handle imx_chip->pinctrl_pins_pwm ==
ERR_PTR(-EPROBE_DEFER) similar to imx_chip->pwm_gpiod ==
ERR_PTR(-EPROBE_DEFER)?

> + dev_dbg(&pdev->dev, "PWM pinctrl information incomplete\n");

I wouldn't call that "incomplete". It's incomplete for the gpio
switching trick, but enough in general.

> + devm_pinctrl_put(imx_chip->pinctrl);
> + imx_chip->pinctrl = NULL;
> + }
> +
> + return 0;
> +}
> +
>  static void imx_pwm_get_state(struct pwm_chip *chip,
>   struct pwm_device *pwm, struct pwm_state *state)
>  {
> @@ -306,7 +343,31 @@ static int imx_pwm_apply_v2(struct pwm_chip *chip, 
> struct pwm_device *pwm,
>   MX3_PWMCR_POUTC_INVERTED);
>  
>   writel(cr, imx->mmio_base + MX3_PWMCR);
> +
> +