diff mbox series

[V2] soc: imx8m: Add remove function

Message ID 20241217015826.1374497-1-peng.fan@oss.nxp.com (mailing list archive)
State New
Headers show
Series [V2] soc: imx8m: Add remove function | expand

Commit Message

Peng Fan (OSS) Dec. 17, 2024, 1:58 a.m. UTC
From: Peng Fan <peng.fan@nxp.com>

Unregister the cpufreq device and soc device in remove path, otherwise
there will be warning when do removing test:
sysfs: cannot create duplicate filename '/devices/platform/imx-cpufreq-dt'
CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.13.0-rc1-next-20241204
Hardware name: NXP i.MX8MPlus EVK board (DT)

Fixes: 9cc832d37799 ("soc: imx8m: Probe the SoC driver as platform driver")
Signed-off-by: Peng Fan <peng.fan@nxp.com>
---

V2:
 Add err check when create the cpufreq platform device

 drivers/soc/imx/soc-imx8m.c | 41 +++++++++++++++++++++++++++++++------
 1 file changed, 35 insertions(+), 6 deletions(-)

Comments

Marco Felsch Dec. 17, 2024, 8:26 a.m. UTC | #1
Hi Peng,

thanks for you patch.

On 24-12-17, Peng Fan (OSS) wrote:
> From: Peng Fan <peng.fan@nxp.com>
> 
> Unregister the cpufreq device and soc device in remove path, otherwise

After reconsidiering the patch approach, I think we shouldn't add a
.remove() function and instead should make use of the devm_add_action()
mechanism for the proper unregister calls.

> there will be warning when do removing test:
> sysfs: cannot create duplicate filename '/devices/platform/imx-cpufreq-dt'
> CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.13.0-rc1-next-20241204
> Hardware name: NXP i.MX8MPlus EVK board (DT)
> 
> Fixes: 9cc832d37799 ("soc: imx8m: Probe the SoC driver as platform driver")
> Signed-off-by: Peng Fan <peng.fan@nxp.com>
> ---
> 
> V2:
>  Add err check when create the cpufreq platform device

thank you for addressing this.

Regards,
  Marco

>  drivers/soc/imx/soc-imx8m.c | 41 +++++++++++++++++++++++++++++++------
>  1 file changed, 35 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/soc/imx/soc-imx8m.c b/drivers/soc/imx/soc-imx8m.c
> index 8ac7658e3d52..97c8718c2aa1 100644
> --- a/drivers/soc/imx/soc-imx8m.c
> +++ b/drivers/soc/imx/soc-imx8m.c
> @@ -33,6 +33,11 @@ struct imx8_soc_data {
>  	int (*soc_revision)(u32 *socrev, u64 *socuid);
>  };
>  
> +struct imx8m_soc_priv {
> +	struct soc_device *soc_dev;
> +	struct platform_device *cpufreq_dev;
> +};
> +
>  #ifdef CONFIG_HAVE_ARM_SMCCC
>  static u32 imx8mq_soc_revision_from_atf(void)
>  {
> @@ -195,10 +200,11 @@ static __maybe_unused const struct of_device_id imx8_soc_match[] = {
>  static int imx8m_soc_probe(struct platform_device *pdev)
>  {
>  	struct soc_device_attribute *soc_dev_attr;
> +	struct platform_device *cpufreq_dev;
>  	const struct imx8_soc_data *data;
>  	struct device *dev = &pdev->dev;
>  	const struct of_device_id *id;
> -	struct soc_device *soc_dev;
> +	struct imx8m_soc_priv *priv;
>  	u32 soc_rev = 0;
>  	u64 soc_uid = 0;
>  	int ret;
> @@ -207,6 +213,10 @@ static int imx8m_soc_probe(struct platform_device *pdev)
>  	if (!soc_dev_attr)
>  		return -ENOMEM;
>  
> +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> +	if (!priv)
> +		return -ENOMEM;
> +
>  	soc_dev_attr->family = "Freescale i.MX";
>  
>  	ret = of_property_read_string(of_root, "model", &soc_dev_attr->machine);
> @@ -235,21 +245,40 @@ static int imx8m_soc_probe(struct platform_device *pdev)
>  	if (!soc_dev_attr->serial_number)
>  		return -ENOMEM;
>  
> -	soc_dev = soc_device_register(soc_dev_attr);
> -	if (IS_ERR(soc_dev))
> -		return PTR_ERR(soc_dev);
> +	priv->soc_dev = soc_device_register(soc_dev_attr);
> +	if (IS_ERR(priv->soc_dev))
> +		return PTR_ERR(priv->soc_dev);
>  
>  	pr_info("SoC: %s revision %s\n", soc_dev_attr->soc_id,
>  		soc_dev_attr->revision);
>  
> -	if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT))
> -		platform_device_register_simple("imx-cpufreq-dt", -1, NULL, 0);
> +	if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT)) {
> +		cpufreq_dev = platform_device_register_simple("imx-cpufreq-dt", -1, NULL, 0);
> +		if (!IS_ERR(cpufreq_dev))
> +			priv->cpufreq_dev = cpufreq_dev;
> +		else
> +			dev_err(dev, "Failed to create imx-cpufreq-dev device: %ld",
> +				PTR_ERR(cpufreq_dev));
> +	}
> +
> +	platform_set_drvdata(pdev, priv);
>  
>  	return 0;
>  }
>  
> +static void imx8m_soc_remove(struct platform_device *pdev)
> +{
> +	struct imx8m_soc_priv *priv = platform_get_drvdata(pdev);
> +
> +	if (priv->cpufreq_dev)
> +		platform_device_unregister(priv->cpufreq_dev);
> +
> +	soc_device_unregister(priv->soc_dev);
> +}
> +
>  static struct platform_driver imx8m_soc_driver = {
>  	.probe = imx8m_soc_probe,
> +	.remove	= imx8m_soc_remove,
>  	.driver = {
>  		.name = "imx8m-soc",
>  	},
> -- 
> 2.37.1
> 
> 
>
Peng Fan Dec. 17, 2024, 9:16 a.m. UTC | #2
> Subject: Re: [PATCH V2] soc: imx8m: Add remove function
> 
> Hi Peng,
> 
> thanks for you patch.
> 
> On 24-12-17, Peng Fan (OSS) wrote:
> > From: Peng Fan <peng.fan@nxp.com>
> >
> > Unregister the cpufreq device and soc device in remove path,
> otherwise
> 
> After reconsidiering the patch approach, I think we shouldn't add a
> .remove() function and instead should make use of the
> devm_add_action() mechanism for the proper unregister calls.

Would you please share why devm_add_action is preferred?

Something as below?
+static void imx8m_soc_remove(void *data)
+{
+       struct imx8m_soc_priv *priv = data;
+
+       if (priv->cpufreq_dev)
+               platform_device_unregister(priv->cpufreq_dev);
+
+       soc_device_unregister(priv->soc_dev);
+}
+
In Probe:
+       return devm_add_action(&pdev->dev, imx8m_soc_remove, priv);

Regards,
Peng.

> 
> > there will be warning when do removing test:
> > sysfs: cannot create duplicate filename '/devices/platform/imx-
> cpufreq-dt'
> > CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted
> > 6.13.0-rc1-next-20241204 Hardware name: NXP i.MX8MPlus EVK
> board (DT)
> >
> > Fixes: 9cc832d37799 ("soc: imx8m: Probe the SoC driver as platform
> > driver")
> > Signed-off-by: Peng Fan <peng.fan@nxp.com>
> > ---
> >
> > V2:
> >  Add err check when create the cpufreq platform device
> 
> thank you for addressing this.
> 
> Regards,
>   Marco
> 
> >  drivers/soc/imx/soc-imx8m.c | 41
> > +++++++++++++++++++++++++++++++------
> >  1 file changed, 35 insertions(+), 6 deletions(-)
> >
> > diff --git a/drivers/soc/imx/soc-imx8m.c b/drivers/soc/imx/soc-
> imx8m.c
> > index 8ac7658e3d52..97c8718c2aa1 100644
> > --- a/drivers/soc/imx/soc-imx8m.c
> > +++ b/drivers/soc/imx/soc-imx8m.c
> > @@ -33,6 +33,11 @@ struct imx8_soc_data {
> >  	int (*soc_revision)(u32 *socrev, u64 *socuid);  };
> >
> > +struct imx8m_soc_priv {
> > +	struct soc_device *soc_dev;
> > +	struct platform_device *cpufreq_dev; };
> > +
> >  #ifdef CONFIG_HAVE_ARM_SMCCC
> >  static u32 imx8mq_soc_revision_from_atf(void)
> >  {
> > @@ -195,10 +200,11 @@ static __maybe_unused const struct
> of_device_id
> > imx8_soc_match[] = {  static int imx8m_soc_probe(struct
> > platform_device *pdev)  {
> >  	struct soc_device_attribute *soc_dev_attr;
> > +	struct platform_device *cpufreq_dev;
> >  	const struct imx8_soc_data *data;
> >  	struct device *dev = &pdev->dev;
> >  	const struct of_device_id *id;
> > -	struct soc_device *soc_dev;
> > +	struct imx8m_soc_priv *priv;
> >  	u32 soc_rev = 0;
> >  	u64 soc_uid = 0;
> >  	int ret;
> > @@ -207,6 +213,10 @@ static int imx8m_soc_probe(struct
> platform_device *pdev)
> >  	if (!soc_dev_attr)
> >  		return -ENOMEM;
> >
> > +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> > +	if (!priv)
> > +		return -ENOMEM;
> > +
> >  	soc_dev_attr->family = "Freescale i.MX";
> >
> >  	ret = of_property_read_string(of_root, "model",
> > &soc_dev_attr->machine); @@ -235,21 +245,40 @@ static int
> imx8m_soc_probe(struct platform_device *pdev)
> >  	if (!soc_dev_attr->serial_number)
> >  		return -ENOMEM;
> >
> > -	soc_dev = soc_device_register(soc_dev_attr);
> > -	if (IS_ERR(soc_dev))
> > -		return PTR_ERR(soc_dev);
> > +	priv->soc_dev = soc_device_register(soc_dev_attr);
> > +	if (IS_ERR(priv->soc_dev))
> > +		return PTR_ERR(priv->soc_dev);
> >
> >  	pr_info("SoC: %s revision %s\n", soc_dev_attr->soc_id,
> >  		soc_dev_attr->revision);
> >
> > -	if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT))
> > -		platform_device_register_simple("imx-cpufreq-dt", -1,
> NULL, 0);
> > +	if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT)) {
> > +		cpufreq_dev = platform_device_register_simple("imx-
> cpufreq-dt", -1, NULL, 0);
> > +		if (!IS_ERR(cpufreq_dev))
> > +			priv->cpufreq_dev = cpufreq_dev;
> > +		else
> > +			dev_err(dev, "Failed to create imx-cpufreq-
> dev device: %ld",
> > +				PTR_ERR(cpufreq_dev));
> > +	}
> > +
> > +	platform_set_drvdata(pdev, priv);
> >
> >  	return 0;
> >  }
> >
> > +static void imx8m_soc_remove(struct platform_device *pdev) {
> > +	struct imx8m_soc_priv *priv = platform_get_drvdata(pdev);
> > +
> > +	if (priv->cpufreq_dev)
> > +		platform_device_unregister(priv->cpufreq_dev);
> > +
> > +	soc_device_unregister(priv->soc_dev);
> > +}
> > +
> >  static struct platform_driver imx8m_soc_driver = {
> >  	.probe = imx8m_soc_probe,
> > +	.remove	= imx8m_soc_remove,
> >  	.driver = {
> >  		.name = "imx8m-soc",
> >  	},
> > --
> > 2.37.1
> >
> >
> >
Marco Felsch Dec. 17, 2024, 10:46 a.m. UTC | #3
On 24-12-17, Peng Fan wrote:
> > Subject: Re: [PATCH V2] soc: imx8m: Add remove function
> > 
> > Hi Peng,
> > 
> > thanks for you patch.
> > 
> > On 24-12-17, Peng Fan (OSS) wrote:
> > > From: Peng Fan <peng.fan@nxp.com>
> > >
> > > Unregister the cpufreq device and soc device in remove path,
> > otherwise
> > 
> > After reconsidiering the patch approach, I think we shouldn't add a
> > .remove() function and instead should make use of the
> > devm_add_action() mechanism for the proper unregister calls.
> 
> Would you please share why devm_add_action is preferred?
> 
> Something as below?
> +static void imx8m_soc_remove(void *data)
> +{
> +       struct imx8m_soc_priv *priv = data;
> +
> +       if (priv->cpufreq_dev)
> +               platform_device_unregister(priv->cpufreq_dev);
> +
> +       soc_device_unregister(priv->soc_dev);
> +}

I would split it into two action functions due to the following reasons:
 - cleanup during the probe() if something fails afterwards
 - no need for the if(priv->cpufreq_dev) check.

> In Probe:
> +       return devm_add_action(&pdev->dev, imx8m_soc_remove, priv);

The actions should be added directly after the succeful device
registration, e.g.
 - after soc_device_register(soc_dev_attr) and
 - after platform_device_register_simple()

Regards,
   Marco

> 
> Regards,
> Peng.
> 
> > 
> > > there will be warning when do removing test:
> > > sysfs: cannot create duplicate filename '/devices/platform/imx-
> > cpufreq-dt'
> > > CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted
> > > 6.13.0-rc1-next-20241204 Hardware name: NXP i.MX8MPlus EVK
> > board (DT)
> > >
> > > Fixes: 9cc832d37799 ("soc: imx8m: Probe the SoC driver as platform
> > > driver")
> > > Signed-off-by: Peng Fan <peng.fan@nxp.com>
> > > ---
> > >
> > > V2:
> > >  Add err check when create the cpufreq platform device
> > 
> > thank you for addressing this.
> > 
> > Regards,
> >   Marco
> > 
> > >  drivers/soc/imx/soc-imx8m.c | 41
> > > +++++++++++++++++++++++++++++++------
> > >  1 file changed, 35 insertions(+), 6 deletions(-)
> > >
> > > diff --git a/drivers/soc/imx/soc-imx8m.c b/drivers/soc/imx/soc-
> > imx8m.c
> > > index 8ac7658e3d52..97c8718c2aa1 100644
> > > --- a/drivers/soc/imx/soc-imx8m.c
> > > +++ b/drivers/soc/imx/soc-imx8m.c
> > > @@ -33,6 +33,11 @@ struct imx8_soc_data {
> > >  	int (*soc_revision)(u32 *socrev, u64 *socuid);  };
> > >
> > > +struct imx8m_soc_priv {
> > > +	struct soc_device *soc_dev;
> > > +	struct platform_device *cpufreq_dev; };
> > > +
> > >  #ifdef CONFIG_HAVE_ARM_SMCCC
> > >  static u32 imx8mq_soc_revision_from_atf(void)
> > >  {
> > > @@ -195,10 +200,11 @@ static __maybe_unused const struct
> > of_device_id
> > > imx8_soc_match[] = {  static int imx8m_soc_probe(struct
> > > platform_device *pdev)  {
> > >  	struct soc_device_attribute *soc_dev_attr;
> > > +	struct platform_device *cpufreq_dev;
> > >  	const struct imx8_soc_data *data;
> > >  	struct device *dev = &pdev->dev;
> > >  	const struct of_device_id *id;
> > > -	struct soc_device *soc_dev;
> > > +	struct imx8m_soc_priv *priv;
> > >  	u32 soc_rev = 0;
> > >  	u64 soc_uid = 0;
> > >  	int ret;
> > > @@ -207,6 +213,10 @@ static int imx8m_soc_probe(struct
> > platform_device *pdev)
> > >  	if (!soc_dev_attr)
> > >  		return -ENOMEM;
> > >
> > > +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> > > +	if (!priv)
> > > +		return -ENOMEM;
> > > +
> > >  	soc_dev_attr->family = "Freescale i.MX";
> > >
> > >  	ret = of_property_read_string(of_root, "model",
> > > &soc_dev_attr->machine); @@ -235,21 +245,40 @@ static int
> > imx8m_soc_probe(struct platform_device *pdev)
> > >  	if (!soc_dev_attr->serial_number)
> > >  		return -ENOMEM;
> > >
> > > -	soc_dev = soc_device_register(soc_dev_attr);
> > > -	if (IS_ERR(soc_dev))
> > > -		return PTR_ERR(soc_dev);
> > > +	priv->soc_dev = soc_device_register(soc_dev_attr);
> > > +	if (IS_ERR(priv->soc_dev))
> > > +		return PTR_ERR(priv->soc_dev);
> > >
> > >  	pr_info("SoC: %s revision %s\n", soc_dev_attr->soc_id,
> > >  		soc_dev_attr->revision);
> > >
> > > -	if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT))
> > > -		platform_device_register_simple("imx-cpufreq-dt", -1,
> > NULL, 0);
> > > +	if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT)) {
> > > +		cpufreq_dev = platform_device_register_simple("imx-
> > cpufreq-dt", -1, NULL, 0);
> > > +		if (!IS_ERR(cpufreq_dev))
> > > +			priv->cpufreq_dev = cpufreq_dev;
> > > +		else
> > > +			dev_err(dev, "Failed to create imx-cpufreq-
> > dev device: %ld",
> > > +				PTR_ERR(cpufreq_dev));
> > > +	}
> > > +
> > > +	platform_set_drvdata(pdev, priv);
> > >
> > >  	return 0;
> > >  }
> > >
> > > +static void imx8m_soc_remove(struct platform_device *pdev) {
> > > +	struct imx8m_soc_priv *priv = platform_get_drvdata(pdev);
> > > +
> > > +	if (priv->cpufreq_dev)
> > > +		platform_device_unregister(priv->cpufreq_dev);
> > > +
> > > +	soc_device_unregister(priv->soc_dev);
> > > +}
> > > +
> > >  static struct platform_driver imx8m_soc_driver = {
> > >  	.probe = imx8m_soc_probe,
> > > +	.remove	= imx8m_soc_remove,
> > >  	.driver = {
> > >  		.name = "imx8m-soc",
> > >  	},
> > > --
> > > 2.37.1
> > >
> > >
> > >
>
Peng Fan Dec. 17, 2024, 12:48 p.m. UTC | #4
> Subject: Re: [PATCH V2] soc: imx8m: Add remove function
> 
> On 24-12-17, Peng Fan wrote:
> > > Subject: Re: [PATCH V2] soc: imx8m: Add remove function
> > >
> > > Hi Peng,
> > >
> > > thanks for you patch.
> > >
> > > On 24-12-17, Peng Fan (OSS) wrote:
> > > > From: Peng Fan <peng.fan@nxp.com>
> > > >
> > > > Unregister the cpufreq device and soc device in remove path,
> > > otherwise
> > >
> > > After reconsidiering the patch approach, I think we shouldn't add a
> > > .remove() function and instead should make use of the
> > > devm_add_action() mechanism for the proper unregister calls.
> >
> > Would you please share why devm_add_action is preferred?
> >
> > Something as below?
> > +static void imx8m_soc_remove(void *data) {
> > +       struct imx8m_soc_priv *priv = data;
> > +
> > +       if (priv->cpufreq_dev)
> > +               platform_device_unregister(priv->cpufreq_dev);
> > +
> > +       soc_device_unregister(priv->soc_dev);
> > +}
> 
> I would split it into two action functions due to the following reasons:
>  - cleanup during the probe() if something fails afterwards
>  - no need for the if(priv->cpufreq_dev) check.
> 
> > In Probe:
> > +       return devm_add_action(&pdev->dev, imx8m_soc_remove,
> priv);
> 
> The actions should be added directly after the succeful device
> registration, e.g.
>  - after soc_device_register(soc_dev_attr) and
>  - after platform_device_register_simple()

With two devm_add_action, I thought below code would work,
devm_add_action(dev, soc_device_unregister, (void *)soc_dev);
devm_add_action(dev, platform_device_unregister, (void *)cpufreq_dev);

But devm_add_action expects: void (*action)(void *),
So I need introduce two functions to wrap soc_device_unregister,
and platform_device_unregister.

This seems not good. Using one devm_add_action just need
one wrap function, comments?

Thanks,
Peng.

> 
> Regards,
>    Marco
> 
> >
> > Regards,
> > Peng.
> >
> > >
> > > > there will be warning when do removing test:
> > > > sysfs: cannot create duplicate filename '/devices/platform/imx-
> > > cpufreq-dt'
> > > > CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted
> > > > 6.13.0-rc1-next-20241204 Hardware name: NXP i.MX8MPlus EVK
> > > board (DT)
> > > >
> > > > Fixes: 9cc832d37799 ("soc: imx8m: Probe the SoC driver as
> platform
> > > > driver")
> > > > Signed-off-by: Peng Fan <peng.fan@nxp.com>
> > > > ---
> > > >
> > > > V2:
> > > >  Add err check when create the cpufreq platform device
> > >
> > > thank you for addressing this.
> > >
> > > Regards,
> > >   Marco
> > >
> > > >  drivers/soc/imx/soc-imx8m.c | 41
> > > > +++++++++++++++++++++++++++++++------
> > > >  1 file changed, 35 insertions(+), 6 deletions(-)
> > > >
> > > > diff --git a/drivers/soc/imx/soc-imx8m.c b/drivers/soc/imx/soc-
> > > imx8m.c
> > > > index 8ac7658e3d52..97c8718c2aa1 100644
> > > > --- a/drivers/soc/imx/soc-imx8m.c
> > > > +++ b/drivers/soc/imx/soc-imx8m.c
> > > > @@ -33,6 +33,11 @@ struct imx8_soc_data {
> > > >  	int (*soc_revision)(u32 *socrev, u64 *socuid);  };
> > > >
> > > > +struct imx8m_soc_priv {
> > > > +	struct soc_device *soc_dev;
> > > > +	struct platform_device *cpufreq_dev; };
> > > > +
> > > >  #ifdef CONFIG_HAVE_ARM_SMCCC
> > > >  static u32 imx8mq_soc_revision_from_atf(void)
> > > >  {
> > > > @@ -195,10 +200,11 @@ static __maybe_unused const struct
> > > of_device_id
> > > > imx8_soc_match[] = {  static int imx8m_soc_probe(struct
> > > > platform_device *pdev)  {
> > > >  	struct soc_device_attribute *soc_dev_attr;
> > > > +	struct platform_device *cpufreq_dev;
> > > >  	const struct imx8_soc_data *data;
> > > >  	struct device *dev = &pdev->dev;
> > > >  	const struct of_device_id *id;
> > > > -	struct soc_device *soc_dev;
> > > > +	struct imx8m_soc_priv *priv;
> > > >  	u32 soc_rev = 0;
> > > >  	u64 soc_uid = 0;
> > > >  	int ret;
> > > > @@ -207,6 +213,10 @@ static int imx8m_soc_probe(struct
> > > platform_device *pdev)
> > > >  	if (!soc_dev_attr)
> > > >  		return -ENOMEM;
> > > >
> > > > +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> > > > +	if (!priv)
> > > > +		return -ENOMEM;
> > > > +
> > > >  	soc_dev_attr->family = "Freescale i.MX";
> > > >
> > > >  	ret = of_property_read_string(of_root, "model",
> > > > &soc_dev_attr->machine); @@ -235,21 +245,40 @@ static int
> > > imx8m_soc_probe(struct platform_device *pdev)
> > > >  	if (!soc_dev_attr->serial_number)
> > > >  		return -ENOMEM;
> > > >
> > > > -	soc_dev = soc_device_register(soc_dev_attr);
> > > > -	if (IS_ERR(soc_dev))
> > > > -		return PTR_ERR(soc_dev);
> > > > +	priv->soc_dev = soc_device_register(soc_dev_attr);
> > > > +	if (IS_ERR(priv->soc_dev))
> > > > +		return PTR_ERR(priv->soc_dev);
> > > >
> > > >  	pr_info("SoC: %s revision %s\n", soc_dev_attr->soc_id,
> > > >  		soc_dev_attr->revision);
> > > >
> > > > -	if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT))
> > > > -		platform_device_register_simple("imx-cpufreq-dt", -1,
> > > NULL, 0);
> > > > +	if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT)) {
> > > > +		cpufreq_dev = platform_device_register_simple("imx-
> > > cpufreq-dt", -1, NULL, 0);
> > > > +		if (!IS_ERR(cpufreq_dev))
> > > > +			priv->cpufreq_dev = cpufreq_dev;
> > > > +		else
> > > > +			dev_err(dev, "Failed to create imx-cpufreq-
> > > dev device: %ld",
> > > > +				PTR_ERR(cpufreq_dev));
> > > > +	}
> > > > +
> > > > +	platform_set_drvdata(pdev, priv);
> > > >
> > > >  	return 0;
> > > >  }
> > > >
> > > > +static void imx8m_soc_remove(struct platform_device *pdev) {
> > > > +	struct imx8m_soc_priv *priv = platform_get_drvdata(pdev);
> > > > +
> > > > +	if (priv->cpufreq_dev)
> > > > +		platform_device_unregister(priv->cpufreq_dev);
> > > > +
> > > > +	soc_device_unregister(priv->soc_dev);
> > > > +}
> > > > +
> > > >  static struct platform_driver imx8m_soc_driver = {
> > > >  	.probe = imx8m_soc_probe,
> > > > +	.remove	= imx8m_soc_remove,
> > > >  	.driver = {
> > > >  		.name = "imx8m-soc",
> > > >  	},
> > > > --
> > > > 2.37.1
> > > >
> > > >
> > > >
> >
Marco Felsch Dec. 17, 2024, 1:22 p.m. UTC | #5
On 24-12-17, Peng Fan wrote:
> > Subject: Re: [PATCH V2] soc: imx8m: Add remove function
> > 
> > On 24-12-17, Peng Fan wrote:
> > > > Subject: Re: [PATCH V2] soc: imx8m: Add remove function
> > > >
> > > > Hi Peng,
> > > >
> > > > thanks for you patch.
> > > >
> > > > On 24-12-17, Peng Fan (OSS) wrote:
> > > > > From: Peng Fan <peng.fan@nxp.com>
> > > > >
> > > > > Unregister the cpufreq device and soc device in remove path,
> > > > otherwise
> > > >
> > > > After reconsidiering the patch approach, I think we shouldn't add a
> > > > .remove() function and instead should make use of the
> > > > devm_add_action() mechanism for the proper unregister calls.
> > >
> > > Would you please share why devm_add_action is preferred?
> > >
> > > Something as below?
> > > +static void imx8m_soc_remove(void *data) {
> > > +       struct imx8m_soc_priv *priv = data;
> > > +
> > > +       if (priv->cpufreq_dev)
> > > +               platform_device_unregister(priv->cpufreq_dev);
> > > +
> > > +       soc_device_unregister(priv->soc_dev);
> > > +}
> > 
> > I would split it into two action functions due to the following reasons:
> >  - cleanup during the probe() if something fails afterwards
> >  - no need for the if(priv->cpufreq_dev) check.
> > 
> > > In Probe:
> > > +       return devm_add_action(&pdev->dev, imx8m_soc_remove,
> > priv);
> > 
> > The actions should be added directly after the succeful device
> > registration, e.g.
> >  - after soc_device_register(soc_dev_attr) and
> >  - after platform_device_register_simple()
> 
> With two devm_add_action, I thought below code would work,
> devm_add_action(dev, soc_device_unregister, (void *)soc_dev);
> devm_add_action(dev, platform_device_unregister, (void *)cpufreq_dev);

Passing the soc_dev and cpufreq_dev as args would remove the
imx8m_soc_priv :)

> But devm_add_action expects: void (*action)(void *),
> So I need introduce two functions to wrap soc_device_unregister,
> and platform_device_unregister.

This is a common pattern.

> This seems not good. Using one devm_add_action just need
> one wrap function, comments?

It's more future proof in case the driver becomes more complex.

Regards,
  Marco

> 
> Thanks,
> Peng.
> 
> > 
> > Regards,
> >    Marco
> > 
> > >
> > > Regards,
> > > Peng.
> > >
> > > >
> > > > > there will be warning when do removing test:
> > > > > sysfs: cannot create duplicate filename '/devices/platform/imx-
> > > > cpufreq-dt'
> > > > > CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted
> > > > > 6.13.0-rc1-next-20241204 Hardware name: NXP i.MX8MPlus EVK
> > > > board (DT)
> > > > >
> > > > > Fixes: 9cc832d37799 ("soc: imx8m: Probe the SoC driver as
> > platform
> > > > > driver")
> > > > > Signed-off-by: Peng Fan <peng.fan@nxp.com>
> > > > > ---
> > > > >
> > > > > V2:
> > > > >  Add err check when create the cpufreq platform device
> > > >
> > > > thank you for addressing this.
> > > >
> > > > Regards,
> > > >   Marco
> > > >
> > > > >  drivers/soc/imx/soc-imx8m.c | 41
> > > > > +++++++++++++++++++++++++++++++------
> > > > >  1 file changed, 35 insertions(+), 6 deletions(-)
> > > > >
> > > > > diff --git a/drivers/soc/imx/soc-imx8m.c b/drivers/soc/imx/soc-
> > > > imx8m.c
> > > > > index 8ac7658e3d52..97c8718c2aa1 100644
> > > > > --- a/drivers/soc/imx/soc-imx8m.c
> > > > > +++ b/drivers/soc/imx/soc-imx8m.c
> > > > > @@ -33,6 +33,11 @@ struct imx8_soc_data {
> > > > >  	int (*soc_revision)(u32 *socrev, u64 *socuid);  };
> > > > >
> > > > > +struct imx8m_soc_priv {
> > > > > +	struct soc_device *soc_dev;
> > > > > +	struct platform_device *cpufreq_dev; };
> > > > > +
> > > > >  #ifdef CONFIG_HAVE_ARM_SMCCC
> > > > >  static u32 imx8mq_soc_revision_from_atf(void)
> > > > >  {
> > > > > @@ -195,10 +200,11 @@ static __maybe_unused const struct
> > > > of_device_id
> > > > > imx8_soc_match[] = {  static int imx8m_soc_probe(struct
> > > > > platform_device *pdev)  {
> > > > >  	struct soc_device_attribute *soc_dev_attr;
> > > > > +	struct platform_device *cpufreq_dev;
> > > > >  	const struct imx8_soc_data *data;
> > > > >  	struct device *dev = &pdev->dev;
> > > > >  	const struct of_device_id *id;
> > > > > -	struct soc_device *soc_dev;
> > > > > +	struct imx8m_soc_priv *priv;
> > > > >  	u32 soc_rev = 0;
> > > > >  	u64 soc_uid = 0;
> > > > >  	int ret;
> > > > > @@ -207,6 +213,10 @@ static int imx8m_soc_probe(struct
> > > > platform_device *pdev)
> > > > >  	if (!soc_dev_attr)
> > > > >  		return -ENOMEM;
> > > > >
> > > > > +	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
> > > > > +	if (!priv)
> > > > > +		return -ENOMEM;
> > > > > +
> > > > >  	soc_dev_attr->family = "Freescale i.MX";
> > > > >
> > > > >  	ret = of_property_read_string(of_root, "model",
> > > > > &soc_dev_attr->machine); @@ -235,21 +245,40 @@ static int
> > > > imx8m_soc_probe(struct platform_device *pdev)
> > > > >  	if (!soc_dev_attr->serial_number)
> > > > >  		return -ENOMEM;
> > > > >
> > > > > -	soc_dev = soc_device_register(soc_dev_attr);
> > > > > -	if (IS_ERR(soc_dev))
> > > > > -		return PTR_ERR(soc_dev);
> > > > > +	priv->soc_dev = soc_device_register(soc_dev_attr);
> > > > > +	if (IS_ERR(priv->soc_dev))
> > > > > +		return PTR_ERR(priv->soc_dev);
> > > > >
> > > > >  	pr_info("SoC: %s revision %s\n", soc_dev_attr->soc_id,
> > > > >  		soc_dev_attr->revision);
> > > > >
> > > > > -	if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT))
> > > > > -		platform_device_register_simple("imx-cpufreq-dt", -1,
> > > > NULL, 0);
> > > > > +	if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT)) {
> > > > > +		cpufreq_dev = platform_device_register_simple("imx-
> > > > cpufreq-dt", -1, NULL, 0);
> > > > > +		if (!IS_ERR(cpufreq_dev))
> > > > > +			priv->cpufreq_dev = cpufreq_dev;
> > > > > +		else
> > > > > +			dev_err(dev, "Failed to create imx-cpufreq-
> > > > dev device: %ld",
> > > > > +				PTR_ERR(cpufreq_dev));
> > > > > +	}
> > > > > +
> > > > > +	platform_set_drvdata(pdev, priv);
> > > > >
> > > > >  	return 0;
> > > > >  }
> > > > >
> > > > > +static void imx8m_soc_remove(struct platform_device *pdev) {
> > > > > +	struct imx8m_soc_priv *priv = platform_get_drvdata(pdev);
> > > > > +
> > > > > +	if (priv->cpufreq_dev)
> > > > > +		platform_device_unregister(priv->cpufreq_dev);
> > > > > +
> > > > > +	soc_device_unregister(priv->soc_dev);
> > > > > +}
> > > > > +
> > > > >  static struct platform_driver imx8m_soc_driver = {
> > > > >  	.probe = imx8m_soc_probe,
> > > > > +	.remove	= imx8m_soc_remove,
> > > > >  	.driver = {
> > > > >  		.name = "imx8m-soc",
> > > > >  	},
> > > > > --
> > > > > 2.37.1
> > > > >
> > > > >
> > > > >
> > >
>
diff mbox series

Patch

diff --git a/drivers/soc/imx/soc-imx8m.c b/drivers/soc/imx/soc-imx8m.c
index 8ac7658e3d52..97c8718c2aa1 100644
--- a/drivers/soc/imx/soc-imx8m.c
+++ b/drivers/soc/imx/soc-imx8m.c
@@ -33,6 +33,11 @@  struct imx8_soc_data {
 	int (*soc_revision)(u32 *socrev, u64 *socuid);
 };
 
+struct imx8m_soc_priv {
+	struct soc_device *soc_dev;
+	struct platform_device *cpufreq_dev;
+};
+
 #ifdef CONFIG_HAVE_ARM_SMCCC
 static u32 imx8mq_soc_revision_from_atf(void)
 {
@@ -195,10 +200,11 @@  static __maybe_unused const struct of_device_id imx8_soc_match[] = {
 static int imx8m_soc_probe(struct platform_device *pdev)
 {
 	struct soc_device_attribute *soc_dev_attr;
+	struct platform_device *cpufreq_dev;
 	const struct imx8_soc_data *data;
 	struct device *dev = &pdev->dev;
 	const struct of_device_id *id;
-	struct soc_device *soc_dev;
+	struct imx8m_soc_priv *priv;
 	u32 soc_rev = 0;
 	u64 soc_uid = 0;
 	int ret;
@@ -207,6 +213,10 @@  static int imx8m_soc_probe(struct platform_device *pdev)
 	if (!soc_dev_attr)
 		return -ENOMEM;
 
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)
+		return -ENOMEM;
+
 	soc_dev_attr->family = "Freescale i.MX";
 
 	ret = of_property_read_string(of_root, "model", &soc_dev_attr->machine);
@@ -235,21 +245,40 @@  static int imx8m_soc_probe(struct platform_device *pdev)
 	if (!soc_dev_attr->serial_number)
 		return -ENOMEM;
 
-	soc_dev = soc_device_register(soc_dev_attr);
-	if (IS_ERR(soc_dev))
-		return PTR_ERR(soc_dev);
+	priv->soc_dev = soc_device_register(soc_dev_attr);
+	if (IS_ERR(priv->soc_dev))
+		return PTR_ERR(priv->soc_dev);
 
 	pr_info("SoC: %s revision %s\n", soc_dev_attr->soc_id,
 		soc_dev_attr->revision);
 
-	if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT))
-		platform_device_register_simple("imx-cpufreq-dt", -1, NULL, 0);
+	if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT)) {
+		cpufreq_dev = platform_device_register_simple("imx-cpufreq-dt", -1, NULL, 0);
+		if (!IS_ERR(cpufreq_dev))
+			priv->cpufreq_dev = cpufreq_dev;
+		else
+			dev_err(dev, "Failed to create imx-cpufreq-dev device: %ld",
+				PTR_ERR(cpufreq_dev));
+	}
+
+	platform_set_drvdata(pdev, priv);
 
 	return 0;
 }
 
+static void imx8m_soc_remove(struct platform_device *pdev)
+{
+	struct imx8m_soc_priv *priv = platform_get_drvdata(pdev);
+
+	if (priv->cpufreq_dev)
+		platform_device_unregister(priv->cpufreq_dev);
+
+	soc_device_unregister(priv->soc_dev);
+}
+
 static struct platform_driver imx8m_soc_driver = {
 	.probe = imx8m_soc_probe,
+	.remove	= imx8m_soc_remove,
 	.driver = {
 		.name = "imx8m-soc",
 	},