Skip to contentSkip to conversation

NFC: nxp-nci: Add pad supply voltage pvdd-supply

PATCH Superseded Raymond Hackley 3 participants 9 messages Jun 9, 2023

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover NFC: nxp-nci: Add pad supply voltage pvdd-supply

v2: Use dev_err_probe() and drop unnecessary nxp_nci_i2c_poweroff()

PN547/553, QN310/330 chips on some devices require a pad supply voltage (PVDD). Otherwise, the NFC won't power up.

Implement support for pad supply voltage pvdd-supply that is enabled by the nxp-nci driver so that the regulator gets enabled when needed.

#1 lore raw

PATCH v2 1/2 dt-bindings: nfc: nxp-nci: document pvdd-supply

PN547/553, QN310/330 chips on some devices require a pad supply voltage (PVDD). Otherwise, the NFC won't power up.

Document support for pad supply voltage pvdd-supply that is enabled by the nxp-nci driver so that the regulator gets enabled when needed.

---
 Documentation/devicetree/bindings/net/nfc/nxp,nci.yaml | 5 +++++
 1 file changed, 5 insertions(+)
Documentation/devicetree/bindings/net/nfc/nxp,nci.yaml +5 -0
index 6924aff0b2c5..d5a4f935c2ce 100644
@@ -25,6 +25,11 @@ properties:
   firmware-gpios:
     description: Output GPIO pin used to enter firmware download mode
 
+  pvdd-supply:
+    description: |
+      Optional regulator that provides pad supply voltage for some
+      controllers
+
   interrupts:
     maxItems: 1
 
Signature · 1 line
2.30.2
#2 lore raw

PATCH v2 2/2 NFC: nxp-nci: Add pad supply voltage pvdd-supply

In reply to Raymond Hackley

PN547/553, QN310/330 chips on some devices require a pad supply voltage (PVDD). Otherwise, the NFC won't power up.

Implement support for pad supply voltage pvdd-supply that is enabled by the nxp-nci driver so that the regulator gets enabled when needed.

---
 drivers/nfc/nxp-nci/i2c.c | 42 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
drivers/nfc/nxp-nci/i2c.c +42 -0
index d4c299be7949..1b8877757cee 100644
@@ -35,6 +35,7 @@ struct nxp_nci_i2c_phy {
 
 	struct gpio_desc *gpiod_en;
 	struct gpio_desc *gpiod_fw;
+	struct regulator *pvdd;
 
 	int hard_fault; /*
 			 * < 0 if hardware error occurred (e.g. i2c err)
@@ -263,6 +264,22 @@ static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = {
 	{ }
 };
 
+static void nxp_nci_i2c_poweroff(void *data)
+{
+	struct nxp_nci_i2c_phy *phy = data;
+	struct device *dev = &phy->i2c_dev->dev;
+	struct regulator *pvdd = phy->pvdd;
+	int r;
+
+	if (!IS_ERR(pvdd) && regulator_is_enabled(pvdd)) {
+		r = regulator_disable(pvdd);
+		if (r < 0)
+			dev_warn(dev,
+				 "Failed to disable regulator pvdd: %d\n",
+				 r);
+	}
+}
+
 static int nxp_nci_i2c_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
@@ -298,6 +315,29 @@ static int nxp_nci_i2c_probe(struct i2c_client *client)
 		return PTR_ERR(phy->gpiod_fw);
 	}
 
+	phy->pvdd = devm_regulator_get_optional(dev, "pvdd");
+	if (IS_ERR(phy->pvdd)) {
+		r = PTR_ERR(phy->pvdd);
+		if (r != -ENODEV)
+			return dev_err_probe(dev, r,
+					     "Failed to get regulator pvdd\n");
+	} else {
+		r = regulator_enable(phy->pvdd);
+		if (r < 0) {
+			nfc_err(dev,
+				"Failed to enable regulator pvdd: %d\n",
+				r);
+			return r;
+		}
+	}
+
+	r = devm_add_action_or_reset(dev, nxp_nci_i2c_poweroff, phy);
+	if (r < 0) {
+		nfc_err(dev, "Failed to install poweroff handler: %d\n",
+			r);
+		return r;
+	}
+
 	r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
 			  NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
 	if (r < 0)
@@ -319,6 +359,8 @@ static void nxp_nci_i2c_remove(struct i2c_client *client)
 
 	nxp_nci_remove(phy->ndev);
 	free_irq(client->irq, phy);
+
+	nxp_nci_i2c_poweroff(phy);
 }
 
 static const struct i2c_device_id nxp_nci_i2c_id_table[] = {
Signature · 1 line
2.30.2
#3 lore raw
Quoted 25 lines · Raymond Hackley

PN547/553, QN310/330 chips on some devices require a pad supply voltage (PVDD). Otherwise, the NFC won't power up.

Implement support for pad supply voltage pvdd-supply that is enabled by the nxp-nci driver so that the regulator gets enabled when needed.

---
 drivers/nfc/nxp-nci/i2c.c | 42 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
drivers/nfc/nxp-nci/i2c.c +1 -0
index d4c299be7949..1b8877757cee 100644
@@ -35,6 +35,7 @@ struct nxp_nci_i2c_phy {
 
 	struct gpio_desc *gpiod_en;
 	struct gpio_desc *gpiod_fw;
+	struct regulator *pvdd;
 
 	int hard_fault; /*
 			 * < 0 if hardware error occurred (e.g. i2c err)
@@ -263,6 +264,22 @@ static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = {
 	{ }

drivers/nfc/nxp-nci/i2c.c static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = { · line 274

Quoted 10 lines
drivers/nfc/nxp-nci/i2c.c +8 -0
 };
 
+static void nxp_nci_i2c_poweroff(void *data)
+{
+	struct nxp_nci_i2c_phy *phy = data;
+	struct device *dev = &phy->i2c_dev->dev;
+	struct regulator *pvdd = phy->pvdd;
+	int r;
+
+	if (!IS_ERR(pvdd) && regulator_is_enabled(pvdd)) {

Why do you need these checks? This should be called in correct context, so when regulator is valid and enabled. If you have such checks it suggests that code is buggy and this is being called in wrong contexts.

drivers/nfc/nxp-nci/i2c.c static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = { · line 279

Quoted 5 lines
drivers/nfc/nxp-nci/i2c.c +5 -0
+		r = regulator_disable(pvdd);
+		if (r < 0)
+			dev_warn(dev,
+				 "Failed to disable regulator pvdd: %d\n",
+				 r);

Weird wrapping. Why r is wrapped?

Quoted 12 lines
drivers/nfc/nxp-nci/i2c.c +5 -0
+	}
+}
+
 static int nxp_nci_i2c_probe(struct i2c_client *client)
 {
 	struct device *dev = &client->dev;
@@ -298,6 +315,29 @@ static int nxp_nci_i2c_probe(struct i2c_client *client)
 		return PTR_ERR(phy->gpiod_fw);
 	}
 
+	phy->pvdd = devm_regulator_get_optional(dev, "pvdd");
+	if (IS_ERR(phy->pvdd)) {

drivers/nfc/nxp-nci/i2c.c static int nxp_nci_i2c_probe(struct i2c_client *client) · line 329

Quoted 10 lines
drivers/nfc/nxp-nci/i2c.c +10 -0
+		r = PTR_ERR(phy->pvdd);
+		if (r != -ENODEV)
+			return dev_err_probe(dev, r,
+					     "Failed to get regulator pvdd\n");
+	} else {
+		r = regulator_enable(phy->pvdd);
+		if (r < 0) {
+			nfc_err(dev,
+				"Failed to enable regulator pvdd: %d\n",
+				r);

Weird wrapping. Why r is wrapped?

drivers/nfc/nxp-nci/i2c.c static int nxp_nci_i2c_probe(struct i2c_client *client) · line 337

Quoted 8 lines
drivers/nfc/nxp-nci/i2c.c +8 -0
+			return r;
+		}
+	}
+
+	r = devm_add_action_or_reset(dev, nxp_nci_i2c_poweroff, phy);
+	if (r < 0) {
+		nfc_err(dev, "Failed to install poweroff handler: %d\n",
+			r);

Weird wrapping. Why r is wrapped?

Just move it to the success path of enabling regulator.

drivers/nfc/nxp-nci/i2c.c static void nxp_nci_i2c_remove(struct i2c_client *client) · line 363

Quoted 12 lines
drivers/nfc/nxp-nci/i2c.c +5 -0
+		return r;
+	}
+
 	r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
 			  NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
 	if (r < 0)
@@ -319,6 +359,8 @@ static void nxp_nci_i2c_remove(struct i2c_client *client)
 
 	nxp_nci_remove(phy->ndev);
 	free_irq(client->irq, phy);
+
+	nxp_nci_i2c_poweroff(phy);

Why? This code is buggy...

Best regards,
Krzysztof

#4 lore raw

Hi Krzysztof,

Quoted 29 lines · Krzysztof Kozlowski
Quoted 27 lines · Raymond Hackley

PN547/553, QN310/330 chips on some devices require a pad supply voltage (PVDD). Otherwise, the NFC won't power up.

Implement support for pad supply voltage pvdd-supply that is enabled by the nxp-nci driver so that the regulator gets enabled when needed.

---
drivers/nfc/nxp-nci/i2c.c | 42 +++++++++++++++++++++++++++++++++++++++

1 file changed, 42 insertions(+)
drivers/nfc/nxp-nci/i2c.c +1 -0
index d4c299be7949..1b8877757cee 100644
@@ -35,6 +35,7 @@ struct nxp_nci_i2c_phy {
 
 struct gpio_desc *gpiod_en;
 struct gpio_desc *gpiod_fw;
+ struct regulator *pvdd;
 

int hard_fault; /*
* < 0 if hardware error occurred (e.g. i2c err)

drivers/nfc/nxp-nci/i2c.c +0 -0
@@ -263,6 +264,22 @@ static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = {
 { }
 };
 
Quoted 14 lines

drivers/nfc/nxp-nci/i2c.c static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = { · line 274

Quoted 8 lines
drivers/nfc/nxp-nci/i2c.c +8 -0
+static void nxp_nci_i2c_poweroff(void *data)
+{
+ struct nxp_nci_i2c_phy *phy = data;
+ struct device *dev = &phy->i2c_dev->dev;
+ struct regulator *pvdd = phy->pvdd;
+ int r;
+
+ if (!IS_ERR(pvdd) && regulator_is_enabled(pvdd)) {

Why do you need these checks? This should be called in correct context, so when regulator is valid and enabled. If you have such checks it suggests that code is buggy and this is being called in wrong contexts.

First condition !IS_ERR(pvdd) is to check if pvdd exists. Some devices, msm8916-samsung-serranove for example, doesn't need pvdd or have it bound in the device tree:

https://github.com/torvalds/linux/commit/ab0f0987e035f908d670fed7d86efa6fac66c0bb

Without !IS_ERR(pvdd), checking it with regulator_is_enabled(pvdd):

[   50.161882] 8<--- cut here ---
[   50.161906] Unable to handle kernel paging request at virtual address fffffff9 when read
[   50.161916] [fffffff9] *pgd=affff841, *pte=00000000, *ppte=00000000
[   50.161938] Internal error: Oops: 27 [#1] PREEMPT SMP ARM

Or disabling it directly with regulator_disable(pvdd):

[   69.439827] 8<--- cut here ---
[   69.439841] Unable to handle kernel NULL pointer dereference at virtual address 00000045 when read
[   69.439852] [00000045] *pgd=00000000
[   69.439864] Internal error: Oops: 5 [#1] PREEMPT SMP ARM

Second condition regulator_is_enabled(pvdd) is to make sure that pvdd is disabled with balance.

Similar checks can be found here:

https://elixir.bootlin.com/linux/v6.4-rc5/source/drivers/staging/greybus/arche-apb-ctrl.c#L208

Quoted 45 lines

drivers/nfc/nxp-nci/i2c.c static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = { · line 279

Quoted 5 lines
drivers/nfc/nxp-nci/i2c.c +5 -0
+ r = regulator_disable(pvdd);
+ if (r < 0)
+ dev_warn(dev,
+ "Failed to disable regulator pvdd: %d\n",
+ r);

Weird wrapping. Why r is wrapped?

Quoted 12 lines
drivers/nfc/nxp-nci/i2c.c +5 -0
+ }
+}
+
 static int nxp_nci_i2c_probe(struct i2c_client *client)
 {
 struct device *dev = &client->dev;
@@ -298,6 +315,29 @@ static int nxp_nci_i2c_probe(struct i2c_client *client)
 return PTR_ERR(phy->gpiod_fw);
 }
 
+ phy->pvdd = devm_regulator_get_optional(dev, "pvdd");
+ if (IS_ERR(phy->pvdd)) {

drivers/nfc/nxp-nci/i2c.c static int nxp_nci_i2c_probe(struct i2c_client *client) · line 329

Quoted 10 lines
drivers/nfc/nxp-nci/i2c.c +10 -0
+ r = PTR_ERR(phy->pvdd);
+ if (r != -ENODEV)
+ return dev_err_probe(dev, r,
+ "Failed to get regulator pvdd\n");
+ } else {
+ r = regulator_enable(phy->pvdd);
+ if (r < 0) {
+ nfc_err(dev,
+ "Failed to enable regulator pvdd: %d\n",
+ r);

Weird wrapping. Why r is wrapped?

drivers/nfc/nxp-nci/i2c.c static int nxp_nci_i2c_probe(struct i2c_client *client) · line 337

Quoted 8 lines
drivers/nfc/nxp-nci/i2c.c +8 -0
+ return r;
+ }
+ }
+
+ r = devm_add_action_or_reset(dev, nxp_nci_i2c_poweroff, phy);
+ if (r < 0) {
+ nfc_err(dev, "Failed to install poweroff handler: %d\n",
+ r);
Quoted 4 lines

Weird wrapping. Why r is wrapped?

Just move it to the success path of enabling regulator.

Yes. This will be fixed in v3.

Quoted 8 lines
Quoted 8 lines
drivers/nfc/nxp-nci/i2c.c +3 -0
+ return r;
+ }
+
 r = nxp_nci_probe(phy, &client->dev, &i2c_phy_ops,
 NXP_NCI_I2C_MAX_PAYLOAD, &phy->ndev);
 if (r < 0)
@@ -319,6 +359,8 @@ static void nxp_nci_i2c_remove(struct i2c_client *client)
 
Quoted 8 lines

drivers/nfc/nxp-nci/i2c.c static void nxp_nci_i2c_remove(struct i2c_client *client) · line 363

Quoted 4 lines
drivers/nfc/nxp-nci/i2c.c +2 -0
 nxp_nci_remove(phy->ndev);
 free_irq(client->irq, phy);
+
+ nxp_nci_i2c_poweroff(phy);

Why? This code is buggy...

I don't have a good reason to keep it and will drop it in v3.

Regards,
Raymond

Quoted 4 lines

Best regards,
Krzysztof

#5 lore raw
Quoted 49 lines · Raymond Hackley

Hi Krzysztof,

Quoted 29 lines · Krzysztof Kozlowski
Quoted 27 lines · Raymond Hackley

PN547/553, QN310/330 chips on some devices require a pad supply voltage (PVDD). Otherwise, the NFC won't power up.

Implement support for pad supply voltage pvdd-supply that is enabled by the nxp-nci driver so that the regulator gets enabled when needed.

---
drivers/nfc/nxp-nci/i2c.c | 42 +++++++++++++++++++++++++++++++++++++++

1 file changed, 42 insertions(+)
drivers/nfc/nxp-nci/i2c.c +1 -0
index d4c299be7949..1b8877757cee 100644
@@ -35,6 +35,7 @@ struct nxp_nci_i2c_phy {
 
 struct gpio_desc *gpiod_en;
 struct gpio_desc *gpiod_fw;
+ struct regulator *pvdd;
 

int hard_fault; /*
* < 0 if hardware error occurred (e.g. i2c err)

drivers/nfc/nxp-nci/i2c.c +0 -0
@@ -263,6 +264,22 @@ static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = {
 { }
 };
 
Quoted 14 lines

drivers/nfc/nxp-nci/i2c.c static const struct acpi_gpio_mapping acpi_nxp_nci_gpios[] = { · line 274

Quoted 8 lines
drivers/nfc/nxp-nci/i2c.c +8 -0
+static void nxp_nci_i2c_poweroff(void *data)
+{
+ struct nxp_nci_i2c_phy *phy = data;
+ struct device *dev = &phy->i2c_dev->dev;
+ struct regulator *pvdd = phy->pvdd;
+ int r;
+
+ if (!IS_ERR(pvdd) && regulator_is_enabled(pvdd)) {

Why do you need these checks? This should be called in correct context, so when regulator is valid and enabled. If you have such checks it suggests that code is buggy and this is being called in wrong contexts.

Quoted 3 lines

First condition !IS_ERR(pvdd) is to check if pvdd exists. Some devices, msm8916-samsung-serranove for example, doesn't need pvdd or have it bound in the device tree:

If regulator is missing you should get a dummy.

But anyway the code will not be executed if you don't get proper regulator.

Quoted 12 lines

https://github.com/torvalds/linux/commit/ab0f0987e035f908d670fed7d86efa6fac66c0bb

Without !IS_ERR(pvdd), checking it with regulator_is_enabled(pvdd):

[   50.161882] 8<--- cut here ---
[   50.161906] Unable to handle kernel paging request at virtual address fffffff9 when read
[   50.161916] [fffffff9] *pgd=affff841, *pte=00000000, *ppte=00000000
[   50.161938] Internal error: Oops: 27 [#1] PREEMPT SMP ARM

Or disabling it directly with regulator_disable(pvdd):

Quoted 8 lines
[   69.439827] 8<--- cut here ---
[   69.439841] Unable to handle kernel NULL pointer dereference at virtual address 00000045 when read
[   69.439852] [00000045] *pgd=00000000
[   69.439864] Internal error: Oops: 5 [#1] PREEMPT SMP ARM

Second condition regulator_is_enabled(pvdd) is to make sure that pvdd is disabled with balance.

So you have buggy code and to hide the bug you add checks? No, make the code correct so the check is not needed.

Quoted 3 lines

Similar checks can be found here:

https://elixir.bootlin.com/linux/v6.4-rc5/source/drivers/staging/greybus/arche-apb-ctrl.c#L208

staging driver is not an example...

Quoted 1 line

Best regards,
Krzysztof

#6 lore raw
Quoted 8 lines · Krzysztof Kozlowski
Quoted 4 lines · Raymond Hackley

On Friday, June 9th, 2023 at 3:46 PM, Krzysztof Kozlowski <[email protected]> wrote:

Second condition regulator_is_enabled(pvdd) is to make sure that pvdd is disabled with balance.

So you have buggy code and to hide the bug you add checks? No, make the code correct so the check is not needed.

Specifically your driver should only ever call regulator_disable() to balance out regulator_enable() calls it made itself and it should know how many of those it has done. regulator_is_enabled() should only ever be used during probe if for some reason it is important to figure out if the device is already powered for startup, this should be very unusual. If something else enabled the regualtor then whatever did that needs to undo those enables, not another driver.

#7 lore raw

Hi Mark,

Quoted 7 lines · Mark Brown

Specifically your driver should only ever call regulator_disable() to balance out regulator_enable() calls it made itself and it should know how many of those it has done. regulator_is_enabled() should only ever be used during probe if for some reason it is important to figure out if the device is already powered for startup, this should be very unusual. If something else enabled the regualtor then whatever did that needs to undo those enables, not another driver.

Thnak you for explanation. I should drop regulator_is_enabled() here since it's misused.

Regards,
Raymond

#8 lore raw
In reply to Krzysztof Kozlowski

Hi Krzysztof,

Quoted 4 lines · Krzysztof Kozlowski
Quoted 4 lines
Quoted 3 lines

Why do you need these checks? This should be called in correct context, so when regulator is valid and enabled. If you have such checks it suggests that code is buggy and this is being called in wrong contexts.

Quoted 9 lines
Quoted 3 lines

First condition !IS_ERR(pvdd) is to check if pvdd exists. Some devices, msm8916-samsung-serranove for example, doesn't need pvdd or have it bound in the device tree:

If regulator is missing you should get a dummy.

But anyway the code will not be executed if you don't get proper regulator.

The current patch set is using devm_regulator_get_optional() instead of devm_regulator_get(), which doesn't get a dummy regulator.

Quoted 16 lines
Quoted 16 lines

https://github.com/torvalds/linux/commit/ab0f0987e035f908d670fed7d86efa6fac66c0bb

Without !IS_ERR(pvdd), checking it with regulator_is_enabled(pvdd):

[ 50.161882] 8<--- cut here ---
[ 50.161906] Unable to handle kernel paging request at virtual address fffffff9 when read
[ 50.161916] [fffffff9] *pgd=affff841, *pte=00000000, *ppte=00000000
[ 50.161938] Internal error: Oops: 27 [#1] PREEMPT SMP ARM

Or disabling it directly with regulator_disable(pvdd):

[ 69.439827] 8<--- cut here ---
[ 69.439841] Unable to handle kernel NULL pointer dereference at virtual address 00000045 when read
[ 69.439852] [00000045] *pgd=00000000
[ 69.439864] Internal error: Oops: 5 [#1] PREEMPT SMP ARM
Quoted 7 lines
Quoted 2 lines

Second condition regulator_is_enabled(pvdd) is to make sure that pvdd is disabled with balance.

So you have buggy code and to hide the bug you add checks? No, make the code correct so the check is not needed.

Do you mean that I should use devm_regulator_get() instead in order to get a dummy regulator, so that it can disable pvdd without unnecessary checks? Actually there is v4 with those buggy codes and checks dropped. Please do let me know if I am understanding and doing it correctly. I would send it after proper period of cooldown.

Regards,
Raymond