Skip to contentSkip to conversation

pwm: add support for Intel Low Power Subsystem PWM

PATCH Superseded Chew Chiau Ee 4 participants 12 messages Jan 20, 2014 – Feb 27, 2014

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 pwm: add support for Intel Low Power Subsystem PWM

From: Mika Westerberg <[email protected]>

Add support for Intel Low Power I/O subsystem PWM controllers found on Intel BayTrail SoC.

---
 drivers/pwm/Kconfig    |   10 +++
 drivers/pwm/Makefile   |    1 +
 drivers/pwm/pwm-lpss.c |  207 ++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 218 insertions(+), 0 deletions(-)
 create mode 100644 drivers/pwm/pwm-lpss.c
drivers/pwm/Kconfig +10 -0
index 6a2a1e0a..4f75589 100644
@@ -109,6 +109,16 @@ config PWM_LPC32XX
 	  To compile this driver as a module, choose M here: the module
 	  will be called pwm-lpc32xx.
 
+config PWM_LPSS
+	tristate "Intel LPSS PWM support"
+	depends on ACPI
+	help
+	  Generic PWM framework driver for Intel Low Power Subsystem PWM
+	  controller.
+
+	  To compile this driver as a module, choose M here: the module
+	  will be called pwm-lpss.
+
 config PWM_MXS
 	tristate "Freescale MXS PWM support"
 	depends on ARCH_MXS && OF
drivers/pwm/Makefile +1 -0
index 1b99cfb..d5b316f 100644
@@ -8,6 +8,7 @@ obj-$(CONFIG_PWM_EP93XX)	+= pwm-ep93xx.o
 obj-$(CONFIG_PWM_IMX)		+= pwm-imx.o
 obj-$(CONFIG_PWM_JZ4740)	+= pwm-jz4740.o
 obj-$(CONFIG_PWM_LPC32XX)	+= pwm-lpc32xx.o
+obj-$(CONFIG_PWM_LPSS)		+= pwm-lpss.o
 obj-$(CONFIG_PWM_MXS)		+= pwm-mxs.o
 obj-$(CONFIG_PWM_PCA9685)	+= pwm-pca9685.o
 obj-$(CONFIG_PWM_PUV3)		+= pwm-puv3.o
drivers/pwm/pwm-lpss.c new file +207 -0

207 lines not on this page · raw message

Signature · 1 line
1.7.4.4
#1 lore raw
Quoted 20 lines
drivers/pwm/pwm-lpss.c new file +20 -0
+static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
+	int duty_ns, int period_ns)
+{
+	struct pwm_lpss_chip *lpwm = to_lpwm(chip);
+	u8 on_time_div;
+	unsigned long c = clk_get_rate(lpwm->clk);
+	unsigned long long base_unit, hz = 1000000000UL;
+	u32 ctrl;
+
+	do_div(hz, period_ns);
+
+	/* The equation is: base_unit = ((hz / c) * 65536) + correction */
+	base_unit = hz * 65536;
+	do_div(base_unit, c);
+	base_unit += PWM_DIVISION_CORRECTION;
+	if (base_unit > PWM_LIMIT)
+		return -EINVAL;
+
+	if (duty_ns <= 0)
+		duty_ns = 1;

drivers/pwm/pwm-lpss.c line 85

Quoted 10 lines
drivers/pwm/pwm-lpss.c new file +10 -0
+	on_time_div = 255 - (255 * duty_ns / period_ns);
+
+	ctrl = readl(lpwm->regs + PWM);
+	ctrl &= ~(PWM_BASE_UNIT_MASK | PWM_ON_TIME_DIV_MASK);
+	ctrl |= (u16) base_unit << PWM_BASE_UNIT_SHIFT;
+	ctrl |= on_time_div;
+	/* request PWM to update on next cycle */
+	ctrl |= PWM_SW_UPDATE;
+	writel(ctrl, lpwm->regs + PWM);
+

Who handles the locking on all these functions. The pwm layer doesn't but simnply exports stuff like pwm_config() directly to other bits of the kernel so you are not guaranteed to be called via sysfs.

(This btw looks to be a problem with a pile of the other pwm drivers, and with the pwm core code which doesn't properly lock its own handling of pwm->duty_cycle and pwm->period in pwm_config(), nor pwm->polarity in pwm_set_polarity).

I think the core config methods need some kind of locking ?

Alan

#2 lore raw
Quoted 35 lines · One Thousand Gnomes
Quoted 20 lines
drivers/pwm/pwm-lpss.c new file +20 -0
+static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
+	int duty_ns, int period_ns)
+{
+	struct pwm_lpss_chip *lpwm = to_lpwm(chip);
+	u8 on_time_div;
+	unsigned long c = clk_get_rate(lpwm->clk);
+	unsigned long long base_unit, hz = 1000000000UL;
+	u32 ctrl;
+
+	do_div(hz, period_ns);
+
+	/* The equation is: base_unit = ((hz / c) * 65536) + correction */
+	base_unit = hz * 65536;
+	do_div(base_unit, c);
+	base_unit += PWM_DIVISION_CORRECTION;
+	if (base_unit > PWM_LIMIT)
+		return -EINVAL;
+
+	if (duty_ns <= 0)
+		duty_ns = 1;

drivers/pwm/pwm-lpss.c line 85

Quoted 10 lines
drivers/pwm/pwm-lpss.c new file +10 -0
+	on_time_div = 255 - (255 * duty_ns / period_ns);
+
+	ctrl = readl(lpwm->regs + PWM);
+	ctrl &= ~(PWM_BASE_UNIT_MASK | PWM_ON_TIME_DIV_MASK);
+	ctrl |= (u16) base_unit << PWM_BASE_UNIT_SHIFT;
+	ctrl |= on_time_div;
+	/* request PWM to update on next cycle */
+	ctrl |= PWM_SW_UPDATE;
+	writel(ctrl, lpwm->regs + PWM);
+

Who handles the locking on all these functions. The pwm layer doesn't but simnply exports stuff like pwm_config() directly to other bits of the kernel so you are not guaranteed to be called via sysfs.

Quoted 4 lines

(This btw looks to be a problem with a pile of the other pwm drivers, and with the pwm core code which doesn't properly lock its own handling of pwm->duty_cycle and pwm->period in pwm_config(), nor pwm->polarity in pwm_set_polarity).

Good point. I checked few PWM drivers as well and none of them (including this one) does any locking around ->config().

Quoted 1 line

I think the core config methods need some kind of locking ?

Thierry, any comments on this?

#3 lore raw
Quoted 41 lines · Mika Westerberg
Quoted 35 lines · One Thousand Gnomes
Quoted 20 lines
drivers/pwm/pwm-lpss.c new file +20 -0
+static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
+	int duty_ns, int period_ns)
+{
+	struct pwm_lpss_chip *lpwm = to_lpwm(chip);
+	u8 on_time_div;
+	unsigned long c = clk_get_rate(lpwm->clk);
+	unsigned long long base_unit, hz = 1000000000UL;
+	u32 ctrl;
+
+	do_div(hz, period_ns);
+
+	/* The equation is: base_unit = ((hz / c) * 65536) + correction */
+	base_unit = hz * 65536;
+	do_div(base_unit, c);
+	base_unit += PWM_DIVISION_CORRECTION;
+	if (base_unit > PWM_LIMIT)
+		return -EINVAL;
+
+	if (duty_ns <= 0)
+		duty_ns = 1;

drivers/pwm/pwm-lpss.c line 85

Quoted 10 lines
drivers/pwm/pwm-lpss.c new file +10 -0
+	on_time_div = 255 - (255 * duty_ns / period_ns);
+
+	ctrl = readl(lpwm->regs + PWM);
+	ctrl &= ~(PWM_BASE_UNIT_MASK | PWM_ON_TIME_DIV_MASK);
+	ctrl |= (u16) base_unit << PWM_BASE_UNIT_SHIFT;
+	ctrl |= on_time_div;
+	/* request PWM to update on next cycle */
+	ctrl |= PWM_SW_UPDATE;
+	writel(ctrl, lpwm->regs + PWM);
+

Who handles the locking on all these functions. The pwm layer doesn't but simnply exports stuff like pwm_config() directly to other bits of the kernel so you are not guaranteed to be called via sysfs.

Quoted 4 lines

(This btw looks to be a problem with a pile of the other pwm drivers, and with the pwm core code which doesn't properly lock its own handling of pwm->duty_cycle and pwm->period in pwm_config(), nor pwm->polarity in pwm_set_polarity).

Quoted 6 lines

Good point. I checked few PWM drivers as well and none of them (including this one) does any locking around ->config().

Quoted 1 line

I think the core config methods need some kind of locking ?

Thierry, any comments on this?

The idea behind this is that only a single user can have access to a given PWM device at a time. The PWM device's PWMF_REQUESTED flag is set (and cleared) under the pwm_lock and any subsequent users will not be able to use that specific device (pwm_request() return -EBUSY).

There is obviously an assumption here that each user knows what they are doing and aren't calling any of the public pwm_*() functions concurrently. I haven't come across a situation where this is actually a problem because typically these functions are called either via sysfs or some other higher-level where synchronization is already properly handled.

So the only thing that drivers should be taking care of is synchronizing access to registers common to multiple PWM devices.

Does that clarify things?

Thierry

#4 lore raw
Quoted 12 lines · Thierry Reding

The idea behind this is that only a single user can have access to a given PWM device at a time. The PWM device's PWMF_REQUESTED flag is set (and cleared) under the pwm_lock and any subsequent users will not be able to use that specific device (pwm_request() return -EBUSY).

There is obviously an assumption here that each user knows what they are doing and aren't calling any of the public pwm_*() functions concurrently. I haven't come across a situation where this is actually a problem because typically these functions are called either via sysfs or some other higher-level where synchronization is already properly handled.

Quoted 2 lines

So the only thing that drivers should be taking care of is synchronizing access to registers common to multiple PWM devices.

OK, and since LPSS PWM don't share registers we shouldn't need to do anything here.

Quoted 1 line

Does that clarify things?

It does for me, thanks for the explanation.

#5 lore raw
Quoted 24 lines

-----Original Message-----
From: Mika Westerberg [mailto:[email protected]]
Sent: Wednesday, January 22, 2014 5:31 PM
To: Thierry Reding
Cc: One Thousand Gnomes; Chew, Chiau Ee; [email protected]; linux- [email protected]; Chew, Kean Ho; Chang, Rebecca Swee Fun
Subject: Re: [PATCH] pwm: add support for Intel Low Power Subsystem PWM

Quoted 12 lines · Thierry Reding

The idea behind this is that only a single user can have access to a given PWM device at a time. The PWM device's PWMF_REQUESTED flag is set (and cleared) under the pwm_lock and any subsequent users will not be able to use that specific device (pwm_request() return -EBUSY).

There is obviously an assumption here that each user knows what they are doing and aren't calling any of the public pwm_*() functions concurrently. I haven't come across a situation where this is actually a problem because typically these functions are called either via sysfs or some other higher-level where synchronization is already properly handled.

Quoted 2 lines

So the only thing that drivers should be taking care of is synchronizing access to registers common to multiple PWM devices.

Quoted 6 lines

OK, and since LPSS PWM don't share registers we shouldn't need to do anything here.

Quoted 1 line

Does that clarify things?

It does for me, thanks for the explanation.

Hi Thierry,

Would like to find out whether this pwm patch ready to get accepted into mainline kernel? Didn't mean to be pushy :).
Btw, in order to have the Intel Baytrail PWM controller working, it has dependency on the acpi_lpss.c which I have sent the patch (https://lkml.org/lkml/2014/2/18/127) for upstream as well and cc you in the loop.

Thanks,
Chiau Ee

#7 lore raw
In reply to Chew Chiau Ee

On Tue, Jan 21, 2014 at 02:00:08AM +0800, Chew Chiau Ee wrote:
[...]

Quoted 1 line
drivers/pwm/pwm-lpss.c +0 -0

[...]

Quoted 18 lines
drivers/pwm/pwm-lpss.c +18 -0
+/*
+ * Intel Low Power Subsystem PWM controller driver
+ *
+ * Copyright (C) 2014, Intel Corporation
+ * Author: Mika Westerberg <[email protected]>
+ * Author: Chew Kean Ho <[email protected]>
+ * Author: Chang Rebecca Swee Fun <[email protected]>
+ * Author: Chew Chiau Ee <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/acpi.h>
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/kernel.h>

drivers/pwm/pwm-lpss.c line 28

Quoted 10 lines
drivers/pwm/pwm-lpss.c +10 -0
+#include <linux/module.h>
+#include <linux/pwm.h>
+#include <linux/platform_device.h>
+
+#define PWM				0x00000000
+#define PWM_ENABLE			BIT(31)
+#define PWM_SW_UPDATE			BIT(30)
+#define PWM_BASE_UNIT_SHIFT		8
+#define PWM_BASE_UNIT_MASK		0x00ffff00
+#define PWM_ON_TIME_DIV_MASK		0x000000ff

Does the documentation really call this "on time"? I've always only seen this called duty-cycle.

Quoted 15 lines
drivers/pwm/pwm-lpss.c +15 -0
+#define PWM_DIVISION_CORRECTION		0x2
+#define PWM_LIMIT			(0x8000 + PWM_DIVISION_CORRECTION)
+
+
+struct pwm_lpss_chip {
+	struct pwm_chip chip;
+	void __iomem *regs;
+	struct clk *clk;
+};
+
+static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip)
+{
+	return container_of(chip, struct pwm_lpss_chip, chip);
+}
+

drivers/pwm/pwm-lpss.c line 53

Quoted 10 lines
drivers/pwm/pwm-lpss.c +10 -0
+static void pwm_lpss_set_state(struct pwm_lpss_chip *lpwm, bool enable)
+{
+	u32 ctrl;
+
+	ctrl = readl(lpwm->regs + PWM);
+	if (enable)
+		ctrl |= PWM_ENABLE;
+	else
+		ctrl &= ~PWM_ENABLE;
+	writel(ctrl, lpwm->regs + PWM);

Nit: could use more blank lines around readl() and writel(), but I'm told that not everybody likes it that way, so if you absolutely must keep it this way, that's fine, too. =)

Also, is it really necessary to turn this into a function? It's only called in three places, where each call site would only require three lines. This function takes up 12 lines in total, so there's no real gain.

drivers/pwm/pwm-lpss.c line 57

Quoted 2 lines
drivers/pwm/pwm-lpss.c +2 -0
+static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
+	int duty_ns, int period_ns)

Align arguments on subsequent lines with those of the first line, please.

drivers/pwm/pwm-lpss.c line 62

Quoted 5 lines
drivers/pwm/pwm-lpss.c +5 -0
+{
+	struct pwm_lpss_chip *lpwm = to_lpwm(chip);
+	u8 on_time_div;
+	unsigned long c = clk_get_rate(lpwm->clk);
+	unsigned long long base_unit, hz = 1000000000UL;

"hz" -> "freq"? "1000000000UL" -> "NSECS_PER_SEC"?

drivers/pwm/pwm-lpss.c line 93

Quoted 5 lines
drivers/pwm/pwm-lpss.c +5 -0
+static int pwm_lpss_enable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+	struct pwm_lpss_chip *lpwm = to_lpwm(chip);
+
+	clk_prepare_enable(lpwm->clk);

You need to check the return value of clk_prepare_enable().

drivers/pwm/pwm-lpss.c line 121

Quoted 9 lines
drivers/pwm/pwm-lpss.c +9 -0
+#ifdef CONFIG_ACPI
+struct pwm_lpss_chip *pwm_lpss_acpi_get_pdata(struct platform_device *pdev)
+{
+	struct pwm_lpss_chip *lpwm;
+	struct resource *r;
+
+	lpwm = devm_kzalloc(&pdev->dev, sizeof(*lpwm), GFP_KERNEL);
+	if (!lpwm) {
+		dev_err(&pdev->dev, "failed to allocate memory for platform data\n");

No need to print this message. You should get one from the allocator itself.

drivers/pwm/pwm-lpss.c line 135

Quoted 11 lines
drivers/pwm/pwm-lpss.c +11 -0
+	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!r) {
+		dev_err(&pdev->dev, "failed to get mmio base\n");
+		return NULL;
+	}
+
+	lpwm->clk = devm_clk_get(&pdev->dev, NULL);
+	if (IS_ERR(lpwm->clk)) {
+		dev_err(&pdev->dev, "failed to get pwm clk\n");
+		return NULL;
+	}

"pwm" -> "PWM", "clk" -> "clock", please.

drivers/pwm/pwm-lpss.c line 140

Quoted 5 lines
drivers/pwm/pwm-lpss.c +5 -0
+
+	lpwm->chip.base = -1;
+
+	lpwm->regs = devm_request_and_ioremap(&pdev->dev, r);
+	if (!lpwm->regs)

devm_ioremap_resource()? If so, it returns an ERR_PTR() encoded error code on failure, so make sure to check with IS_ERR(lpwm->regs) instead. Also you can get rid of the extra error checking after the call to platform_get_resource() because devm_ioremap_resource() checks for it already.

Furthermore the ordering of calls is somewhat unusual here. I'd prefer platform_get_resource() followed by devm_ioremap_resource() directly.

Quoted 7 lines
drivers/pwm/pwm-lpss.c +7 -0
+		return NULL;
+
+	return lpwm;
+}
+
+static const struct acpi_device_id pwm_lpss_acpi_match[] = {
+	{ "80860F08", 0 },

drivers/pwm/pwm-lpss.c line 157

Quoted 10 lines
drivers/pwm/pwm-lpss.c +10 -0
+	{ "80860F09", 0 },
+	{ },
+};
+MODULE_DEVICE_TABLE(acpi, pwm_lpss_acpi_match);
+#else
+struct pwm_lpss_chip *pwm_lpss_acpi_get_pdata(struct platform_device *pdev)
+{
+	return NULL;
+}
+#endif

I think that #else is completely dead code since the driver depends on ACPI and therefore CONFIG_ACPI will always be enabled when building this driver.

drivers/pwm/pwm-lpss.c line 165

Quoted 7 lines
drivers/pwm/pwm-lpss.c +7 -0
+static int pwm_lpss_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct pwm_lpss_chip *lpwm;
+	int ret;
+
+	lpwm = dev_get_platdata(dev);

struct pwm_lpss_chip is defined in this source file, how can anybody else know what to fill platform_data with?

Quoted 5 lines
drivers/pwm/pwm-lpss.c +5 -0
+	if (!lpwm) {
+		lpwm = pwm_lpss_acpi_get_pdata(pdev);
+		if (!lpwm)
+			return -ENODEV;
+	}

[...]

drivers/pwm/pwm-lpss.c line 197

Quoted 4 lines
drivers/pwm/pwm-lpss.c +4 -0
+static struct platform_driver pwm_lpss_driver = {
+	.driver = {
+		.name = "pwm-lpss",
+		.acpi_match_table = ACPI_PTR(pwm_lpss_acpi_match),

ACPI_PTR not needed since the table will always be there.

drivers/pwm/pwm-lpss.c line 206

Quoted 9 lines
drivers/pwm/pwm-lpss.c +9 -0
+	},
+	.probe = pwm_lpss_probe,
+	.remove = pwm_lpss_remove,
+};
+module_platform_driver(pwm_lpss_driver);
+
+MODULE_DESCRIPTION("PWM driver for Intel LPSS");
+MODULE_AUTHOR("Mika Westerberg <[email protected]>");
+MODULE_LICENSE("GPL");

The file header says GPL v2, but "GPL" here means "GPL v2 or later".

Thierry

#8 lore raw
Quoted 27 lines · Thierry Reding

On Tue, Jan 21, 2014 at 02:00:08AM +0800, Chew Chiau Ee wrote:
[...]

Quoted 1 line
drivers/pwm/pwm-lpss.c +0 -0

[...]

Quoted 23 lines
drivers/pwm/pwm-lpss.c +23 -0
+/*
+ * Intel Low Power Subsystem PWM controller driver
+ *
+ * Copyright (C) 2014, Intel Corporation
+ * Author: Mika Westerberg <[email protected]>
+ * Author: Chew Kean Ho <[email protected]>
+ * Author: Chang Rebecca Swee Fun <[email protected]>
+ * Author: Chew Chiau Ee <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/acpi.h>
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/pwm.h>
+#include <linux/platform_device.h>
+
+#define PWM				0x00000000
Quoted 8 lines

drivers/pwm/pwm-lpss.c line 28

Quoted 5 lines
drivers/pwm/pwm-lpss.c +5 -0
+#define PWM_ENABLE			BIT(31)
+#define PWM_SW_UPDATE			BIT(30)
+#define PWM_BASE_UNIT_SHIFT		8
+#define PWM_BASE_UNIT_MASK		0x00ffff00
+#define PWM_ON_TIME_DIV_MASK		0x000000ff

Does the documentation really call this "on time"? I've always only seen this called duty-cycle.

Yes, it's called like that in the documentation.

Quoted 31 lines
Quoted 16 lines
drivers/pwm/pwm-lpss.c +15 -0
+#define PWM_DIVISION_CORRECTION		0x2
+#define PWM_LIMIT			(0x8000 + PWM_DIVISION_CORRECTION)
+
+
+struct pwm_lpss_chip {
+	struct pwm_chip chip;
+	void __iomem *regs;
+	struct clk *clk;
+};
+
+static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip)
+{
+	return container_of(chip, struct pwm_lpss_chip, chip);
+}
+

drivers/pwm/pwm-lpss.c line 53

Quoted 10 lines
drivers/pwm/pwm-lpss.c +10 -0
+static void pwm_lpss_set_state(struct pwm_lpss_chip *lpwm, bool enable)
+{
+	u32 ctrl;
+
+	ctrl = readl(lpwm->regs + PWM);
+	if (enable)
+		ctrl |= PWM_ENABLE;
+	else
+		ctrl &= ~PWM_ENABLE;
+	writel(ctrl, lpwm->regs + PWM);

Nit: could use more blank lines around readl() and writel(), but I'm told that not everybody likes it that way, so if you absolutely must keep it this way, that's fine, too. =)

Quoted 4 lines

Also, is it really necessary to turn this into a function? It's only called in three places, where each call site would only require three lines. This function takes up 12 lines in total, so there's no real gain.

Good point.

Quoted 6 lines

drivers/pwm/pwm-lpss.c line 57

Quoted 3 lines
drivers/pwm/pwm-lpss.c +2 -0
+static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device *pwm,
+	int duty_ns, int period_ns)

Align arguments on subsequent lines with those of the first line, please.

OK

Quoted 8 lines

drivers/pwm/pwm-lpss.c line 62

Quoted 6 lines
drivers/pwm/pwm-lpss.c +5 -0
+{
+	struct pwm_lpss_chip *lpwm = to_lpwm(chip);
+	u8 on_time_div;
+	unsigned long c = clk_get_rate(lpwm->clk);
+	unsigned long long base_unit, hz = 1000000000UL;

"hz" -> "freq"? "1000000000UL" -> "NSECS_PER_SEC"?

OK

Quoted 8 lines

drivers/pwm/pwm-lpss.c line 93

Quoted 6 lines
drivers/pwm/pwm-lpss.c +5 -0
+static int pwm_lpss_enable(struct pwm_chip *chip, struct pwm_device *pwm)
+{
+	struct pwm_lpss_chip *lpwm = to_lpwm(chip);
+
+	clk_prepare_enable(lpwm->clk);

You need to check the return value of clk_prepare_enable().

Indeed. Will fix.

Quoted 13 lines

drivers/pwm/pwm-lpss.c line 121

Quoted 10 lines
drivers/pwm/pwm-lpss.c +9 -0
+#ifdef CONFIG_ACPI
+struct pwm_lpss_chip *pwm_lpss_acpi_get_pdata(struct platform_device *pdev)
+{
+	struct pwm_lpss_chip *lpwm;
+	struct resource *r;
+
+	lpwm = devm_kzalloc(&pdev->dev, sizeof(*lpwm), GFP_KERNEL);
+	if (!lpwm) {
+		dev_err(&pdev->dev, "failed to allocate memory for platform data\n");

No need to print this message. You should get one from the allocator itself.

OK

Quoted 14 lines

drivers/pwm/pwm-lpss.c line 135

Quoted 12 lines
drivers/pwm/pwm-lpss.c +11 -0
+	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!r) {
+		dev_err(&pdev->dev, "failed to get mmio base\n");
+		return NULL;
+	}
+
+	lpwm->clk = devm_clk_get(&pdev->dev, NULL);
+	if (IS_ERR(lpwm->clk)) {
+		dev_err(&pdev->dev, "failed to get pwm clk\n");
+		return NULL;
+	}

"pwm" -> "PWM", "clk" -> "clock", please.

OK

Quoted 13 lines

drivers/pwm/pwm-lpss.c line 140

Quoted 6 lines
drivers/pwm/pwm-lpss.c +5 -0
+
+	lpwm->chip.base = -1;
+
+	lpwm->regs = devm_request_and_ioremap(&pdev->dev, r);
+	if (!lpwm->regs)

devm_ioremap_resource()? If so, it returns an ERR_PTR() encoded error code on failure, so make sure to check with IS_ERR(lpwm->regs) instead. Also you can get rid of the extra error checking after the call to platform_get_resource() because devm_ioremap_resource() checks for it already.

Quoted 2 lines

Furthermore the ordering of calls is somewhat unusual here. I'd prefer platform_get_resource() followed by devm_ioremap_resource() directly.

Yup, we will change that.

Quoted 14 lines
Quoted 14 lines
drivers/pwm/pwm-lpss.c +13 -0
+		return NULL;
+
+	return lpwm;
+}
+
+static const struct acpi_device_id pwm_lpss_acpi_match[] = {
+	{ "80860F08", 0 },
+	{ "80860F09", 0 },
+	{ },
+};
+MODULE_DEVICE_TABLE(acpi, pwm_lpss_acpi_match);
+#else
+struct pwm_lpss_chip *pwm_lpss_acpi_get_pdata(struct platform_device *pdev)
Quoted 8 lines

drivers/pwm/pwm-lpss.c line 157

Quoted 4 lines
drivers/pwm/pwm-lpss.c +4 -0
+{
+	return NULL;
+}
+#endif

I think that #else is completely dead code since the driver depends on ACPI and therefore CONFIG_ACPI will always be enabled when building this driver.

We are getting PCI enumeration for this device as well but for now we can drop the code and add it back later if needed.

Quoted 11 lines

drivers/pwm/pwm-lpss.c line 165

Quoted 8 lines
drivers/pwm/pwm-lpss.c +7 -0
+static int pwm_lpss_probe(struct platform_device *pdev)
+{
+	struct device *dev = &pdev->dev;
+	struct pwm_lpss_chip *lpwm;
+	int ret;
+
+	lpwm = dev_get_platdata(dev);

struct pwm_lpss_chip is defined in this source file, how can anybody else know what to fill platform_data with?

Good point. Chiau Ee, do you recall why that thing is there in the first place?

Quoted 6 lines
Quoted 5 lines
drivers/pwm/pwm-lpss.c +5 -0
+	if (!lpwm) {
+		lpwm = pwm_lpss_acpi_get_pdata(pdev);
+		if (!lpwm)
+			return -ENODEV;
+	}

[...]

Quoted 6 lines

drivers/pwm/pwm-lpss.c line 197

Quoted 4 lines
drivers/pwm/pwm-lpss.c +4 -0
+static struct platform_driver pwm_lpss_driver = {
+	.driver = {
+		.name = "pwm-lpss",
+		.acpi_match_table = ACPI_PTR(pwm_lpss_acpi_match),

ACPI_PTR not needed since the table will always be there.

OK.

Quoted 12 lines

drivers/pwm/pwm-lpss.c line 206

Quoted 10 lines
drivers/pwm/pwm-lpss.c +9 -0
+	},
+	.probe = pwm_lpss_probe,
+	.remove = pwm_lpss_remove,
+};
+module_platform_driver(pwm_lpss_driver);
+
+MODULE_DESCRIPTION("PWM driver for Intel LPSS");
+MODULE_AUTHOR("Mika Westerberg <[email protected]>");
+MODULE_LICENSE("GPL");

The file header says GPL v2, but "GPL" here means "GPL v2 or later".

OK, we will change that to GPLv2.

Thanks a lot for your review! We will do the changes and submit v2.

#9 lore raw
Quoted 210 lines

-----Original Message-----
From: Mika Westerberg [mailto:[email protected]]
Sent: Thursday, February 27, 2014 5:02 PM
To: Thierry Reding
Cc: Chew, Chiau Ee; [email protected]; [email protected]; Chew, Kean Ho; Chang, Rebecca Swee Fun
Subject: Re: [PATCH] pwm: add support for Intel Low Power Subsystem PWM

Quoted 30 lines · Thierry Reding

On Tue, Jan 21, 2014 at 02:00:08AM +0800, Chew Chiau Ee wrote:
[...]

Quoted 1 line
drivers/pwm/pwm-lpss.c +0 -0

[...]

Quoted 26 lines
drivers/pwm/pwm-lpss.c +26 -0
+/*
+ * Intel Low Power Subsystem PWM controller driver
+ *
+ * Copyright (C) 2014, Intel Corporation
+ * Author: Mika Westerberg <[email protected]>
+ * Author: Chew Kean Ho <[email protected]>
+ * Author: Chang Rebecca Swee Fun
+<[email protected]>
+ * Author: Chew Chiau Ee <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+modify
+ * it under the terms of the GNU General Public License version 2
+as
+ * published by the Free Software Foundation.
+ */
+
+#include <linux/acpi.h>
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/pwm.h>
+#include <linux/platform_device.h>
+
+#define PWM				0x00000000
Quoted 8 lines

drivers/pwm/pwm-lpss.c line 28

Quoted 5 lines
drivers/pwm/pwm-lpss.c +5 -0
+#define PWM_ENABLE			BIT(31)
+#define PWM_SW_UPDATE			BIT(30)
+#define PWM_BASE_UNIT_SHIFT		8
+#define PWM_BASE_UNIT_MASK		0x00ffff00
+#define PWM_ON_TIME_DIV_MASK		0x000000ff

Does the documentation really call this "on time"? I've always only seen this called duty-cycle.

Yes, it's called like that in the documentation.

Quoted 3 lines

drivers/pwm/pwm-lpss.c

Quoted 3 lines
drivers/pwm/pwm-lpss.c +2 -0
+#define PWM_DIVISION_CORRECTION		0x2
+#define PWM_LIMIT			(0x8000 +

PWM_DIVISION_CORRECTION)

Quoted 27 lines
Quoted 12 lines
drivers/pwm/pwm-lpss.c +12 -0
+
+
+struct pwm_lpss_chip {
+	struct pwm_chip chip;
+	void __iomem *regs;
+	struct clk *clk;
+};
+
+static inline struct pwm_lpss_chip *to_lpwm(struct pwm_chip *chip)
+{
+	return container_of(chip, struct pwm_lpss_chip, chip); }
+

drivers/pwm/pwm-lpss.c line 53

Quoted 10 lines
drivers/pwm/pwm-lpss.c +10 -0
+static void pwm_lpss_set_state(struct pwm_lpss_chip *lpwm, bool
+enable) {
+	u32 ctrl;
+
+	ctrl = readl(lpwm->regs + PWM);
+	if (enable)
+		ctrl |= PWM_ENABLE;
+	else
+		ctrl &= ~PWM_ENABLE;
+	writel(ctrl, lpwm->regs + PWM);

Nit: could use more blank lines around readl() and writel(), but I'm told that not everybody likes it that way, so if you absolutely must keep it this way, that's fine, too. =)

Quoted 4 lines

Also, is it really necessary to turn this into a function? It's only called in three places, where each call site would only require three lines. This function takes up 12 lines in total, so there's no real gain.

Good point.

Quoted 2 lines
Quoted 2 lines

+static int pwm_lpss_config(struct pwm_chip *chip, struct pwm_device

*pwm,

Quoted 4 lines

drivers/pwm/pwm-lpss.c line 57

Quoted 1 line
drivers/pwm/pwm-lpss.c +1 -0
+	int duty_ns, int period_ns)

Align arguments on subsequent lines with those of the first line, please.

OK

Quoted 8 lines

drivers/pwm/pwm-lpss.c line 62

Quoted 6 lines
drivers/pwm/pwm-lpss.c +5 -0
+{
+	struct pwm_lpss_chip *lpwm = to_lpwm(chip);
+	u8 on_time_div;
+	unsigned long c = clk_get_rate(lpwm->clk);
+	unsigned long long base_unit, hz = 1000000000UL;

"hz" -> "freq"? "1000000000UL" -> "NSECS_PER_SEC"?

OK

Quoted 8 lines

drivers/pwm/pwm-lpss.c line 93

Quoted 6 lines
drivers/pwm/pwm-lpss.c +5 -0
+static int pwm_lpss_enable(struct pwm_chip *chip, struct pwm_device
+*pwm) {
+	struct pwm_lpss_chip *lpwm = to_lpwm(chip);
+
+	clk_prepare_enable(lpwm->clk);

You need to check the return value of clk_prepare_enable().

Indeed. Will fix.

Quoted 14 lines

drivers/pwm/pwm-lpss.c

Quoted 11 lines
drivers/pwm/pwm-lpss.c +10 -0
+#ifdef CONFIG_ACPI
+struct pwm_lpss_chip *pwm_lpss_acpi_get_pdata(struct
+platform_device *pdev) {
+	struct pwm_lpss_chip *lpwm;
+	struct resource *r;
+
+	lpwm = devm_kzalloc(&pdev->dev, sizeof(*lpwm), GFP_KERNEL);
+	if (!lpwm) {
+		dev_err(&pdev->dev, "failed to allocate memory for platform
+data\n");

No need to print this message. You should get one from the allocator itself.

OK

Quoted 14 lines

drivers/pwm/pwm-lpss.c line 135

Quoted 12 lines
drivers/pwm/pwm-lpss.c +11 -0
+	r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!r) {
+		dev_err(&pdev->dev, "failed to get mmio base\n");
+		return NULL;
+	}
+
+	lpwm->clk = devm_clk_get(&pdev->dev, NULL);
+	if (IS_ERR(lpwm->clk)) {
+		dev_err(&pdev->dev, "failed to get pwm clk\n");
+		return NULL;
+	}

"pwm" -> "PWM", "clk" -> "clock", please.

OK

Quoted 13 lines

drivers/pwm/pwm-lpss.c line 140

Quoted 6 lines
drivers/pwm/pwm-lpss.c +5 -0
+
+	lpwm->chip.base = -1;
+
+	lpwm->regs = devm_request_and_ioremap(&pdev->dev, r);
+	if (!lpwm->regs)

devm_ioremap_resource()? If so, it returns an ERR_PTR() encoded error code on failure, so make sure to check with IS_ERR(lpwm->regs) instead. Also you can get rid of the extra error checking after the call to platform_get_resource() because devm_ioremap_resource() checks for it already.

Quoted 2 lines

Furthermore the ordering of calls is somewhat unusual here. I'd prefer platform_get_resource() followed by devm_ioremap_resource() directly.

Yup, we will change that.

Quoted 13 lines
Quoted 13 lines
drivers/pwm/pwm-lpss.c +12 -0
+		return NULL;
+
+	return lpwm;
+}
+
+static const struct acpi_device_id pwm_lpss_acpi_match[] = {
+	{ "80860F08", 0 },
+	{ "80860F09", 0 },
+	{ },
+};
+MODULE_DEVICE_TABLE(acpi, pwm_lpss_acpi_match); #else struct
+pwm_lpss_chip *pwm_lpss_acpi_get_pdata(struct platform_device
Quoted 8 lines

drivers/pwm/pwm-lpss.c line 157

Quoted 4 lines
drivers/pwm/pwm-lpss.c +4 -0
+*pdev) {
+	return NULL;
+}
+#endif

I think that #else is completely dead code since the driver depends on ACPI and therefore CONFIG_ACPI will always be enabled when building this driver.

We are getting PCI enumeration for this device as well but for now we can drop the code and add it back later if needed.

Quoted 10 lines

drivers/pwm/pwm-lpss.c line 165

Quoted 7 lines
drivers/pwm/pwm-lpss.c +6 -0
+static int pwm_lpss_probe(struct platform_device *pdev) {
+	struct device *dev = &pdev->dev;
+	struct pwm_lpss_chip *lpwm;
+	int ret;
+
+	lpwm = dev_get_platdata(dev);

struct pwm_lpss_chip is defined in this source file, how can anybody else know what to fill platform_data with?

Quoted 1 line

Good point. Chiau Ee, do you recall why that thing is there in the first place?

This is added because we would eventually want to have PCI enumeration support added. I think we shall find a way to better enable both the ACPI enumeration and PCI enumeration support.

Quoted 26 lines
Quoted 7 lines
Quoted 6 lines
drivers/pwm/pwm-lpss.c +5 -0
+	if (!lpwm) {
+		lpwm = pwm_lpss_acpi_get_pdata(pdev);
+		if (!lpwm)
+			return -ENODEV;
+	}

[...]

Quoted 6 lines

drivers/pwm/pwm-lpss.c line 197

Quoted 4 lines
drivers/pwm/pwm-lpss.c +4 -0
+static struct platform_driver pwm_lpss_driver = {
+	.driver = {
+		.name = "pwm-lpss",
+		.acpi_match_table = ACPI_PTR(pwm_lpss_acpi_match),

ACPI_PTR not needed since the table will always be there.

OK.

Quoted 9 lines

drivers/pwm/pwm-lpss.c

Quoted 9 lines
drivers/pwm/pwm-lpss.c +8 -0
+	},
+	.probe = pwm_lpss_probe,
+	.remove = pwm_lpss_remove,
+};
+module_platform_driver(pwm_lpss_driver);
+
+MODULE_DESCRIPTION("PWM driver for Intel LPSS");
+MODULE_AUTHOR("Mika Westerberg

<[email protected]>");

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

+MODULE_LICENSE("GPL");

The file header says GPL v2, but "GPL" here means "GPL v2 or later".

OK, we will change that to GPLv2.

Thanks a lot for your review! We will do the changes and submit v2.

Ok. I will make the changes accordingly.

#10 lore raw
Quoted 10 lines · Chiau Ee
Quoted 10 lines
Quoted 9 lines

drivers/pwm/pwm-lpss.c line 165

Quoted 6 lines
drivers/pwm/pwm-lpss.c new file +6 -0
+static int pwm_lpss_probe(struct platform_device *pdev) {
+	struct device *dev = &pdev->dev;
+	struct pwm_lpss_chip *lpwm;
+	int ret;
+
+	lpwm = dev_get_platdata(dev);

struct pwm_lpss_chip is defined in this source file, how can anybody else know what to fill platform_data with?

Quoted 5 lines
Quoted 1 line

Good point. Chiau Ee, do you recall why that thing is there in the first place?

This is added because we would eventually want to have PCI enumeration support added. I think we shall find a way to better enable both the ACPI enumeration and PCI enumeration support.

Agreed. For now, can you please drop the platform data thing from the driver? Thanks.

Quoted 27 lines
Quoted 27 lines
Quoted 6 lines
Quoted 5 lines
drivers/pwm/pwm-lpss.c new file +5 -0
+	if (!lpwm) {
+		lpwm = pwm_lpss_acpi_get_pdata(pdev);
+		if (!lpwm)
+			return -ENODEV;
+	}

[...]

Quoted 6 lines

drivers/pwm/pwm-lpss.c line 197

Quoted 4 lines
drivers/pwm/pwm-lpss.c new file +4 -0
+static struct platform_driver pwm_lpss_driver = {
+	.driver = {
+		.name = "pwm-lpss",
+		.acpi_match_table = ACPI_PTR(pwm_lpss_acpi_match),

ACPI_PTR not needed since the table will always be there.

OK.

Quoted 9 lines

drivers/pwm/pwm-lpss.c

Quoted 9 lines
drivers/pwm/pwm-lpss.c new file +8 -0
+	},
+	.probe = pwm_lpss_probe,
+	.remove = pwm_lpss_remove,
+};
+module_platform_driver(pwm_lpss_driver);
+
+MODULE_DESCRIPTION("PWM driver for Intel LPSS");
+MODULE_AUTHOR("Mika Westerberg

<[email protected]>");

Quoted 2 lines
Quoted 1 line

+MODULE_LICENSE("GPL");

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

The file header says GPL v2, but "GPL" here means "GPL v2 or later".

OK, we will change that to GPLv2.

Thanks a lot for your review! We will do the changes and submit v2.

Ok. I will make the changes accordingly.

Thanks.

#11 lore raw
Quoted 21 lines

-----Original Message-----
From: Mika Westerberg [mailto:[email protected]]
Sent: Thursday, February 27, 2014 9:14 PM
To: Chew, Chiau Ee
Cc: Thierry Reding; [email protected]; [email protected]; Chew, Kean Ho; Chang, Rebecca Swee Fun
Subject: Re: [PATCH] pwm: add support for Intel Low Power Subsystem PWM

Quoted 10 lines · Chiau Ee
Quoted 10 lines
Quoted 9 lines

drivers/pwm/pwm-lpss.c line 165

Quoted 6 lines
drivers/pwm/pwm-lpss.c new file +6 -0
+static int pwm_lpss_probe(struct platform_device *pdev) {
+	struct device *dev = &pdev->dev;
+	struct pwm_lpss_chip *lpwm;
+	int ret;
+
+	lpwm = dev_get_platdata(dev);

struct pwm_lpss_chip is defined in this source file, how can anybody else know what to fill platform_data with?

Quoted 9 lines
Quoted 5 lines
Quoted 1 line

Good point. Chiau Ee, do you recall why that thing is there in the first place?

This is added because we would eventually want to have PCI enumeration support added. I think we shall find a way to better enable both the ACPI enumeration and PCI enumeration support.

Agreed. For now, can you please drop the platform data thing from the driver? Thanks.

Ok. Noted.

Quoted 36 lines
Quoted 27 lines
Quoted 27 lines
Quoted 6 lines
Quoted 5 lines
drivers/pwm/pwm-lpss.c new file +5 -0
+	if (!lpwm) {
+		lpwm = pwm_lpss_acpi_get_pdata(pdev);
+		if (!lpwm)
+			return -ENODEV;
+	}

[...]

Quoted 6 lines

drivers/pwm/pwm-lpss.c line 197

Quoted 4 lines
drivers/pwm/pwm-lpss.c new file +4 -0
+static struct platform_driver pwm_lpss_driver = {
+	.driver = {
+		.name = "pwm-lpss",
+		.acpi_match_table = ACPI_PTR(pwm_lpss_acpi_match),

ACPI_PTR not needed since the table will always be there.

OK.

Quoted 9 lines

drivers/pwm/pwm-lpss.c

Quoted 9 lines
drivers/pwm/pwm-lpss.c new file +8 -0
+	},
+	.probe = pwm_lpss_probe,
+	.remove = pwm_lpss_remove,
+};
+module_platform_driver(pwm_lpss_driver);
+
+MODULE_DESCRIPTION("PWM driver for Intel LPSS");
+MODULE_AUTHOR("Mika Westerberg

<[email protected]>");

Quoted 2 lines
Quoted 1 line

+MODULE_LICENSE("GPL");

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

The file header says GPL v2, but "GPL" here means "GPL v2 or later".

OK, we will change that to GPLv2.

Thanks a lot for your review! We will do the changes and submit v2.

Ok. I will make the changes accordingly.

Thanks.