[v1,1/3] pwm: dwc: Add 16 channel support for Intel Elkhart Lake

Message ID 20240122030238.29437-2-raag.jadav@intel.com
State New
Headers
Series DesignWare PWM improvements |

Commit Message

Raag Jadav Jan. 22, 2024, 3:02 a.m. UTC
  Intel Elkhart Lake PSE includes two instances of PWM as a single PCI
function with 8 channels each. Add support for the remaining channels.

Signed-off-by: Raag Jadav <raag.jadav@intel.com>
Tested-by: Lakshmi Sowjanya D <lakshmi.sowjanya.d@intel.com>
---
 drivers/pwm/pwm-dwc.c | 51 +++++++++++++++++++++++++++++++++++--------
 drivers/pwm/pwm-dwc.h |  5 +++++
 2 files changed, 47 insertions(+), 9 deletions(-)
  

Comments

Andy Shevchenko Jan. 28, 2024, 2:53 p.m. UTC | #1
On Mon, Jan 22, 2024 at 08:32:36AM +0530, Raag Jadav wrote:
> Intel Elkhart Lake PSE includes two instances of PWM as a single PCI
> function with 8 channels each. Add support for the remaining channels.

..

> +static int dwc_pwm_init(struct device *dev, const struct dwc_pwm_info *info, void __iomem *base)
> +{
> +	/* Default values for single instance devices */
> +	unsigned int nr = 1, size = 0;
> +	int i, ret;
> +
> +	/* Fill up values from driver_data, if any */
> +	if (info) {
> +		nr = info->nr;
> +		size = info->size;
> +	}
> +
> +	for (i = 0; i < nr; i++) {
> +		struct dwc_pwm *dwc;
> +
> +		dwc = dwc_pwm_alloc(dev);
> +		if (!dwc)
> +			return -ENOMEM;
> +
> +		dwc->base = base + (i * size);
> +
> +		ret = devm_pwmchip_add(dev, &dwc->chip);
> +		if (ret)
> +			return ret;
> +	}
> +
> +	return 0;
> +}

Why not doing this slightly differently?

First option: Always provide driver data (info is never NULL).

Second option, have the body of the for-loop be factored to a helper
dwc_pwm_init_one() and here

	if (!info)
		return dwc_pwm_init_one(..., 1, 0);

	for (i = 0; i < info->nr; i++) {
		ret = dwc_pwm_init_one(...);
		if (ret)
			return ret;
	}
  
Raag Jadav Jan. 30, 2024, 10:30 a.m. UTC | #2
On Sun, Jan 28, 2024 at 04:53:24PM +0200, Andy Shevchenko wrote:
> On Mon, Jan 22, 2024 at 08:32:36AM +0530, Raag Jadav wrote:
> > Intel Elkhart Lake PSE includes two instances of PWM as a single PCI
> > function with 8 channels each. Add support for the remaining channels.
> 
> ...
> 
> > +static int dwc_pwm_init(struct device *dev, const struct dwc_pwm_info *info, void __iomem *base)
> > +{
> > +	/* Default values for single instance devices */
> > +	unsigned int nr = 1, size = 0;
> > +	int i, ret;
> > +
> > +	/* Fill up values from driver_data, if any */
> > +	if (info) {
> > +		nr = info->nr;
> > +		size = info->size;
> > +	}
> > +
> > +	for (i = 0; i < nr; i++) {
> > +		struct dwc_pwm *dwc;
> > +
> > +		dwc = dwc_pwm_alloc(dev);
> > +		if (!dwc)
> > +			return -ENOMEM;
> > +
> > +		dwc->base = base + (i * size);
> > +
> > +		ret = devm_pwmchip_add(dev, &dwc->chip);
> > +		if (ret)
> > +			return ret;
> > +	}
> > +
> > +	return 0;
> > +}
> 
> Why not doing this slightly differently?
> 
> First option: Always provide driver data (info is never NULL).

Allowing empty driver_data would save us from adding dummy info
for single instance devices in the future.

> Second option, have the body of the for-loop be factored to a helper
> dwc_pwm_init_one() and here
> 
> 	if (!info)
> 		return dwc_pwm_init_one(..., 1, 0);
> 
> 	for (i = 0; i < info->nr; i++) {
> 		ret = dwc_pwm_init_one(...);
> 		if (ret)
> 			return ret;
> 	}

Considering above, we're looking at something like this.

static int dwc_pwm_init_one(struct device *dev, void __iomem *base, unsigned int size)
{
        struct dwc_pwm *dwc;

        dwc = dwc_pwm_alloc(dev);
        if (!dwc)
                return -ENOMEM;

        dwc->base = base + size;

        return devm_pwmchip_add(dev, &dwc->chip);
}

	...

        if (info) {
                for (i = 0; i < info->nr; i++) {
                        ret = dwc_pwm_init_one(dev, base, i * info->size);
                        if (ret)
                                return ret;
                }
        } else {
                ret = dwc_pwm_init_one(dev, base, 0);
                if (ret)
                        return ret;
        }
	...

Raag
  
Andy Shevchenko Feb. 1, 2024, 11:26 a.m. UTC | #3
On Tue, Jan 30, 2024 at 12:30:23PM +0200, Raag Jadav wrote:
> On Sun, Jan 28, 2024 at 04:53:24PM +0200, Andy Shevchenko wrote:
> > On Mon, Jan 22, 2024 at 08:32:36AM +0530, Raag Jadav wrote:
> > > Intel Elkhart Lake PSE includes two instances of PWM as a single PCI
> > > function with 8 channels each. Add support for the remaining channels.

..

> > First option: Always provide driver data (info is never NULL).
> 
> Allowing empty driver_data would save us from adding dummy info
> for single instance devices in the future.

Which may be too premature "optimisation". Why? Because if we ever have
something like pci_dev_get_match_data(), the empty will mean NULL, and
we may not get difference between empty and missing one.

> > Second option, have the body of the for-loop be factored to a helper
> > dwc_pwm_init_one() and here
> > 
> > 	if (!info)
> > 		return dwc_pwm_init_one(..., 1, 0);
> > 
> > 	for (i = 0; i < info->nr; i++) {
> > 		ret = dwc_pwm_init_one(...);
> > 		if (ret)
> > 			return ret;
> > 	}
> 
> Considering above, we're looking at something like this.

As one option, yes.

> static int dwc_pwm_init_one(struct device *dev, void __iomem *base, unsigned int size)
> {
>         struct dwc_pwm *dwc;
> 
>         dwc = dwc_pwm_alloc(dev);
>         if (!dwc)
>                 return -ENOMEM;
> 
>         dwc->base = base + size;
> 
>         return devm_pwmchip_add(dev, &dwc->chip);
> }
  
Raag Jadav Feb. 2, 2024, 4:02 a.m. UTC | #4
On Thu, Feb 01, 2024 at 01:26:53PM +0200, Andy Shevchenko wrote:
> On Tue, Jan 30, 2024 at 12:30:23PM +0200, Raag Jadav wrote:
> > On Sun, Jan 28, 2024 at 04:53:24PM +0200, Andy Shevchenko wrote:
> > > On Mon, Jan 22, 2024 at 08:32:36AM +0530, Raag Jadav wrote:
> > > > Intel Elkhart Lake PSE includes two instances of PWM as a single PCI
> > > > function with 8 channels each. Add support for the remaining channels.
> 
> ...
> 
> > > First option: Always provide driver data (info is never NULL).
> > 
> > Allowing empty driver_data would save us from adding dummy info
> > for single instance devices in the future.
> 
> Which may be too premature "optimisation". Why? Because if we ever have
> something like pci_dev_get_match_data(), the empty will mean NULL, and
> we may not get difference between empty and missing one.

Not sure if I'm able to find such a helper as of now, but fair.
I can change it in v2 if Jarkko is okay with it.

Raag
  
Raag Jadav Feb. 7, 2024, 11:48 a.m. UTC | #5
On Fri, Feb 02, 2024 at 06:02:46AM +0200, Raag Jadav wrote:
> On Thu, Feb 01, 2024 at 01:26:53PM +0200, Andy Shevchenko wrote:
> > On Tue, Jan 30, 2024 at 12:30:23PM +0200, Raag Jadav wrote:
> > > On Sun, Jan 28, 2024 at 04:53:24PM +0200, Andy Shevchenko wrote:
> > > > On Mon, Jan 22, 2024 at 08:32:36AM +0530, Raag Jadav wrote:
> > > > > Intel Elkhart Lake PSE includes two instances of PWM as a single PCI
> > > > > function with 8 channels each. Add support for the remaining channels.
> > 
> > ...
> > 
> > > > First option: Always provide driver data (info is never NULL).
> > > 
> > > Allowing empty driver_data would save us from adding dummy info
> > > for single instance devices in the future.
> > 
> > Which may be too premature "optimisation". Why? Because if we ever have
> > something like pci_dev_get_match_data(), the empty will mean NULL, and
> > we may not get difference between empty and missing one.
> 
> Not sure if I'm able to find such a helper as of now, but fair.
> I can change it in v2 if Jarkko is okay with it.

Hi Jarkko,

If you agree with Andy's comments, please let me know.
Will send out a v2 accordingly.

Raag
  
Jarkko Nikula Feb. 7, 2024, 1:11 p.m. UTC | #6
On 2/7/24 13:48, Raag Jadav wrote:
> On Fri, Feb 02, 2024 at 06:02:46AM +0200, Raag Jadav wrote:
>> On Thu, Feb 01, 2024 at 01:26:53PM +0200, Andy Shevchenko wrote:
>>> On Tue, Jan 30, 2024 at 12:30:23PM +0200, Raag Jadav wrote:
>>>> On Sun, Jan 28, 2024 at 04:53:24PM +0200, Andy Shevchenko wrote:
>>>>> On Mon, Jan 22, 2024 at 08:32:36AM +0530, Raag Jadav wrote:
>>>>>> Intel Elkhart Lake PSE includes two instances of PWM as a single PCI
>>>>>> function with 8 channels each. Add support for the remaining channels.
>>>
>>> ...
>>>
>>>>> First option: Always provide driver data (info is never NULL).
>>>>
>>>> Allowing empty driver_data would save us from adding dummy info
>>>> for single instance devices in the future.
>>>
>>> Which may be too premature "optimisation". Why? Because if we ever have
>>> something like pci_dev_get_match_data(), the empty will mean NULL, and
>>> we may not get difference between empty and missing one.
>>
>> Not sure if I'm able to find such a helper as of now, but fair.
>> I can change it in v2 if Jarkko is okay with it.
> 
> Hi Jarkko,
> 
> If you agree with Andy's comments, please let me know.
> Will send out a v2 accordingly.
> 
Ah, sorry, I didn't have opinion.
  

Patch

diff --git a/drivers/pwm/pwm-dwc.c b/drivers/pwm/pwm-dwc.c
index 4929354f8cd9..fd64313cb38d 100644
--- a/drivers/pwm/pwm-dwc.c
+++ b/drivers/pwm/pwm-dwc.c
@@ -25,16 +25,48 @@ 
 
 #include "pwm-dwc.h"
 
+/* Elkhart Lake */
+static const struct dwc_pwm_info ehl_pwm_info = {
+	.nr = 2,
+	.size = 0x1000,
+};
+
+static int dwc_pwm_init(struct device *dev, const struct dwc_pwm_info *info, void __iomem *base)
+{
+	/* Default values for single instance devices */
+	unsigned int nr = 1, size = 0;
+	int i, ret;
+
+	/* Fill up values from driver_data, if any */
+	if (info) {
+		nr = info->nr;
+		size = info->size;
+	}
+
+	for (i = 0; i < nr; i++) {
+		struct dwc_pwm *dwc;
+
+		dwc = dwc_pwm_alloc(dev);
+		if (!dwc)
+			return -ENOMEM;
+
+		dwc->base = base + (i * size);
+
+		ret = devm_pwmchip_add(dev, &dwc->chip);
+		if (ret)
+			return ret;
+	}
+
+	return 0;
+}
+
 static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
 {
+	const struct dwc_pwm_info *info;
 	struct device *dev = &pci->dev;
-	struct dwc_pwm *dwc;
+	void __iomem *base;
 	int ret;
 
-	dwc = dwc_pwm_alloc(dev);
-	if (!dwc)
-		return -ENOMEM;
-
 	ret = pcim_enable_device(pci);
 	if (ret) {
 		dev_err(dev, "Failed to enable device (%pe)\n", ERR_PTR(ret));
@@ -49,13 +81,14 @@  static int dwc_pwm_probe(struct pci_dev *pci, const struct pci_device_id *id)
 		return ret;
 	}
 
-	dwc->base = pcim_iomap_table(pci)[0];
-	if (!dwc->base) {
+	base = pcim_iomap_table(pci)[0];
+	if (!base) {
 		dev_err(dev, "Base address missing\n");
 		return -ENOMEM;
 	}
 
-	ret = devm_pwmchip_add(dev, &dwc->chip);
+	info = (const struct dwc_pwm_info *)id->driver_data;
+	ret = dwc_pwm_init(dev, info, base);
 	if (ret)
 		return ret;
 
@@ -109,7 +142,7 @@  static int dwc_pwm_resume(struct device *dev)
 static DEFINE_SIMPLE_DEV_PM_OPS(dwc_pwm_pm_ops, dwc_pwm_suspend, dwc_pwm_resume);
 
 static const struct pci_device_id dwc_pwm_id_table[] = {
-	{ PCI_VDEVICE(INTEL, 0x4bb7) }, /* Elkhart Lake */
+	{ PCI_VDEVICE(INTEL, 0x4bb7), (kernel_ulong_t)&ehl_pwm_info },
 	{  }	/* Terminating Entry */
 };
 MODULE_DEVICE_TABLE(pci, dwc_pwm_id_table);
diff --git a/drivers/pwm/pwm-dwc.h b/drivers/pwm/pwm-dwc.h
index 64795247c54c..c9bbfc77b568 100644
--- a/drivers/pwm/pwm-dwc.h
+++ b/drivers/pwm/pwm-dwc.h
@@ -33,6 +33,11 @@  MODULE_IMPORT_NS(dwc_pwm);
 #define DWC_TIM_CTRL_INT_MASK	BIT(2)
 #define DWC_TIM_CTRL_PWM	BIT(3)
 
+struct dwc_pwm_info {
+	unsigned int nr;
+	unsigned int size;
+};
+
 struct dwc_pwm_ctx {
 	u32 cnt;
 	u32 cnt2;