[v1,1/5] pwm: mediatek: add a property "mediatek,num-pwms"

Message ID de316385a84e079d3b23891e61c959094b9fa7e5.1547781007.git.ryder.lee@mediatek.com
State New
Headers show
Series
  • [v1,1/5] pwm: mediatek: add a property "mediatek,num-pwms"
Related show

Commit Message

Ryder Lee Jan. 18, 2019, 3:24 a.m.
This adds a property "mediatek,num-pwms" to avoid having an endless
list of compatibles with no differences for the same driver.

Thus, the driver should have backwards compatibility to older DTs.

Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
---
Changes since v1: add some checks for backwards compatibility.
---
 drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

Comments

Uwe Kleine-König Jan. 18, 2019, 7:59 a.m. | #1
Hello,

On Fri, Jan 18, 2019 at 11:24:41AM +0800, Ryder Lee wrote:
> This adds a property "mediatek,num-pwms" to avoid having an endless
> list of compatibles with no differences for the same driver.
> 
> Thus, the driver should have backwards compatibility to older DTs.

I still think Thierry should bless "num-pwms" without vendor prefix.

> Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
> ---
> Changes since v1: add some checks for backwards compatibility.
> ---
>  drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++---------
>  1 file changed, 18 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
> index eb6674c..81b7e5e 100644
> --- a/drivers/pwm/pwm-mediatek.c
> +++ b/drivers/pwm/pwm-mediatek.c
> @@ -55,7 +55,7 @@ enum {
>  };
>  
>  struct mtk_pwm_platform_data {

Unrelated to this patch: This name is bad. This struct is not used as
platform_data and so should better be named mtk_pwm_of_data. While at
criticizing existing stuff: I'd prefer pwm_mediatek as common prefix to
match the filename.

> -	unsigned int num_pwms;
> +	unsigned int num_pwms;	/* it should not be used in the future SoCs */

I'd drop this comment in favour of a runtime warning.

>  	bool pwm45_fixup;
>  	bool has_clks;
>  };
> @@ -226,27 +226,36 @@ static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
>  
>  static int mtk_pwm_probe(struct platform_device *pdev)
>  {
> -	const struct mtk_pwm_platform_data *data;
> +	struct device_node *np = pdev->dev.of_node;
>  	struct mtk_pwm_chip *pc;
>  	struct resource *res;
> -	unsigned int i;
> +	unsigned int i, num_pwms;
>  	int ret;
>  
>  	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
>  	if (!pc)
>  		return -ENOMEM;
>  
> -	data = of_device_get_match_data(&pdev->dev);
> -	if (data == NULL)
> -		return -EINVAL;
> -	pc->soc = data;
> +	pc->soc = of_device_get_match_data(&pdev->dev);

This might return NULL which ...

>  
>  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
>  	pc->regs = devm_ioremap_resource(&pdev->dev, res);
>  	if (IS_ERR(pc->regs))
>  		return PTR_ERR(pc->regs);
>  
> -	for (i = 0; i < data->num_pwms + 2 && pc->soc->has_clks; i++) {
> +	/* Check if we have set 'num-pwms' in DTs. */
> +	ret = of_property_read_u32(np, "mediatek,num-pwms", &num_pwms);
> +	if (ret < 0) {
> +		/* If no, fallback to use SoC data for backwards compatibility. */
> +		if (pc->soc->num_pwms) {

... here then results in a NULL pointer dereference. I think you want

		if (pc->soc)

here.

> +			num_pwms = pc->soc->num_pwms;
> +		} else {
> +			dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
> +			return ret;
> +		}
> +	}
> +
> +	for (i = 0; i < num_pwms + 2 && pc->soc->has_clks; i++) {
>  		pc->clks[i] = devm_clk_get(&pdev->dev, mtk_pwm_clk_name[i]);
>  		if (IS_ERR(pc->clks[i])) {
>  			dev_err(&pdev->dev, "clock: %s fail: %ld\n",
> @@ -260,7 +269,7 @@ static int mtk_pwm_probe(struct platform_device *pdev)
>  	pc->chip.dev = &pdev->dev;
>  	pc->chip.ops = &mtk_pwm_ops;
>  	pc->chip.base = -1;
> -	pc->chip.npwm = data->num_pwms;
> +	pc->chip.npwm = num_pwms;
>  
>  	ret = pwmchip_add(&pc->chip);
>  	if (ret < 0) {

Best regards
Uwe
Uwe Kleine-König Jan. 18, 2019, 8:05 a.m. | #2
Hello,

just realized another issue while looking up a driver detail ...

On Fri, Jan 18, 2019 at 11:24:41AM +0800, Ryder Lee wrote:
> This adds a property "mediatek,num-pwms" to avoid having an endless
> list of compatibles with no differences for the same driver.
> 
> Thus, the driver should have backwards compatibility to older DTs.
> 
> Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
> ---
> Changes since v1: add some checks for backwards compatibility.
> ---
>  drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++---------
>  1 file changed, 18 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
> index eb6674c..81b7e5e 100644
> --- a/drivers/pwm/pwm-mediatek.c
> +++ b/drivers/pwm/pwm-mediatek.c
> @@ -55,7 +55,7 @@ enum {
>  };
>  
>  struct mtk_pwm_platform_data {
> -	unsigned int num_pwms;
> +	unsigned int num_pwms;	/* it should not be used in the future SoCs */
>  	bool pwm45_fixup;
>  	bool has_clks;
>  };
> @@ -226,27 +226,36 @@ static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
>  
>  static int mtk_pwm_probe(struct platform_device *pdev)
>  {
> -	const struct mtk_pwm_platform_data *data;
> +	struct device_node *np = pdev->dev.of_node;
>  	struct mtk_pwm_chip *pc;
>  	struct resource *res;
> -	unsigned int i;
> +	unsigned int i, num_pwms;
>  	int ret;
>  
>  	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
>  	if (!pc)
>  		return -ENOMEM;
>  
> -	data = of_device_get_match_data(&pdev->dev);
> -	if (data == NULL)
> -		return -EINVAL;
> -	pc->soc = data;
> +	pc->soc = of_device_get_match_data(&pdev->dev);
>  
>  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
>  	pc->regs = devm_ioremap_resource(&pdev->dev, res);
>  	if (IS_ERR(pc->regs))
>  		return PTR_ERR(pc->regs);
>  
> -	for (i = 0; i < data->num_pwms + 2 && pc->soc->has_clks; i++) {
> +	/* Check if we have set 'num-pwms' in DTs. */
> +	ret = of_property_read_u32(np, "mediatek,num-pwms", &num_pwms);
> +	if (ret < 0) {
> +		/* If no, fallback to use SoC data for backwards compatibility. */
> +		if (pc->soc->num_pwms) {
> +			num_pwms = pc->soc->num_pwms;
> +		} else {
> +			dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
> +			return ret;
> +		}
> +	}
> +
> +	for (i = 0; i < num_pwms + 2 && pc->soc->has_clks; i++) {
>  		pc->clks[i] = devm_clk_get(&pdev->dev, mtk_pwm_clk_name[i]);

If a dt contains

	mediatek,num-pwms = <17>;

you're accessing pc->clks[18] which is an out-of-bounds access, so
better check the limit or allocate the clks array dynamically.

Best regards
Uwe
Matthias Brugger Jan. 18, 2019, 8:43 a.m. | #3
On 18/01/2019 04:24, Ryder Lee wrote:
> This adds a property "mediatek,num-pwms" to avoid having an endless
> list of compatibles with no differences for the same driver.
> 
> Thus, the driver should have backwards compatibility to older DTs.
> 
> Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
> ---
> Changes since v1: add some checks for backwards compatibility.
> ---
>  drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++---------
>  1 file changed, 18 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
> index eb6674c..81b7e5e 100644
> --- a/drivers/pwm/pwm-mediatek.c
> +++ b/drivers/pwm/pwm-mediatek.c
> @@ -55,7 +55,7 @@ enum {
>  };
>  
>  struct mtk_pwm_platform_data {
> -	unsigned int num_pwms;
> +	unsigned int num_pwms;	/* it should not be used in the future SoCs */
>  	bool pwm45_fixup;
>  	bool has_clks;
>  };
> @@ -226,27 +226,36 @@ static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
>  
>  static int mtk_pwm_probe(struct platform_device *pdev)
>  {
> -	const struct mtk_pwm_platform_data *data;
> +	struct device_node *np = pdev->dev.of_node;
>  	struct mtk_pwm_chip *pc;
>  	struct resource *res;
> -	unsigned int i;
> +	unsigned int i, num_pwms;
>  	int ret;
>  
>  	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
>  	if (!pc)
>  		return -ENOMEM;
>  
> -	data = of_device_get_match_data(&pdev->dev);
> -	if (data == NULL)
> -		return -EINVAL;
> -	pc->soc = data;
> +	pc->soc = of_device_get_match_data(&pdev->dev);
>  
>  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
>  	pc->regs = devm_ioremap_resource(&pdev->dev, res);
>  	if (IS_ERR(pc->regs))
>  		return PTR_ERR(pc->regs);
>  
> -	for (i = 0; i < data->num_pwms + 2 && pc->soc->has_clks; i++) {
> +	/* Check if we have set 'num-pwms' in DTs. */
> +	ret = of_property_read_u32(np, "mediatek,num-pwms", &num_pwms);
> +	if (ret < 0) {
> +		/* If no, fallback to use SoC data for backwards compatibility. */
> +		if (pc->soc->num_pwms) {
> +			num_pwms = pc->soc->num_pwms;

Maybe that's bike shedding, but I think it would be better to carve out the
num_pwms from the mtk_pwm_platform_data and check against the compatible here.
With a expressive comment it will help other driver developers to not start
adding num_pwms in the platform data in their first attempt.

Regards,
Matthias

> +		} else {
> +			dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
> +			return ret;
> +		}
> +	}
> +
> +	for (i = 0; i < num_pwms + 2 && pc->soc->has_clks; i++) {
>  		pc->clks[i] = devm_clk_get(&pdev->dev, mtk_pwm_clk_name[i]);
>  		if (IS_ERR(pc->clks[i])) {
>  			dev_err(&pdev->dev, "clock: %s fail: %ld\n",
> @@ -260,7 +269,7 @@ static int mtk_pwm_probe(struct platform_device *pdev)
>  	pc->chip.dev = &pdev->dev;
>  	pc->chip.ops = &mtk_pwm_ops;
>  	pc->chip.base = -1;
> -	pc->chip.npwm = data->num_pwms;
> +	pc->chip.npwm = num_pwms;
>  
>  	ret = pwmchip_add(&pc->chip);
>  	if (ret < 0) {
>
Ryder Lee Jan. 18, 2019, 9:42 a.m. | #4
On Fri, 2019-01-18 at 08:59 +0100, Uwe Kleine-König wrote:
> Hello,
> 
> On Fri, Jan 18, 2019 at 11:24:41AM +0800, Ryder Lee wrote:
> > This adds a property "mediatek,num-pwms" to avoid having an endless
> > list of compatibles with no differences for the same driver.
> > 
> > Thus, the driver should have backwards compatibility to older DTs.
> 
> I still think Thierry should bless "num-pwms" without vendor prefix.

Okay.

> > Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
> > ---
> > Changes since v1: add some checks for backwards compatibility.
> > ---
> >  drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++---------
> >  1 file changed, 18 insertions(+), 9 deletions(-)
> > 
> > diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
> > index eb6674c..81b7e5e 100644
> > --- a/drivers/pwm/pwm-mediatek.c
> > +++ b/drivers/pwm/pwm-mediatek.c
> > @@ -55,7 +55,7 @@ enum {
> >  };
> >  
> >  struct mtk_pwm_platform_data {
> 
> Unrelated to this patch: This name is bad. This struct is not used as
> platform_data and so should better be named mtk_pwm_of_data. While at
> criticizing existing stuff: I'd prefer pwm_mediatek as common prefix to
> match the filename.

I think we can take care about that in another patch.

> > -	unsigned int num_pwms;
> > +	unsigned int num_pwms;	/* it should not be used in the future SoCs */
> 
> I'd drop this comment in favour of a runtime warning.

Sorry, I can't get you here.

> >  	bool pwm45_fixup;
> >  	bool has_clks;
> >  };
> > @@ -226,27 +226,36 @@ static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
> >  
> >  static int mtk_pwm_probe(struct platform_device *pdev)
> >  {
> > -	const struct mtk_pwm_platform_data *data;
> > +	struct device_node *np = pdev->dev.of_node;
> >  	struct mtk_pwm_chip *pc;
> >  	struct resource *res;
> > -	unsigned int i;
> > +	unsigned int i, num_pwms;
> >  	int ret;
> >  
> >  	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
> >  	if (!pc)
> >  		return -ENOMEM;
> >  
> > -	data = of_device_get_match_data(&pdev->dev);
> > -	if (data == NULL)
> > -		return -EINVAL;
> > -	pc->soc = data;
> > +	pc->soc = of_device_get_match_data(&pdev->dev);
> 
> This might return NULL which ...

The only way to call probe() is to match an entry in
mtk_pwm_of_match[], so match cannot be NULL.

> >  
> >  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> >  	pc->regs = devm_ioremap_resource(&pdev->dev, res);
> >  	if (IS_ERR(pc->regs))
> >  		return PTR_ERR(pc->regs);
> >  
> > -	for (i = 0; i < data->num_pwms + 2 && pc->soc->has_clks; i++) {
> > +	/* Check if we have set 'num-pwms' in DTs. */
> > +	ret = of_property_read_u32(np, "mediatek,num-pwms", &num_pwms);
> > +	if (ret < 0) {
> > +		/* If no, fallback to use SoC data for backwards compatibility. */
> > +		if (pc->soc->num_pwms) {
> 
> ... here then results in a NULL pointer dereference. I think you want

So we have no chance to get a NULL pointer, right?

> 		if (pc->soc)
> 
> here.
> 
> > +			num_pwms = pc->soc->num_pwms;
> > +		} else {
> > +			dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
> > +			return ret;
> > +		}
> > +	}
> > +
> > +	for (i = 0; i < num_pwms + 2 && pc->soc->has_clks; i++) {
> >  		pc->clks[i] = devm_clk_get(&pdev->dev, mtk_pwm_clk_name[i]);
> >  		if (IS_ERR(pc->clks[i])) {
> >  			dev_err(&pdev->dev, "clock: %s fail: %ld\n",
> > @@ -260,7 +269,7 @@ static int mtk_pwm_probe(struct platform_device *pdev)
> >  	pc->chip.dev = &pdev->dev;
> >  	pc->chip.ops = &mtk_pwm_ops;
> >  	pc->chip.base = -1;
> > -	pc->chip.npwm = data->num_pwms;
> > +	pc->chip.npwm = num_pwms;
> >  
> >  	ret = pwmchip_add(&pc->chip);
> >  	if (ret < 0) {
> 
> Best regards
> Uwe
>
Ryder Lee Jan. 18, 2019, 9:47 a.m. | #5
On Fri, 2019-01-18 at 09:05 +0100, Uwe Kleine-König wrote:
> Hello,
> 
> just realized another issue while looking up a driver detail ...
> 
> On Fri, Jan 18, 2019 at 11:24:41AM +0800, Ryder Lee wrote:
> > This adds a property "mediatek,num-pwms" to avoid having an endless
> > list of compatibles with no differences for the same driver.
> > 
> > Thus, the driver should have backwards compatibility to older DTs.
> > 
> > Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
> > ---
> > Changes since v1: add some checks for backwards compatibility.
> > ---
> >  drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++---------
> >  1 file changed, 18 insertions(+), 9 deletions(-)
> > 
> > diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
> > index eb6674c..81b7e5e 100644
> > --- a/drivers/pwm/pwm-mediatek.c
> > +++ b/drivers/pwm/pwm-mediatek.c
> > @@ -55,7 +55,7 @@ enum {
> >  };
> >  
> >  struct mtk_pwm_platform_data {
> > -	unsigned int num_pwms;
> > +	unsigned int num_pwms;	/* it should not be used in the future SoCs */
> >  	bool pwm45_fixup;
> >  	bool has_clks;
> >  };
> > @@ -226,27 +226,36 @@ static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
> >  
> >  static int mtk_pwm_probe(struct platform_device *pdev)
> >  {
> > -	const struct mtk_pwm_platform_data *data;
> > +	struct device_node *np = pdev->dev.of_node;
> >  	struct mtk_pwm_chip *pc;
> >  	struct resource *res;
> > -	unsigned int i;
> > +	unsigned int i, num_pwms;
> >  	int ret;
> >  
> >  	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
> >  	if (!pc)
> >  		return -ENOMEM;
> >  
> > -	data = of_device_get_match_data(&pdev->dev);
> > -	if (data == NULL)
> > -		return -EINVAL;
> > -	pc->soc = data;
> > +	pc->soc = of_device_get_match_data(&pdev->dev);
> >  
> >  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> >  	pc->regs = devm_ioremap_resource(&pdev->dev, res);
> >  	if (IS_ERR(pc->regs))
> >  		return PTR_ERR(pc->regs);
> >  
> > -	for (i = 0; i < data->num_pwms + 2 && pc->soc->has_clks; i++) {
> > +	/* Check if we have set 'num-pwms' in DTs. */
> > +	ret = of_property_read_u32(np, "mediatek,num-pwms", &num_pwms);
> > +	if (ret < 0) {
> > +		/* If no, fallback to use SoC data for backwards compatibility. */
> > +		if (pc->soc->num_pwms) {
> > +			num_pwms = pc->soc->num_pwms;
> > +		} else {
> > +			dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
> > +			return ret;
> > +		}
> > +	}
> > +
> > +	for (i = 0; i < num_pwms + 2 && pc->soc->has_clks; i++) {
> >  		pc->clks[i] = devm_clk_get(&pdev->dev, mtk_pwm_clk_name[i]);
> 
> If a dt contains
> 
> 	mediatek,num-pwms = <17>;
> 
> you're accessing pc->clks[18] which is an out-of-bounds access, so
> better check the limit or allocate the clks array dynamically.
> 

Thanks for the reminder. I will fix it in v2.

Ryder
Uwe Kleine-König Jan. 18, 2019, 9:53 a.m. | #6
Hello Ryder,

On Fri, Jan 18, 2019 at 05:42:54PM +0800, Ryder Lee wrote:
> On Fri, 2019-01-18 at 08:59 +0100, Uwe Kleine-König wrote:
> > Hello,
> > 
> > On Fri, Jan 18, 2019 at 11:24:41AM +0800, Ryder Lee wrote:
> > > This adds a property "mediatek,num-pwms" to avoid having an endless
> > > list of compatibles with no differences for the same driver.
> > > 
> > > Thus, the driver should have backwards compatibility to older DTs.
> > 
> > I still think Thierry should bless "num-pwms" without vendor prefix.
> 
> Okay.
> 
> > > Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
> > > ---
> > > Changes since v1: add some checks for backwards compatibility.
> > > ---
> > >  drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++---------
> > >  1 file changed, 18 insertions(+), 9 deletions(-)
> > > 
> > > diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
> > > index eb6674c..81b7e5e 100644
> > > --- a/drivers/pwm/pwm-mediatek.c
> > > +++ b/drivers/pwm/pwm-mediatek.c
> > > @@ -55,7 +55,7 @@ enum {
> > >  };
> > >  
> > >  struct mtk_pwm_platform_data {
> > 
> > Unrelated to this patch: This name is bad. This struct is not used as
> > platform_data and so should better be named mtk_pwm_of_data. While at
> > criticizing existing stuff: I'd prefer pwm_mediatek as common prefix to
> > match the filename.
> 
> I think we can take care about that in another patch.

That's what I wanted to say, right. Do you follow up?

> > > -	unsigned int num_pwms;
> > > +	unsigned int num_pwms;	/* it should not be used in the future SoCs */
> > 
> > I'd drop this comment in favour of a runtime warning.
> 
> Sorry, I can't get you here.

I'd do a

	dev_warn(dev, "dt didn't specify number of PWMs, falling back to %d\n", pc->soc->num_pwms);

to make people aware that updating the dt would be nice.

> 
> > >  	bool pwm45_fixup;
> > >  	bool has_clks;
> > >  };
> > > @@ -226,27 +226,36 @@ static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
> > >  
> > >  static int mtk_pwm_probe(struct platform_device *pdev)
> > >  {
> > > -	const struct mtk_pwm_platform_data *data;
> > > +	struct device_node *np = pdev->dev.of_node;
> > >  	struct mtk_pwm_chip *pc;
> > >  	struct resource *res;
> > > -	unsigned int i;
> > > +	unsigned int i, num_pwms;
> > >  	int ret;
> > >  
> > >  	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
> > >  	if (!pc)
> > >  		return -ENOMEM;
> > >  
> > > -	data = of_device_get_match_data(&pdev->dev);
> > > -	if (data == NULL)
> > > -		return -EINVAL;
> > > -	pc->soc = data;
> > > +	pc->soc = of_device_get_match_data(&pdev->dev);
> > 
> > This might return NULL which ...
> 
> The only way to call probe() is to match an entry in
> mtk_pwm_of_match[], so match cannot be NULL.

(<pedantic>Theoretically the driver can be probed by device name, but
that is not what I meant here.</pedantic>).

You're right, as long as all entries in mtk_pwm_of_match have a non-NULL
.data entry, you're fine. I somehow thought there might be some without
one. I wouldn't oppose to check for that anyhow as a defensive measure.

> > > [...]
> > > +	/* Check if we have set 'num-pwms' in DTs. */
> > > +	ret = of_property_read_u32(np, "mediatek,num-pwms", &num_pwms);
> > > +	if (ret < 0) {
> > > +		/* If no, fallback to use SoC data for backwards compatibility. */
> > > +		if (pc->soc->num_pwms) {
> > 
> > ... here then results in a NULL pointer dereference. I think you want
> 
> So we have no chance to get a NULL pointer, right?

Ack.

Best regards
Uwe
Ryder Lee Jan. 18, 2019, 10:01 a.m. | #7
On Fri, 2019-01-18 at 10:53 +0100, Uwe Kleine-König wrote:
> Hello Ryder,
> 
> On Fri, Jan 18, 2019 at 05:42:54PM +0800, Ryder Lee wrote:
> > On Fri, 2019-01-18 at 08:59 +0100, Uwe Kleine-König wrote:
> > > Hello,
> > > 
> > > On Fri, Jan 18, 2019 at 11:24:41AM +0800, Ryder Lee wrote:
> > > > This adds a property "mediatek,num-pwms" to avoid having an endless
> > > > list of compatibles with no differences for the same driver.
> > > > 
> > > > Thus, the driver should have backwards compatibility to older DTs.
> > > 
> > > I still think Thierry should bless "num-pwms" without vendor prefix.
> > 
> > Okay.
> > 
> > > > Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
> > > > ---
> > > > Changes since v1: add some checks for backwards compatibility.
> > > > ---
> > > >  drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++---------
> > > >  1 file changed, 18 insertions(+), 9 deletions(-)
> > > > 
> > > > diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
> > > > index eb6674c..81b7e5e 100644
> > > > --- a/drivers/pwm/pwm-mediatek.c
> > > > +++ b/drivers/pwm/pwm-mediatek.c
> > > > @@ -55,7 +55,7 @@ enum {
> > > >  };
> > > >  
> > > >  struct mtk_pwm_platform_data {
> > > 
> > > Unrelated to this patch: This name is bad. This struct is not used as
> > > platform_data and so should better be named mtk_pwm_of_data. While at
> > > criticizing existing stuff: I'd prefer pwm_mediatek as common prefix to
> > > match the filename.
> > 
> > I think we can take care about that in another patch.
> 
> That's what I wanted to say, right. Do you follow up?

Yes, I will do that.

> > > > -	unsigned int num_pwms;
> > > > +	unsigned int num_pwms;	/* it should not be used in the future SoCs */
> > > 
> > > I'd drop this comment in favour of a runtime warning.
> > 
> > Sorry, I can't get you here.
> 
> I'd do a
> 
> 	dev_warn(dev, "dt didn't specify number of PWMs, falling back to %d\n", pc->soc->num_pwms);
> 
> to make people aware that updating the dt would be nice.

Okay!

Thanks
Ryder
Ryder Lee Jan. 19, 2019, 2:54 a.m. | #8
On Fri, 2019-01-18 at 09:43 +0100, Matthias Brugger wrote:
> 
> On 18/01/2019 04:24, Ryder Lee wrote:
> > This adds a property "mediatek,num-pwms" to avoid having an endless
> > list of compatibles with no differences for the same driver.
> > 
> > Thus, the driver should have backwards compatibility to older DTs.
> > 
> > Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
> > ---
> > Changes since v1: add some checks for backwards compatibility.
> > ---
> >  drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++---------
> >  1 file changed, 18 insertions(+), 9 deletions(-)
> > 
> > diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
> > index eb6674c..81b7e5e 100644
> > --- a/drivers/pwm/pwm-mediatek.c
> > +++ b/drivers/pwm/pwm-mediatek.c
> > @@ -55,7 +55,7 @@ enum {
> >  };
> >  
> >  struct mtk_pwm_platform_data {
> > -	unsigned int num_pwms;
> > +	unsigned int num_pwms;	/* it should not be used in the future SoCs */
> >  	bool pwm45_fixup;
> >  	bool has_clks;
> >  };
> > @@ -226,27 +226,36 @@ static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
> >  
> >  static int mtk_pwm_probe(struct platform_device *pdev)
> >  {
> > -	const struct mtk_pwm_platform_data *data;
> > +	struct device_node *np = pdev->dev.of_node;
> >  	struct mtk_pwm_chip *pc;
> >  	struct resource *res;
> > -	unsigned int i;
> > +	unsigned int i, num_pwms;
> >  	int ret;
> >  
> >  	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
> >  	if (!pc)
> >  		return -ENOMEM;
> >  
> > -	data = of_device_get_match_data(&pdev->dev);
> > -	if (data == NULL)
> > -		return -EINVAL;
> > -	pc->soc = data;
> > +	pc->soc = of_device_get_match_data(&pdev->dev);
> >  
> >  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> >  	pc->regs = devm_ioremap_resource(&pdev->dev, res);
> >  	if (IS_ERR(pc->regs))
> >  		return PTR_ERR(pc->regs);
> >  
> > -	for (i = 0; i < data->num_pwms + 2 && pc->soc->has_clks; i++) {
> > +	/* Check if we have set 'num-pwms' in DTs. */
> > +	ret = of_property_read_u32(np, "mediatek,num-pwms", &num_pwms);
> > +	if (ret < 0) {
> > +		/* If no, fallback to use SoC data for backwards compatibility. */
> > +		if (pc->soc->num_pwms) {
> > +			num_pwms = pc->soc->num_pwms;
> 
> Maybe that's bike shedding, but I think it would be better to carve out the
> num_pwms from the mtk_pwm_platform_data and check against the compatible here.

I'm not sure how to properly curve it out? I think we still need this
variable to save the specific value for some legacy SoCs (with older
DTs).

> With a expressive comment it will help other driver developers to not start
> adding num_pwms in the platform data in their first attempt.

Definitely.

Ryder
> 
> > +		} else {
> > +			dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
> > +			return ret;
> > +		}
> > +	}
> > +
> > +	for (i = 0; i < num_pwms + 2 && pc->soc->has_clks; i++) {
> >  		pc->clks[i] = devm_clk_get(&pdev->dev, mtk_pwm_clk_name[i]);
> >  		if (IS_ERR(pc->clks[i])) {
> >  			dev_err(&pdev->dev, "clock: %s fail: %ld\n",
> > @@ -260,7 +269,7 @@ static int mtk_pwm_probe(struct platform_device *pdev)
> >  	pc->chip.dev = &pdev->dev;
> >  	pc->chip.ops = &mtk_pwm_ops;
> >  	pc->chip.base = -1;
> > -	pc->chip.npwm = data->num_pwms;
> > +	pc->chip.npwm = num_pwms;
> >  
> >  	ret = pwmchip_add(&pc->chip);
> >  	if (ret < 0) {
> >
Uwe Kleine-König Jan. 21, 2019, 8:49 a.m. | #9
On Sat, Jan 19, 2019 at 10:54:47AM +0800, Ryder Lee wrote:
> On Fri, 2019-01-18 at 09:43 +0100, Matthias Brugger wrote:
> > 
> > On 18/01/2019 04:24, Ryder Lee wrote:
> > > This adds a property "mediatek,num-pwms" to avoid having an endless
> > > list of compatibles with no differences for the same driver.
> > > 
> > > Thus, the driver should have backwards compatibility to older DTs.
> > > 
> > > Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
> > > ---
> > > Changes since v1: add some checks for backwards compatibility.
> > > ---
> > >  drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++---------
> > >  1 file changed, 18 insertions(+), 9 deletions(-)
> > > 
> > > diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
> > > index eb6674c..81b7e5e 100644
> > > --- a/drivers/pwm/pwm-mediatek.c
> > > +++ b/drivers/pwm/pwm-mediatek.c
> > > @@ -55,7 +55,7 @@ enum {
> > >  };
> > >  
> > >  struct mtk_pwm_platform_data {
> > > -	unsigned int num_pwms;
> > > +	unsigned int num_pwms;	/* it should not be used in the future SoCs */
> > >  	bool pwm45_fixup;
> > >  	bool has_clks;
> > >  };
> > > @@ -226,27 +226,36 @@ static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
> > >  
> > >  static int mtk_pwm_probe(struct platform_device *pdev)
> > >  {
> > > -	const struct mtk_pwm_platform_data *data;
> > > +	struct device_node *np = pdev->dev.of_node;
> > >  	struct mtk_pwm_chip *pc;
> > >  	struct resource *res;
> > > -	unsigned int i;
> > > +	unsigned int i, num_pwms;
> > >  	int ret;
> > >  
> > >  	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
> > >  	if (!pc)
> > >  		return -ENOMEM;
> > >  
> > > -	data = of_device_get_match_data(&pdev->dev);
> > > -	if (data == NULL)
> > > -		return -EINVAL;
> > > -	pc->soc = data;
> > > +	pc->soc = of_device_get_match_data(&pdev->dev);
> > >  
> > >  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> > >  	pc->regs = devm_ioremap_resource(&pdev->dev, res);
> > >  	if (IS_ERR(pc->regs))
> > >  		return PTR_ERR(pc->regs);
> > >  
> > > -	for (i = 0; i < data->num_pwms + 2 && pc->soc->has_clks; i++) {
> > > +	/* Check if we have set 'num-pwms' in DTs. */
> > > +	ret = of_property_read_u32(np, "mediatek,num-pwms", &num_pwms);
> > > +	if (ret < 0) {
> > > +		/* If no, fallback to use SoC data for backwards compatibility. */
> > > +		if (pc->soc->num_pwms) {
> > > +			num_pwms = pc->soc->num_pwms;
> > 
> > Maybe that's bike shedding, but I think it would be better to carve out the
> > num_pwms from the mtk_pwm_platform_data and check against the compatible here.
> 
> I'm not sure how to properly curve it out? I think we still need this
> variable to save the specific value for some legacy SoCs (with older
> DTs).

I guess he means  something like:

	if (is_compatible_to_variant_A(dev))
		num_pwms = 12;
	else if (is_compatible_to_variant_B(dev))
		num_pwms = 2;

. In my eyes the bike shed should be light red and I prefer to collect
the fallback num_pwms in the compatible_data as is to keep the code
simpler. Maybe rename the member from num_pwms to fallback_num_pwms to
make it more obvious that it doesn't represent the actually used value.

> > With a expressive comment it will help other driver developers to not start
> > adding num_pwms in the platform data in their first attempt.
> 
> Definitely.

My suggestion was to add a dev_warn, which IMHO is still better than a
comment.

Best regards
Uwe
Ryder Lee Jan. 25, 2019, 3:48 a.m. | #10
+John

HI John,

On Mon, 2019-01-21 at 16:49 +0800, Uwe Kleine-König wrote:
> On Sat, Jan 19, 2019 at 10:54:47AM +0800, Ryder Lee wrote:
> > On Fri, 2019-01-18 at 09:43 +0100, Matthias Brugger wrote:
> > >
> > > On 18/01/2019 04:24, Ryder Lee wrote:
> > > > This adds a property "mediatek,num-pwms" to avoid having an endless
> > > > list of compatibles with no differences for the same driver.
> > > >
> > > > Thus, the driver should have backwards compatibility to older DTs.
> > > >
> > > > Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
> > > > ---
> > > > Changes since v1: add some checks for backwards compatibility.
> > > > ---
> > > >  drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++---------
> > > >  1 file changed, 18 insertions(+), 9 deletions(-)
> > > >
> > > > diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
> > > > index eb6674c..81b7e5e 100644
> > > > --- a/drivers/pwm/pwm-mediatek.c
> > > > +++ b/drivers/pwm/pwm-mediatek.c
> > > > @@ -55,7 +55,7 @@ enum {
> > > >  };
> > > >
> > > >  struct mtk_pwm_platform_data {
> > > > - unsigned int num_pwms;
> > > > + unsigned int num_pwms;  /* it should not be used in the future SoCs */
> > > >   bool pwm45_fixup;
> > > >   bool has_clks;
> > > >  };
> > > > @@ -226,27 +226,36 @@ static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
> > > >
> > > >  static int mtk_pwm_probe(struct platform_device *pdev)
> > > >  {
> > > > - const struct mtk_pwm_platform_data *data;
> > > > + struct device_node *np = pdev->dev.of_node;
> > > >   struct mtk_pwm_chip *pc;
> > > >   struct resource *res;
> > > > - unsigned int i;
> > > > + unsigned int i, num_pwms;
> > > >   int ret;
> > > >
> > > >   pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
> > > >   if (!pc)
> > > >           return -ENOMEM;
> > > >
> > > > - data = of_device_get_match_data(&pdev->dev);
> > > > - if (data == NULL)
> > > > -         return -EINVAL;
> > > > - pc->soc = data;
> > > > + pc->soc = of_device_get_match_data(&pdev->dev);
> > > >
> > > >   res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> > > >   pc->regs = devm_ioremap_resource(&pdev->dev, res);
> > > >   if (IS_ERR(pc->regs))
> > > >           return PTR_ERR(pc->regs);
> > > >
> > > > - for (i = 0; i < data->num_pwms + 2 && pc->soc->has_clks; i++) {
> > > > + /* Check if we have set 'num-pwms' in DTs. */
> > > > + ret = of_property_read_u32(np, "mediatek,num-pwms", &num_pwms);
> > > > + if (ret < 0) {
> > > > +         /* If no, fallback to use SoC data for backwards compatibility. */
> > > > +         if (pc->soc->num_pwms) {
> > > > +                 num_pwms = pc->soc->num_pwms;
> > >
> > > Maybe that's bike shedding, but I think it would be better to carve out the
> > > num_pwms from the mtk_pwm_platform_data and check against the compatible here.
> >
> > I'm not sure how to properly curve it out? I think we still need this
> > variable to save the specific value for some legacy SoCs (with older
> > DTs).
> 
> I guess he means  something like:
> 
>         if (is_compatible_to_variant_A(dev))
>                 num_pwms = 12;
>         else if (is_compatible_to_variant_B(dev))
>                 num_pwms = 2;
> 
> . In my eyes the bike shed should be light red and I prefer to collect
> the fallback num_pwms in the compatible_data as is to keep the code
> simpler. Maybe rename the member from num_pwms to fallback_num_pwms to
> make it more obvious that it doesn't represent the actually used value.
> 
> > > With a expressive comment it will help other driver developers to not start
> > > adding num_pwms in the platform data in their first attempt.
> >
> > Definitely.
> 
> My suggestion was to add a dev_warn, which IMHO is still better than a
> comment.
> 
> Best regards
> Uwe
> 

Unrelated to this patch: I'm ready to send v2 to allocate the clks array
dynamically, but I guess MT7628 couldn't work in original code.


In mtk_pwm_config():

	clk = pc->clks[MTK_CLK_PWM1 + pwm->hwpwm]; 
	....
	resolution = (u64)NSEC_PER_SEC * 1000;
	do_div(resolution, clk_get_rate(clk));
	....

I think clk should be NULL and resolution is always 0 here. 


Ryder
Ryder Lee Jan. 25, 2019, 3:52 a.m. | #11
+John

HI John,

On Mon, 2019-01-21 at 16:49 +0800, Uwe Kleine-König wrote:
> On Sat, Jan 19, 2019 at 10:54:47AM +0800, Ryder Lee wrote:
> > On Fri, 2019-01-18 at 09:43 +0100, Matthias Brugger wrote:
> > >
> > > On 18/01/2019 04:24, Ryder Lee wrote:
> > > > This adds a property "mediatek,num-pwms" to avoid having an endless
> > > > list of compatibles with no differences for the same driver.
> > > >
> > > > Thus, the driver should have backwards compatibility to older DTs.
> > > >
> > > > Signed-off-by: Ryder Lee <ryder.lee@mediatek.com>
> > > > ---
> > > > Changes since v1: add some checks for backwards compatibility.
> > > > ---
> > > >  drivers/pwm/pwm-mediatek.c | 27 ++++++++++++++++++---------
> > > >  1 file changed, 18 insertions(+), 9 deletions(-)
> > > >
> > > > diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
> > > > index eb6674c..81b7e5e 100644
> > > > --- a/drivers/pwm/pwm-mediatek.c
> > > > +++ b/drivers/pwm/pwm-mediatek.c
> > > > @@ -55,7 +55,7 @@ enum {
> > > >  };
> > > >
> > > >  struct mtk_pwm_platform_data {
> > > > - unsigned int num_pwms;
> > > > + unsigned int num_pwms;  /* it should not be used in the future SoCs */
> > > >   bool pwm45_fixup;
> > > >   bool has_clks;
> > > >  };
> > > > @@ -226,27 +226,36 @@ static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
> > > >
> > > >  static int mtk_pwm_probe(struct platform_device *pdev)
> > > >  {
> > > > - const struct mtk_pwm_platform_data *data;
> > > > + struct device_node *np = pdev->dev.of_node;
> > > >   struct mtk_pwm_chip *pc;
> > > >   struct resource *res;
> > > > - unsigned int i;
> > > > + unsigned int i, num_pwms;
> > > >   int ret;
> > > >
> > > >   pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
> > > >   if (!pc)
> > > >           return -ENOMEM;
> > > >
> > > > - data = of_device_get_match_data(&pdev->dev);
> > > > - if (data == NULL)
> > > > -         return -EINVAL;
> > > > - pc->soc = data;
> > > > + pc->soc = of_device_get_match_data(&pdev->dev);
> > > >
> > > >   res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> > > >   pc->regs = devm_ioremap_resource(&pdev->dev, res);
> > > >   if (IS_ERR(pc->regs))
> > > >           return PTR_ERR(pc->regs);
> > > >
> > > > - for (i = 0; i < data->num_pwms + 2 && pc->soc->has_clks; i++) {
> > > > + /* Check if we have set 'num-pwms' in DTs. */
> > > > + ret = of_property_read_u32(np, "mediatek,num-pwms", &num_pwms);
> > > > + if (ret < 0) {
> > > > +         /* If no, fallback to use SoC data for backwards compatibility. */
> > > > +         if (pc->soc->num_pwms) {
> > > > +                 num_pwms = pc->soc->num_pwms;
> > >
> > > Maybe that's bike shedding, but I think it would be better to carve out the
> > > num_pwms from the mtk_pwm_platform_data and check against the compatible here.
> >
> > I'm not sure how to properly curve it out? I think we still need this
> > variable to save the specific value for some legacy SoCs (with older
> > DTs).
> 
> I guess he means  something like:
> 
>         if (is_compatible_to_variant_A(dev))
>                 num_pwms = 12;
>         else if (is_compatible_to_variant_B(dev))
>                 num_pwms = 2;
> 
> . In my eyes the bike shed should be light red and I prefer to collect
> the fallback num_pwms in the compatible_data as is to keep the code
> simpler. Maybe rename the member from num_pwms to fallback_num_pwms to
> make it more obvious that it doesn't represent the actually used value.
> 
> > > With a expressive comment it will help other driver developers to not start
> > > adding num_pwms in the platform data in their first attempt.
> >
> > Definitely.
> 
> My suggestion was to add a dev_warn, which IMHO is still better than a
> comment.
> 
> Best regards
> Uwe
> 

Unrelated to this patch: I'm ready to send v2 to allocate the clks array
dynamically, but I guess MT7628 couldn't work in original code.


In mtk_pwm_config():

        clk = pc->clks[MTK_CLK_PWM1 + pwm->hwpwm]; 
        ....
        resolution = (u64)NSEC_PER_SEC * 1000;
        do_div(resolution, clk_get_rate(clk));
        ....

I think clk should be NULL and resolution is always 0 here. 


Ryder

Patch

diff --git a/drivers/pwm/pwm-mediatek.c b/drivers/pwm/pwm-mediatek.c
index eb6674c..81b7e5e 100644
--- a/drivers/pwm/pwm-mediatek.c
+++ b/drivers/pwm/pwm-mediatek.c
@@ -55,7 +55,7 @@  enum {
 };
 
 struct mtk_pwm_platform_data {
-	unsigned int num_pwms;
+	unsigned int num_pwms;	/* it should not be used in the future SoCs */
 	bool pwm45_fixup;
 	bool has_clks;
 };
@@ -226,27 +226,36 @@  static void mtk_pwm_disable(struct pwm_chip *chip, struct pwm_device *pwm)
 
 static int mtk_pwm_probe(struct platform_device *pdev)
 {
-	const struct mtk_pwm_platform_data *data;
+	struct device_node *np = pdev->dev.of_node;
 	struct mtk_pwm_chip *pc;
 	struct resource *res;
-	unsigned int i;
+	unsigned int i, num_pwms;
 	int ret;
 
 	pc = devm_kzalloc(&pdev->dev, sizeof(*pc), GFP_KERNEL);
 	if (!pc)
 		return -ENOMEM;
 
-	data = of_device_get_match_data(&pdev->dev);
-	if (data == NULL)
-		return -EINVAL;
-	pc->soc = data;
+	pc->soc = of_device_get_match_data(&pdev->dev);
 
 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 	pc->regs = devm_ioremap_resource(&pdev->dev, res);
 	if (IS_ERR(pc->regs))
 		return PTR_ERR(pc->regs);
 
-	for (i = 0; i < data->num_pwms + 2 && pc->soc->has_clks; i++) {
+	/* Check if we have set 'num-pwms' in DTs. */
+	ret = of_property_read_u32(np, "mediatek,num-pwms", &num_pwms);
+	if (ret < 0) {
+		/* If no, fallback to use SoC data for backwards compatibility. */
+		if (pc->soc->num_pwms) {
+			num_pwms = pc->soc->num_pwms;
+		} else {
+			dev_err(&pdev->dev, "failed to get pwm number: %d\n", ret);
+			return ret;
+		}
+	}
+
+	for (i = 0; i < num_pwms + 2 && pc->soc->has_clks; i++) {
 		pc->clks[i] = devm_clk_get(&pdev->dev, mtk_pwm_clk_name[i]);
 		if (IS_ERR(pc->clks[i])) {
 			dev_err(&pdev->dev, "clock: %s fail: %ld\n",
@@ -260,7 +269,7 @@  static int mtk_pwm_probe(struct platform_device *pdev)
 	pc->chip.dev = &pdev->dev;
 	pc->chip.ops = &mtk_pwm_ops;
 	pc->chip.base = -1;
-	pc->chip.npwm = data->num_pwms;
+	pc->chip.npwm = num_pwms;
 
 	ret = pwmchip_add(&pc->chip);
 	if (ret < 0) {