Re: [PATCH v8 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2021-03-15 Thread Nicolas Saenz Julienne
Hi Uwe,

On Fri, 2021-03-12 at 21:12 +0100, Uwe Kleine-König wrote:
> Hello Nicolas,
> > +// SPDX-License-Identifier: GPL-2.0
> > +/*
> > + * Copyright 2020 Nicolas Saenz Julienne 
> 
> 2021?

Yes.

> > + * For more information on Raspberry Pi's PoE hat see:
> > + * https://www.raspberrypi.org/products/poe-hat/
> 
> Out of personal interest: Is this hat also able to power a RPi CM4?

I haven't tested it, and can't at the moment (no PoE injector available). But
the physical pin layout, and routing in CM4's IO board fits the hat. So I'd say
yes.

> > + * Limitations:
> > + *  - No disable bit, so a disabled PWM is simulated by duty_cycle 0
> > + *  - Only normal polarity
> > + *  - Fixed 12.5 kHz period
> > + *
> > + * The current period is completed when HW is reconfigured.
> > + */
> 
> Other than that as mentioned in the previous round: This looks good,
> 
> Reviewed-by: Uwe Kleine-König 

Thanks!

> What is your thought about how to get this series merged?
> At least input, staging, armsoc, clk, reset anf firmware are touched. Do you
> prepare a branch for merging in the relevant trees (once you have all the
> necessary Acks)?

As per Linusw suggestion I'll send a pull request myself into the SoC tree and
hope for the best. :)

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v8 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2021-03-12 Thread Nicolas Saenz Julienne
Adds support to control the PWM bus available in official Raspberry Pi
PoE HAT. Only RPi's co-processor has access to it, so commands have to
be sent through RPi's firmware mailbox interface.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v7:
 - Remove unwarranted RPI_PWM_DEF_DUTY_REG usage

 Changes since v6:
- Use %pe
- Round divisions properly
- Use dev_err_probe()
- Pass check_patch

Changes since v3:
 - Rename compatible string to be more explicit WRT to bus's limitations

Changes since v2:
 - Use devm_rpi_firmware_get()
 - Rename driver
 - Small cleanups

Changes since v1:
 - Use default pwm bindings and get rid of xlate() function
 - Correct spelling errors
 - Correct apply() function
 - Round values
 - Fix divisions in arm32 mode
 - Small cleanups

 drivers/pwm/Kconfig   |   9 ++
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi-poe.c | 206 ++
 3 files changed, 216 insertions(+)
 create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c

diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index a7a7a9f26aef..d3371ac7b871 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -431,6 +431,15 @@ config PWM_PXA
  To compile this driver as a module, choose M here: the module
  will be called pwm-pxa.
 
+config PWM_RASPBERRYPI_POE
+   tristate "Raspberry Pi Firwmware PoE Hat PWM support"
+   # Make sure not 'y' when RASPBERRYPI_FIRMWARE is 'm'. This can only
+   # happen when COMPILE_TEST=y, hence the added !RASPBERRYPI_FIRMWARE.
+   depends on RASPBERRYPI_FIRMWARE || (COMPILE_TEST && 
!RASPBERRYPI_FIRMWARE)
+   help
+ Enable Raspberry Pi firmware controller PWM bus used to control the
+ official RPI PoE hat
+
 config PWM_RCAR
tristate "Renesas R-Car PWM support"
depends on ARCH_RENESAS || COMPILE_TEST
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index 4e35a55fa7b6..d3879619bd76 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -39,6 +39,7 @@ obj-$(CONFIG_PWM_NTXEC)   += pwm-ntxec.o
 obj-$(CONFIG_PWM_OMAP_DMTIMER) += pwm-omap-dmtimer.o
 obj-$(CONFIG_PWM_PCA9685)  += pwm-pca9685.o
 obj-$(CONFIG_PWM_PXA)  += pwm-pxa.o
+obj-$(CONFIG_PWM_RASPBERRYPI_POE)  += pwm-raspberrypi-poe.o
 obj-$(CONFIG_PWM_RCAR) += pwm-rcar.o
 obj-$(CONFIG_PWM_RENESAS_TPU)  += pwm-renesas-tpu.o
 obj-$(CONFIG_PWM_ROCKCHIP) += pwm-rockchip.o
diff --git a/drivers/pwm/pwm-raspberrypi-poe.c 
b/drivers/pwm/pwm-raspberrypi-poe.c
new file mode 100644
index ..71ade5e55069
--- /dev/null
+++ b/drivers/pwm/pwm-raspberrypi-poe.c
@@ -0,0 +1,206 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2020 Nicolas Saenz Julienne 
+ * For more information on Raspberry Pi's PoE hat see:
+ * https://www.raspberrypi.org/products/poe-hat/
+ *
+ * Limitations:
+ *  - No disable bit, so a disabled PWM is simulated by duty_cycle 0
+ *  - Only normal polarity
+ *  - Fixed 12.5 kHz period
+ *
+ * The current period is completed when HW is reconfigured.
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#define RPI_PWM_MAX_DUTY   255
+#define RPI_PWM_PERIOD_NS  8 /* 12.5 kHz */
+
+#define RPI_PWM_CUR_DUTY_REG   0x0
+
+struct raspberrypi_pwm {
+   struct rpi_firmware *firmware;
+   struct pwm_chip chip;
+   unsigned int duty_cycle;
+};
+
+struct raspberrypi_pwm_prop {
+   __le32 reg;
+   __le32 val;
+   __le32 ret;
+} __packed;
+
+static inline
+struct raspberrypi_pwm *raspberrypi_pwm_from_chip(struct pwm_chip *chip)
+{
+   return container_of(chip, struct raspberrypi_pwm, chip);
+}
+
+static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware,
+   u32 reg, u32 val)
+{
+   struct raspberrypi_pwm_prop msg = {
+   .reg = cpu_to_le32(reg),
+   .val = cpu_to_le32(val),
+   };
+   int ret;
+
+   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
+   , sizeof(msg));
+   if (ret)
+   return ret;
+   if (msg.ret)
+   return -EIO;
+
+   return 0;
+}
+
+static int raspberrypi_pwm_get_property(struct rpi_firmware *firmware,
+   u32 reg, u32 *val)
+{
+   struct raspberrypi_pwm_prop msg = {
+   .reg = reg
+   };
+   int ret;
+
+   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
+   , sizeof(msg));
+   if (ret)
+   return ret;
+   if (msg.ret)
+   return -EIO;
+
+   *val = le32_to_cpu(msg.val);
+
+   return 0;
+}
+
+static void raspberrypi_pwm_get_state(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ struct pwm_state *state)
+{
+   st

[PATCH v8 09/11] dt-bindings: pwm: Add binding for RPi firmware PWM bus

2021-03-12 Thread Nicolas Saenz Julienne
The PWM bus controlling the fan in RPi's official PoE hat can only be
controlled by the board's co-processor.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Rob Herring 
---
Changes since v4:
 - Rename compatible string to be more explicit with the bus' limitations

Changes since v3:
 - Fix example

Changes since v1:
 - Update bindings to use 2 #pwm-cells

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml | 20 +++
 .../pwm/raspberrypi,firmware-poe-pwm.h| 13 
 2 files changed, 33 insertions(+)
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h

diff --git 
a/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml 
b/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
index a2c63c8b1d10..e3664eab0f6a 100644
--- 
a/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
+++ 
b/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
@@ -64,6 +64,21 @@ properties:
   - compatible
   - "#reset-cells"
 
+  pwm:
+type: object
+
+properties:
+  compatible:
+const: raspberrypi,firmware-poe-pwm
+
+  "#pwm-cells":
+# See pwm.yaml in this directory for a description of the cells format.
+const: 2
+
+required:
+  - compatible
+  - "#pwm-cells"
+
 additionalProperties: false
 
 required:
@@ -87,5 +102,10 @@ examples:
 compatible = "raspberrypi,firmware-reset";
 #reset-cells = <1>;
 };
+
+pwm: pwm {
+compatible = "raspberrypi,firmware-poe-pwm";
+#pwm-cells = <2>;
+};
 };
 ...
diff --git a/include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h 
b/include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h
new file mode 100644
index ..27c5ce68847b
--- /dev/null
+++ b/include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2020 Nicolas Saenz Julienne
+ * Author: Nicolas Saenz Julienne 
+ */
+
+#ifndef _DT_BINDINGS_RASPBERRYPI_FIRMWARE_PWM_H
+#define _DT_BINDINGS_RASPBERRYPI_FIRMWARE_PWM_H
+
+#define RASPBERRYPI_FIRMWARE_PWM_POE   0
+#define RASPBERRYPI_FIRMWARE_PWM_NUM   1
+
+#endif
-- 
2.30.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v8 10/11] DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support

2021-03-12 Thread Nicolas Saenz Julienne
This is an example on how to enable the fan on top of RPi's official PoE
hat.

Signed-off-by: Nicolas Saenz Julienne 
---
 arch/arm/boot/dts/bcm2711-rpi-4-b.dts | 54 +++
 1 file changed, 54 insertions(+)

diff --git a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts 
b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
index 3b4ab947492a..6897c7831d09 100644
--- a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
+++ b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
@@ -5,6 +5,7 @@
 #include "bcm283x-rpi-usb-peripheral.dtsi"
 
 #include 
+#include 
 
 / {
compatible = "raspberrypi,4-model-b", "brcm,bcm2711";
@@ -69,6 +70,54 @@ sd_vcc_reg: sd_vcc_reg {
enable-active-high;
gpio = < 6 GPIO_ACTIVE_HIGH>;
};
+
+   fan: pwm-fan {
+   compatible = "pwm-fan";
+   cooling-levels = <0 50 150 255>;
+   #cooling-cells = <2>;
+   pwms = < RASPBERRYPI_FIRMWARE_PWM_POE 8>;
+   };
+
+   thermal-zones {
+   cpu_thermal: cpu-thermal {
+   trips {
+   threshold: trip-point@0 {
+   temperature = <45000>;
+   hysteresis = <5000>;
+   type = "active";
+   };
+
+   target: trip-point@1 {
+   temperature = <5>;
+   hysteresis = <2000>;
+   type = "active";
+   };
+
+   cpu_hot: cpu_hot@0 {
+   temperature = <55000>;
+   hysteresis = <2000>;
+   type = "active";
+   };
+   };
+
+   cooling-maps {
+   map0 {
+   trip = <>;
+   cooling-device = < 0 1>;
+   };
+
+   map1 {
+   trip = <>;
+   cooling-device = < 1 2>;
+   };
+
+   map2 {
+   trip = <_hot>;
+   cooling-device = < 2 3>;
+   };
+   };
+   };
+   };
 };
 
  {
@@ -104,6 +153,11 @@ reset: reset {
compatible = "raspberrypi,firmware-reset";
#reset-cells = <1>;
};
+
+   fwpwm: pwm {
+   compatible = "raspberrypi,firmware-poe-pwm";
+   #pwm-cells = <2>;
+   };
 };
 
  {
-- 
2.30.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v8 08/11] input: raspberrypi-ts: Release firmware handle when not needed

2021-03-12 Thread Nicolas Saenz Julienne
There is no use for the firmware interface after getting the touch
buffer address, so release it.

Signed-off-by: Nicolas Saenz Julienne 
Acked-by: Dmitry Torokhov 
Reviewed-by: Florian Fainelli 
---

Changes since v5:
 - Correct commit message

Changes since v3:
 - Release firmware handle in probe function

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/input/touchscreen/raspberrypi-ts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/raspberrypi-ts.c 
b/drivers/input/touchscreen/raspberrypi-ts.c
index ef6aaed217cf..5000f5fd9ec3 100644
--- a/drivers/input/touchscreen/raspberrypi-ts.c
+++ b/drivers/input/touchscreen/raspberrypi-ts.c
@@ -160,7 +160,7 @@ static int rpi_ts_probe(struct platform_device *pdev)
touchbuf = (u32)ts->fw_regs_phys;
error = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF,
  , sizeof(touchbuf));
-
+   rpi_firmware_put(fw);
if (error || touchbuf != 0) {
dev_warn(dev, "Failed to set touchbuf, %d\n", error);
return error;
-- 
2.30.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v8 07/11] staging: vchiq: Release firmware handle on unbind

2021-03-12 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
Reviewed-by: Greg Kroah-Hartman 
---
 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c 
b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
index 8b2b4771f420..e39897c38e6a 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
@@ -2738,7 +2738,7 @@ static int vchiq_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   drvdata->fw = rpi_firmware_get(fw_node);
+   drvdata->fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!drvdata->fw)
return -EPROBE_DEFER;
-- 
2.30.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v8 06/11] soc: bcm: raspberrypi-power: Release firmware handle on unbind

2021-03-12 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
---
 drivers/soc/bcm/raspberrypi-power.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/soc/bcm/raspberrypi-power.c 
b/drivers/soc/bcm/raspberrypi-power.c
index 5d1aacdd84ef..068715d6e66d 100644
--- a/drivers/soc/bcm/raspberrypi-power.c
+++ b/drivers/soc/bcm/raspberrypi-power.c
@@ -177,7 +177,7 @@ static int rpi_power_probe(struct platform_device *pdev)
return -ENODEV;
}
 
-   rpi_domains->fw = rpi_firmware_get(fw_np);
+   rpi_domains->fw = devm_rpi_firmware_get(>dev, fw_np);
of_node_put(fw_np);
if (!rpi_domains->fw)
return -EPROBE_DEFER;
-- 
2.30.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v8 05/11] reset: raspberrypi: Release firmware handle on unbind

2021-03-12 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
---
 drivers/reset/reset-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/reset/reset-raspberrypi.c 
b/drivers/reset/reset-raspberrypi.c
index 02f59c06f69b..fa23db554bcf 100644
--- a/drivers/reset/reset-raspberrypi.c
+++ b/drivers/reset/reset-raspberrypi.c
@@ -82,7 +82,7 @@ static int rpi_reset_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(np);
+   fw = devm_rpi_firmware_get(>dev, np);
of_node_put(np);
if (!fw)
return -EPROBE_DEFER;
-- 
2.30.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v8 04/11] gpio: raspberrypi-exp: Release firmware handle on unbind

2021-03-12 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
Acked-by: Bartosz Golaszewski 
---
 drivers/gpio/gpio-raspberrypi-exp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpio/gpio-raspberrypi-exp.c 
b/drivers/gpio/gpio-raspberrypi-exp.c
index bb100e0124e6..64a552ecc2ad 100644
--- a/drivers/gpio/gpio-raspberrypi-exp.c
+++ b/drivers/gpio/gpio-raspberrypi-exp.c
@@ -208,7 +208,7 @@ static int rpi_exp_gpio_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(fw_node);
+   fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!fw)
return -EPROBE_DEFER;
-- 
2.30.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v8 03/11] clk: bcm: rpi: Release firmware handle on unbind

2021-03-12 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
Acked-by: Stephen Boyd 

 drivers/clk/bcm/clk-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
---
 drivers/clk/bcm/clk-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/bcm/clk-raspberrypi.c 
b/drivers/clk/bcm/clk-raspberrypi.c
index f89b9cfc4309..dd3b71eafabf 100644
--- a/drivers/clk/bcm/clk-raspberrypi.c
+++ b/drivers/clk/bcm/clk-raspberrypi.c
@@ -314,7 +314,7 @@ static int raspberrypi_clk_probe(struct platform_device 
*pdev)
return -ENOENT;
}
 
-   firmware = rpi_firmware_get(firmware_node);
+   firmware = devm_rpi_firmware_get(>dev, firmware_node);
of_node_put(firmware_node);
if (!firmware)
return -EPROBE_DEFER;
-- 
2.30.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v8 02/11] firmware: raspberrypi: Introduce devm_rpi_firmware_get()

2021-03-12 Thread Nicolas Saenz Julienne
It'll simplify the firmware handling for most consumers.

Suggested-by: Bartosz Golaszewski 
Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
Reviewed-by: Bartosz Golaszewski 
---

Changes since v4:
 - Rearrange function calls for clarity, same functionality

Changes since v2:
- Create devm_rpi_firmware_get()

 drivers/firmware/raspberrypi.c | 29 ++
 include/soc/bcm2835/raspberrypi-firmware.h |  8 ++
 2 files changed, 37 insertions(+)

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index b65e4c495772..250e01680742 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -243,6 +243,13 @@ void rpi_firmware_put(struct rpi_firmware *fw)
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_put);
 
+static void devm_rpi_firmware_put(void *data)
+{
+   struct rpi_firmware *fw = data;
+
+   rpi_firmware_put(fw);
+}
+
 static int rpi_firmware_probe(struct platform_device *pdev)
 {
struct device *dev = >dev;
@@ -331,6 +338,28 @@ struct rpi_firmware *rpi_firmware_get(struct device_node 
*firmware_node)
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_get);
 
+/**
+ * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
+ * @firmware_node:Pointer to the firmware Device Tree node.
+ *
+ * Returns NULL is the firmware device is not ready.
+ */
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+  struct device_node *firmware_node)
+{
+   struct rpi_firmware *fw;
+
+   fw = rpi_firmware_get(firmware_node);
+   if (!fw)
+   return NULL;
+
+   if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
+   return NULL;
+
+   return fw;
+}
+EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
+
 static const struct of_device_id rpi_firmware_of_match[] = {
{ .compatible = "raspberrypi,bcm2835-firmware", },
{},
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
b/include/soc/bcm2835/raspberrypi-firmware.h
index fdfef7fe40df..73ad784fca96 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -142,6 +142,8 @@ int rpi_firmware_property_list(struct rpi_firmware *fw,
   void *data, size_t tag_size);
 void rpi_firmware_put(struct rpi_firmware *fw);
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+  struct device_node *firmware_node);
 #else
 static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
void *data, size_t len)
@@ -160,6 +162,12 @@ static inline struct rpi_firmware *rpi_firmware_get(struct 
device_node *firmware
 {
return NULL;
 }
+
+static inline struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+   struct device_node *firmware_node)
+{
+   return NULL;
+}
 #endif
 
 #endif /* __SOC_RASPBERRY_FIRMWARE_H__ */
-- 
2.30.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v8 00/11] Raspberry Pi PoE HAT fan support

2021-03-12 Thread Nicolas Saenz Julienne
The aim of this series is to add support to the fan found on RPi's PoE
HAT. Some commentary on the design can be found below. But the important
part to the people CC'd here not involved with PWM is that, in order to
achieve this properly, we also have to fix the firmware interface the
driver uses to communicate with the PWM bus (and many other low level
functions). Specifically, we have to make sure the firmware interface
isn't unbound while consumers are still up. So, patch #1 & #2 introduce
reference counting in the firmware interface driver and patches #3 to #8
update all firmware users. Patches #9 to #11 introduce the new PWM
driver.

I sent everything as a single series as the final version of the PWM
drivers depends on the firmware fixes, but I'll be happy to split this
into two separate series if you think it's better.

--- Original cover letter below ---

This series aims at adding support to RPi's official PoE HAT fan[1].

The HW setup is the following:

| Raspberry Pi   | PoE HAT|
 arm core -> Mailbox -> RPi co-processor -> I2C -> Atmel MCU -> PWM -> FAN

The arm cores have only access to the mailbox interface, as i2c0, even if
physically accessible, is to be used solely by the co-processor
(VideoCore 4/6).

This series implements a PWM bus, and has pwm-fan sitting on top of it as per
this discussion: https://lkml.org/lkml/2018/9/2/486. Although this design has a
series of shortcomings:

- It depends on a DT binding: it's not flexible if a new hat shows up with new
  functionality, we're not 100% sure we'll be able to expand it without
  breaking backwards compatibility. But without it we can't make use of DT
  thermal-zones, which IMO is overkill.

- We're using pwm-fan, writing a hwmon driver would, again, give us more
  flexibility, but it's not really needed at the moment.

I personally think that it's not worth the effort, it's unlikely we'll get
things right in advance. And ultimately, if the RPi people come up with
something new, we can always write a new driver/bindings from scratch (as in
not reusing previous code).

That said, I'm more than happy to change things if there is a consensus that
another design will do the trick.

[1] https://www.raspberrypi.org/blog/introducing-power-over-ethernet-poe-hat/

---

Changes since v7:
 - Remove unwarranted RPI_PWM_DEF_DUTY_REG usage

Changes since v6:
 - Address PWM driver comments

Changes since v5:
 - Small cleanups
 - Add extra code comments

Changes since v4:
 - Cleanup devm calls
 - Rename compatible string so it's unique to the PoE HAT

Changes since v3:
 - Split first patch, #1 introduces refcount, then #2 the devm function
 - Fix touchscreen function
 - Use kref

Changes since v2:
 - Introduce devm_rpi_firmware_get()
 - Small cleanups in PWM driver

Changes since v1:
 - Address PWM driver changes
 - Fix binding, now with 2 cells

Nicolas Saenz Julienne (11):
  firmware: raspberrypi: Keep count of all consumers
  firmware: raspberrypi: Introduce devm_rpi_firmware_get()
  clk: bcm: rpi: Release firmware handle on unbind
  gpio: raspberrypi-exp: Release firmware handle on unbind
  reset: raspberrypi: Release firmware handle on unbind
  soc: bcm: raspberrypi-power: Release firmware handle on unbind
  staging: vchiq: Release firmware handle on unbind
  input: raspberrypi-ts: Release firmware handle when not needed
  dt-bindings: pwm: Add binding for RPi firmware PWM bus
  DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support
  pwm: Add Raspberry Pi Firmware based PWM bus

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml |  20 ++
 arch/arm/boot/dts/bcm2711-rpi-4-b.dts |  54 +
 drivers/clk/bcm/clk-raspberrypi.c |   2 +-
 drivers/firmware/raspberrypi.c|  69 +-
 drivers/gpio/gpio-raspberrypi-exp.c   |   2 +-
 drivers/input/touchscreen/raspberrypi-ts.c|   2 +-
 drivers/pwm/Kconfig   |   9 +
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi-poe.c | 206 ++
 drivers/reset/reset-raspberrypi.c |   2 +-
 drivers/soc/bcm/raspberrypi-power.c   |   2 +-
 .../interface/vchiq_arm/vchiq_arm.c   |   2 +-
 .../pwm/raspberrypi,firmware-poe-pwm.h|  13 ++
 include/soc/bcm2835/raspberrypi-firmware.h|  10 +
 14 files changed, 385 insertions(+), 9 deletions(-)
 create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h

-- 
2.30.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v8 01/11] firmware: raspberrypi: Keep count of all consumers

2021-03-12 Thread Nicolas Saenz Julienne
When unbinding the firmware device we need to make sure it has no
consumers left. Otherwise we'd leave them with a firmware handle
pointing at freed memory.

Keep a reference count of all consumers and introduce rpi_firmware_put()
which will permit automatically decrease the reference count upon
unbinding consumer drivers.

Suggested-by: Uwe Kleine-König 
Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
Reviewed-by: Stephen Boyd 
Reviewed-by: Bartosz Golaszewski 
---

Changes since v5:
 - Add comment to avoid people blindly switching the memory allocation
   to the devm variant.
 - Fix function documentation as per Florian's comment.

Changes since v3:
 - Use kref instead of waiting on refcount

 drivers/firmware/raspberrypi.c | 40 --
 include/soc/bcm2835/raspberrypi-firmware.h |  2 ++
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index 30259dc9b805..b65e4c495772 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -7,6 +7,7 @@
  */
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -27,6 +28,8 @@ struct rpi_firmware {
struct mbox_chan *chan; /* The property channel. */
struct completion c;
u32 enabled;
+
+   struct kref consumers;
 };
 
 static DEFINE_MUTEX(transaction_lock);
@@ -225,12 +228,31 @@ static void rpi_register_clk_driver(struct device *dev)
-1, NULL, 0);
 }
 
+static void rpi_firmware_delete(struct kref *kref)
+{
+   struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
+  consumers);
+
+   mbox_free_channel(fw->chan);
+   kfree(fw);
+}
+
+void rpi_firmware_put(struct rpi_firmware *fw)
+{
+   kref_put(>consumers, rpi_firmware_delete);
+}
+EXPORT_SYMBOL_GPL(rpi_firmware_put);
+
 static int rpi_firmware_probe(struct platform_device *pdev)
 {
struct device *dev = >dev;
struct rpi_firmware *fw;
 
-   fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
+   /*
+* Memory will be freed by rpi_firmware_delete() once all users have
+* released their firmware handles. Don't use devm_kzalloc() here.
+*/
+   fw = kzalloc(sizeof(*fw), GFP_KERNEL);
if (!fw)
return -ENOMEM;
 
@@ -247,6 +269,7 @@ static int rpi_firmware_probe(struct platform_device *pdev)
}
 
init_completion(>c);
+   kref_init(>consumers);
 
platform_set_drvdata(pdev, fw);
 
@@ -275,7 +298,8 @@ static int rpi_firmware_remove(struct platform_device *pdev)
rpi_hwmon = NULL;
platform_device_unregister(rpi_clk);
rpi_clk = NULL;
-   mbox_free_channel(fw->chan);
+
+   rpi_firmware_put(fw);
 
return 0;
 }
@@ -284,16 +308,26 @@ static int rpi_firmware_remove(struct platform_device 
*pdev)
  * rpi_firmware_get - Get pointer to rpi_firmware structure.
  * @firmware_node:Pointer to the firmware Device Tree node.
  *
+ * The reference to rpi_firmware has to be released with rpi_firmware_put().
+ *
  * Returns NULL is the firmware device is not ready.
  */
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
 {
struct platform_device *pdev = of_find_device_by_node(firmware_node);
+   struct rpi_firmware *fw;
 
if (!pdev)
return NULL;
 
-   return platform_get_drvdata(pdev);
+   fw = platform_get_drvdata(pdev);
+   if (!fw)
+   return NULL;
+
+   if (!kref_get_unless_zero(>consumers))
+   return NULL;
+
+   return fw;
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_get);
 
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
b/include/soc/bcm2835/raspberrypi-firmware.h
index cc9cdbc66403..fdfef7fe40df 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -140,6 +140,7 @@ int rpi_firmware_property(struct rpi_firmware *fw,
  u32 tag, void *data, size_t len);
 int rpi_firmware_property_list(struct rpi_firmware *fw,
   void *data, size_t tag_size);
+void rpi_firmware_put(struct rpi_firmware *fw);
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
 #else
 static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
@@ -154,6 +155,7 @@ static inline int rpi_firmware_property_list(struct 
rpi_firmware *fw,
return -ENOSYS;
 }
 
+static inline void rpi_firmware_put(struct rpi_firmware *fw) { }
 static inline struct rpi_firmware *rpi_firmware_get(struct device_node 
*firmware_node)
 {
return NULL;
-- 
2.30.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v7 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2021-03-11 Thread Nicolas Saenz Julienne
On Thu, 2021-03-11 at 14:18 +0100, Uwe Kleine-König wrote:
> Hello Nicolas,
> 
> On Thu, Mar 11, 2021 at 02:01:00PM +0100, Nicolas Saenz Julienne wrote:
> > On Wed, 2021-03-10 at 12:50 +0100, Uwe Kleine-König wrote:
> > > On Mon, Jan 18, 2021 at 01:32:44PM +0100, Nicolas Saenz Julienne wrote:
> > 
> > [...]
> > 
> > > > +   /*
> > > > +* This sets the default duty cycle after resetting the board, 
> > > > we
> > > > +* updated it every time to mimic Raspberry Pi's downstream's 
> > > > driver
> > > > +* behaviour.
> > > > +*/
> > > > +   ret = raspberrypi_pwm_set_property(rpipwm->firmware, 
> > > > RPI_PWM_DEF_DUTY_REG,
> > > > +  duty_cycle);
> > > > +   if (ret) {
> > > > +   dev_err(chip->dev, "Failed to set default duty cycle: 
> > > > %pe\n",
> > > > +   ERR_PTR(ret));
> > > > +   return ret;
> > > 
> > > This only has an effect for the next reboot, right?
> > 
> > It effects all reboots until it's further changed.
> > 
> > > If so I wonder if it is a good idea in general. (Think: The current PWM
> > > setting enables a motor that makes a self-driving car move at 100 km/h.
> > > Consider the rpi crashes, do I want to car to pick up driving 100 km/h at
> > > power up even before Linux is up again?)
> > 
> > I get your point. But this isn't used as a general purpose PWM. For now the
> > interface is solely there to drive a PWM fan that's arguably harmless. This
> > doesn't mean that the RPi foundation will not reuse the firmware interface 
> > for
> > other means in the future. In such case we can always use a new DT 
> > compatible
> > and bypass this feature (the current DT string is
> > 'raspberrypi,firmware-poe-pwm', which is specific to this use-case).
> > 
> > My aim here is to be on par feature wise with RPi's downstream 
> > implementation.
> 
> Just because the downstream kernel does it should not be the (single)
> reason to do that. My gut feeling is: For a motor restoring the PWM
> config on reboot is bad and for a fan it doesn't really hurt if it
> doesn't restart automatically. So I'd prefer to to drop this feature.

Fair enough, I'll remove it then.

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v7 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2021-03-11 Thread Nicolas Saenz Julienne
Hi Uwe,
thanks for taking the time to look into this. :)

On Wed, 2021-03-10 at 12:50 +0100, Uwe Kleine-König wrote:
> Hello Nicolas,
> 
> On Mon, Jan 18, 2021 at 01:32:44PM +0100, Nicolas Saenz Julienne wrote:

[...]

> > +   /*
> > +* This sets the default duty cycle after resetting the board, we
> > +* updated it every time to mimic Raspberry Pi's downstream's driver
> > +* behaviour.
> > +*/
> > +   ret = raspberrypi_pwm_set_property(rpipwm->firmware, 
> > RPI_PWM_DEF_DUTY_REG,
> > +  duty_cycle);
> > +   if (ret) {
> > +   dev_err(chip->dev, "Failed to set default duty cycle: %pe\n",
> > +   ERR_PTR(ret));
> > +   return ret;
> 
> This only has an effect for the next reboot, right?

It effects all reboots until it's further changed.

> If so I wonder if it is a good idea in general. (Think: The current PWM
> setting enables a motor that makes a self-driving car move at 100 km/h.
> Consider the rpi crashes, do I want to car to pick up driving 100 km/h at
> power up even before Linux is up again?)

I get your point. But this isn't used as a general purpose PWM. For now the
interface is solely there to drive a PWM fan that's arguably harmless. This
doesn't mean that the RPi foundation will not reuse the firmware interface for
other means in the future. In such case we can always use a new DT compatible
and bypass this feature (the current DT string is
'raspberrypi,firmware-poe-pwm', which is specific to this use-case).

My aim here is to be on par feature wise with RPi's downstream implementation.
So as for them to be able to use it as is and avoid duplication. Now, if this
is blocking the driver from being merged, I'd rather remove it. It'll be a
patch for the downstream kernel to maintain, but better than nothing.

> And if we agree it's a good idea: Should raspberrypi_pwm_apply return 0 if
> setting the duty cycle succeeded and only setting the default didn't?

Good point. I don't think so. We'd be also missing on the following by
returning early:

rpipwm->duty_cycle = duty_cycle;

I propose to change it to a 'best effort' approach, if it fails, log it and
continue successfully.

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v7 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2021-03-09 Thread Nicolas Saenz Julienne
On Mon, 2021-01-18 at 13:32 +0100, Nicolas Saenz Julienne wrote:
> Adds support to control the PWM bus available in official Raspberry Pi
> PoE HAT. Only RPi's co-processor has access to it, so commands have to
> be sent through RPi's firmware mailbox interface.
> 
> Signed-off-by: Nicolas Saenz Julienne 
> 
> ---

ping :)

> Changes since v6:
> - Use %pe
> - Round divisions properly
> - Use dev_err_probe()
> - Pass check_patch
> 
> Changes since v3:
>  - Rename compatible string to be more explicit WRT to bus's limitations
> 
> Changes since v2:
>  - Use devm_rpi_firmware_get()
>  - Rename driver
>  - Small cleanups
> 
> Changes since v1:
>  - Use default pwm bindings and get rid of xlate() function
>  - Correct spelling errors
>  - Correct apply() function
>  - Round values
>  - Fix divisions in arm32 mode
>  - Small cleanups
> 
>  drivers/pwm/Kconfig   |   9 ++
>  drivers/pwm/Makefile  |   1 +
>  drivers/pwm/pwm-raspberrypi-poe.c | 220 ++
>  3 files changed, 230 insertions(+)
>  create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c
> 
> diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
> index 0937e1c047ac..75e2344703b3 100644
> --- a/drivers/pwm/Kconfig
> +++ b/drivers/pwm/Kconfig
> @@ -423,6 +423,15 @@ config PWM_PXA
>     To compile this driver as a module, choose M here: the module
>     will be called pwm-pxa.
>  
> 
> +config PWM_RASPBERRYPI_POE
> + tristate "Raspberry Pi Firwmware PoE Hat PWM support"
> + # Make sure not 'y' when RASPBERRYPI_FIRMWARE is 'm'. This can only
> + # happen when COMPILE_TEST=y, hence the added !RASPBERRYPI_FIRMWARE.
> + depends on RASPBERRYPI_FIRMWARE || (COMPILE_TEST && 
> !RASPBERRYPI_FIRMWARE)
> + help
> +   Enable Raspberry Pi firmware controller PWM bus used to control the
> +   official RPI PoE hat
> +
>  config PWM_RCAR
>   tristate "Renesas R-Car PWM support"
>   depends on ARCH_RENESAS || COMPILE_TEST
> diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
> index 18b89d7fd092..ed28d7bd4c64 100644
> --- a/drivers/pwm/Makefile
> +++ b/drivers/pwm/Makefile
> @@ -38,6 +38,7 @@ obj-$(CONFIG_PWM_MXS)   += pwm-mxs.o
>  obj-$(CONFIG_PWM_OMAP_DMTIMER)   += pwm-omap-dmtimer.o
>  obj-$(CONFIG_PWM_PCA9685)+= pwm-pca9685.o
>  obj-$(CONFIG_PWM_PXA)+= pwm-pxa.o
> +obj-$(CONFIG_PWM_RASPBERRYPI_POE)+= pwm-raspberrypi-poe.o
>  obj-$(CONFIG_PWM_RCAR)   += pwm-rcar.o
>  obj-$(CONFIG_PWM_RENESAS_TPU)+= pwm-renesas-tpu.o
>  obj-$(CONFIG_PWM_ROCKCHIP)   += pwm-rockchip.o
> diff --git a/drivers/pwm/pwm-raspberrypi-poe.c 
> b/drivers/pwm/pwm-raspberrypi-poe.c
> new file mode 100644
> index ..ca845e8fabe6
> --- /dev/null
> +++ b/drivers/pwm/pwm-raspberrypi-poe.c
> @@ -0,0 +1,220 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright 2020 Nicolas Saenz Julienne 
> + * For more information on Raspberry Pi's PoE hat see:
> + * https://www.raspberrypi.org/products/poe-hat/
> + *
> + * Limitations:
> + *  - No disable bit, so a disabled PWM is simulated by duty_cycle 0
> + *  - Only normal polarity
> + *  - Fixed 12.5 kHz period
> + *
> + * The current period is completed when HW is reconfigured.
> + */
> +
> +#include 
> +#include 
> +#include 
> +#include 
> +
> +#include 
> +#include 
> +
> +#define RPI_PWM_MAX_DUTY 255
> +#define RPI_PWM_PERIOD_NS8 /* 12.5 kHz */
> +
> +#define RPI_PWM_CUR_DUTY_REG 0x0
> +#define RPI_PWM_DEF_DUTY_REG 0x1
> +
> +struct raspberrypi_pwm {
> + struct rpi_firmware *firmware;
> + struct pwm_chip chip;
> + unsigned int duty_cycle;
> +};
> +
> +struct raspberrypi_pwm_prop {
> + __le32 reg;
> + __le32 val;
> + __le32 ret;
> +} __packed;
> +
> +static inline
> +struct raspberrypi_pwm *raspberrypi_pwm_from_chip(struct pwm_chip *chip)
> +{
> + return container_of(chip, struct raspberrypi_pwm, chip);
> +}
> +
> +static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware,
> + u32 reg, u32 val)
> +{
> + struct raspberrypi_pwm_prop msg = {
> + .reg = cpu_to_le32(reg),
> + .val = cpu_to_le32(val),
> + };
> + int ret;
> +
> + ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
> + , sizeof(msg));
> + if (ret)
> + return ret;
> + if (msg.ret)
> + return -EIO;
> +
> + return 0;
> +}
> +
> +stati

Re: [PATCH v7 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2021-02-08 Thread Nicolas Saenz Julienne
On Mon, 2021-01-18 at 13:32 +0100, Nicolas Saenz Julienne wrote:
> Adds support to control the PWM bus available in official Raspberry Pi
> PoE HAT. Only RPi's co-processor has access to it, so commands have to
> be sent through RPi's firmware mailbox interface.
> 
> Signed-off-by: Nicolas Saenz Julienne 
> 
> ---

ping :)

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v7 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2021-01-18 Thread Nicolas Saenz Julienne
Adds support to control the PWM bus available in official Raspberry Pi
PoE HAT. Only RPi's co-processor has access to it, so commands have to
be sent through RPi's firmware mailbox interface.

Signed-off-by: Nicolas Saenz Julienne 

---
Changes since v6:
- Use %pe
- Round divisions properly
- Use dev_err_probe()
- Pass check_patch

Changes since v3:
 - Rename compatible string to be more explicit WRT to bus's limitations

Changes since v2:
 - Use devm_rpi_firmware_get()
 - Rename driver
 - Small cleanups

Changes since v1:
 - Use default pwm bindings and get rid of xlate() function
 - Correct spelling errors
 - Correct apply() function
 - Round values
 - Fix divisions in arm32 mode
 - Small cleanups

 drivers/pwm/Kconfig   |   9 ++
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi-poe.c | 220 ++
 3 files changed, 230 insertions(+)
 create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c

diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 0937e1c047ac..75e2344703b3 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -423,6 +423,15 @@ config PWM_PXA
  To compile this driver as a module, choose M here: the module
  will be called pwm-pxa.
 
+config PWM_RASPBERRYPI_POE
+   tristate "Raspberry Pi Firwmware PoE Hat PWM support"
+   # Make sure not 'y' when RASPBERRYPI_FIRMWARE is 'm'. This can only
+   # happen when COMPILE_TEST=y, hence the added !RASPBERRYPI_FIRMWARE.
+   depends on RASPBERRYPI_FIRMWARE || (COMPILE_TEST && 
!RASPBERRYPI_FIRMWARE)
+   help
+ Enable Raspberry Pi firmware controller PWM bus used to control the
+ official RPI PoE hat
+
 config PWM_RCAR
tristate "Renesas R-Car PWM support"
depends on ARCH_RENESAS || COMPILE_TEST
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index 18b89d7fd092..ed28d7bd4c64 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -38,6 +38,7 @@ obj-$(CONFIG_PWM_MXS) += pwm-mxs.o
 obj-$(CONFIG_PWM_OMAP_DMTIMER) += pwm-omap-dmtimer.o
 obj-$(CONFIG_PWM_PCA9685)  += pwm-pca9685.o
 obj-$(CONFIG_PWM_PXA)  += pwm-pxa.o
+obj-$(CONFIG_PWM_RASPBERRYPI_POE)  += pwm-raspberrypi-poe.o
 obj-$(CONFIG_PWM_RCAR) += pwm-rcar.o
 obj-$(CONFIG_PWM_RENESAS_TPU)  += pwm-renesas-tpu.o
 obj-$(CONFIG_PWM_ROCKCHIP) += pwm-rockchip.o
diff --git a/drivers/pwm/pwm-raspberrypi-poe.c 
b/drivers/pwm/pwm-raspberrypi-poe.c
new file mode 100644
index ..ca845e8fabe6
--- /dev/null
+++ b/drivers/pwm/pwm-raspberrypi-poe.c
@@ -0,0 +1,220 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2020 Nicolas Saenz Julienne 
+ * For more information on Raspberry Pi's PoE hat see:
+ * https://www.raspberrypi.org/products/poe-hat/
+ *
+ * Limitations:
+ *  - No disable bit, so a disabled PWM is simulated by duty_cycle 0
+ *  - Only normal polarity
+ *  - Fixed 12.5 kHz period
+ *
+ * The current period is completed when HW is reconfigured.
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#define RPI_PWM_MAX_DUTY   255
+#define RPI_PWM_PERIOD_NS  8 /* 12.5 kHz */
+
+#define RPI_PWM_CUR_DUTY_REG   0x0
+#define RPI_PWM_DEF_DUTY_REG   0x1
+
+struct raspberrypi_pwm {
+   struct rpi_firmware *firmware;
+   struct pwm_chip chip;
+   unsigned int duty_cycle;
+};
+
+struct raspberrypi_pwm_prop {
+   __le32 reg;
+   __le32 val;
+   __le32 ret;
+} __packed;
+
+static inline
+struct raspberrypi_pwm *raspberrypi_pwm_from_chip(struct pwm_chip *chip)
+{
+   return container_of(chip, struct raspberrypi_pwm, chip);
+}
+
+static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware,
+   u32 reg, u32 val)
+{
+   struct raspberrypi_pwm_prop msg = {
+   .reg = cpu_to_le32(reg),
+   .val = cpu_to_le32(val),
+   };
+   int ret;
+
+   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
+   , sizeof(msg));
+   if (ret)
+   return ret;
+   if (msg.ret)
+   return -EIO;
+
+   return 0;
+}
+
+static int raspberrypi_pwm_get_property(struct rpi_firmware *firmware,
+   u32 reg, u32 *val)
+{
+   struct raspberrypi_pwm_prop msg = {
+   .reg = reg
+   };
+   int ret;
+
+   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
+   , sizeof(msg));
+   if (ret)
+   return ret;
+   if (msg.ret)
+   return -EIO;
+
+   *val = le32_to_cpu(msg.val);
+
+   return 0;
+}
+
+static void raspberrypi_pwm_get_state(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ struct pwm_state *state)
+{
+   struct raspberrypi_pwm *rpipwm = ra

[PATCH v7 07/11] staging: vchiq: Release firmware handle on unbind

2021-01-18 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
---
 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c 
b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
index 59e45dc03a97..f27072837247 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
@@ -2734,7 +2734,7 @@ static int vchiq_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   drvdata->fw = rpi_firmware_get(fw_node);
+   drvdata->fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!drvdata->fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v7 09/11] dt-bindings: pwm: Add binding for RPi firmware PWM bus

2021-01-18 Thread Nicolas Saenz Julienne
The PWM bus controlling the fan in RPi's official PoE hat can only be
controlled by the board's co-processor.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Rob Herring 

---
Changes since v4:
 - Rename compatible string to be more explicit with the bus' limitations

Changes since v3:
 - Fix example

Changes since v1:
 - Update bindings to use 2 #pwm-cells

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml | 20 +++
 .../pwm/raspberrypi,firmware-poe-pwm.h| 13 
 2 files changed, 33 insertions(+)
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h

diff --git 
a/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml 
b/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
index a2c63c8b1d10..e3664eab0f6a 100644
--- 
a/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
+++ 
b/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
@@ -64,6 +64,21 @@ properties:
   - compatible
   - "#reset-cells"
 
+  pwm:
+type: object
+
+properties:
+  compatible:
+const: raspberrypi,firmware-poe-pwm
+
+  "#pwm-cells":
+# See pwm.yaml in this directory for a description of the cells format.
+const: 2
+
+required:
+  - compatible
+  - "#pwm-cells"
+
 additionalProperties: false
 
 required:
@@ -87,5 +102,10 @@ examples:
 compatible = "raspberrypi,firmware-reset";
 #reset-cells = <1>;
 };
+
+pwm: pwm {
+compatible = "raspberrypi,firmware-poe-pwm";
+#pwm-cells = <2>;
+};
 };
 ...
diff --git a/include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h 
b/include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h
new file mode 100644
index ..27c5ce68847b
--- /dev/null
+++ b/include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2020 Nicolas Saenz Julienne
+ * Author: Nicolas Saenz Julienne 
+ */
+
+#ifndef _DT_BINDINGS_RASPBERRYPI_FIRMWARE_PWM_H
+#define _DT_BINDINGS_RASPBERRYPI_FIRMWARE_PWM_H
+
+#define RASPBERRYPI_FIRMWARE_PWM_POE   0
+#define RASPBERRYPI_FIRMWARE_PWM_NUM   1
+
+#endif
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v7 06/11] soc: bcm: raspberrypi-power: Release firmware handle on unbind

2021-01-18 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
---
 drivers/soc/bcm/raspberrypi-power.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/soc/bcm/raspberrypi-power.c 
b/drivers/soc/bcm/raspberrypi-power.c
index 5d1aacdd84ef..068715d6e66d 100644
--- a/drivers/soc/bcm/raspberrypi-power.c
+++ b/drivers/soc/bcm/raspberrypi-power.c
@@ -177,7 +177,7 @@ static int rpi_power_probe(struct platform_device *pdev)
return -ENODEV;
}
 
-   rpi_domains->fw = rpi_firmware_get(fw_np);
+   rpi_domains->fw = devm_rpi_firmware_get(>dev, fw_np);
of_node_put(fw_np);
if (!rpi_domains->fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v7 10/11] DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support

2021-01-18 Thread Nicolas Saenz Julienne
This is an example on how to enable the fan on top of RPi's official PoE
hat.

Signed-off-by: Nicolas Saenz Julienne 
---
 arch/arm/boot/dts/bcm2711-rpi-4-b.dts | 54 +++
 1 file changed, 54 insertions(+)

diff --git a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts 
b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
index 403bacf986eb..9ae44620c5b8 100644
--- a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
+++ b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
@@ -5,6 +5,7 @@
 #include "bcm283x-rpi-usb-peripheral.dtsi"
 
 #include 
+#include 
 
 / {
compatible = "raspberrypi,4-model-b", "brcm,bcm2711";
@@ -68,6 +69,54 @@ sd_vcc_reg: sd_vcc_reg {
enable-active-high;
gpio = < 6 GPIO_ACTIVE_HIGH>;
};
+
+   fan: pwm-fan {
+   compatible = "pwm-fan";
+   cooling-levels = <0 50 150 255>;
+   #cooling-cells = <2>;
+   pwms = < RASPBERRYPI_FIRMWARE_PWM_POE 8>;
+   };
+
+   thermal-zones {
+   cpu_thermal: cpu-thermal {
+   trips {
+   threshold: trip-point@0 {
+   temperature = <45000>;
+   hysteresis = <5000>;
+   type = "active";
+   };
+
+   target: trip-point@1 {
+   temperature = <5>;
+   hysteresis = <2000>;
+   type = "active";
+   };
+
+   cpu_hot: cpu_hot@0 {
+   temperature = <55000>;
+   hysteresis = <2000>;
+   type = "active";
+   };
+   };
+
+   cooling-maps {
+   map0 {
+   trip = <>;
+   cooling-device = < 0 1>;
+   };
+
+   map1 {
+   trip = <>;
+   cooling-device = < 1 2>;
+   };
+
+   map2 {
+   trip = <_hot>;
+   cooling-device = < 2 3>;
+   };
+   };
+   };
+   };
 };
 
  {
@@ -103,6 +152,11 @@ reset: reset {
compatible = "raspberrypi,firmware-reset";
#reset-cells = <1>;
};
+
+   fwpwm: pwm {
+   compatible = "raspberrypi,firmware-poe-pwm";
+   #pwm-cells = <2>;
+   };
 };
 
  {
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v7 08/11] input: raspberrypi-ts: Release firmware handle when not needed

2021-01-18 Thread Nicolas Saenz Julienne
There is no use for the firmware interface after getting the touch
buffer address, so release it.

Signed-off-by: Nicolas Saenz Julienne 
Acked-by: Dmitry Torokhov 
Reviewed-by: Florian Fainelli 

---
Changes since v5:
 - Correct commit message

Changes since v3:
 - Release firmware handle in probe function

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/input/touchscreen/raspberrypi-ts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/raspberrypi-ts.c 
b/drivers/input/touchscreen/raspberrypi-ts.c
index ef6aaed217cf..5000f5fd9ec3 100644
--- a/drivers/input/touchscreen/raspberrypi-ts.c
+++ b/drivers/input/touchscreen/raspberrypi-ts.c
@@ -160,7 +160,7 @@ static int rpi_ts_probe(struct platform_device *pdev)
touchbuf = (u32)ts->fw_regs_phys;
error = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF,
  , sizeof(touchbuf));
-
+   rpi_firmware_put(fw);
if (error || touchbuf != 0) {
dev_warn(dev, "Failed to set touchbuf, %d\n", error);
return error;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v7 05/11] reset: raspberrypi: Release firmware handle on unbind

2021-01-18 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
---
 drivers/reset/reset-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/reset/reset-raspberrypi.c 
b/drivers/reset/reset-raspberrypi.c
index 02f59c06f69b..fa23db554bcf 100644
--- a/drivers/reset/reset-raspberrypi.c
+++ b/drivers/reset/reset-raspberrypi.c
@@ -82,7 +82,7 @@ static int rpi_reset_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(np);
+   fw = devm_rpi_firmware_get(>dev, np);
of_node_put(np);
if (!fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v7 04/11] gpio: raspberrypi-exp: Release firmware handle on unbind

2021-01-18 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
Acked-by: Bartosz Golaszewski 
---
 drivers/gpio/gpio-raspberrypi-exp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpio/gpio-raspberrypi-exp.c 
b/drivers/gpio/gpio-raspberrypi-exp.c
index bb100e0124e6..64a552ecc2ad 100644
--- a/drivers/gpio/gpio-raspberrypi-exp.c
+++ b/drivers/gpio/gpio-raspberrypi-exp.c
@@ -208,7 +208,7 @@ static int rpi_exp_gpio_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(fw_node);
+   fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v7 00/11] Raspberry Pi PoE HAT fan support

2021-01-18 Thread Nicolas Saenz Julienne
The aim of this series is to add support to the fan found on RPi's PoE
HAT. Some commentary on the design can be found below. But the important
part to the people CC'd here not involved with PWM is that, in order to
achieve this properly, we also have to fix the firmware interface the
driver uses to communicate with the PWM bus (and many other low level
functions). Specifically, we have to make sure the firmware interface
isn't unbound while consumers are still up. So, patch #1 & #2 introduce
reference counting in the firmware interface driver and patches #3 to #8
update all firmware users. Patches #9 to #11 introduce the new PWM
driver.

I sent everything as a single series as the final version of the PWM
drivers depends on the firmware fixes, but I'll be happy to split this
into two separate series if you think it's better.

--- Original cover letter below ---

This series aims at adding support to RPi's official PoE HAT fan[1].

The HW setup is the following:

| Raspberry Pi   | PoE HAT|
 arm core -> Mailbox -> RPi co-processor -> I2C -> Atmel MCU -> PWM -> FAN

The arm cores have only access to the mailbox interface, as i2c0, even if
physically accessible, is to be used solely by the co-processor
(VideoCore 4/6).

This series implements a PWM bus, and has pwm-fan sitting on top of it as per
this discussion: https://lkml.org/lkml/2018/9/2/486. Although this design has a
series of shortcomings:

- It depends on a DT binding: it's not flexible if a new hat shows up with new
  functionality, we're not 100% sure we'll be able to expand it without
  breaking backwards compatibility. But without it we can't make use of DT
  thermal-zones, which IMO is overkill.

- We're using pwm-fan, writing a hwmon driver would, again, give us more
  flexibility, but it's not really needed at the moment.

I personally think that it's not worth the effort, it's unlikely we'll get
things right in advance. And ultimately, if the RPi people come up with
something new, we can always write a new driver/bindings from scratch (as in
not reusing previous code).

That said, I'm more than happy to change things if there is a consensus that
another design will do the trick.

[1] https://www.raspberrypi.org/blog/introducing-power-over-ethernet-poe-hat/

---

Changes since v6:
 - Address PWM driver comments

Changes since v5:
 - Small cleanups
 - Add extra code comments

Changes since v4:
 - Cleanup devm calls
 - Rename compatible string so it's unique to the PoE HAT

Changes since v3:
 - Split first patch, #1 introduces refcount, then #2 the devm function
 - Fix touchscreen function
 - Use kref

Changes since v2:
 - Introduce devm_rpi_firmware_get()
 - Small cleanups in PWM driver

Changes since v1:
 - Address PWM driver changes
 - Fix binding, now with 2 cells

Nicolas Saenz Julienne (11):
  firmware: raspberrypi: Keep count of all consumers
  firmware: raspberrypi: Introduce devm_rpi_firmware_get()
  clk: bcm: rpi: Release firmware handle on unbind
  gpio: raspberrypi-exp: Release firmware handle on unbind
  reset: raspberrypi: Release firmware handle on unbind
  soc: bcm: raspberrypi-power: Release firmware handle on unbind
  staging: vchiq: Release firmware handle on unbind
  input: raspberrypi-ts: Release firmware handle when not needed
  dt-bindings: pwm: Add binding for RPi firmware PWM bus
  DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support
  pwm: Add Raspberry Pi Firmware based PWM bus

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml |  20 ++
 arch/arm/boot/dts/bcm2711-rpi-4-b.dts |  54 +
 drivers/clk/bcm/clk-raspberrypi.c |   2 +-
 drivers/firmware/raspberrypi.c|  69 +-
 drivers/gpio/gpio-raspberrypi-exp.c   |   2 +-
 drivers/input/touchscreen/raspberrypi-ts.c|   2 +-
 drivers/pwm/Kconfig   |   9 +
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi-poe.c | 220 ++
 drivers/reset/reset-raspberrypi.c |   2 +-
 drivers/soc/bcm/raspberrypi-power.c   |   2 +-
 .../interface/vchiq_arm/vchiq_arm.c   |   2 +-
 .../pwm/raspberrypi,firmware-poe-pwm.h|  13 ++
 include/soc/bcm2835/raspberrypi-firmware.h|  10 +
 14 files changed, 399 insertions(+), 9 deletions(-)
 create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h

-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v7 02/11] firmware: raspberrypi: Introduce devm_rpi_firmware_get()

2021-01-18 Thread Nicolas Saenz Julienne
It'll simplify the firmware handling for most consumers.

Suggested-by: Bartosz Golaszewski 
Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
Reviewed-by: Bartosz Golaszewski 

---
Changes since v4:
 - Rearrange function calls for clarity, same functionality

Changes since v2:
- Create devm_rpi_firmware_get()

 drivers/firmware/raspberrypi.c | 29 ++
 include/soc/bcm2835/raspberrypi-firmware.h |  8 ++
 2 files changed, 37 insertions(+)

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index b65e4c495772..250e01680742 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -243,6 +243,13 @@ void rpi_firmware_put(struct rpi_firmware *fw)
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_put);
 
+static void devm_rpi_firmware_put(void *data)
+{
+   struct rpi_firmware *fw = data;
+
+   rpi_firmware_put(fw);
+}
+
 static int rpi_firmware_probe(struct platform_device *pdev)
 {
struct device *dev = >dev;
@@ -331,6 +338,28 @@ struct rpi_firmware *rpi_firmware_get(struct device_node 
*firmware_node)
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_get);
 
+/**
+ * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
+ * @firmware_node:Pointer to the firmware Device Tree node.
+ *
+ * Returns NULL is the firmware device is not ready.
+ */
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+  struct device_node *firmware_node)
+{
+   struct rpi_firmware *fw;
+
+   fw = rpi_firmware_get(firmware_node);
+   if (!fw)
+   return NULL;
+
+   if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
+   return NULL;
+
+   return fw;
+}
+EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
+
 static const struct of_device_id rpi_firmware_of_match[] = {
{ .compatible = "raspberrypi,bcm2835-firmware", },
{},
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
b/include/soc/bcm2835/raspberrypi-firmware.h
index fdfef7fe40df..73ad784fca96 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -142,6 +142,8 @@ int rpi_firmware_property_list(struct rpi_firmware *fw,
   void *data, size_t tag_size);
 void rpi_firmware_put(struct rpi_firmware *fw);
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+  struct device_node *firmware_node);
 #else
 static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
void *data, size_t len)
@@ -160,6 +162,12 @@ static inline struct rpi_firmware *rpi_firmware_get(struct 
device_node *firmware
 {
return NULL;
 }
+
+static inline struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+   struct device_node *firmware_node)
+{
+   return NULL;
+}
 #endif
 
 #endif /* __SOC_RASPBERRY_FIRMWARE_H__ */
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v7 03/11] clk: bcm: rpi: Release firmware handle on unbind

2021-01-18 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
Acked-by: Stephen Boyd 

 drivers/clk/bcm/clk-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/bcm/clk-raspberrypi.c 
b/drivers/clk/bcm/clk-raspberrypi.c
index f89b9cfc4309..dd3b71eafabf 100644
--- a/drivers/clk/bcm/clk-raspberrypi.c
+++ b/drivers/clk/bcm/clk-raspberrypi.c
@@ -314,7 +314,7 @@ static int raspberrypi_clk_probe(struct platform_device 
*pdev)
return -ENOENT;
}
 
-   firmware = rpi_firmware_get(firmware_node);
+   firmware = devm_rpi_firmware_get(>dev, firmware_node);
of_node_put(firmware_node);
if (!firmware)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v7 01/11] firmware: raspberrypi: Keep count of all consumers

2021-01-18 Thread Nicolas Saenz Julienne
When unbinding the firmware device we need to make sure it has no
consumers left. Otherwise we'd leave them with a firmware handle
pointing at freed memory.

Keep a reference count of all consumers and introduce rpi_firmware_put()
which will permit automatically decrease the reference count upon
unbinding consumer drivers.

Suggested-by: Uwe Kleine-König 
Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
Reviewed-by: Stephen Boyd 
Reviewed-by: Bartosz Golaszewski 

---

Changes since v5:
 - Add comment to avoid people blindly switching the memory allocation
   to the devm variant.
 - Fix function documentation as per Florian's comment.

Changes since v3:
 - Use kref instead of waiting on refcount

 drivers/firmware/raspberrypi.c | 40 --
 include/soc/bcm2835/raspberrypi-firmware.h |  2 ++
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index 30259dc9b805..b65e4c495772 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -7,6 +7,7 @@
  */
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -27,6 +28,8 @@ struct rpi_firmware {
struct mbox_chan *chan; /* The property channel. */
struct completion c;
u32 enabled;
+
+   struct kref consumers;
 };
 
 static DEFINE_MUTEX(transaction_lock);
@@ -225,12 +228,31 @@ static void rpi_register_clk_driver(struct device *dev)
-1, NULL, 0);
 }
 
+static void rpi_firmware_delete(struct kref *kref)
+{
+   struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
+  consumers);
+
+   mbox_free_channel(fw->chan);
+   kfree(fw);
+}
+
+void rpi_firmware_put(struct rpi_firmware *fw)
+{
+   kref_put(>consumers, rpi_firmware_delete);
+}
+EXPORT_SYMBOL_GPL(rpi_firmware_put);
+
 static int rpi_firmware_probe(struct platform_device *pdev)
 {
struct device *dev = >dev;
struct rpi_firmware *fw;
 
-   fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
+   /*
+* Memory will be freed by rpi_firmware_delete() once all users have
+* released their firmware handles. Don't use devm_kzalloc() here.
+*/
+   fw = kzalloc(sizeof(*fw), GFP_KERNEL);
if (!fw)
return -ENOMEM;
 
@@ -247,6 +269,7 @@ static int rpi_firmware_probe(struct platform_device *pdev)
}
 
init_completion(>c);
+   kref_init(>consumers);
 
platform_set_drvdata(pdev, fw);
 
@@ -275,7 +298,8 @@ static int rpi_firmware_remove(struct platform_device *pdev)
rpi_hwmon = NULL;
platform_device_unregister(rpi_clk);
rpi_clk = NULL;
-   mbox_free_channel(fw->chan);
+
+   rpi_firmware_put(fw);
 
return 0;
 }
@@ -284,16 +308,26 @@ static int rpi_firmware_remove(struct platform_device 
*pdev)
  * rpi_firmware_get - Get pointer to rpi_firmware structure.
  * @firmware_node:Pointer to the firmware Device Tree node.
  *
+ * The reference to rpi_firmware has to be released with rpi_firmware_put().
+ *
  * Returns NULL is the firmware device is not ready.
  */
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
 {
struct platform_device *pdev = of_find_device_by_node(firmware_node);
+   struct rpi_firmware *fw;
 
if (!pdev)
return NULL;
 
-   return platform_get_drvdata(pdev);
+   fw = platform_get_drvdata(pdev);
+   if (!fw)
+   return NULL;
+
+   if (!kref_get_unless_zero(>consumers))
+   return NULL;
+
+   return fw;
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_get);
 
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
b/include/soc/bcm2835/raspberrypi-firmware.h
index cc9cdbc66403..fdfef7fe40df 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -140,6 +140,7 @@ int rpi_firmware_property(struct rpi_firmware *fw,
  u32 tag, void *data, size_t len);
 int rpi_firmware_property_list(struct rpi_firmware *fw,
   void *data, size_t tag_size);
+void rpi_firmware_put(struct rpi_firmware *fw);
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
 #else
 static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
@@ -154,6 +155,7 @@ static inline int rpi_firmware_property_list(struct 
rpi_firmware *fw,
return -ENOSYS;
 }
 
+static inline void rpi_firmware_put(struct rpi_firmware *fw) { }
 static inline struct rpi_firmware *rpi_firmware_get(struct device_node 
*firmware_node)
 {
return NULL;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v6 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2021-01-14 Thread Nicolas Saenz Julienne
Hi Uwe, thanks for the review.

On Tue, 2021-01-12 at 10:18 +0100, Uwe Kleine-König wrote:

[...]

> > +duty_cycle = DIV_ROUND_CLOSEST_ULL(state->duty_cycle * 
> > RPI_PWM_MAX_DUTY,
> > +  RPI_PWM_PERIOD_NS);
> 
> ... and round down here.
> 
> Just to be sure: writing RPI_PWM_MAX_DUTY (i.e. 255) yields 100% duty
> cycle, right?

Yes, at 255 the signal is flat.

> > +else
> > +duty_cycle = RPI_PWM_MAX_DUTY;
> > +
> > +   if (duty_cycle == rpipwm->duty_cycle)
> > +   return 0;
> > +
> > +   ret = raspberrypi_pwm_set_property(rpipwm->firmware, 
> > RPI_PWM_CUR_DUTY_REG,
> > +  duty_cycle);
> > +   if (ret) {
> > +   dev_err(chip->dev, "Failed to set duty cycle: %d\n", ret);
> > +   return ret;
> > +   }
> > +
> > +   /*
> > +* This sets the default duty cycle after resetting the board, we
> > +* updated it every time to mimic Raspberry Pi's downstream's driver
> > +* behaviour.
> > +*/
> > +   ret = raspberrypi_pwm_set_property(rpipwm->firmware, 
> > RPI_PWM_DEF_DUTY_REG,
> > +  duty_cycle);
> > +   if (ret) {
> > +   dev_err(chip->dev, "Failed to set default duty cycle: %d\n", 
> > ret);
> > +   return ret;
> > +   }
> > +
> > +rpipwm->duty_cycle = duty_cycle;
> 
> Please use tabs for indention. (The general hint is to use checkpatch
> which (I hope) tells you about problems like this.)

Sorry for that.

I took note of the rest of comments and will update the code.

Regards,
Nicolas




signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v6 00/11] Raspberry Pi PoE HAT fan support

2021-01-11 Thread Nicolas Saenz Julienne
On Fri, 2020-12-11 at 17:47 +0100, Nicolas Saenz Julienne wrote:
> The aim of this series is to add support to the fan found on RPi's PoE
> HAT. Some commentary on the design can be found below. But the imporant
> part to the people CC'd here not involved with PWM is that, in order to
> achieve this properly, we also have to fix the firmware interface the
> driver uses to communicate with the PWM bus (and many other low level
> functions). Specifically, we have to make sure the firmware interface
> isn't unbound while consumers are still up. So, patch #1 & #2 introduce
> reference counting in the firwmware interface driver and patches #3 to
> #8 update all firmware users. Patches #9 to #11 introduce the new PWM
> driver.
> 
> I sent everything as a single series as the final version of the PWM
> drivers depends on the firwmare fixes, but I'll be happy to split this
> into two separate series if you think it's better.
> 
> --- Original cover letter below ---
> 
> This series aims at adding support to RPi's official PoE HAT fan[1].
> 
> The HW setup is the following:
> 
> > Raspberry Pi   | PoE HAT|
>  arm core -> Mailbox -> RPi co-processor -> I2C -> Atmel MCU -> PWM -> FAN
> 
> The arm cores have only access to the mailbox interface, as i2c0, even if
> physically accessible, is to be used solely by the co-processor
> (VideoCore 4/6).
> 
> This series implements a PWM bus, and has pwm-fan sitting on top of it as per
> this discussion: https://lkml.org/lkml/2018/9/2/486. Although this design has 
> a
> series of shortcomings:
> 
> - It depends on a DT binding: it's not flexible if a new hat shows up with new
>   functionality, we're not 100% sure we'll be able to expand it without
>   breaking backwards compatibility. But without it we can't make use of DT
>   thermal-zones, which IMO is overkill.
> 
> - We're using pwm-fan, writing a hwmon driver would, again, give us more
>   flexibility, but it's not really needed at the moment.
> 
> I personally think that it's not worth the effort, it's unlikely we'll get
> things right in advance. And ultimately, if the RPi people come up with
> something new, we can always write a new driver/bindings from scratch (as in
> not reusing previous code).
> 
> That said, I'm more than happy to change things if there is a consensus that
> another design will do the trick.
> 
> [1] https://www.raspberrypi.org/blog/introducing-power-over-ethernet-poe-hat/
> 
> ---

I'd say at this point the series is pretty clean and, AFAIK, there aren't any
objections. I'm not so sure who should take it, given that it covers numerous
subsystems. Any suggestions on how to handle it?

Regards,
Nicolas

> Changes since v5:
>  - Small cleanups
>  - Add extra code comments
> 
> Changes since v4:
>  - Cleanup devm calls
>  - Rename compatible string so it's unique to the PoE HAT
> 
> Changes since v3:
>  - Split first patch, #1 introduces refcount, then #2 the devm function
>  - Fix touchscreen function
>  - Use kref
> 
> Changes since v2:
>  - Introduce devm_rpi_firmware_get()
>  - Small cleanups in PWM driver
> 
> Changes since v1:
>  - Address PWM driver changes
>  - Fix binding, now with 2 cells
> 
> Nicolas Saenz Julienne (11):
>   firmware: raspberrypi: Keep count of all consumers
>   firmware: raspberrypi: Introduce devm_rpi_firmware_get()
>   clk: bcm: rpi: Release firmware handle on unbind
>   gpio: raspberrypi-exp: Release firmware handle on unbind
>   reset: raspberrypi: Release firmware handle on unbind
>   soc: bcm: raspberrypi-power: Release firmware handle on unbind
>   staging: vchiq: Release firmware handle on unbind
>   input: raspberrypi-ts: Release firmware handle when not needed
>   dt-bindings: pwm: Add binding for RPi firmware PWM bus
>   DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support
>   pwm: Add Raspberry Pi Firmware based PWM bus
> 
>  .../arm/bcm/raspberrypi,bcm2835-firmware.yaml |  20 ++
>  arch/arm/boot/dts/bcm2711-rpi-4-b.dts |  54 +
>  drivers/clk/bcm/clk-raspberrypi.c |   2 +-
>  drivers/firmware/raspberrypi.c|  69 +-
>  drivers/gpio/gpio-raspberrypi-exp.c   |   2 +-
>  drivers/input/touchscreen/raspberrypi-ts.c|   2 +-
>  drivers/pwm/Kconfig   |   9 +
>  drivers/pwm/Makefile  |   1 +
>  drivers/pwm/pwm-raspberrypi-poe.c | 216 ++
>  drivers/reset/reset-raspberrypi.c |   2 +-
>  drivers/soc/bcm/raspberrypi-power.c   |   2 +-
>  .../interface/vchiq_arm/vchiq_arm.c   |   2 +-
>  .../pwm/raspberrypi,firmware-poe-pwm.h|  13

Re: [PATCH v2 0/3] A trio of vchiq bulk transfer fixes

2021-01-11 Thread Nicolas Saenz Julienne
On Tue, 2021-01-05 at 16:20 +, Phil Elwell wrote:
> The recent batch of vchiq improvements broke bulk transfers in two ways:
> 
> 1. The userdata associated with a transfer was lost in the case that a
>    non-blocking mode was used.
> 
> 2. The 64-bit ioctl compatibility shim for a bulk transfer used the
>    wrong ioctl command.
> 
> This patch set fixes both of those bugs, and adds a security-related
> note to the TODO file.
> 
> Changes in v2:
> - Expand the commit message on patch 1 to clarify the impact of the
>   bug, and add Tested-by.
> - Add commit 3 with an additional TODO item.
> - Change the name of the patch set to be numerically accurate.
> 
> Phil Elwell (3):
>   staging: vchiq: Fix bulk userdata handling
>   staging: vchiq: Fix bulk transfers on 64-bit builds
>   staging: vc04_services: Add a note to the TODO
> 
>  drivers/staging/vc04_services/interface/TODO| 4 
>  .../staging/vc04_services/interface/vchiq_arm/vchiq_arm.c   | 6 --
>  2 files changed, 8 insertions(+), 2 deletions(-)
> 

Reviewed-by: Nicolas Saenz Julienne 

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v6 02/11] firmware: raspberrypi: Introduce devm_rpi_firmware_get()

2020-12-16 Thread Nicolas Saenz Julienne
On Wed, 2020-12-16 at 11:35 +0100, Bartosz Golaszewski wrote:
> On Fri, Dec 11, 2020 at 5:48 PM Nicolas Saenz Julienne
>  wrote:
> > 
> > It'll simplify the firmware handling for most consumers.
> > 
> > Suggested-by: Bartosz Golaszewski 
> > Signed-off-by: Nicolas Saenz Julienne 
> > Reviewed-by: Florian Fainelli 
> > ---
> > 
> > Changes since v4:
> >  - Rearrange function calls for clarity, same functionality
> > 
> > Changes since v2:
> > - Create devm_rpi_firmware_get()
> > 
> >  drivers/firmware/raspberrypi.c | 29 ++
> >  include/soc/bcm2835/raspberrypi-firmware.h |  8 ++
> >  2 files changed, 37 insertions(+)
> > 
> > diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
> > index b65e4c495772..250e01680742 100644
> > --- a/drivers/firmware/raspberrypi.c
> > +++ b/drivers/firmware/raspberrypi.c
> > @@ -243,6 +243,13 @@ void rpi_firmware_put(struct rpi_firmware *fw)
> >  }
> >  EXPORT_SYMBOL_GPL(rpi_firmware_put);
> > 
> > +static void devm_rpi_firmware_put(void *data)
> > +{
> > +   struct rpi_firmware *fw = data;
> > +
> > +   rpi_firmware_put(fw);
> > +}
> > +
> >  static int rpi_firmware_probe(struct platform_device *pdev)
> >  {
> > struct device *dev = >dev;
> > @@ -331,6 +338,28 @@ struct rpi_firmware *rpi_firmware_get(struct 
> > device_node *firmware_node)
> >  }
> >  EXPORT_SYMBOL_GPL(rpi_firmware_get);
> > 
> > +/**
> > + * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
> > + * @firmware_node:Pointer to the firmware Device Tree node.
> > + *
> > + * Returns NULL is the firmware device is not ready.
> > + */
> > +struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
> > +  struct device_node 
> > *firmware_node)
> > +{
> > +   struct rpi_firmware *fw;
> > +
> > +   fw = rpi_firmware_get(firmware_node);
> > +   if (!fw)
> > +   return NULL;
> > +
> > +   if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
> > +   return NULL;
> > +
> > +   return fw;
> > +}
> > +EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
> > +
> >  static const struct of_device_id rpi_firmware_of_match[] = {
> > { .compatible = "raspberrypi,bcm2835-firmware", },
> > {},
> > diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
> > b/include/soc/bcm2835/raspberrypi-firmware.h
> > index fdfef7fe40df..73ad784fca96 100644
> > --- a/include/soc/bcm2835/raspberrypi-firmware.h
> > +++ b/include/soc/bcm2835/raspberrypi-firmware.h
> > @@ -142,6 +142,8 @@ int rpi_firmware_property_list(struct rpi_firmware *fw,
> >    void *data, size_t tag_size);
> >  void rpi_firmware_put(struct rpi_firmware *fw);
> >  struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
> > +struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
> > +  struct device_node 
> > *firmware_node);
> >  #else
> >  static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
> > void *data, size_t len)
> > @@ -160,6 +162,12 @@ static inline struct rpi_firmware 
> > *rpi_firmware_get(struct device_node *firmware
> >  {
> > return NULL;
> >  }
> > +
> > +static inline struct rpi_firmware *devm_rpi_firmware_get(struct device 
> > *dev,
> > +   struct device_node *firmware_node)
> > +{
> > +   return NULL;
> > +}
> >  #endif
> > 
> >  #endif /* __SOC_RASPBERRY_FIRMWARE_H__ */
> > --
> > 2.29.2
> > 
> 
> Reviewed-by: Bartosz Golaszewski 

Thanks for the reviews!



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v6 07/11] staging: vchiq: Release firmware handle on unbind

2020-12-11 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
---
 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c 
b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
index f500a7043805..6c196cade4a0 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
@@ -2732,7 +2732,7 @@ static int vchiq_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   drvdata->fw = rpi_firmware_get(fw_node);
+   drvdata->fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!drvdata->fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v6 09/11] dt-bindings: pwm: Add binding for RPi firmware PWM bus

2020-12-11 Thread Nicolas Saenz Julienne
The PWM bus controlling the fan in RPi's official PoE hat can only be
controlled by the board's co-processor.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Rob Herring 
---

Changes since v4:
 - Rename compatible string to be more explicit with the bus' limitations

Changes since v3:
 - Fix example

Changes since v1:
 - Update bindings to use 2 #pwm-cells

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml | 20 +++
 .../pwm/raspberrypi,firmware-poe-pwm.h| 13 
 2 files changed, 33 insertions(+)
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h

diff --git 
a/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml 
b/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
index a2c63c8b1d10..e3664eab0f6a 100644
--- 
a/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
+++ 
b/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
@@ -64,6 +64,21 @@ properties:
   - compatible
   - "#reset-cells"
 
+  pwm:
+type: object
+
+properties:
+  compatible:
+const: raspberrypi,firmware-poe-pwm
+
+  "#pwm-cells":
+# See pwm.yaml in this directory for a description of the cells format.
+const: 2
+
+required:
+  - compatible
+  - "#pwm-cells"
+
 additionalProperties: false
 
 required:
@@ -87,5 +102,10 @@ examples:
 compatible = "raspberrypi,firmware-reset";
 #reset-cells = <1>;
 };
+
+pwm: pwm {
+compatible = "raspberrypi,firmware-poe-pwm";
+#pwm-cells = <2>;
+};
 };
 ...
diff --git a/include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h 
b/include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h
new file mode 100644
index ..27c5ce68847b
--- /dev/null
+++ b/include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2020 Nicolas Saenz Julienne
+ * Author: Nicolas Saenz Julienne 
+ */
+
+#ifndef _DT_BINDINGS_RASPBERRYPI_FIRMWARE_PWM_H
+#define _DT_BINDINGS_RASPBERRYPI_FIRMWARE_PWM_H
+
+#define RASPBERRYPI_FIRMWARE_PWM_POE   0
+#define RASPBERRYPI_FIRMWARE_PWM_NUM   1
+
+#endif
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v6 10/11] DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support

2020-12-11 Thread Nicolas Saenz Julienne
This is an example on how to enable the fan on top of RPi's official PoE
hat.

Signed-off-by: Nicolas Saenz Julienne 
---
 arch/arm/boot/dts/bcm2711-rpi-4-b.dts | 54 +++
 1 file changed, 54 insertions(+)

diff --git a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts 
b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
index 403bacf986eb..9ae44620c5b8 100644
--- a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
+++ b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
@@ -5,6 +5,7 @@
 #include "bcm283x-rpi-usb-peripheral.dtsi"
 
 #include 
+#include 
 
 / {
compatible = "raspberrypi,4-model-b", "brcm,bcm2711";
@@ -68,6 +69,54 @@ sd_vcc_reg: sd_vcc_reg {
enable-active-high;
gpio = < 6 GPIO_ACTIVE_HIGH>;
};
+
+   fan: pwm-fan {
+   compatible = "pwm-fan";
+   cooling-levels = <0 50 150 255>;
+   #cooling-cells = <2>;
+   pwms = < RASPBERRYPI_FIRMWARE_PWM_POE 8>;
+   };
+
+   thermal-zones {
+   cpu_thermal: cpu-thermal {
+   trips {
+   threshold: trip-point@0 {
+   temperature = <45000>;
+   hysteresis = <5000>;
+   type = "active";
+   };
+
+   target: trip-point@1 {
+   temperature = <5>;
+   hysteresis = <2000>;
+   type = "active";
+   };
+
+   cpu_hot: cpu_hot@0 {
+   temperature = <55000>;
+   hysteresis = <2000>;
+   type = "active";
+   };
+   };
+
+   cooling-maps {
+   map0 {
+   trip = <>;
+   cooling-device = < 0 1>;
+   };
+
+   map1 {
+   trip = <>;
+   cooling-device = < 1 2>;
+   };
+
+   map2 {
+   trip = <_hot>;
+   cooling-device = < 2 3>;
+   };
+   };
+   };
+   };
 };
 
  {
@@ -103,6 +152,11 @@ reset: reset {
compatible = "raspberrypi,firmware-reset";
#reset-cells = <1>;
};
+
+   fwpwm: pwm {
+   compatible = "raspberrypi,firmware-poe-pwm";
+   #pwm-cells = <2>;
+   };
 };
 
  {
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v6 08/11] input: raspberrypi-ts: Release firmware handle when not needed

2020-12-11 Thread Nicolas Saenz Julienne
There is no use for the firmware interface after getting the touch
buffer address, so release it.

Signed-off-by: Nicolas Saenz Julienne 
Acked-by: Dmitry Torokhov 
---

Changes since v5:
 - Correct commit message

Changes since v3:
 - Release firmware handle in probe function

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/input/touchscreen/raspberrypi-ts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/raspberrypi-ts.c 
b/drivers/input/touchscreen/raspberrypi-ts.c
index ef6aaed217cf..5000f5fd9ec3 100644
--- a/drivers/input/touchscreen/raspberrypi-ts.c
+++ b/drivers/input/touchscreen/raspberrypi-ts.c
@@ -160,7 +160,7 @@ static int rpi_ts_probe(struct platform_device *pdev)
touchbuf = (u32)ts->fw_regs_phys;
error = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF,
  , sizeof(touchbuf));
-
+   rpi_firmware_put(fw);
if (error || touchbuf != 0) {
dev_warn(dev, "Failed to set touchbuf, %d\n", error);
return error;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v6 06/11] soc: bcm: raspberrypi-power: Release firmware handle on unbind

2020-12-11 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
---
 drivers/soc/bcm/raspberrypi-power.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/soc/bcm/raspberrypi-power.c 
b/drivers/soc/bcm/raspberrypi-power.c
index 5d1aacdd84ef..068715d6e66d 100644
--- a/drivers/soc/bcm/raspberrypi-power.c
+++ b/drivers/soc/bcm/raspberrypi-power.c
@@ -177,7 +177,7 @@ static int rpi_power_probe(struct platform_device *pdev)
return -ENODEV;
}
 
-   rpi_domains->fw = rpi_firmware_get(fw_np);
+   rpi_domains->fw = devm_rpi_firmware_get(>dev, fw_np);
of_node_put(fw_np);
if (!rpi_domains->fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v6 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2020-12-11 Thread Nicolas Saenz Julienne
Adds support to control the PWM bus available in official Raspberry Pi
PoE HAT. Only RPi's co-processor has access to it, so commands have to
be sent through RPi's firmware mailbox interface.

Signed-off-by: Nicolas Saenz Julienne 
---

Changes since v3:
 - Rename compatible string to be more explicit WRT to bus's limitations

Changes since v2:
 - Use devm_rpi_firmware_get()
 - Rename driver
 - Small cleanups

Changes since v1:
 - Use default pwm bindings and get rid of xlate() function
 - Correct spelling errors
 - Correct apply() function
 - Round values
 - Fix divisions in arm32 mode
 - Small cleanups

 drivers/pwm/Kconfig   |   9 ++
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi-poe.c | 216 ++
 3 files changed, 226 insertions(+)
 create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c

diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 0937e1c047ac..75e2344703b3 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -423,6 +423,15 @@ config PWM_PXA
  To compile this driver as a module, choose M here: the module
  will be called pwm-pxa.
 
+config PWM_RASPBERRYPI_POE
+   tristate "Raspberry Pi Firwmware PoE Hat PWM support"
+   # Make sure not 'y' when RASPBERRYPI_FIRMWARE is 'm'. This can only
+   # happen when COMPILE_TEST=y, hence the added !RASPBERRYPI_FIRMWARE.
+   depends on RASPBERRYPI_FIRMWARE || (COMPILE_TEST && 
!RASPBERRYPI_FIRMWARE)
+   help
+ Enable Raspberry Pi firmware controller PWM bus used to control the
+ official RPI PoE hat
+
 config PWM_RCAR
tristate "Renesas R-Car PWM support"
depends on ARCH_RENESAS || COMPILE_TEST
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index 18b89d7fd092..ed28d7bd4c64 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -38,6 +38,7 @@ obj-$(CONFIG_PWM_MXS) += pwm-mxs.o
 obj-$(CONFIG_PWM_OMAP_DMTIMER) += pwm-omap-dmtimer.o
 obj-$(CONFIG_PWM_PCA9685)  += pwm-pca9685.o
 obj-$(CONFIG_PWM_PXA)  += pwm-pxa.o
+obj-$(CONFIG_PWM_RASPBERRYPI_POE)  += pwm-raspberrypi-poe.o
 obj-$(CONFIG_PWM_RCAR) += pwm-rcar.o
 obj-$(CONFIG_PWM_RENESAS_TPU)  += pwm-renesas-tpu.o
 obj-$(CONFIG_PWM_ROCKCHIP) += pwm-rockchip.o
diff --git a/drivers/pwm/pwm-raspberrypi-poe.c 
b/drivers/pwm/pwm-raspberrypi-poe.c
new file mode 100644
index ..24b498839fcc
--- /dev/null
+++ b/drivers/pwm/pwm-raspberrypi-poe.c
@@ -0,0 +1,216 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2020 Nicolas Saenz Julienne 
+ * For more information on Raspberry Pi's PoE hat see:
+ * https://www.raspberrypi.org/products/poe-hat/
+ *
+ * Limitations:
+ *  - No disable bit, so a disabled PWM is simulated by duty_cycle 0
+ *  - Only normal polarity
+ *  - Fixed 12.5 kHz period
+ *
+ * The current period is completed when HW is reconfigured.
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#define RPI_PWM_MAX_DUTY   255
+#define RPI_PWM_PERIOD_NS  8 /* 12.5 kHz */
+
+#define RPI_PWM_CUR_DUTY_REG   0x0
+#define RPI_PWM_DEF_DUTY_REG   0x1
+
+struct raspberrypi_pwm {
+   struct rpi_firmware *firmware;
+   struct pwm_chip chip;
+   unsigned int duty_cycle;
+};
+
+struct raspberrypi_pwm_prop {
+   __le32 reg;
+   __le32 val;
+   __le32 ret;
+} __packed;
+
+static inline struct raspberrypi_pwm *to_raspberrypi_pwm(struct pwm_chip *chip)
+{
+   return container_of(chip, struct raspberrypi_pwm, chip);
+}
+
+static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware,
+   u32 reg, u32 val)
+{
+   struct raspberrypi_pwm_prop msg = {
+   .reg = cpu_to_le32(reg),
+   .val = cpu_to_le32(val),
+   };
+   int ret;
+
+   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
+   , sizeof(msg));
+   if (ret)
+   return ret;
+   if (msg.ret)
+   return -EIO;
+
+   return 0;
+}
+
+static int raspberrypi_pwm_get_property(struct rpi_firmware *firmware,
+   u32 reg, u32 *val)
+{
+   struct raspberrypi_pwm_prop msg = {
+   .reg = reg
+   };
+   int ret;
+
+   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
+   , sizeof(msg));
+   if (ret)
+   return ret;
+   if (msg.ret)
+   return -EIO;
+
+   *val = le32_to_cpu(msg.val);
+
+   return 0;
+}
+
+static void raspberrypi_pwm_get_state(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ struct pwm_state *state)
+{
+   struct raspberrypi_pwm *rpipwm = to_raspberrypi_pwm(chip);
+
+   state->period = RPI_PWM_PERIOD_NS;
+   state->duty_cycle = DI

[PATCH v6 03/11] clk: bcm: rpi: Release firmware handle on unbind

2020-12-11 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
---
 drivers/clk/bcm/clk-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/bcm/clk-raspberrypi.c 
b/drivers/clk/bcm/clk-raspberrypi.c
index f89b9cfc4309..dd3b71eafabf 100644
--- a/drivers/clk/bcm/clk-raspberrypi.c
+++ b/drivers/clk/bcm/clk-raspberrypi.c
@@ -314,7 +314,7 @@ static int raspberrypi_clk_probe(struct platform_device 
*pdev)
return -ENOENT;
}
 
-   firmware = rpi_firmware_get(firmware_node);
+   firmware = devm_rpi_firmware_get(>dev, firmware_node);
of_node_put(firmware_node);
if (!firmware)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v6 02/11] firmware: raspberrypi: Introduce devm_rpi_firmware_get()

2020-12-11 Thread Nicolas Saenz Julienne
It'll simplify the firmware handling for most consumers.

Suggested-by: Bartosz Golaszewski 
Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
---

Changes since v4:
 - Rearrange function calls for clarity, same functionality

Changes since v2:
- Create devm_rpi_firmware_get()

 drivers/firmware/raspberrypi.c | 29 ++
 include/soc/bcm2835/raspberrypi-firmware.h |  8 ++
 2 files changed, 37 insertions(+)

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index b65e4c495772..250e01680742 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -243,6 +243,13 @@ void rpi_firmware_put(struct rpi_firmware *fw)
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_put);
 
+static void devm_rpi_firmware_put(void *data)
+{
+   struct rpi_firmware *fw = data;
+
+   rpi_firmware_put(fw);
+}
+
 static int rpi_firmware_probe(struct platform_device *pdev)
 {
struct device *dev = >dev;
@@ -331,6 +338,28 @@ struct rpi_firmware *rpi_firmware_get(struct device_node 
*firmware_node)
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_get);
 
+/**
+ * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
+ * @firmware_node:Pointer to the firmware Device Tree node.
+ *
+ * Returns NULL is the firmware device is not ready.
+ */
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+  struct device_node *firmware_node)
+{
+   struct rpi_firmware *fw;
+
+   fw = rpi_firmware_get(firmware_node);
+   if (!fw)
+   return NULL;
+
+   if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
+   return NULL;
+
+   return fw;
+}
+EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
+
 static const struct of_device_id rpi_firmware_of_match[] = {
{ .compatible = "raspberrypi,bcm2835-firmware", },
{},
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
b/include/soc/bcm2835/raspberrypi-firmware.h
index fdfef7fe40df..73ad784fca96 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -142,6 +142,8 @@ int rpi_firmware_property_list(struct rpi_firmware *fw,
   void *data, size_t tag_size);
 void rpi_firmware_put(struct rpi_firmware *fw);
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+  struct device_node *firmware_node);
 #else
 static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
void *data, size_t len)
@@ -160,6 +162,12 @@ static inline struct rpi_firmware *rpi_firmware_get(struct 
device_node *firmware
 {
return NULL;
 }
+
+static inline struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+   struct device_node *firmware_node)
+{
+   return NULL;
+}
 #endif
 
 #endif /* __SOC_RASPBERRY_FIRMWARE_H__ */
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v6 05/11] reset: raspberrypi: Release firmware handle on unbind

2020-12-11 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
---
 drivers/reset/reset-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/reset/reset-raspberrypi.c 
b/drivers/reset/reset-raspberrypi.c
index 02f59c06f69b..fa23db554bcf 100644
--- a/drivers/reset/reset-raspberrypi.c
+++ b/drivers/reset/reset-raspberrypi.c
@@ -82,7 +82,7 @@ static int rpi_reset_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(np);
+   fw = devm_rpi_firmware_get(>dev, np);
of_node_put(np);
if (!fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v6 04/11] gpio: raspberrypi-exp: Release firmware handle on unbind

2020-12-11 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 
Acked-by: Bartosz Golaszewski 
---
 drivers/gpio/gpio-raspberrypi-exp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpio/gpio-raspberrypi-exp.c 
b/drivers/gpio/gpio-raspberrypi-exp.c
index bb100e0124e6..64a552ecc2ad 100644
--- a/drivers/gpio/gpio-raspberrypi-exp.c
+++ b/drivers/gpio/gpio-raspberrypi-exp.c
@@ -208,7 +208,7 @@ static int rpi_exp_gpio_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(fw_node);
+   fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v6 00/11] Raspberry Pi PoE HAT fan support

2020-12-11 Thread Nicolas Saenz Julienne
The aim of this series is to add support to the fan found on RPi's PoE
HAT. Some commentary on the design can be found below. But the imporant
part to the people CC'd here not involved with PWM is that, in order to
achieve this properly, we also have to fix the firmware interface the
driver uses to communicate with the PWM bus (and many other low level
functions). Specifically, we have to make sure the firmware interface
isn't unbound while consumers are still up. So, patch #1 & #2 introduce
reference counting in the firwmware interface driver and patches #3 to
#8 update all firmware users. Patches #9 to #11 introduce the new PWM
driver.

I sent everything as a single series as the final version of the PWM
drivers depends on the firwmare fixes, but I'll be happy to split this
into two separate series if you think it's better.

--- Original cover letter below ---

This series aims at adding support to RPi's official PoE HAT fan[1].

The HW setup is the following:

| Raspberry Pi   | PoE HAT|
 arm core -> Mailbox -> RPi co-processor -> I2C -> Atmel MCU -> PWM -> FAN

The arm cores have only access to the mailbox interface, as i2c0, even if
physically accessible, is to be used solely by the co-processor
(VideoCore 4/6).

This series implements a PWM bus, and has pwm-fan sitting on top of it as per
this discussion: https://lkml.org/lkml/2018/9/2/486. Although this design has a
series of shortcomings:

- It depends on a DT binding: it's not flexible if a new hat shows up with new
  functionality, we're not 100% sure we'll be able to expand it without
  breaking backwards compatibility. But without it we can't make use of DT
  thermal-zones, which IMO is overkill.

- We're using pwm-fan, writing a hwmon driver would, again, give us more
  flexibility, but it's not really needed at the moment.

I personally think that it's not worth the effort, it's unlikely we'll get
things right in advance. And ultimately, if the RPi people come up with
something new, we can always write a new driver/bindings from scratch (as in
not reusing previous code).

That said, I'm more than happy to change things if there is a consensus that
another design will do the trick.

[1] https://www.raspberrypi.org/blog/introducing-power-over-ethernet-poe-hat/

---

Changes since v5:
 - Small cleanups
 - Add extra code comments

Changes since v4:
 - Cleanup devm calls
 - Rename compatible string so it's unique to the PoE HAT

Changes since v3:
 - Split first patch, #1 introduces refcount, then #2 the devm function
 - Fix touchscreen function
 - Use kref

Changes since v2:
 - Introduce devm_rpi_firmware_get()
 - Small cleanups in PWM driver

Changes since v1:
 - Address PWM driver changes
 - Fix binding, now with 2 cells

Nicolas Saenz Julienne (11):
  firmware: raspberrypi: Keep count of all consumers
  firmware: raspberrypi: Introduce devm_rpi_firmware_get()
  clk: bcm: rpi: Release firmware handle on unbind
  gpio: raspberrypi-exp: Release firmware handle on unbind
  reset: raspberrypi: Release firmware handle on unbind
  soc: bcm: raspberrypi-power: Release firmware handle on unbind
  staging: vchiq: Release firmware handle on unbind
  input: raspberrypi-ts: Release firmware handle when not needed
  dt-bindings: pwm: Add binding for RPi firmware PWM bus
  DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support
  pwm: Add Raspberry Pi Firmware based PWM bus

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml |  20 ++
 arch/arm/boot/dts/bcm2711-rpi-4-b.dts |  54 +
 drivers/clk/bcm/clk-raspberrypi.c |   2 +-
 drivers/firmware/raspberrypi.c|  69 +-
 drivers/gpio/gpio-raspberrypi-exp.c   |   2 +-
 drivers/input/touchscreen/raspberrypi-ts.c|   2 +-
 drivers/pwm/Kconfig   |   9 +
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi-poe.c | 216 ++
 drivers/reset/reset-raspberrypi.c |   2 +-
 drivers/soc/bcm/raspberrypi-power.c   |   2 +-
 .../interface/vchiq_arm/vchiq_arm.c   |   2 +-
 .../pwm/raspberrypi,firmware-poe-pwm.h|  13 ++
 include/soc/bcm2835/raspberrypi-firmware.h|  10 +
 14 files changed, 395 insertions(+), 9 deletions(-)
 create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h

-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v6 01/11] firmware: raspberrypi: Keep count of all consumers

2020-12-11 Thread Nicolas Saenz Julienne
When unbinding the firmware device we need to make sure it has no
consumers left. Otherwise we'd leave them with a firmware handle
pointing at freed memory.

Keep a reference count of all consumers and introduce rpi_firmware_put()
which will permit automatically decrease the reference count upon
unbinding consumer drivers.

Suggested-by: Uwe Kleine-König 
Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Florian Fainelli 

---
Changes since v5:
 - Add comment to avoid people blindly switching the memory allocation
   to the devm variant.
 - Fix function documentation as per Florian's comment.

 Changes since v3:
- Use kref instead of waiting on refcount

 drivers/firmware/raspberrypi.c | 40 --
 include/soc/bcm2835/raspberrypi-firmware.h |  2 ++
 2 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index 30259dc9b805..b65e4c495772 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -7,6 +7,7 @@
  */
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -27,6 +28,8 @@ struct rpi_firmware {
struct mbox_chan *chan; /* The property channel. */
struct completion c;
u32 enabled;
+
+   struct kref consumers;
 };
 
 static DEFINE_MUTEX(transaction_lock);
@@ -225,12 +228,31 @@ static void rpi_register_clk_driver(struct device *dev)
-1, NULL, 0);
 }
 
+static void rpi_firmware_delete(struct kref *kref)
+{
+   struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
+  consumers);
+
+   mbox_free_channel(fw->chan);
+   kfree(fw);
+}
+
+void rpi_firmware_put(struct rpi_firmware *fw)
+{
+   kref_put(>consumers, rpi_firmware_delete);
+}
+EXPORT_SYMBOL_GPL(rpi_firmware_put);
+
 static int rpi_firmware_probe(struct platform_device *pdev)
 {
struct device *dev = >dev;
struct rpi_firmware *fw;
 
-   fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
+   /*
+* Memory will be freed by rpi_firmware_delete() once all users have
+* released their firmware handles. Don't use devm_kzalloc() here.
+*/
+   fw = kzalloc(sizeof(*fw), GFP_KERNEL);
if (!fw)
return -ENOMEM;
 
@@ -247,6 +269,7 @@ static int rpi_firmware_probe(struct platform_device *pdev)
}
 
init_completion(>c);
+   kref_init(>consumers);
 
platform_set_drvdata(pdev, fw);
 
@@ -275,7 +298,8 @@ static int rpi_firmware_remove(struct platform_device *pdev)
rpi_hwmon = NULL;
platform_device_unregister(rpi_clk);
rpi_clk = NULL;
-   mbox_free_channel(fw->chan);
+
+   rpi_firmware_put(fw);
 
return 0;
 }
@@ -284,16 +308,26 @@ static int rpi_firmware_remove(struct platform_device 
*pdev)
  * rpi_firmware_get - Get pointer to rpi_firmware structure.
  * @firmware_node:Pointer to the firmware Device Tree node.
  *
+ * The reference to rpi_firmware has to be released with rpi_firmware_put().
+ *
  * Returns NULL is the firmware device is not ready.
  */
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
 {
struct platform_device *pdev = of_find_device_by_node(firmware_node);
+   struct rpi_firmware *fw;
 
if (!pdev)
return NULL;
 
-   return platform_get_drvdata(pdev);
+   fw = platform_get_drvdata(pdev);
+   if (!fw)
+   return NULL;
+
+   if (!kref_get_unless_zero(>consumers))
+   return NULL;
+
+   return fw;
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_get);
 
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
b/include/soc/bcm2835/raspberrypi-firmware.h
index cc9cdbc66403..fdfef7fe40df 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -140,6 +140,7 @@ int rpi_firmware_property(struct rpi_firmware *fw,
  u32 tag, void *data, size_t len);
 int rpi_firmware_property_list(struct rpi_firmware *fw,
   void *data, size_t tag_size);
+void rpi_firmware_put(struct rpi_firmware *fw);
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
 #else
 static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
@@ -154,6 +155,7 @@ static inline int rpi_firmware_property_list(struct 
rpi_firmware *fw,
return -ENOSYS;
 }
 
+static inline void rpi_firmware_put(struct rpi_firmware *fw) { }
 static inline struct rpi_firmware *rpi_firmware_get(struct device_node 
*firmware_node)
 {
return NULL;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v5 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2020-12-09 Thread Nicolas Saenz Julienne
Hi All,

On Mon, 2020-11-23 at 19:38 +0100, Nicolas Saenz Julienne wrote:
> Adds support to control the PWM bus available in official Raspberry Pi
> PoE HAT. Only RPi's co-processor has access to it, so commands have to
> be sent through RPi's firmware mailbox interface.
> 
> Signed-off-by: Nicolas Saenz Julienne 
> 

I'll soon send a v6 of this series and was wondering if there is any more
feedback for this patch.

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v5 08/11] input: raspberrypi-ts: Release firmware handle when not needed

2020-12-03 Thread Nicolas Saenz Julienne
On Tue, 2020-12-01 at 22:03 -0800, Dmitry Torokhov wrote:
> Hi Nicolas,
> 
> On Mon, Nov 23, 2020 at 07:38:29PM +0100, Nicolas Saenz Julienne wrote:
> > Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
> > interface when unbinding the device.
> 
> I do not believe this comment is correct any longer. Otherwise:
> 
> Acked-by: Dmitry Torokhov 

Yes, sorry for that. I'll update it.

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v5 01/11] firmware: raspberrypi: Keep count of all consumers

2020-12-03 Thread Nicolas Saenz Julienne
On Thu, 2020-12-03 at 09:05 +0100, Bartosz Golaszewski wrote:
> On Mon, Nov 23, 2020 at 7:38 PM Nicolas Saenz Julienne
>  wrote:
> > 
> > When unbinding the firmware device we need to make sure it has no
> > consumers left. Otherwise we'd leave them with a firmware handle
> > pointing at freed memory.
> > 
> > Keep a reference count of all consumers and introduce rpi_firmware_put()
> > which will permit automatically decrease the reference count upon
> > unbinding consumer drivers.
> > 
> > Suggested-by: Uwe Kleine-König 
> > Signed-off-by: Nicolas Saenz Julienne 
> > ---
> > 
> > Changes since v3:
> > - Use kref instead of waiting on refcount
> > 
> >  drivers/firmware/raspberrypi.c | 37 +++---
> >  include/soc/bcm2835/raspberrypi-firmware.h |  2 ++
> >  2 files changed, 35 insertions(+), 4 deletions(-)
> > 
> > diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
> > index 30259dc9b805..ed793aef7851 100644
> > --- a/drivers/firmware/raspberrypi.c
> > +++ b/drivers/firmware/raspberrypi.c
> > @@ -7,6 +7,7 @@
> >   */
> > 
> >  #include 
> > +#include 
> >  #include 
> >  #include 
> >  #include 
> > @@ -27,6 +28,8 @@ struct rpi_firmware {
> > struct mbox_chan *chan; /* The property channel. */
> > struct completion c;
> > u32 enabled;
> > +
> > +   struct kref consumers;
> >  };
> > 
> >  static DEFINE_MUTEX(transaction_lock);
> > @@ -225,12 +228,27 @@ static void rpi_register_clk_driver(struct device 
> > *dev)
> > -1, NULL, 0);
> >  }
> > 
> > +static void rpi_firmware_delete(struct kref *kref)
> > +{
> > +   struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
> > +  consumers);
> > +
> > +   mbox_free_channel(fw->chan);
> > +   kfree(fw);
> > +}
> > +
> > +void rpi_firmware_put(struct rpi_firmware *fw)
> > +{
> > +   kref_put(>consumers, rpi_firmware_delete);
> > +}
> > +EXPORT_SYMBOL_GPL(rpi_firmware_put);
> > +
> >  static int rpi_firmware_probe(struct platform_device *pdev)
> >  {
> > struct device *dev = >dev;
> > struct rpi_firmware *fw;
> > 
> > -   fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
> 
> One nit from my side: maybe add a comment here saying that you really
> want to use non-managed kzalloc() because you're going to get people
> blindly converting it to devm_kzalloc() very soon.

Good point, I'll change it.

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v5 08/11] input: raspberrypi-ts: Release firmware handle when not needed

2020-11-23 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v3:
 - Release firmware handle in probe function

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/input/touchscreen/raspberrypi-ts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/raspberrypi-ts.c 
b/drivers/input/touchscreen/raspberrypi-ts.c
index ef6aaed217cf..5000f5fd9ec3 100644
--- a/drivers/input/touchscreen/raspberrypi-ts.c
+++ b/drivers/input/touchscreen/raspberrypi-ts.c
@@ -160,7 +160,7 @@ static int rpi_ts_probe(struct platform_device *pdev)
touchbuf = (u32)ts->fw_regs_phys;
error = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF,
  , sizeof(touchbuf));
-
+   rpi_firmware_put(fw);
if (error || touchbuf != 0) {
dev_warn(dev, "Failed to set touchbuf, %d\n", error);
return error;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v5 04/11] gpio: raspberrypi-exp: Release firmware handle on unbind

2020-11-23 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Acked-by: Bartosz Golaszewski 

---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/gpio/gpio-raspberrypi-exp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpio/gpio-raspberrypi-exp.c 
b/drivers/gpio/gpio-raspberrypi-exp.c
index bb100e0124e6..64a552ecc2ad 100644
--- a/drivers/gpio/gpio-raspberrypi-exp.c
+++ b/drivers/gpio/gpio-raspberrypi-exp.c
@@ -208,7 +208,7 @@ static int rpi_exp_gpio_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(fw_node);
+   fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v5 05/11] reset: raspberrypi: Release firmware handle on unbind

2020-11-23 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/reset/reset-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/reset/reset-raspberrypi.c 
b/drivers/reset/reset-raspberrypi.c
index 02f59c06f69b..fa23db554bcf 100644
--- a/drivers/reset/reset-raspberrypi.c
+++ b/drivers/reset/reset-raspberrypi.c
@@ -82,7 +82,7 @@ static int rpi_reset_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(np);
+   fw = devm_rpi_firmware_get(>dev, np);
of_node_put(np);
if (!fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v5 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2020-11-23 Thread Nicolas Saenz Julienne
Adds support to control the PWM bus available in official Raspberry Pi
PoE HAT. Only RPi's co-processor has access to it, so commands have to
be sent through RPi's firmware mailbox interface.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v3:
 - Rename compatible string to be more explicit WRT to bus's limitations

Changes since v2:
 - Use devm_rpi_firmware_get()
 - Rename driver
 - Small cleanups

Changes since v1:
 - Use default pwm bindings and get rid of xlate() function
 - Correct spelling errors
 - Correct apply() function
 - Round values
 - Fix divisions in arm32 mode
 - Small cleanups

 drivers/pwm/Kconfig   |   9 ++
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi-poe.c | 216 ++
 3 files changed, 226 insertions(+)
 create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c

diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 0937e1c047ac..75e2344703b3 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -423,6 +423,15 @@ config PWM_PXA
  To compile this driver as a module, choose M here: the module
  will be called pwm-pxa.
 
+config PWM_RASPBERRYPI_POE
+   tristate "Raspberry Pi Firwmware PoE Hat PWM support"
+   # Make sure not 'y' when RASPBERRYPI_FIRMWARE is 'm'. This can only
+   # happen when COMPILE_TEST=y, hence the added !RASPBERRYPI_FIRMWARE.
+   depends on RASPBERRYPI_FIRMWARE || (COMPILE_TEST && 
!RASPBERRYPI_FIRMWARE)
+   help
+ Enable Raspberry Pi firmware controller PWM bus used to control the
+ official RPI PoE hat
+
 config PWM_RCAR
tristate "Renesas R-Car PWM support"
depends on ARCH_RENESAS || COMPILE_TEST
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index 18b89d7fd092..ed28d7bd4c64 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -38,6 +38,7 @@ obj-$(CONFIG_PWM_MXS) += pwm-mxs.o
 obj-$(CONFIG_PWM_OMAP_DMTIMER) += pwm-omap-dmtimer.o
 obj-$(CONFIG_PWM_PCA9685)  += pwm-pca9685.o
 obj-$(CONFIG_PWM_PXA)  += pwm-pxa.o
+obj-$(CONFIG_PWM_RASPBERRYPI_POE)  += pwm-raspberrypi-poe.o
 obj-$(CONFIG_PWM_RCAR) += pwm-rcar.o
 obj-$(CONFIG_PWM_RENESAS_TPU)  += pwm-renesas-tpu.o
 obj-$(CONFIG_PWM_ROCKCHIP) += pwm-rockchip.o
diff --git a/drivers/pwm/pwm-raspberrypi-poe.c 
b/drivers/pwm/pwm-raspberrypi-poe.c
new file mode 100644
index ..24b498839fcc
--- /dev/null
+++ b/drivers/pwm/pwm-raspberrypi-poe.c
@@ -0,0 +1,216 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2020 Nicolas Saenz Julienne 
+ * For more information on Raspberry Pi's PoE hat see:
+ * https://www.raspberrypi.org/products/poe-hat/
+ *
+ * Limitations:
+ *  - No disable bit, so a disabled PWM is simulated by duty_cycle 0
+ *  - Only normal polarity
+ *  - Fixed 12.5 kHz period
+ *
+ * The current period is completed when HW is reconfigured.
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#define RPI_PWM_MAX_DUTY   255
+#define RPI_PWM_PERIOD_NS  8 /* 12.5 kHz */
+
+#define RPI_PWM_CUR_DUTY_REG   0x0
+#define RPI_PWM_DEF_DUTY_REG   0x1
+
+struct raspberrypi_pwm {
+   struct rpi_firmware *firmware;
+   struct pwm_chip chip;
+   unsigned int duty_cycle;
+};
+
+struct raspberrypi_pwm_prop {
+   __le32 reg;
+   __le32 val;
+   __le32 ret;
+} __packed;
+
+static inline struct raspberrypi_pwm *to_raspberrypi_pwm(struct pwm_chip *chip)
+{
+   return container_of(chip, struct raspberrypi_pwm, chip);
+}
+
+static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware,
+   u32 reg, u32 val)
+{
+   struct raspberrypi_pwm_prop msg = {
+   .reg = cpu_to_le32(reg),
+   .val = cpu_to_le32(val),
+   };
+   int ret;
+
+   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
+   , sizeof(msg));
+   if (ret)
+   return ret;
+   if (msg.ret)
+   return -EIO;
+
+   return 0;
+}
+
+static int raspberrypi_pwm_get_property(struct rpi_firmware *firmware,
+   u32 reg, u32 *val)
+{
+   struct raspberrypi_pwm_prop msg = {
+   .reg = reg
+   };
+   int ret;
+
+   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
+   , sizeof(msg));
+   if (ret)
+   return ret;
+   if (msg.ret)
+   return -EIO;
+
+   *val = le32_to_cpu(msg.val);
+
+   return 0;
+}
+
+static void raspberrypi_pwm_get_state(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ struct pwm_state *state)
+{
+   struct raspberrypi_pwm *rpipwm = to_raspberrypi_pwm(chip);
+
+   state->period = RPI_PWM_PERIOD_NS;
+   state->duty_cycle = DI

[PATCH v5 03/11] clk: bcm: rpi: Release firmware handle on unbind

2020-11-23 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/clk/bcm/clk-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/bcm/clk-raspberrypi.c 
b/drivers/clk/bcm/clk-raspberrypi.c
index f89b9cfc4309..dd3b71eafabf 100644
--- a/drivers/clk/bcm/clk-raspberrypi.c
+++ b/drivers/clk/bcm/clk-raspberrypi.c
@@ -314,7 +314,7 @@ static int raspberrypi_clk_probe(struct platform_device 
*pdev)
return -ENOENT;
}
 
-   firmware = rpi_firmware_get(firmware_node);
+   firmware = devm_rpi_firmware_get(>dev, firmware_node);
of_node_put(firmware_node);
if (!firmware)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v5 10/11] DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support

2020-11-23 Thread Nicolas Saenz Julienne
This is an example on how to enable the fan on top of RPi's official PoE
hat.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v1:
 - Update patch to use 2 pwm cells

 arch/arm/boot/dts/bcm2711-rpi-4-b.dts | 54 +++
 1 file changed, 54 insertions(+)

diff --git a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts 
b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
index 403bacf986eb..9ae44620c5b8 100644
--- a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
+++ b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
@@ -5,6 +5,7 @@
 #include "bcm283x-rpi-usb-peripheral.dtsi"
 
 #include 
+#include 
 
 / {
compatible = "raspberrypi,4-model-b", "brcm,bcm2711";
@@ -68,6 +69,54 @@ sd_vcc_reg: sd_vcc_reg {
enable-active-high;
gpio = < 6 GPIO_ACTIVE_HIGH>;
};
+
+   fan: pwm-fan {
+   compatible = "pwm-fan";
+   cooling-levels = <0 50 150 255>;
+   #cooling-cells = <2>;
+   pwms = < RASPBERRYPI_FIRMWARE_PWM_POE 8>;
+   };
+
+   thermal-zones {
+   cpu_thermal: cpu-thermal {
+   trips {
+   threshold: trip-point@0 {
+   temperature = <45000>;
+   hysteresis = <5000>;
+   type = "active";
+   };
+
+   target: trip-point@1 {
+   temperature = <5>;
+   hysteresis = <2000>;
+   type = "active";
+   };
+
+   cpu_hot: cpu_hot@0 {
+   temperature = <55000>;
+   hysteresis = <2000>;
+   type = "active";
+   };
+   };
+
+   cooling-maps {
+   map0 {
+   trip = <>;
+   cooling-device = < 0 1>;
+   };
+
+   map1 {
+   trip = <>;
+   cooling-device = < 1 2>;
+   };
+
+   map2 {
+   trip = <_hot>;
+   cooling-device = < 2 3>;
+   };
+   };
+   };
+   };
 };
 
  {
@@ -103,6 +152,11 @@ reset: reset {
compatible = "raspberrypi,firmware-reset";
#reset-cells = <1>;
};
+
+   fwpwm: pwm {
+   compatible = "raspberrypi,firmware-poe-pwm";
+   #pwm-cells = <2>;
+   };
 };
 
  {
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v5 09/11] dt-bindings: pwm: Add binding for RPi firmware PWM bus

2020-11-23 Thread Nicolas Saenz Julienne
The PWM bus controlling the fan in RPi's official PoE hat can only be
controlled by the board's co-processor.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Rob Herring 

---

Changes since v4:
 - Rename compatible string to be more explicit with the bus' limitations

Changes since v3:
 - Fix example

Changes since v1:
 - Update bindings to use 2 #pwm-cells

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml | 20 +++
 .../pwm/raspberrypi,firmware-poe-pwm.h| 13 
 2 files changed, 33 insertions(+)
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h

diff --git 
a/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml 
b/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
index a2c63c8b1d10..e3664eab0f6a 100644
--- 
a/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
+++ 
b/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
@@ -64,6 +64,21 @@ properties:
   - compatible
   - "#reset-cells"
 
+  pwm:
+type: object
+
+properties:
+  compatible:
+const: raspberrypi,firmware-poe-pwm
+
+  "#pwm-cells":
+# See pwm.yaml in this directory for a description of the cells format.
+const: 2
+
+required:
+  - compatible
+  - "#pwm-cells"
+
 additionalProperties: false
 
 required:
@@ -87,5 +102,10 @@ examples:
 compatible = "raspberrypi,firmware-reset";
 #reset-cells = <1>;
 };
+
+pwm: pwm {
+compatible = "raspberrypi,firmware-poe-pwm";
+#pwm-cells = <2>;
+};
 };
 ...
diff --git a/include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h 
b/include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h
new file mode 100644
index ..27c5ce68847b
--- /dev/null
+++ b/include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2020 Nicolas Saenz Julienne
+ * Author: Nicolas Saenz Julienne 
+ */
+
+#ifndef _DT_BINDINGS_RASPBERRYPI_FIRMWARE_PWM_H
+#define _DT_BINDINGS_RASPBERRYPI_FIRMWARE_PWM_H
+
+#define RASPBERRYPI_FIRMWARE_PWM_POE   0
+#define RASPBERRYPI_FIRMWARE_PWM_NUM   1
+
+#endif
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v5 06/11] soc: bcm: raspberrypi-power: Release firmware handle on unbind

2020-11-23 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/soc/bcm/raspberrypi-power.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/soc/bcm/raspberrypi-power.c 
b/drivers/soc/bcm/raspberrypi-power.c
index 5d1aacdd84ef..068715d6e66d 100644
--- a/drivers/soc/bcm/raspberrypi-power.c
+++ b/drivers/soc/bcm/raspberrypi-power.c
@@ -177,7 +177,7 @@ static int rpi_power_probe(struct platform_device *pdev)
return -ENODEV;
}
 
-   rpi_domains->fw = rpi_firmware_get(fw_np);
+   rpi_domains->fw = devm_rpi_firmware_get(>dev, fw_np);
of_node_put(fw_np);
if (!rpi_domains->fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v5 01/11] firmware: raspberrypi: Keep count of all consumers

2020-11-23 Thread Nicolas Saenz Julienne
When unbinding the firmware device we need to make sure it has no
consumers left. Otherwise we'd leave them with a firmware handle
pointing at freed memory.

Keep a reference count of all consumers and introduce rpi_firmware_put()
which will permit automatically decrease the reference count upon
unbinding consumer drivers.

Suggested-by: Uwe Kleine-König 
Signed-off-by: Nicolas Saenz Julienne 
---

Changes since v3:
- Use kref instead of waiting on refcount

 drivers/firmware/raspberrypi.c | 37 +++---
 include/soc/bcm2835/raspberrypi-firmware.h |  2 ++
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index 30259dc9b805..ed793aef7851 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -7,6 +7,7 @@
  */
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -27,6 +28,8 @@ struct rpi_firmware {
struct mbox_chan *chan; /* The property channel. */
struct completion c;
u32 enabled;
+
+   struct kref consumers;
 };
 
 static DEFINE_MUTEX(transaction_lock);
@@ -225,12 +228,27 @@ static void rpi_register_clk_driver(struct device *dev)
-1, NULL, 0);
 }
 
+static void rpi_firmware_delete(struct kref *kref)
+{
+   struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
+  consumers);
+
+   mbox_free_channel(fw->chan);
+   kfree(fw);
+}
+
+void rpi_firmware_put(struct rpi_firmware *fw)
+{
+   kref_put(>consumers, rpi_firmware_delete);
+}
+EXPORT_SYMBOL_GPL(rpi_firmware_put);
+
 static int rpi_firmware_probe(struct platform_device *pdev)
 {
struct device *dev = >dev;
struct rpi_firmware *fw;
 
-   fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
+   fw = kzalloc(sizeof(*fw), GFP_KERNEL);
if (!fw)
return -ENOMEM;
 
@@ -247,6 +265,7 @@ static int rpi_firmware_probe(struct platform_device *pdev)
}
 
init_completion(>c);
+   kref_init(>consumers);
 
platform_set_drvdata(pdev, fw);
 
@@ -275,25 +294,35 @@ static int rpi_firmware_remove(struct platform_device 
*pdev)
rpi_hwmon = NULL;
platform_device_unregister(rpi_clk);
rpi_clk = NULL;
-   mbox_free_channel(fw->chan);
+
+   rpi_firmware_put(fw);
 
return 0;
 }
 
 /**
- * rpi_firmware_get - Get pointer to rpi_firmware structure.
  * @firmware_node:Pointer to the firmware Device Tree node.
  *
+ * The reference to rpi_firmware has to be released with rpi_firmware_put().
+ *
  * Returns NULL is the firmware device is not ready.
  */
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
 {
struct platform_device *pdev = of_find_device_by_node(firmware_node);
+   struct rpi_firmware *fw;
 
if (!pdev)
return NULL;
 
-   return platform_get_drvdata(pdev);
+   fw = platform_get_drvdata(pdev);
+   if (!fw)
+   return NULL;
+
+   if (!kref_get_unless_zero(>consumers))
+   return NULL;
+
+   return fw;
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_get);
 
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
b/include/soc/bcm2835/raspberrypi-firmware.h
index cc9cdbc66403..fdfef7fe40df 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -140,6 +140,7 @@ int rpi_firmware_property(struct rpi_firmware *fw,
  u32 tag, void *data, size_t len);
 int rpi_firmware_property_list(struct rpi_firmware *fw,
   void *data, size_t tag_size);
+void rpi_firmware_put(struct rpi_firmware *fw);
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
 #else
 static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
@@ -154,6 +155,7 @@ static inline int rpi_firmware_property_list(struct 
rpi_firmware *fw,
return -ENOSYS;
 }
 
+static inline void rpi_firmware_put(struct rpi_firmware *fw) { }
 static inline struct rpi_firmware *rpi_firmware_get(struct device_node 
*firmware_node)
 {
return NULL;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v5 07/11] staging: vchiq: Release firmware handle on unbind

2020-11-23 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c 
b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
index f500a7043805..6c196cade4a0 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
@@ -2732,7 +2732,7 @@ static int vchiq_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   drvdata->fw = rpi_firmware_get(fw_node);
+   drvdata->fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!drvdata->fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v5 02/11] firmware: raspberrypi: Introduce devm_rpi_firmware_get()

2020-11-23 Thread Nicolas Saenz Julienne
It'll simplify the firmware handling for most consumers.

Suggested-by: Bartosz Golaszewski 
Signed-off-by: Nicolas Saenz Julienne 
---

Changes since v4:
 - Rearrange function calls for clarity, same functionality

Changes since v2:
- Create devm_rpi_firmware_get()

 drivers/firmware/raspberrypi.c | 29 ++
 include/soc/bcm2835/raspberrypi-firmware.h |  8 ++
 2 files changed, 37 insertions(+)

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index ed793aef7851..e2a5f6f59357 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -243,6 +243,13 @@ void rpi_firmware_put(struct rpi_firmware *fw)
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_put);
 
+static void devm_rpi_firmware_put(void *data)
+{
+   struct rpi_firmware *fw = data;
+
+   rpi_firmware_put(fw);
+}
+
 static int rpi_firmware_probe(struct platform_device *pdev)
 {
struct device *dev = >dev;
@@ -326,6 +333,28 @@ struct rpi_firmware *rpi_firmware_get(struct device_node 
*firmware_node)
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_get);
 
+/**
+ * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
+ * @firmware_node:Pointer to the firmware Device Tree node.
+ *
+ * Returns NULL is the firmware device is not ready.
+ */
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+  struct device_node *firmware_node)
+{
+   struct rpi_firmware *fw;
+
+   fw = rpi_firmware_get(firmware_node);
+   if (!fw)
+   return NULL;
+
+   if (devm_add_action_or_reset(dev, devm_rpi_firmware_put, fw))
+   return NULL;
+
+   return fw;
+}
+EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
+
 static const struct of_device_id rpi_firmware_of_match[] = {
{ .compatible = "raspberrypi,bcm2835-firmware", },
{},
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
b/include/soc/bcm2835/raspberrypi-firmware.h
index fdfef7fe40df..73ad784fca96 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -142,6 +142,8 @@ int rpi_firmware_property_list(struct rpi_firmware *fw,
   void *data, size_t tag_size);
 void rpi_firmware_put(struct rpi_firmware *fw);
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+  struct device_node *firmware_node);
 #else
 static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
void *data, size_t len)
@@ -160,6 +162,12 @@ static inline struct rpi_firmware *rpi_firmware_get(struct 
device_node *firmware
 {
return NULL;
 }
+
+static inline struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+   struct device_node *firmware_node)
+{
+   return NULL;
+}
 #endif
 
 #endif /* __SOC_RASPBERRY_FIRMWARE_H__ */
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v5 00/11] Raspberry Pi PoE HAT fan support

2020-11-23 Thread Nicolas Saenz Julienne
The aim of this series is to add support to the fan found on RPi's PoE
HAT. Some commentary on the design can be found below. But the imporant
part to the people CC'd here not involved with PWM is that, in order to
achieve this properly, we also have to fix the firmware interface the
driver uses to communicate with the PWM bus (and many other low level
functions). Specifically, we have to make sure the firmware interface
isn't unbound while consumers are still up. So, patch #1 & #2 introduce
reference counting in the firwmware interface driver and patches #3 to
#8 update all firmware users. Patches #9 to #11 introduce the new PWM
driver.

I sent everything as a single series as the final version of the PWM
drivers depends on the firwmare fixes, but I'll be happy to split this
into two separate series if you think it's better.

--- Original cover letter below ---

This series aims at adding support to RPi's official PoE HAT fan[1].

The HW setup is the following:

| Raspberry Pi   | PoE HAT|
 arm core -> Mailbox -> RPi co-processor -> I2C -> Atmel MCU -> PWM -> FAN

The arm cores have only access to the mailbox interface, as i2c0, even if
physically accessible, is to be used solely by the co-processor
(VideoCore 4/6).

This series implements a PWM bus, and has pwm-fan sitting on top of it as per
this discussion: https://lkml.org/lkml/2018/9/2/486. Although this design has a
series of shortcomings:

- It depends on a DT binding: it's not flexible if a new hat shows up with new
  functionality, we're not 100% sure we'll be able to expand it without
  breaking backwards compatibility. But without it we can't make use of DT
  thermal-zones, which IMO is overkill.

- We're using pwm-fan, writing a hwmon driver would, again, give us more
  flexibility, but it's not really needed at the moment.

I personally think that it's not worth the effort, it's unlikely we'll get
things right in advance. And ultimately, if the RPi people come up with
something new, we can always write a new driver/bindings from scratch (as in
not reusing previous code).

That said, I'm more than happy to change things if there is a consensus that
another design will do the trick.

[1] https://www.raspberrypi.org/blog/introducing-power-over-ethernet-poe-hat/

---

Changes since v4:
 - Cleanup devm calls
 - Rename compatible string so it's unique to the PoE HAT

Changes since v3:
 - Split first patch, #1 introduces refcount, then #2 the devm function
 - Fix touchscreen function
 - Use kref

Changes since v2:
 - Introduce devm_rpi_firmware_get()
 - Small cleanups in PWM driver

Changes since v1:
 - Address PWM driver changes
 - Fix binding, now with 2 cells

Nicolas Saenz Julienne (11):
  firmware: raspberrypi: Keep count of all consumers
  firmware: raspberrypi: Introduce devm_rpi_firmware_get()
  clk: bcm: rpi: Release firmware handle on unbind
  gpio: raspberrypi-exp: Release firmware handle on unbind
  reset: raspberrypi: Release firmware handle on unbind
  soc: bcm: raspberrypi-power: Release firmware handle on unbind
  staging: vchiq: Release firmware handle on unbind
  input: raspberrypi-ts: Release firmware handle when not needed
  dt-bindings: pwm: Add binding for RPi firmware PWM bus
  DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support
  pwm: Add Raspberry Pi Firmware based PWM bus

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml |  20 ++
 arch/arm/boot/dts/bcm2711-rpi-4-b.dts |  54 +
 drivers/clk/bcm/clk-raspberrypi.c |   2 +-
 drivers/firmware/raspberrypi.c|  66 +-
 drivers/gpio/gpio-raspberrypi-exp.c   |   2 +-
 drivers/input/touchscreen/raspberrypi-ts.c|   2 +-
 drivers/pwm/Kconfig   |   9 +
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi-poe.c | 216 ++
 drivers/reset/reset-raspberrypi.c |   2 +-
 drivers/soc/bcm/raspberrypi-power.c   |   2 +-
 .../interface/vchiq_arm/vchiq_arm.c   |   2 +-
 .../pwm/raspberrypi,firmware-poe-pwm.h|  13 ++
 include/soc/bcm2835/raspberrypi-firmware.h|  10 +
 14 files changed, 391 insertions(+), 10 deletions(-)
 create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-poe-pwm.h

-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v4 01/11] firmware: raspberrypi: Keep count of all consumers

2020-11-23 Thread Nicolas Saenz Julienne
On Thu, 2020-11-12 at 23:26 -0800, Dmitry Torokhov wrote:
> On Thu, Nov 12, 2020 at 07:52:14PM +0200, Andy Shevchenko wrote:
> > On Thu, Nov 12, 2020 at 6:40 PM Nicolas Saenz Julienne
> >  wrote:
> > > 
> > > When unbinding the firmware device we need to make sure it has no
> > > consumers left. Otherwise we'd leave them with a firmware handle
> > > pointing at freed memory.
> > > 
> > > Keep a reference count of all consumers and introduce rpi_firmware_put()
> > > which will permit automatically decrease the reference count upon
> > > unbinding consumer drivers.
> > 
> > ...
> > 
> > >  /**
> > > - * rpi_firmware_get - Get pointer to rpi_firmware structure.
> > >   * @firmware_node:Pointer to the firmware Device Tree node.
> > >   *
> > > + * The reference to rpi_firmware has to be released with 
> > > rpi_firmware_put().
> > > + *
> > >   * Returns NULL is the firmware device is not ready.
> > >   */
> > >  struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
> > >  {
> > > struct platform_device *pdev = 
> > > of_find_device_by_node(firmware_node);
> > > +   struct rpi_firmware *fw;
> > > 
> > > if (!pdev)
> > > return NULL;
> > > 
> > > -   return platform_get_drvdata(pdev);
> > > +   fw = platform_get_drvdata(pdev);
> > > +   if (!fw)
> > > +   return NULL;
> > > +
> > > +   if (!kref_get_unless_zero(>consumers))
> > > +   return NULL;
> > 

Hi Andy, Dimitry,

> > Don't we have a more traditional way of doing this, i.e.
> > try_module_get() coupled with get_device() ?
> 
> get_device() will make sure that device is there, but gives no
> assurances that device is bound to a driver, so it will not help with
> the racy access to firmware via platform_get_drvdata() call.

I also looked at using get/put_device() just as a means for refcounting (i.e.
replacing fw->consumers), but I can't make it work either. I'd need a way to
hook up into one of the struct device_ktype release() functions. AFAIK it's not
possible for private uses like this.

IIUC the way to do this would be to bypass platform device and create a special
device class/bus for RPi's firmware dependent devices (I could pretty much copy
SCMI's implementation), but I fear that's overkill.

So, for now I'll stick with the kref based implementation, I'll be happy to
change it if you find a better solution. :)

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v4 02/11] firmware: raspberrypi: Introduce devm_rpi_firmware_get()

2020-11-13 Thread Nicolas Saenz Julienne
On Thu, 2020-11-12 at 18:25 +0100, Bartosz Golaszewski wrote:
> On Thu, Nov 12, 2020 at 5:44 PM Nicolas Saenz Julienne
>  wrote:
> > Itroduce devm_rpi_firmware_get(), it'll simplify the firmware handling
> > for most consumers.
> > 
> > Suggested-by: Bartosz Golaszewski 
> > Signed-off-by: Nicolas Saenz Julienne 
> > ---
> > 
> > Changes since v2:
> > - Introduce devm_rpi_firmware_get()
> > 
> >  drivers/firmware/raspberrypi.c | 31 +-
> >  include/soc/bcm2835/raspberrypi-firmware.h |  8 ++
> >  2 files changed, 38 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
> > index 438e17074a97..4ab2dfdc82ad 100644
> > --- a/drivers/firmware/raspberrypi.c
> > +++ b/drivers/firmware/raspberrypi.c
> > @@ -237,10 +237,17 @@ static void rpi_firmware_delete(struct kref *kref)
> > kfree(fw);
> >  }
> > 
> > -void rpi_firmware_put(struct rpi_firmware *fw)
> > +static void __rpi_firmware_put(void *data)
> >  {
> 
> The '__' prefix is very vague and usually used for unlocked variants
> of functions. The casting to void * in rpi_firmware_put() is also
> unneeded. I would much prefer that the devres release callback be
> called devm_rpi_firmware_put() and that it call rpi_firmware_put()
> which would then call kref_put().

Yes, that's better. I'll change it.

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v4 09/11] dt-bindings: pwm: Add binding for RPi firmware PWM bus

2020-11-12 Thread Nicolas Saenz Julienne
The PWM bus controlling the fan in RPi's official PoE hat can only be
controlled by the board's co-processor.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Rob Herring 
---

Changes since v3:
 - Fix example

Changes since v1:
 - Update bindings to use 2 #pwm-cells

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml | 20 +++
 .../pwm/raspberrypi,firmware-pwm.h| 13 
 2 files changed, 33 insertions(+)
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-pwm.h

diff --git 
a/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml 
b/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
index a2c63c8b1d10..38be3ff69609 100644
--- 
a/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
+++ 
b/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
@@ -64,6 +64,21 @@ properties:
   - compatible
   - "#reset-cells"
 
+  pwm:
+type: object
+
+properties:
+  compatible:
+const: raspberrypi,firmware-pwm
+
+  "#pwm-cells":
+# See pwm.yaml in this directory for a description of the cells format.
+const: 2
+
+required:
+  - compatible
+  - "#pwm-cells"
+
 additionalProperties: false
 
 required:
@@ -87,5 +102,10 @@ examples:
 compatible = "raspberrypi,firmware-reset";
 #reset-cells = <1>;
 };
+
+pwm: pwm {
+compatible = "raspberrypi,firmware-pwm";
+#pwm-cells = <2>;
+};
 };
 ...
diff --git a/include/dt-bindings/pwm/raspberrypi,firmware-pwm.h 
b/include/dt-bindings/pwm/raspberrypi,firmware-pwm.h
new file mode 100644
index ..27c5ce68847b
--- /dev/null
+++ b/include/dt-bindings/pwm/raspberrypi,firmware-pwm.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2020 Nicolas Saenz Julienne
+ * Author: Nicolas Saenz Julienne 
+ */
+
+#ifndef _DT_BINDINGS_RASPBERRYPI_FIRMWARE_PWM_H
+#define _DT_BINDINGS_RASPBERRYPI_FIRMWARE_PWM_H
+
+#define RASPBERRYPI_FIRMWARE_PWM_POE   0
+#define RASPBERRYPI_FIRMWARE_PWM_NUM   1
+
+#endif
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v4 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2020-11-12 Thread Nicolas Saenz Julienne
Adds support to control the PWM bus available in official Raspberry Pi
PoE HAT. Only RPi's co-processor has access to it, so commands have to
be sent through RPi's firmware mailbox interface.

Signed-off-by: Nicolas Saenz Julienne 
---

Changes since v2:
 - Use devm_rpi_firmware_get()
 - Rename driver
 - Small cleanups

Changes since v1:
 - Use default pwm bindings and get rid of xlate() function
 - Correct spelling errors
 - Correct apply() function
 - Round values
 - Fix divisions in arm32 mode
 - Small cleanups

 drivers/pwm/Kconfig   |   9 ++
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi-poe.c | 216 ++
 3 files changed, 226 insertions(+)
 create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c

diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index b2a87fd7e8fb..3445984a5a50 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -409,6 +409,15 @@ config PWM_PXA
  To compile this driver as a module, choose M here: the module
  will be called pwm-pxa.
 
+config PWM_RASPBERRYPI_POE
+   tristate "Raspberry Pi Firwmware PoE Hat PWM support"
+   # Make sure not 'y' when RASPBERRYPI_FIRMWARE is 'm'. This can only
+   # happen when COMPILE_TEST=y, hence the added !RASPBERRYPI_FIRMWARE.
+   depends on RASPBERRYPI_FIRMWARE || (COMPILE_TEST && 
!RASPBERRYPI_FIRMWARE)
+   help
+ Enable Raspberry Pi firmware controller PWM bus used to control the
+ official RPI PoE hat
+
 config PWM_RCAR
tristate "Renesas R-Car PWM support"
depends on ARCH_RENESAS || COMPILE_TEST
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index 18b89d7fd092..ed28d7bd4c64 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -38,6 +38,7 @@ obj-$(CONFIG_PWM_MXS) += pwm-mxs.o
 obj-$(CONFIG_PWM_OMAP_DMTIMER) += pwm-omap-dmtimer.o
 obj-$(CONFIG_PWM_PCA9685)  += pwm-pca9685.o
 obj-$(CONFIG_PWM_PXA)  += pwm-pxa.o
+obj-$(CONFIG_PWM_RASPBERRYPI_POE)  += pwm-raspberrypi-poe.o
 obj-$(CONFIG_PWM_RCAR) += pwm-rcar.o
 obj-$(CONFIG_PWM_RENESAS_TPU)  += pwm-renesas-tpu.o
 obj-$(CONFIG_PWM_ROCKCHIP) += pwm-rockchip.o
diff --git a/drivers/pwm/pwm-raspberrypi-poe.c 
b/drivers/pwm/pwm-raspberrypi-poe.c
new file mode 100644
index ..91cd826a36f3
--- /dev/null
+++ b/drivers/pwm/pwm-raspberrypi-poe.c
@@ -0,0 +1,216 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2020 Nicolas Saenz Julienne 
+ * For more information on Raspberry Pi's PoE hat see:
+ * https://www.raspberrypi.org/products/poe-hat/
+ *
+ * Limitations:
+ *  - No disable bit, so a disabled PWM is simulated by duty_cycle 0
+ *  - Only normal polarity
+ *  - Fixed 12.5 kHz period
+ *
+ * The current period is completed when HW is reconfigured.
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#define RPI_PWM_MAX_DUTY   255
+#define RPI_PWM_PERIOD_NS  8 /* 12.5 kHz */
+
+#define RPI_PWM_CUR_DUTY_REG   0x0
+#define RPI_PWM_DEF_DUTY_REG   0x1
+
+struct raspberrypi_pwm {
+   struct rpi_firmware *firmware;
+   struct pwm_chip chip;
+   unsigned int duty_cycle;
+};
+
+struct raspberrypi_pwm_prop {
+   __le32 reg;
+   __le32 val;
+   __le32 ret;
+} __packed;
+
+static inline struct raspberrypi_pwm *to_raspberrypi_pwm(struct pwm_chip *chip)
+{
+   return container_of(chip, struct raspberrypi_pwm, chip);
+}
+
+static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware,
+   u32 reg, u32 val)
+{
+   struct raspberrypi_pwm_prop msg = {
+   .reg = cpu_to_le32(reg),
+   .val = cpu_to_le32(val),
+   };
+   int ret;
+
+   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
+   , sizeof(msg));
+   if (ret)
+   return ret;
+   if (msg.ret)
+   return -EIO;
+
+   return 0;
+}
+
+static int raspberrypi_pwm_get_property(struct rpi_firmware *firmware,
+   u32 reg, u32 *val)
+{
+   struct raspberrypi_pwm_prop msg = {
+   .reg = reg
+   };
+   int ret;
+
+   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
+   , sizeof(msg));
+   if (ret)
+   return ret;
+   if (msg.ret)
+   return -EIO;
+
+   *val = le32_to_cpu(msg.val);
+
+   return 0;
+}
+
+static void raspberrypi_pwm_get_state(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ struct pwm_state *state)
+{
+   struct raspberrypi_pwm *rpipwm = to_raspberrypi_pwm(chip);
+
+   state->period = RPI_PWM_PERIOD_NS;
+   state->duty_cycle = DIV_ROUND_CLOSEST(rpipwm->duty_cycle * 
RPI_PWM_PERIOD_NS,
+

[PATCH v4 07/11] staging: vchiq: Release firmware handle on unbind

2020-11-12 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c 
b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
index f500a7043805..6c196cade4a0 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
@@ -2732,7 +2732,7 @@ static int vchiq_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   drvdata->fw = rpi_firmware_get(fw_node);
+   drvdata->fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!drvdata->fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v4 02/11] firmware: raspberrypi: Introduce devm_rpi_firmware_get()

2020-11-12 Thread Nicolas Saenz Julienne
Itroduce devm_rpi_firmware_get(), it'll simplify the firmware handling
for most consumers.

Suggested-by: Bartosz Golaszewski 
Signed-off-by: Nicolas Saenz Julienne 
---

Changes since v2:
- Introduce devm_rpi_firmware_get()

 drivers/firmware/raspberrypi.c | 31 +-
 include/soc/bcm2835/raspberrypi-firmware.h |  8 ++
 2 files changed, 38 insertions(+), 1 deletion(-)

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index 438e17074a97..4ab2dfdc82ad 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -237,10 +237,17 @@ static void rpi_firmware_delete(struct kref *kref)
kfree(fw);
 }
 
-void rpi_firmware_put(struct rpi_firmware *fw)
+static void __rpi_firmware_put(void *data)
 {
+   struct rpi_firmware *fw = data;
+
kref_put(>consumers, rpi_firmware_delete);
 }
+
+void rpi_firmware_put(struct rpi_firmware *fw)
+{
+   __rpi_firmware_put(fw);
+}
 EXPORT_SYMBOL_GPL(rpi_firmware_put);
 
 static int rpi_firmware_probe(struct platform_device *pdev)
@@ -326,6 +333,28 @@ struct rpi_firmware *rpi_firmware_get(struct device_node 
*firmware_node)
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_get);
 
+/**
+ * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
+ * @firmware_node:Pointer to the firmware Device Tree node.
+ *
+ * Returns NULL is the firmware device is not ready.
+ */
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+  struct device_node *firmware_node)
+{
+   struct rpi_firmware *fw;
+
+   fw = rpi_firmware_get(firmware_node);
+   if (!fw)
+   return NULL;
+
+   if (devm_add_action_or_reset(dev, __rpi_firmware_put, fw))
+   return NULL;
+
+   return fw;
+}
+EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
+
 static const struct of_device_id rpi_firmware_of_match[] = {
{ .compatible = "raspberrypi,bcm2835-firmware", },
{},
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
b/include/soc/bcm2835/raspberrypi-firmware.h
index fdfef7fe40df..73ad784fca96 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -142,6 +142,8 @@ int rpi_firmware_property_list(struct rpi_firmware *fw,
   void *data, size_t tag_size);
 void rpi_firmware_put(struct rpi_firmware *fw);
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+  struct device_node *firmware_node);
 #else
 static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
void *data, size_t len)
@@ -160,6 +162,12 @@ static inline struct rpi_firmware *rpi_firmware_get(struct 
device_node *firmware
 {
return NULL;
 }
+
+static inline struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+   struct device_node *firmware_node)
+{
+   return NULL;
+}
 #endif
 
 #endif /* __SOC_RASPBERRY_FIRMWARE_H__ */
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v4 10/11] DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support

2020-11-12 Thread Nicolas Saenz Julienne
This is an example on how to enable the fan on top of RPi's official PoE
hat.

Signed-off-by: Nicolas Saenz Julienne 
---

Changes since v1:
 - Update patch to use 2 pwm cells

 arch/arm/boot/dts/bcm2711-rpi-4-b.dts | 54 +++
 1 file changed, 54 insertions(+)

diff --git a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts 
b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
index 09a1182c2936..361db82619a4 100644
--- a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
+++ b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
@@ -5,6 +5,7 @@
 #include "bcm283x-rpi-usb-peripheral.dtsi"
 
 #include 
+#include 
 
 / {
compatible = "raspberrypi,4-model-b", "brcm,bcm2711";
@@ -68,6 +69,54 @@ sd_vcc_reg: sd_vcc_reg {
enable-active-high;
gpio = < 6 GPIO_ACTIVE_HIGH>;
};
+
+   fan: pwm-fan {
+   compatible = "pwm-fan";
+   cooling-levels = <0 50 150 255>;
+   #cooling-cells = <2>;
+   pwms = < RASPBERRYPI_FIRMWARE_PWM_POE 8>;
+   };
+
+   thermal-zones {
+   cpu_thermal: cpu-thermal {
+   trips {
+   threshold: trip-point@0 {
+   temperature = <45000>;
+   hysteresis = <5000>;
+   type = "active";
+   };
+
+   target: trip-point@1 {
+   temperature = <5>;
+   hysteresis = <2000>;
+   type = "active";
+   };
+
+   cpu_hot: cpu_hot@0 {
+   temperature = <55000>;
+   hysteresis = <2000>;
+   type = "active";
+   };
+   };
+
+   cooling-maps {
+   map0 {
+   trip = <>;
+   cooling-device = < 0 1>;
+   };
+
+   map1 {
+   trip = <>;
+   cooling-device = < 1 2>;
+   };
+
+   map2 {
+   trip = <_hot>;
+   cooling-device = < 2 3>;
+   };
+   };
+   };
+   };
 };
 
  {
@@ -103,6 +152,11 @@ reset: reset {
compatible = "raspberrypi,firmware-reset";
#reset-cells = <1>;
};
+
+   fwpwm: pwm {
+   compatible = "raspberrypi,firmware-pwm";
+   #pwm-cells = <2>;
+   };
 };
 
  {
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v4 04/11] gpio: raspberrypi-exp: Release firmware handle on unbind

2020-11-12 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
Acked-by: Bartosz Golaszewski 
---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/gpio/gpio-raspberrypi-exp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpio/gpio-raspberrypi-exp.c 
b/drivers/gpio/gpio-raspberrypi-exp.c
index bb100e0124e6..64a552ecc2ad 100644
--- a/drivers/gpio/gpio-raspberrypi-exp.c
+++ b/drivers/gpio/gpio-raspberrypi-exp.c
@@ -208,7 +208,7 @@ static int rpi_exp_gpio_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(fw_node);
+   fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v4 05/11] reset: raspberrypi: Release firmware handle on unbind

2020-11-12 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/reset/reset-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/reset/reset-raspberrypi.c 
b/drivers/reset/reset-raspberrypi.c
index 02f59c06f69b..fa23db554bcf 100644
--- a/drivers/reset/reset-raspberrypi.c
+++ b/drivers/reset/reset-raspberrypi.c
@@ -82,7 +82,7 @@ static int rpi_reset_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(np);
+   fw = devm_rpi_firmware_get(>dev, np);
of_node_put(np);
if (!fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v4 06/11] soc: bcm: raspberrypi-power: Release firmware handle on unbind

2020-11-12 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/soc/bcm/raspberrypi-power.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/soc/bcm/raspberrypi-power.c 
b/drivers/soc/bcm/raspberrypi-power.c
index 5d1aacdd84ef..068715d6e66d 100644
--- a/drivers/soc/bcm/raspberrypi-power.c
+++ b/drivers/soc/bcm/raspberrypi-power.c
@@ -177,7 +177,7 @@ static int rpi_power_probe(struct platform_device *pdev)
return -ENODEV;
}
 
-   rpi_domains->fw = rpi_firmware_get(fw_np);
+   rpi_domains->fw = devm_rpi_firmware_get(>dev, fw_np);
of_node_put(fw_np);
if (!rpi_domains->fw)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v4 03/11] clk: bcm: rpi: Release firmware handle on unbind

2020-11-12 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/clk/bcm/clk-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/bcm/clk-raspberrypi.c 
b/drivers/clk/bcm/clk-raspberrypi.c
index f89b9cfc4309..dd3b71eafabf 100644
--- a/drivers/clk/bcm/clk-raspberrypi.c
+++ b/drivers/clk/bcm/clk-raspberrypi.c
@@ -314,7 +314,7 @@ static int raspberrypi_clk_probe(struct platform_device 
*pdev)
return -ENOENT;
}
 
-   firmware = rpi_firmware_get(firmware_node);
+   firmware = devm_rpi_firmware_get(>dev, firmware_node);
of_node_put(firmware_node);
if (!firmware)
return -EPROBE_DEFER;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v4 01/11] firmware: raspberrypi: Keep count of all consumers

2020-11-12 Thread Nicolas Saenz Julienne
When unbinding the firmware device we need to make sure it has no
consumers left. Otherwise we'd leave them with a firmware handle
pointing at freed memory.

Keep a reference count of all consumers and introduce rpi_firmware_put()
which will permit automatically decrease the reference count upon
unbinding consumer drivers.

Suggested-by: Uwe Kleine-König 
Signed-off-by: Nicolas Saenz Julienne 
---

Changes since v3:
- Use kref instead of waiting on refcount

 drivers/firmware/raspberrypi.c | 37 +++---
 include/soc/bcm2835/raspberrypi-firmware.h |  2 ++
 2 files changed, 35 insertions(+), 4 deletions(-)

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index 2371d08bdd17..438e17074a97 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -7,6 +7,7 @@
  */
 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -27,6 +28,8 @@ struct rpi_firmware {
struct mbox_chan *chan; /* The property channel. */
struct completion c;
u32 enabled;
+
+   struct kref consumers;
 };
 
 static DEFINE_MUTEX(transaction_lock);
@@ -225,12 +228,27 @@ static void rpi_register_clk_driver(struct device *dev)
-1, NULL, 0);
 }
 
+static void rpi_firmware_delete(struct kref *kref)
+{
+   struct rpi_firmware *fw = container_of(kref, struct rpi_firmware,
+  consumers);
+
+   mbox_free_channel(fw->chan);
+   kfree(fw);
+}
+
+void rpi_firmware_put(struct rpi_firmware *fw)
+{
+   kref_put(>consumers, rpi_firmware_delete);
+}
+EXPORT_SYMBOL_GPL(rpi_firmware_put);
+
 static int rpi_firmware_probe(struct platform_device *pdev)
 {
struct device *dev = >dev;
struct rpi_firmware *fw;
 
-   fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
+   fw = kzalloc(sizeof(*fw), GFP_KERNEL);
if (!fw)
return -ENOMEM;
 
@@ -247,6 +265,7 @@ static int rpi_firmware_probe(struct platform_device *pdev)
}
 
init_completion(>c);
+   kref_init(>consumers);
 
platform_set_drvdata(pdev, fw);
 
@@ -275,25 +294,35 @@ static int rpi_firmware_remove(struct platform_device 
*pdev)
rpi_hwmon = NULL;
platform_device_unregister(rpi_clk);
rpi_clk = NULL;
-   mbox_free_channel(fw->chan);
+
+   rpi_firmware_put(fw);
 
return 0;
 }
 
 /**
- * rpi_firmware_get - Get pointer to rpi_firmware structure.
  * @firmware_node:Pointer to the firmware Device Tree node.
  *
+ * The reference to rpi_firmware has to be released with rpi_firmware_put().
+ *
  * Returns NULL is the firmware device is not ready.
  */
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
 {
struct platform_device *pdev = of_find_device_by_node(firmware_node);
+   struct rpi_firmware *fw;
 
if (!pdev)
return NULL;
 
-   return platform_get_drvdata(pdev);
+   fw = platform_get_drvdata(pdev);
+   if (!fw)
+   return NULL;
+
+   if (!kref_get_unless_zero(>consumers))
+   return NULL;
+
+   return fw;
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_get);
 
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
b/include/soc/bcm2835/raspberrypi-firmware.h
index cc9cdbc66403..fdfef7fe40df 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -140,6 +140,7 @@ int rpi_firmware_property(struct rpi_firmware *fw,
  u32 tag, void *data, size_t len);
 int rpi_firmware_property_list(struct rpi_firmware *fw,
   void *data, size_t tag_size);
+void rpi_firmware_put(struct rpi_firmware *fw);
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
 #else
 static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
@@ -154,6 +155,7 @@ static inline int rpi_firmware_property_list(struct 
rpi_firmware *fw,
return -ENOSYS;
 }
 
+static inline void rpi_firmware_put(struct rpi_firmware *fw) { }
 static inline struct rpi_firmware *rpi_firmware_get(struct device_node 
*firmware_node)
 {
return NULL;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v4 08/11] input: raspberrypi-ts: Release firmware handle when not needed

2020-11-12 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 
---

Changes since v3:
 - Release firmware handle in probe function

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/input/touchscreen/raspberrypi-ts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/raspberrypi-ts.c 
b/drivers/input/touchscreen/raspberrypi-ts.c
index ef6aaed217cf..5000f5fd9ec3 100644
--- a/drivers/input/touchscreen/raspberrypi-ts.c
+++ b/drivers/input/touchscreen/raspberrypi-ts.c
@@ -160,7 +160,7 @@ static int rpi_ts_probe(struct platform_device *pdev)
touchbuf = (u32)ts->fw_regs_phys;
error = rpi_firmware_property(fw, RPI_FIRMWARE_FRAMEBUFFER_SET_TOUCHBUF,
  , sizeof(touchbuf));
-
+   rpi_firmware_put(fw);
if (error || touchbuf != 0) {
dev_warn(dev, "Failed to set touchbuf, %d\n", error);
return error;
-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v4 00/11] Raspberry Pi PoE HAT fan support

2020-11-12 Thread Nicolas Saenz Julienne
The aim of this series is to add support to the fan found on RPi's PoE
HAT. Some commentary on the design can be found below. But the imporant
part to the people CC'd here not involved with PWM is that, in order to
achieve this properly, we also have to fix the firmware interface the
driver uses to communicate with the PWM bus (and many other low level
functions). Specifically, we have to make sure the firmware interface
isn't unbound while consumers are still up. So, patch #1 & #2 introduce
reference counting in the firwmware interface driver and patches #3 to
#8 update all firmware users. Patches #9 to #11 introduce the new PWM
driver.

I sent everything as a single series as the final version of the PWM
drivers depends on the firwmare fixes, but I'll be happy to split this
into two separate series if you think it's better.

--- Original cover letter below ---

This series aims at adding support to RPi's official PoE HAT fan[1].

The HW setup is the following:

| Raspberry Pi   | PoE HAT|
 arm core -> Mailbox -> RPi co-processor -> I2C -> Atmel MCU -> PWM -> FAN

The arm cores have only access to the mailbox interface, as i2c0, even if
physically accessible, is to be used solely by the co-processor
(VideoCore 4/6).

This series implements a PWM bus, and has pwm-fan sitting on top of it as per
this discussion: https://lkml.org/lkml/2018/9/2/486. Although this design has a
series of shortcomings:

- It depends on a DT binding: it's not flexible if a new hat shows up with new
  functionality, we're not 100% sure we'll be able to expand it without
  breaking backwards compatibility. But without it we can't make use of DT
  thermal-zones, which IMO is overkill.

- We're using pwm-fan, writing a hwmon driver would, again, give us more
  flexibility, but it's not really needed at the moment.

I personally think that it's not worth the effort, it's unlikely we'll get
things right in advance. And ultimately, if the RPi people come up with
something new, we can always write a new driver/bindings from scratch (as in
not reusing previous code).

That said, I'm more than happy to change things if there is a consensus that
another design will do the trick.

[1] https://www.raspberrypi.org/blog/introducing-power-over-ethernet-poe-hat/

---

Changes since v3:
 - Split first patch, #1 introduces refcount, then #2 the devm function
 - Fix touchscreen function
 - Use kref

Changes since v2:
 - Introduce devm_rpi_firmware_get()
 - Small cleanups in PWM driver

Changes since v1:
 - Address PWM driver changes
 - Fix binding, now with 2 cells

Nicolas Saenz Julienne (11):
  firmware: raspberrypi: Keep count of all consumers
  firmware: raspberrypi: Introduce devm_rpi_firmware_get()
  clk: bcm: rpi: Release firmware handle on unbind
  gpio: raspberrypi-exp: Release firmware handle on unbind
  reset: raspberrypi: Release firmware handle on unbind
  soc: bcm: raspberrypi-power: Release firmware handle on unbind
  staging: vchiq: Release firmware handle on unbind
  input: raspberrypi-ts: Release firmware handle when not needed
  dt-bindings: pwm: Add binding for RPi firmware PWM bus
  DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support
  pwm: Add Raspberry Pi Firmware based PWM bus

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml |  20 ++
 arch/arm/boot/dts/bcm2711-rpi-4-b.dts |  54 +
 drivers/clk/bcm/clk-raspberrypi.c |   2 +-
 drivers/firmware/raspberrypi.c|  66 +-
 drivers/gpio/gpio-raspberrypi-exp.c   |   2 +-
 drivers/input/touchscreen/raspberrypi-ts.c|   2 +-
 drivers/pwm/Kconfig   |   9 +
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi-poe.c | 216 ++
 drivers/reset/reset-raspberrypi.c |   2 +-
 drivers/soc/bcm/raspberrypi-power.c   |   2 +-
 .../interface/vchiq_arm/vchiq_arm.c   |   2 +-
 .../pwm/raspberrypi,firmware-pwm.h|  13 ++
 include/soc/bcm2835/raspberrypi-firmware.h|  10 +
 14 files changed, 391 insertions(+), 10 deletions(-)
 create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-pwm.h

-- 
2.29.2

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v3 07/11] input: raspberrypi-ts: Release firmware handle when not needed

2020-11-12 Thread Nicolas Saenz Julienne
On Wed, 2020-11-11 at 17:45 -0800, Dmitry Torokhov wrote:
> Hi Nicolas,
> 
> On Wed, Nov 04, 2020 at 11:39:33AM +0100, Nicolas Saenz Julienne wrote:
> > Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
> > interface when unbinding the device.
> 
> Unless I am mistaken this driver does not really need the firmware
> structure past rpi_ts_probe(), and will be fine if it disappears earlier
> than unbind time.

Yes, I missed that. Will update it.

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v3 01/11] firmware: raspberrypi: Introduce devm_rpi_firmware_get()

2020-11-10 Thread Nicolas Saenz Julienne
Hi Bartosz, thanks for the feedback.

On Thu, 2020-11-05 at 10:42 +0100, Bartosz Golaszewski wrote:
> On Thu, Nov 5, 2020 at 10:28 AM Nicolas Saenz Julienne
>  wrote:
> > Hi Bartosz, thanks for the review.
> > 
> > On Thu, 2020-11-05 at 10:13 +0100, Bartosz Golaszewski wrote:
> > > > +/**
> > > > + * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
> > > > + * @firmware_node:Pointer to the firmware Device Tree node.
> > > > + *
> > > > + * Returns NULL is the firmware device is not ready.
> > > > + */
> > > > +struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
> > > > +  struct device_node 
> > > > *firmware_node)
> > > > +{
> > > > +   struct platform_device *pdev = 
> > > > of_find_device_by_node(firmware_node);
> > > > +   struct rpi_firmware *fw;
> > > > +
> > > > +   if (!pdev)
> > > > +   return NULL;
> > > > +
> > > > +   fw = platform_get_drvdata(pdev);
> > > > +   if (!fw)
> > > > +   return NULL;
> > > > +
> > > > +   if (!refcount_inc_not_zero(>consumers))
> > > > +   return NULL;
> > > > +
> > > > +   if (devm_add_action_or_reset(dev, rpi_firmware_put, fw))
> > > > +   return NULL;
> > > > +
> > > > +   return fw;
> > > > +}
> > > > +EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
> > > 
> > > Usually I'd expect the devres variant to simply call
> > > rpi_firmware_get() and then schedule a release callback which would
> > > call whatever function is the release counterpart for it currently.
> > > Devres actions are for drivers which want to schedule some more
> > > unusual tasks at driver detach. Any reason for designing it this way?
> > 
> > Yes, see patch #8 where I get rid of rpi_firmware_get() altogether after
> > converting all users to devres. Since there is no use for the vanilla 
> > version
> > of the function anymore, I figured it'd be better to merge everything into
> > devm_rpi_firmware_get(). That said it's not something I have strong feelings
> > about.
> > 
> 
> I see. So the previous version didn't really have any reference
> counting and it leaked the reference returned by
> of_find_device_by_node(), got it. Could you just clarify for me the
> logic behind the wait_queue in rpi_firmware_remove()? If the firmware
> driver gets detached and remove() stops on the wait_queue - it will be
> stuck until the last user releases the firmware. I'm not sure this is
> correct.

Yes, that's what I meant to implement.

> I'd prefer to see a kref with a release callback and remove
> would simply decrease the kref and return. Each user would do the same
> and then after the last user is detached the firmware would be
> destroyed.

Sounds good to me. I'll update it.

> Don't we really have some centralized firmware subsystem that would handle 
> this?

Sadly no, this is an RPi specific thing, it doesn't live behind a standard like
other firmware based protocols (for ex. SCMI), and evolves as the needs arise.

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v3 01/11] firmware: raspberrypi: Introduce devm_rpi_firmware_get()

2020-11-05 Thread Nicolas Saenz Julienne
Hi Bartosz, thanks for the review.

On Thu, 2020-11-05 at 10:13 +0100, Bartosz Golaszewski wrote:
> > +/**
> > + * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
> > + * @firmware_node:Pointer to the firmware Device Tree node.
> > + *
> > + * Returns NULL is the firmware device is not ready.
> > + */
> > +struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
> > +  struct device_node 
> > *firmware_node)
> > +{
> > +   struct platform_device *pdev = 
> > of_find_device_by_node(firmware_node);
> > +   struct rpi_firmware *fw;
> > +
> > +   if (!pdev)
> > +   return NULL;
> > +
> > +   fw = platform_get_drvdata(pdev);
> > +   if (!fw)
> > +   return NULL;
> > +
> > +   if (!refcount_inc_not_zero(>consumers))
> > +   return NULL;
> > +
> > +   if (devm_add_action_or_reset(dev, rpi_firmware_put, fw))
> > +   return NULL;
> > +
> > +   return fw;
> > +}
> > +EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
> 
> Usually I'd expect the devres variant to simply call
> rpi_firmware_get() and then schedule a release callback which would
> call whatever function is the release counterpart for it currently.
> Devres actions are for drivers which want to schedule some more
> unusual tasks at driver detach. Any reason for designing it this way?

Yes, see patch #8 where I get rid of rpi_firmware_get() altogether after
converting all users to devres. Since there is no use for the vanilla version
of the function anymore, I figured it'd be better to merge everything into
devm_rpi_firmware_get(). That said it's not something I have strong feelings
about.

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v3 09/11] dt-bindings: pwm: Add binding for RPi firmware PWM bus

2020-11-04 Thread Nicolas Saenz Julienne
On Wed, 2020-11-04 at 13:06 -0600, Rob Herring wrote:
> On Wed, 04 Nov 2020 11:39:35 +0100, Nicolas Saenz Julienne wrote:
> > The PWM bus controlling the fan in RPi's official PoE hat can only be
> > controlled by the board's co-processor.
> > 
> > Signed-off-by: Nicolas Saenz Julienne 
> > Reviewed-by: Rob Herring 
> > 
> > ---
> > 
> > Changes since v1:
> >  - Update bindings to use 2 #pwm-cells
> > 
> >  .../arm/bcm/raspberrypi,bcm2835-firmware.yaml | 20 +++
> >  .../pwm/raspberrypi,firmware-pwm.h| 13 
> >  2 files changed, 33 insertions(+)
> >  create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-pwm.h
> > 
> 
> My bot found errors running 'make dt_binding_check' on your patch:
> 
> yamllint warnings/errors:
> 
> dtschema/dtc warnings/errors:
> /builds/robherring/linux-dt-review/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.example.dt.yaml:
>  firmware: pwm:#pwm-cells:0:0: 2 was expected

Yes I forgot to update the example...

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v3 01/11] firmware: raspberrypi: Introduce devm_rpi_firmware_get()

2020-11-04 Thread Nicolas Saenz Julienne
When unbinding the firmware device we need to make sure it has no
consumers left. Otherwise we'd leave them with a firmware handle
pointing at freed memory.

Keep a reference count of all consumers and introduce
devm_rpi_firmware_get() which will automatically decrease the reference
count upon unbinding consumer drivers.

Suggested-by: Uwe Kleine-König 
Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v2:
 - Create devm_rpi_firmware_get()

 drivers/firmware/raspberrypi.c | 46 ++
 include/soc/bcm2835/raspberrypi-firmware.h |  8 
 2 files changed, 54 insertions(+)

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index 2371d08bdd17..74bdb3bde9dc 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -11,7 +11,9 @@
 #include 
 #include 
 #include 
+#include 
 #include 
+#include 
 #include 
 
 #define MBOX_MSG(chan, data28) (((data28) & ~0xf) | ((chan) & 0xf))
@@ -27,6 +29,9 @@ struct rpi_firmware {
struct mbox_chan *chan; /* The property channel. */
struct completion c;
u32 enabled;
+
+   refcount_t consumers;
+   wait_queue_head_t wait;
 };
 
 static DEFINE_MUTEX(transaction_lock);
@@ -247,6 +252,8 @@ static int rpi_firmware_probe(struct platform_device *pdev)
}
 
init_completion(>c);
+   refcount_set(>consumers, 1);
+   init_waitqueue_head(>wait);
 
platform_set_drvdata(pdev, fw);
 
@@ -275,11 +282,21 @@ static int rpi_firmware_remove(struct platform_device 
*pdev)
rpi_hwmon = NULL;
platform_device_unregister(rpi_clk);
rpi_clk = NULL;
+
+   wait_event(fw->wait, refcount_dec_if_one(>consumers));
mbox_free_channel(fw->chan);
 
return 0;
 }
 
+static void rpi_firmware_put(void *data)
+{
+   struct rpi_firmware *fw = data;
+
+   refcount_dec(>consumers);
+   wake_up(>wait);
+}
+
 /**
  * rpi_firmware_get - Get pointer to rpi_firmware structure.
  * @firmware_node:Pointer to the firmware Device Tree node.
@@ -297,6 +314,35 @@ struct rpi_firmware *rpi_firmware_get(struct device_node 
*firmware_node)
 }
 EXPORT_SYMBOL_GPL(rpi_firmware_get);
 
+/**
+ * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
+ * @firmware_node:Pointer to the firmware Device Tree node.
+ *
+ * Returns NULL is the firmware device is not ready.
+ */
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+  struct device_node *firmware_node)
+{
+   struct platform_device *pdev = of_find_device_by_node(firmware_node);
+   struct rpi_firmware *fw;
+
+   if (!pdev)
+   return NULL;
+
+   fw = platform_get_drvdata(pdev);
+   if (!fw)
+   return NULL;
+
+   if (!refcount_inc_not_zero(>consumers))
+   return NULL;
+
+   if (devm_add_action_or_reset(dev, rpi_firmware_put, fw))
+   return NULL;
+
+   return fw;
+}
+EXPORT_SYMBOL_GPL(devm_rpi_firmware_get);
+
 static const struct of_device_id rpi_firmware_of_match[] = {
{ .compatible = "raspberrypi,bcm2835-firmware", },
{},
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
b/include/soc/bcm2835/raspberrypi-firmware.h
index cc9cdbc66403..8fe64f53a394 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -141,6 +141,8 @@ int rpi_firmware_property(struct rpi_firmware *fw,
 int rpi_firmware_property_list(struct rpi_firmware *fw,
   void *data, size_t tag_size);
 struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
+struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+  struct device_node *firmware_node);
 #else
 static inline int rpi_firmware_property(struct rpi_firmware *fw, u32 tag,
void *data, size_t len)
@@ -158,6 +160,12 @@ static inline struct rpi_firmware *rpi_firmware_get(struct 
device_node *firmware
 {
return NULL;
 }
+
+static inline struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
+   struct device_node *firmware_node)
+{
+   return NULL;
+}
 #endif
 
 #endif /* __SOC_RASPBERRY_FIRMWARE_H__ */
-- 
2.29.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v3 06/11] staging: vchiq: Release firmware handle on unbind

2020-11-04 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c 
b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
index 01125d9f991b..3c25e237e334 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
@@ -2723,7 +2723,7 @@ static int vchiq_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   drvdata->fw = rpi_firmware_get(fw_node);
+   drvdata->fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!drvdata->fw)
return -EPROBE_DEFER;
-- 
2.29.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v3 11/11] pwm: Add Raspberry Pi Firmware based PWM bus

2020-11-04 Thread Nicolas Saenz Julienne
Adds support to control the PWM bus available in official Raspberry Pi
PoE HAT. Only RPi's co-processor has access to it, so commands have to
be sent through RPi's firmware mailbox interface.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v2:
 - Use devm_rpi_firmware_get()
 - Rename driver
 - Small cleanups as per Andy Shevchenko's comments

Changes since v1:
 - Use default pwm bindings and get rid of xlate() function
 - Correct spelling errors
 - Correct apply() function
 - Round values
 - Fix divisions in arm32 mode
 - Small cleanups

 drivers/pwm/Kconfig   |   9 ++
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi-poe.c | 216 ++
 3 files changed, 226 insertions(+)
 create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c

diff --git a/drivers/pwm/Kconfig b/drivers/pwm/Kconfig
index 63be5362fd3a..ab31615f07fc 100644
--- a/drivers/pwm/Kconfig
+++ b/drivers/pwm/Kconfig
@@ -379,6 +379,15 @@ config PWM_PXA
  To compile this driver as a module, choose M here: the module
  will be called pwm-pxa.
 
+config PWM_RASPBERRYPI_POE
+   tristate "Raspberry Pi Firwmware PoE Hat PWM support"
+   # Make sure not 'y' when RASPBERRYPI_FIRMWARE is 'm'. This can only
+   # happen when COMPILE_TEST=y, hence the added !RASPBERRYPI_FIRMWARE.
+   depends on RASPBERRYPI_FIRMWARE || (COMPILE_TEST && 
!RASPBERRYPI_FIRMWARE)
+   help
+ Enable Raspberry Pi firmware controller PWM bus used to control the
+ official RPI PoE hat
+
 config PWM_RCAR
tristate "Renesas R-Car PWM support"
depends on ARCH_RENESAS || COMPILE_TEST
diff --git a/drivers/pwm/Makefile b/drivers/pwm/Makefile
index cbdcd55d69ee..7ecbbbcb323a 100644
--- a/drivers/pwm/Makefile
+++ b/drivers/pwm/Makefile
@@ -35,6 +35,7 @@ obj-$(CONFIG_PWM_MXS) += pwm-mxs.o
 obj-$(CONFIG_PWM_OMAP_DMTIMER) += pwm-omap-dmtimer.o
 obj-$(CONFIG_PWM_PCA9685)  += pwm-pca9685.o
 obj-$(CONFIG_PWM_PXA)  += pwm-pxa.o
+obj-$(CONFIG_PWM_RASPBERRYPI_POE)  += pwm-raspberrypi-poe.o
 obj-$(CONFIG_PWM_RCAR) += pwm-rcar.o
 obj-$(CONFIG_PWM_RENESAS_TPU)  += pwm-renesas-tpu.o
 obj-$(CONFIG_PWM_ROCKCHIP) += pwm-rockchip.o
diff --git a/drivers/pwm/pwm-raspberrypi-poe.c 
b/drivers/pwm/pwm-raspberrypi-poe.c
new file mode 100644
index ..91cd826a36f3
--- /dev/null
+++ b/drivers/pwm/pwm-raspberrypi-poe.c
@@ -0,0 +1,216 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2020 Nicolas Saenz Julienne 
+ * For more information on Raspberry Pi's PoE hat see:
+ * https://www.raspberrypi.org/products/poe-hat/
+ *
+ * Limitations:
+ *  - No disable bit, so a disabled PWM is simulated by duty_cycle 0
+ *  - Only normal polarity
+ *  - Fixed 12.5 kHz period
+ *
+ * The current period is completed when HW is reconfigured.
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+#include 
+#include 
+
+#define RPI_PWM_MAX_DUTY   255
+#define RPI_PWM_PERIOD_NS  8 /* 12.5 kHz */
+
+#define RPI_PWM_CUR_DUTY_REG   0x0
+#define RPI_PWM_DEF_DUTY_REG   0x1
+
+struct raspberrypi_pwm {
+   struct rpi_firmware *firmware;
+   struct pwm_chip chip;
+   unsigned int duty_cycle;
+};
+
+struct raspberrypi_pwm_prop {
+   __le32 reg;
+   __le32 val;
+   __le32 ret;
+} __packed;
+
+static inline struct raspberrypi_pwm *to_raspberrypi_pwm(struct pwm_chip *chip)
+{
+   return container_of(chip, struct raspberrypi_pwm, chip);
+}
+
+static int raspberrypi_pwm_set_property(struct rpi_firmware *firmware,
+   u32 reg, u32 val)
+{
+   struct raspberrypi_pwm_prop msg = {
+   .reg = cpu_to_le32(reg),
+   .val = cpu_to_le32(val),
+   };
+   int ret;
+
+   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
+   , sizeof(msg));
+   if (ret)
+   return ret;
+   if (msg.ret)
+   return -EIO;
+
+   return 0;
+}
+
+static int raspberrypi_pwm_get_property(struct rpi_firmware *firmware,
+   u32 reg, u32 *val)
+{
+   struct raspberrypi_pwm_prop msg = {
+   .reg = reg
+   };
+   int ret;
+
+   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_GET_POE_HAT_VAL,
+   , sizeof(msg));
+   if (ret)
+   return ret;
+   if (msg.ret)
+   return -EIO;
+
+   *val = le32_to_cpu(msg.val);
+
+   return 0;
+}
+
+static void raspberrypi_pwm_get_state(struct pwm_chip *chip,
+ struct pwm_device *pwm,
+ struct pwm_state *state)
+{
+   struct raspberrypi_pwm *rpipwm = to_raspberrypi_pwm(chip);
+
+   state->period = RPI_PWM_PERIOD_NS;
+   state->duty_cycle = DIV_ROUND_CLOSEST(rpipw

[PATCH v3 09/11] dt-bindings: pwm: Add binding for RPi firmware PWM bus

2020-11-04 Thread Nicolas Saenz Julienne
The PWM bus controlling the fan in RPi's official PoE hat can only be
controlled by the board's co-processor.

Signed-off-by: Nicolas Saenz Julienne 
Reviewed-by: Rob Herring 

---

Changes since v1:
 - Update bindings to use 2 #pwm-cells

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml | 20 +++
 .../pwm/raspberrypi,firmware-pwm.h| 13 
 2 files changed, 33 insertions(+)
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-pwm.h

diff --git 
a/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml 
b/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
index a2c63c8b1d10..8029ce958c48 100644
--- 
a/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
+++ 
b/Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml
@@ -64,6 +64,21 @@ properties:
   - compatible
   - "#reset-cells"
 
+  pwm:
+type: object
+
+properties:
+  compatible:
+const: raspberrypi,firmware-pwm
+
+  "#pwm-cells":
+# See pwm.yaml in this directory for a description of the cells format.
+const: 2
+
+required:
+  - compatible
+  - "#pwm-cells"
+
 additionalProperties: false
 
 required:
@@ -87,5 +102,10 @@ examples:
 compatible = "raspberrypi,firmware-reset";
 #reset-cells = <1>;
 };
+
+pwm: pwm {
+compatible = "raspberrypi,firmware-pwm";
+#pwm-cells = <1>;
+};
 };
 ...
diff --git a/include/dt-bindings/pwm/raspberrypi,firmware-pwm.h 
b/include/dt-bindings/pwm/raspberrypi,firmware-pwm.h
new file mode 100644
index ..27c5ce68847b
--- /dev/null
+++ b/include/dt-bindings/pwm/raspberrypi,firmware-pwm.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Copyright (c) 2020 Nicolas Saenz Julienne
+ * Author: Nicolas Saenz Julienne 
+ */
+
+#ifndef _DT_BINDINGS_RASPBERRYPI_FIRMWARE_PWM_H
+#define _DT_BINDINGS_RASPBERRYPI_FIRMWARE_PWM_H
+
+#define RASPBERRYPI_FIRMWARE_PWM_POE   0
+#define RASPBERRYPI_FIRMWARE_PWM_NUM   1
+
+#endif
-- 
2.29.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v3 07/11] input: raspberrypi-ts: Release firmware handle when not needed

2020-11-04 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/input/touchscreen/raspberrypi-ts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/input/touchscreen/raspberrypi-ts.c 
b/drivers/input/touchscreen/raspberrypi-ts.c
index ef6aaed217cf..efed0efa91d9 100644
--- a/drivers/input/touchscreen/raspberrypi-ts.c
+++ b/drivers/input/touchscreen/raspberrypi-ts.c
@@ -134,7 +134,7 @@ static int rpi_ts_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(fw_node);
+   fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!fw)
return -EPROBE_DEFER;
-- 
2.29.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v3 08/11] firmware: raspberrypi: Get rid of rpi_firmware_get()

2020-11-04 Thread Nicolas Saenz Julienne
There a no users left to the function as they all converted to
devm_rpi_firmware_get(). Just get rid of it.

Signed-off-by: Nicolas Saenz Julienne 
---
 drivers/firmware/raspberrypi.c | 17 -
 include/soc/bcm2835/raspberrypi-firmware.h |  6 --
 2 files changed, 23 deletions(-)

diff --git a/drivers/firmware/raspberrypi.c b/drivers/firmware/raspberrypi.c
index 74bdb3bde9dc..67f7105f3fd2 100644
--- a/drivers/firmware/raspberrypi.c
+++ b/drivers/firmware/raspberrypi.c
@@ -297,23 +297,6 @@ static void rpi_firmware_put(void *data)
wake_up(>wait);
 }
 
-/**
- * rpi_firmware_get - Get pointer to rpi_firmware structure.
- * @firmware_node:Pointer to the firmware Device Tree node.
- *
- * Returns NULL is the firmware device is not ready.
- */
-struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node)
-{
-   struct platform_device *pdev = of_find_device_by_node(firmware_node);
-
-   if (!pdev)
-   return NULL;
-
-   return platform_get_drvdata(pdev);
-}
-EXPORT_SYMBOL_GPL(rpi_firmware_get);
-
 /**
  * devm_rpi_firmware_get - Get pointer to rpi_firmware structure.
  * @firmware_node:Pointer to the firmware Device Tree node.
diff --git a/include/soc/bcm2835/raspberrypi-firmware.h 
b/include/soc/bcm2835/raspberrypi-firmware.h
index 8fe64f53a394..eaa4e516e4c6 100644
--- a/include/soc/bcm2835/raspberrypi-firmware.h
+++ b/include/soc/bcm2835/raspberrypi-firmware.h
@@ -140,7 +140,6 @@ int rpi_firmware_property(struct rpi_firmware *fw,
  u32 tag, void *data, size_t len);
 int rpi_firmware_property_list(struct rpi_firmware *fw,
   void *data, size_t tag_size);
-struct rpi_firmware *rpi_firmware_get(struct device_node *firmware_node);
 struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
   struct device_node *firmware_node);
 #else
@@ -156,11 +155,6 @@ static inline int rpi_firmware_property_list(struct 
rpi_firmware *fw,
return -ENOSYS;
 }
 
-static inline struct rpi_firmware *rpi_firmware_get(struct device_node 
*firmware_node)
-{
-   return NULL;
-}
-
 static inline struct rpi_firmware *devm_rpi_firmware_get(struct device *dev,
struct device_node *firmware_node)
 {
-- 
2.29.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v3 04/11] reset: raspberrypi: Release firmware handle on unbind

2020-11-04 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/reset/reset-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/reset/reset-raspberrypi.c 
b/drivers/reset/reset-raspberrypi.c
index 02f59c06f69b..fa23db554bcf 100644
--- a/drivers/reset/reset-raspberrypi.c
+++ b/drivers/reset/reset-raspberrypi.c
@@ -82,7 +82,7 @@ static int rpi_reset_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(np);
+   fw = devm_rpi_firmware_get(>dev, np);
of_node_put(np);
if (!fw)
return -EPROBE_DEFER;
-- 
2.29.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v3 03/11] gpio: raspberrypi-exp: Release firmware handle on unbind

2020-11-04 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/gpio/gpio-raspberrypi-exp.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/gpio/gpio-raspberrypi-exp.c 
b/drivers/gpio/gpio-raspberrypi-exp.c
index bb100e0124e6..64a552ecc2ad 100644
--- a/drivers/gpio/gpio-raspberrypi-exp.c
+++ b/drivers/gpio/gpio-raspberrypi-exp.c
@@ -208,7 +208,7 @@ static int rpi_exp_gpio_probe(struct platform_device *pdev)
return -ENOENT;
}
 
-   fw = rpi_firmware_get(fw_node);
+   fw = devm_rpi_firmware_get(>dev, fw_node);
of_node_put(fw_node);
if (!fw)
return -EPROBE_DEFER;
-- 
2.29.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v3 10/11] DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support

2020-11-04 Thread Nicolas Saenz Julienne
This is an example on how to enable the fan on top of RPi's official PoE
hat.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v1:
 - Update patch to use 2 pwm cells

 arch/arm/boot/dts/bcm2711-rpi-4-b.dts | 54 +++
 1 file changed, 54 insertions(+)

diff --git a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts 
b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
index 09a1182c2936..361db82619a4 100644
--- a/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
+++ b/arch/arm/boot/dts/bcm2711-rpi-4-b.dts
@@ -5,6 +5,7 @@
 #include "bcm283x-rpi-usb-peripheral.dtsi"
 
 #include 
+#include 
 
 / {
compatible = "raspberrypi,4-model-b", "brcm,bcm2711";
@@ -68,6 +69,54 @@ sd_vcc_reg: sd_vcc_reg {
enable-active-high;
gpio = < 6 GPIO_ACTIVE_HIGH>;
};
+
+   fan: pwm-fan {
+   compatible = "pwm-fan";
+   cooling-levels = <0 50 150 255>;
+   #cooling-cells = <2>;
+   pwms = < RASPBERRYPI_FIRMWARE_PWM_POE 8>;
+   };
+
+   thermal-zones {
+   cpu_thermal: cpu-thermal {
+   trips {
+   threshold: trip-point@0 {
+   temperature = <45000>;
+   hysteresis = <5000>;
+   type = "active";
+   };
+
+   target: trip-point@1 {
+   temperature = <5>;
+   hysteresis = <2000>;
+   type = "active";
+   };
+
+   cpu_hot: cpu_hot@0 {
+   temperature = <55000>;
+   hysteresis = <2000>;
+   type = "active";
+   };
+   };
+
+   cooling-maps {
+   map0 {
+   trip = <>;
+   cooling-device = < 0 1>;
+   };
+
+   map1 {
+   trip = <>;
+   cooling-device = < 1 2>;
+   };
+
+   map2 {
+   trip = <_hot>;
+   cooling-device = < 2 3>;
+   };
+   };
+   };
+   };
 };
 
  {
@@ -103,6 +152,11 @@ reset: reset {
compatible = "raspberrypi,firmware-reset";
#reset-cells = <1>;
};
+
+   fwpwm: pwm {
+   compatible = "raspberrypi,firmware-pwm";
+   #pwm-cells = <2>;
+   };
 };
 
  {
-- 
2.29.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v3 05/11] soc: bcm: raspberrypi-power: Release firmware handle on unbind

2020-11-04 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/soc/bcm/raspberrypi-power.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/soc/bcm/raspberrypi-power.c 
b/drivers/soc/bcm/raspberrypi-power.c
index 5d1aacdd84ef..068715d6e66d 100644
--- a/drivers/soc/bcm/raspberrypi-power.c
+++ b/drivers/soc/bcm/raspberrypi-power.c
@@ -177,7 +177,7 @@ static int rpi_power_probe(struct platform_device *pdev)
return -ENODEV;
}
 
-   rpi_domains->fw = rpi_firmware_get(fw_np);
+   rpi_domains->fw = devm_rpi_firmware_get(>dev, fw_np);
of_node_put(fw_np);
if (!rpi_domains->fw)
return -EPROBE_DEFER;
-- 
2.29.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v3 02/11] clk: bcm: rpi: Release firmware handle on unbind

2020-11-04 Thread Nicolas Saenz Julienne
Use devm_rpi_firmware_get() so as to make sure we release RPi's firmware
interface when unbinding the device.

Signed-off-by: Nicolas Saenz Julienne 

---

Changes since v2:
 - Use devm_rpi_firmware_get(), instead of remove function

 drivers/clk/bcm/clk-raspberrypi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/clk/bcm/clk-raspberrypi.c 
b/drivers/clk/bcm/clk-raspberrypi.c
index f89b9cfc4309..dd3b71eafabf 100644
--- a/drivers/clk/bcm/clk-raspberrypi.c
+++ b/drivers/clk/bcm/clk-raspberrypi.c
@@ -314,7 +314,7 @@ static int raspberrypi_clk_probe(struct platform_device 
*pdev)
return -ENOENT;
}
 
-   firmware = rpi_firmware_get(firmware_node);
+   firmware = devm_rpi_firmware_get(>dev, firmware_node);
of_node_put(firmware_node);
if (!firmware)
return -EPROBE_DEFER;
-- 
2.29.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v3 00/11] Raspberry Pi PoE HAT fan support

2020-11-04 Thread Nicolas Saenz Julienne
The aim of this series is to add support to the fan found on RPi's PoE
HAT. Some commentary on the design can be found below. But the imporant
part to the people CC'd here not involved with PWM is that, in order to
achieve this properly, we also have to fix the firmware interface the
driver uses to communicate with the PWM bus (and many other low level
functions). Specifically, we have to make sure the firmware interface
isn't unbound while consumers are still up. So, patch #1 introduces
reference counting in the firwmware interface driver and patches #2 to
#7 update all firmware users. Patches #8 to #10 introduce the new PWM
driver.

I sent everything as a single series as the final version of the PWM
drivers depends on the firwmare fixes, but I'll be happy to split this
into two separate series if you think it's better.

--- Original cover letter below ---

This series aims at adding support to RPi's official PoE HAT fan[1].

The HW setup is the following:

| Raspberry Pi   | PoE HAT|
 arm core -> Mailbox -> RPi co-processor -> I2C -> Atmel MCU -> PWM -> FAN

The arm cores have only access to the mailbox interface, as i2c0, even if
physically accessible, is to be used solely by the co-processor
(VideoCore 4/6).

This series implements a PWM bus, and has pwm-fan sitting on top of it as per
this discussion: https://lkml.org/lkml/2018/9/2/486. Although this design has a
series of shortcomings:

- It depends on a DT binding: it's not flexible if a new hat shows up with new
  functionality, we're not 100% sure we'll be able to expand it without
  breaking backwards compatibility. But without it we can't make use of DT
  thermal-zones, which IMO is overkill.

- We're using pwm-fan, writing a hwmon driver would, again, give us more
  flexibility, but it's not really needed at the moment.

I personally think that it's not worth the effort, it's unlikely we'll get
things right in advance. And ultimately, if the RPi people come up with
something new, we can always write a new driver/bindings from scratch (as in
not reusing previous code).

That said, I'm more than happy to change things if there is a consensus that
another design will do the trick.

[1] https://www.raspberrypi.org/blog/introducing-power-over-ethernet-poe-hat/

---

Changes since v2:
 - Introduce devm_rpi_firmware_get()
 - Small cleanups in PWM driver

Changes since v1:
 - Address PWM driver changes
 - Fix binding, now with 2 cells
 - Add reference count to rpi_firmware_get()

Nicolas Saenz Julienne (11):
  firmware: raspberrypi: Introduce devm_rpi_firmware_get()
  clk: bcm: rpi: Release firmware handle on unbind
  gpio: raspberrypi-exp: Release firmware handle on unbind
  reset: raspberrypi: Release firmware handle on unbind
  soc: bcm: raspberrypi-power: Release firmware handle on unbind
  staging: vchiq: Release firmware handle on unbind
  input: raspberrypi-ts: Release firmware handle when not needed
  firmware: raspberrypi: Get rid of rpi_firmware_get()
  dt-bindings: pwm: Add binding for RPi firmware PWM bus
  DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support
  pwm: Add Raspberry Pi Firmware based PWM bus

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml |  20 ++
 arch/arm/boot/dts/bcm2711-rpi-4-b.dts |  54 +
 drivers/clk/bcm/clk-raspberrypi.c |   2 +-
 drivers/firmware/raspberrypi.c|  37 ++-
 drivers/gpio/gpio-raspberrypi-exp.c   |   2 +-
 drivers/input/touchscreen/raspberrypi-ts.c|   2 +-
 drivers/pwm/Kconfig   |   9 +
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi-poe.c | 216 ++
 drivers/reset/reset-raspberrypi.c |   2 +-
 drivers/soc/bcm/raspberrypi-power.c   |   2 +-
 .../interface/vchiq_arm/vchiq_arm.c   |   2 +-
 .../pwm/raspberrypi,firmware-pwm.h|  13 ++
 include/soc/bcm2835/raspberrypi-firmware.h|   6 +-
 14 files changed, 356 insertions(+), 12 deletions(-)
 create mode 100644 drivers/pwm/pwm-raspberrypi-poe.c
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-pwm.h

-- 
2.29.1

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v2 01/10] firmware: raspberrypi: Introduce rpi_firmware_put()

2020-10-26 Thread Nicolas Saenz Julienne
On Thu, 2020-10-22 at 21:46 +0300, Andy Shevchenko wrote:
> On Thu, Oct 22, 2020 at 9:06 PM Nicolas Saenz Julienne
>  wrote:
> > When unbinding the firmware device we need to make sure it has no
> > consumers left. Otherwise we'd leave them with a firmware handle
> > pointing at freed memory.
> > 
> > Keep a reference count of all consumers and make sure they all finished
> > unbinding before we do.
> 
> Wait, if it's a device, why do we need all these?
> get_device() / put_device() along with module_get() / module_put()
> should be sufficient, no?

Could you expand here a little, I do see how I could use get_device()'s
reference count. But it seems to me I'd be digging way too deep into kobj in
order to get the functionality I need.

If you meant to say that it should be automatically taken care by the platform
bus, just FYI we're using 'simple-mfd' in DT. Where firmware supplier is the
parent and all consumers are children.

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v2 03/10] gpio: raspberrypi-exp: Release firmware handle on unbind

2020-10-26 Thread Nicolas Saenz Julienne
On Mon, 2020-10-26 at 15:40 +0100, Bartosz Golaszewski wrote:
> On Thu, Oct 22, 2020 at 5:59 PM Nicolas Saenz Julienne
>  wrote:
> > Upon unbinding the device make sure we release RPi's firmware interface.
> > 
> > Signed-off-by: Nicolas Saenz Julienne 
> > ---
> >  drivers/gpio/gpio-raspberrypi-exp.c | 14 +-
> >  1 file changed, 13 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/gpio/gpio-raspberrypi-exp.c 
> > b/drivers/gpio/gpio-raspberrypi-exp.c
> > index bb100e0124e6..c008336e1131 100644
> > --- a/drivers/gpio/gpio-raspberrypi-exp.c
> > +++ b/drivers/gpio/gpio-raspberrypi-exp.c
> > @@ -231,8 +231,19 @@ static int rpi_exp_gpio_probe(struct platform_device 
> > *pdev)
> > rpi_gpio->gc.get = rpi_exp_gpio_get;
> > rpi_gpio->gc.set = rpi_exp_gpio_set;
> > rpi_gpio->gc.can_sleep = true;
> > +   platform_set_drvdata(pdev, rpi_gpio);
> > 
> > -   return devm_gpiochip_add_data(dev, _gpio->gc, rpi_gpio);
> > +   return gpiochip_add_data(_gpio->gc, rpi_gpio);
> > +}
> > +
> > +static int rpi_exp_gpio_remove(struct platform_device *pdev)
> > +{
> > +   struct rpi_exp_gpio *rpi_gpio = platform_get_drvdata(pdev);
> > +
> > +   gpiochip_remove(_gpio->gc);
> > +   rpi_firmware_put(rpi_gpio->fw);
> > +
> > +   return 0;
> >  }
> > 
> >  static const struct of_device_id rpi_exp_gpio_ids[] = {
> > @@ -247,6 +258,7 @@ static struct platform_driver rpi_exp_gpio_driver = {
> > .of_match_table = of_match_ptr(rpi_exp_gpio_ids),
> > },
> > .probe  = rpi_exp_gpio_probe,
> > +   .remove = rpi_exp_gpio_remove,
> >  };
> >  module_platform_driver(rpi_exp_gpio_driver);
> > 
> > --
> > 2.28.0
> > 
> 
> Why not introduce devm_rpi_firmware_get()? That would allow you to
> keep the driver elegant without re-adding remove().

I like the idea, I'll look into it.

Thanks,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v2 05/10] soc: bcm: raspberrypi-power: Release firmware handle on unbind

2020-10-26 Thread Nicolas Saenz Julienne
On Thu, 2020-10-22 at 17:58 +0200, Nicolas Saenz Julienne wrote:
> Upon unbinding the device make sure we release RPi's firmware interface.
> 
> Signed-off-by: Nicolas Saenz Julienne 
> ---
>  drivers/soc/bcm/raspberrypi-power.c | 15 +++
>  1 file changed, 15 insertions(+)
> 
> diff --git a/drivers/soc/bcm/raspberrypi-power.c 
> b/drivers/soc/bcm/raspberrypi-power.c
> index 5d1aacdd84ef..a0b38db5886c 100644
> --- a/drivers/soc/bcm/raspberrypi-power.c
> +++ b/drivers/soc/bcm/raspberrypi-power.c
> @@ -225,6 +225,20 @@ static int rpi_power_probe(struct platform_device *pdev)
>   return 0;
>  }
>  
> +static int rpi_power_remove(struct platform_device *pdev)
> +{
> + struct rpi_power_domains *rpi_domains = platform_get_drvdata(pdev);
> +
> + of_genpd_del_provider(dev->of_node);
> +
> + for (i = 0; i < RPI_POWER_DOMAIN_COUNT; i++)
> + pm_genpd_remove(_domains->domains[i].base);
> +
> + rpi_firmware_put(rpi_domaina->fw);

I Just realised I failed to squash a fix for this patch, so this will not
build. Sorry for that.

Regards,
Nicolas

> +
> + return 0;
> +}
> +
>  static const struct of_device_id rpi_power_of_match[] = {
>   { .compatible = "raspberrypi,bcm2835-power", },
>   {},
> @@ -237,6 +251,7 @@ static struct platform_driver rpi_power_driver = {
>   .of_match_table = rpi_power_of_match,
>   },
>   .probe  = rpi_power_probe,
> + .remove = rpi_power_remove,
>  };
>  builtin_platform_driver(rpi_power_driver);
>  



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


Re: [PATCH v2 10/10] pwm: Add Raspberry Pi Firmware based PWM bus

2020-10-26 Thread Nicolas Saenz Julienne
Hi Andy, thanks for the review!

On Thu, 2020-10-22 at 21:53 +0300, Andy Shevchenko wrote:
> On Thu, Oct 22, 2020 at 9:05 PM Nicolas Saenz Julienne
>  wrote:
> > Adds support to control the PWM bus available in official Raspberry Pi
> > PoE HAT. Only RPi's co-processor has access to it, so commands have to
> > be sent through RPi's firmware mailbox interface.
> >  drivers/pwm/pwm-raspberrypi.c | 221 ++
> 
> Name is completely confusing.
> Please, make it unique enough to understand that this is exactly the
> device it serves for.
> 
> For example, pwm-rpi-poe is better.

Sounds reasonable, I'll change that.

> 
> ...
> 
> > + *  - Only normal polarity
> 
> Can't it be emulated? Isn't it 100% - duty cycle % ?

I guess it can, OTOH given the rather specific use case, I doubt it'll be
worth the effort.

> > +#include 
> > +#include 
> > +#include 
> > +#include 
> 
> ...
> 
> > +   ret = rpi_firmware_property(firmware, RPI_FIRMWARE_SET_POE_HAT_VAL,
> > +   , sizeof(msg));
> > +   if (ret)
> > +   return ret;
> > +   else if (msg.ret)
> 
> Redundant 'else'

Noted.

> > +   firmware_node = of_get_parent(dev->of_node);
> > +   if (!firmware_node) {
> > +   dev_err(dev, "Missing firmware node\n");
> > +   return -ENOENT;
> > +   }
> > +
> > +   firmware = rpi_firmware_get(firmware_node);
> > +   of_node_put(firmware_node);
> > +   if (!firmware)
> > +   return -EPROBE_DEFER;
> 
> Looks like a hack.

This is the pattern we've been using on all firmware dependent devices so far.
Feel free to suggest a better way, I'll be happy to look into it.

> 
> ...
> 
> > +   ret = pwmchip_remove(>chip);
> > +   if (!ret)
> > +   rpi_firmware_put(rpipwm->firmware);
> > +
> > +   return ret;
> 
> Can't you use the usual pattern?

Yes of course. Don't know why I went this way.

Regards,
Nicolas



signature.asc
Description: This is a digitally signed message part
___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v2 07/10] input: raspberrypi-ts: Release firmware handle when not needed

2020-10-22 Thread Nicolas Saenz Julienne
After passing the DMA buffer address through the firmware interface,
release the firmware handle, we won't need it anymore.

Signed-off-by: Nicolas Saenz Julienne 
---
 drivers/input/touchscreen/raspberrypi-ts.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/input/touchscreen/raspberrypi-ts.c 
b/drivers/input/touchscreen/raspberrypi-ts.c
index ef6aaed217cf..29c878a00018 100644
--- a/drivers/input/touchscreen/raspberrypi-ts.c
+++ b/drivers/input/touchscreen/raspberrypi-ts.c
@@ -165,6 +165,7 @@ static int rpi_ts_probe(struct platform_device *pdev)
dev_warn(dev, "Failed to set touchbuf, %d\n", error);
return error;
}
+   rpi_firmware_put(fw);
 
input = devm_input_allocate_device(dev);
if (!input) {
-- 
2.28.0

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v2 00/10] Raspberry Pi PoE HAT fan support

2020-10-22 Thread Nicolas Saenz Julienne
The aim of this series is to add support to the fan found on RPi's PoE
HAT. Some commentary on the design can be found below. But the imporant
part to the people CC'd here not involved with PWM is that, in order to
achieve this properly, we also have to fix the firmware interface the
driver uses to communicate with the PWM bus (and many other low level
functions). Specifically, we have to make sure the firmware interface
isn't unbound while consumers are still up. So, patch #1 introduces
reference counting in the firwmware interface driver and patches #2 to
#7 update all firmware users. Patches #8 to #10 introduce the new PWM
driver.

I sent everything as a single series as the final version of the PWM
drivers depends on the firwmare fixes, but I'll be happy to split this
into two separate series if you think it's better.

--- Original cover letter below ---

This series aims at adding support to RPi's official PoE HAT fan[1].

The HW setup is the following:

| Raspberry Pi   | PoE HAT|
 arm core -> Mailbox -> RPi co-processor -> I2C -> Atmel MCU -> PWM -> FAN

The arm cores have only access to the mailbox interface, as i2c0, even if
physically accessible, is to be used solely by the co-processor
(VideoCore 4/6).

This series implements a PWM bus, and has pwm-fan sitting on top of it as per
this discussion: https://lkml.org/lkml/2018/9/2/486. Although this design has a
series of shortcomings:

- It depends on a DT binding: it's not flexible if a new hat shows up with new
  functionality, we're not 100% sure we'll be able to expand it without
  breaking backwards compatibility. But without it we can't make use of DT
  thermal-zones, which IMO is overkill.

- We're using pwm-fan, writing a hwmon driver would, again, give us more
  flexibility, but it's not really needed at the moment.

I personally think that it's not worth the effort, it's unlikely we'll get
things right in advance. And ultimately, if the RPi people come up with
something new, we can always write a new driver/bindings from scratch (as in
not reusing previous code).

That said, I'm more than happy to change things if there is a consensus that
another design will do the trick.

[1] https://www.raspberrypi.org/blog/introducing-power-over-ethernet-poe-hat/

---

Changes since v1:
 - Address PWM driver changes
 - Fix binding, now with 2 cells
 - Add reference count to rpi_firmware_get()

Nicolas Saenz Julienne (10):
  firmware: raspberrypi: Introduce rpi_firmware_put()
  clk: bcm: rpi: Release firmware handle on unbind
  gpio: raspberrypi-exp: Release firmware handle on unbind
  reset: raspberrypi: Release firmware handle on unbind
  soc: bcm: raspberrypi-power: Release firmware handle on unbind
  staging: vchiq: Release firmware handle on unbind
  input: raspberrypi-ts: Release firmware handle when not needed
  dt-bindings: pwm: Add binding for RPi firmware PWM bus
  DO NOT MERGE: ARM: dts: Add RPi's official PoE hat support
  pwm: Add Raspberry Pi Firmware based PWM bus

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml |  20 ++
 arch/arm/boot/dts/bcm2711-rpi-4-b.dts |  54 +
 drivers/clk/bcm/clk-raspberrypi.c |   1 +
 drivers/firmware/raspberrypi.c|  30 ++-
 drivers/gpio/gpio-raspberrypi-exp.c   |  14 +-
 drivers/input/touchscreen/raspberrypi-ts.c|   1 +
 drivers/pwm/Kconfig   |   9 +
 drivers/pwm/Makefile  |   1 +
 drivers/pwm/pwm-raspberrypi.c | 221 ++
 drivers/reset/reset-raspberrypi.c |  13 +-
 drivers/soc/bcm/raspberrypi-power.c   |  15 ++
 .../interface/vchiq_arm/vchiq_arm.c   |   3 +
 .../pwm/raspberrypi,firmware-pwm.h|  13 ++
 include/soc/bcm2835/raspberrypi-firmware.h|   3 +
 14 files changed, 395 insertions(+), 3 deletions(-)
 create mode 100644 drivers/pwm/pwm-raspberrypi.c
 create mode 100644 include/dt-bindings/pwm/raspberrypi,firmware-pwm.h

-- 
2.28.0

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v2 02/10] clk: bcm: rpi: Release firmware handle on unbind

2020-10-22 Thread Nicolas Saenz Julienne
Upon unbinding the clock device make sure we release RPi's firmware
interface.

Signed-off-by: Nicolas Saenz Julienne 
---
 drivers/clk/bcm/clk-raspberrypi.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/drivers/clk/bcm/clk-raspberrypi.c 
b/drivers/clk/bcm/clk-raspberrypi.c
index f89b9cfc4309..845510ff7514 100644
--- a/drivers/clk/bcm/clk-raspberrypi.c
+++ b/drivers/clk/bcm/clk-raspberrypi.c
@@ -353,6 +353,7 @@ static int raspberrypi_clk_remove(struct platform_device 
*pdev)
struct raspberrypi_clk *rpi = platform_get_drvdata(pdev);
 
platform_device_unregister(rpi->cpufreq);
+   rpi_firmware_put(rpi->firmware);
 
return 0;
 }
-- 
2.28.0

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v2 03/10] gpio: raspberrypi-exp: Release firmware handle on unbind

2020-10-22 Thread Nicolas Saenz Julienne
Upon unbinding the device make sure we release RPi's firmware interface.

Signed-off-by: Nicolas Saenz Julienne 
---
 drivers/gpio/gpio-raspberrypi-exp.c | 14 +-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/drivers/gpio/gpio-raspberrypi-exp.c 
b/drivers/gpio/gpio-raspberrypi-exp.c
index bb100e0124e6..c008336e1131 100644
--- a/drivers/gpio/gpio-raspberrypi-exp.c
+++ b/drivers/gpio/gpio-raspberrypi-exp.c
@@ -231,8 +231,19 @@ static int rpi_exp_gpio_probe(struct platform_device *pdev)
rpi_gpio->gc.get = rpi_exp_gpio_get;
rpi_gpio->gc.set = rpi_exp_gpio_set;
rpi_gpio->gc.can_sleep = true;
+   platform_set_drvdata(pdev, rpi_gpio);
 
-   return devm_gpiochip_add_data(dev, _gpio->gc, rpi_gpio);
+   return gpiochip_add_data(_gpio->gc, rpi_gpio);
+}
+
+static int rpi_exp_gpio_remove(struct platform_device *pdev)
+{
+   struct rpi_exp_gpio *rpi_gpio = platform_get_drvdata(pdev);
+
+   gpiochip_remove(_gpio->gc);
+   rpi_firmware_put(rpi_gpio->fw);
+
+   return 0;
 }
 
 static const struct of_device_id rpi_exp_gpio_ids[] = {
@@ -247,6 +258,7 @@ static struct platform_driver rpi_exp_gpio_driver = {
.of_match_table = of_match_ptr(rpi_exp_gpio_ids),
},
.probe  = rpi_exp_gpio_probe,
+   .remove = rpi_exp_gpio_remove,
 };
 module_platform_driver(rpi_exp_gpio_driver);
 
-- 
2.28.0

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v2 06/10] staging: vchiq: Release firmware handle on unbind

2020-10-22 Thread Nicolas Saenz Julienne
Upon unbinding the device make sure we release RPi's firmware interface.

Signed-off-by: Nicolas Saenz Julienne 
---
 drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c 
b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
index 01125d9f991b..dfa4d144faa8 100644
--- a/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
+++ b/drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
@@ -2771,11 +2771,14 @@ static int vchiq_probe(struct platform_device *pdev)
 
 static int vchiq_remove(struct platform_device *pdev)
 {
+   struct vchiq_drvdata *drvdata = platform_get_drvdata(pdev);
+
platform_device_unregister(bcm2835_audio);
platform_device_unregister(bcm2835_camera);
vchiq_debugfs_deinit();
device_destroy(vchiq_class, vchiq_devid);
cdev_del(_cdev);
+   rpi_firmware_put(drvdata->fw);
 
return 0;
 }
-- 
2.28.0

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


[PATCH v2 04/10] reset: raspberrypi: Release firmware handle on unbind

2020-10-22 Thread Nicolas Saenz Julienne
Upon unbinding the device make sure we release RPi's firmware interface.

Signed-off-by: Nicolas Saenz Julienne 
---
 drivers/reset/reset-raspberrypi.c | 13 -
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/reset/reset-raspberrypi.c 
b/drivers/reset/reset-raspberrypi.c
index 02f59c06f69b..29311192e2c9 100644
--- a/drivers/reset/reset-raspberrypi.c
+++ b/drivers/reset/reset-raspberrypi.c
@@ -99,7 +99,17 @@ static int rpi_reset_probe(struct platform_device *pdev)
priv->rcdev.ops = _reset_ops;
priv->rcdev.of_node = dev->of_node;
 
-   return devm_reset_controller_register(dev, >rcdev);
+   return reset_controller_register(>rcdev);
+}
+
+static int rpi_reset_remove(struct platform_device *pdev)
+{
+   struct rpi_reset *priv = platform_get_drvdata(pdev);
+
+   reset_controller_unregister(>rcdev);
+   rpi_firmware_put(priv->fw);
+
+   return 0;
 }
 
 static const struct of_device_id rpi_reset_of_match[] = {
@@ -110,6 +120,7 @@ MODULE_DEVICE_TABLE(of, rpi_reset_of_match);
 
 static struct platform_driver rpi_reset_driver = {
.probe  = rpi_reset_probe,
+   .remove = rpi_reset_remove,
.driver = {
.name = "raspberrypi-reset",
.of_match_table = rpi_reset_of_match,
-- 
2.28.0

___
devel mailing list
de...@linuxdriverproject.org
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel


  1   2   3   4   5   >