All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v5 0/4] Add channel type support to pwm-cros-ec
@ 2022-04-20 14:15 Fabio Baltieri
  2022-04-20 14:15 ` [PATCH v5 1/4] dt-bindings: add mfd/cros_ec definitions Fabio Baltieri
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: Fabio Baltieri @ 2022-04-20 14:15 UTC (permalink / raw)
  To: Benson Leung, Guenter Roeck
  Cc: Thierry Reding, Uwe Kleine-König, Lee Jones, Rob Herring,
	chrome-platform, linux-pwm, devicetree, linux-kernel,
	Fabio Baltieri

Hi,

The ChromiumOS EC PWM host command protocol supports specifying the
requested PWM by type rather than channel. [1]

This series adds support for specifying PWM by type rather than channel
number in the pwm-cros-ec driver, which abstracts the node definitions
from the actual hardware configuration from the kernel perspective,
aligns the API with the one used by the bootloader, and allows removing
some dtsi overrides.

Tested on a sc7180-trogdor board, build tested on x86.

Changes from v4:
(https://patchwork.kernel.org/project/chrome-platform/list/?series=632212)
- fixed wrong indentation in the devietree file on patch 3
- added review and ack tags from the previous run

Changes from v3:
(https://patchwork.kernel.org/project/chrome-platform/list/?series=631131)
- actually reworded patch 2 commit description
- reworked patch 2 to use of_device_is_compatible() instead of compatible .data

Changes from v2:
(https://patchwork.kernel.org/project/chrome-platform/list/?series=627837)
- reworded patch 2 commit description
- reworked the driver and dt documentation to use a new compatible rather than
  boolean property
- dropped the comment about build test only, tested on actual hardware
  (trogdor), build test on x86 (with CONFIG_OF=n).

Changes from v1:
(https://patchwork.kernel.org/project/chrome-platform/list/?series=625182)
- fixed the dt include file license
- fixed the property name (s/_/-/)
- rebased on current linus tree (few dts files changed from a soc tree
  pull, so patch 4 needs a recent base to apply correctly)

[1] https://source.chromium.org/chromiumos/chromiumos/codesearch/+/main:src/platform/ec/common/pwm.c;l=24
[2] https://source.chromium.org/chromiumos/chromiumos/codesearch/+/main:src/platform/depthcharge/src/drivers/ec/cros/ec.c;l=1271-1273

Fabio Baltieri (4):
  dt-bindings: add mfd/cros_ec definitions
  pwm: pwm-cros-ec: add channel type support
  dt-bindings: update google,cros-ec-pwm documentation
  arm64: dts: address cros-ec-pwm channels by type

 .../bindings/pwm/google,cros-ec-pwm.yaml      |  9 +-
 .../mt8183-kukui-jacuzzi-fennel-sku1.dts      |  4 +-
 .../dts/mediatek/mt8183-kukui-jacuzzi.dtsi    |  4 +-
 .../arm64/boot/dts/mediatek/mt8183-kukui.dtsi |  1 +
 .../boot/dts/qcom/sc7180-trogdor-coachz.dtsi  |  4 -
 arch/arm64/boot/dts/qcom/sc7180-trogdor.dtsi  |  9 +-
 .../qcom/sc7280-herobrine-herobrine-r0.dts    |  7 +-
 .../arm64/boot/dts/qcom/sc7280-herobrine.dtsi |  7 +-
 .../arm64/boot/dts/qcom/sc7280-idp-ec-h1.dtsi |  4 +-
 arch/arm64/boot/dts/qcom/sdm845-cheza.dtsi    |  7 +-
 .../boot/dts/rockchip/rk3399-gru-bob.dts      |  4 -
 .../dts/rockchip/rk3399-gru-chromebook.dtsi   |  5 +-
 .../boot/dts/rockchip/rk3399-gru-kevin.dts    |  4 -
 arch/arm64/boot/dts/rockchip/rk3399-gru.dtsi  |  1 +
 drivers/pwm/pwm-cros-ec.c                     | 82 +++++++++++++++----
 include/dt-bindings/mfd/cros_ec.h             | 18 ++++
 16 files changed, 121 insertions(+), 49 deletions(-)
 create mode 100644 include/dt-bindings/mfd/cros_ec.h

-- 
2.36.0.rc0.470.gd361397f0d-goog


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v5 1/4] dt-bindings: add mfd/cros_ec definitions
  2022-04-20 14:15 [PATCH v5 0/4] Add channel type support to pwm-cros-ec Fabio Baltieri
@ 2022-04-20 14:15 ` Fabio Baltieri
  2022-04-20 14:15 ` [PATCH v5 2/4] pwm: pwm-cros-ec: add channel type support Fabio Baltieri
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 11+ messages in thread
From: Fabio Baltieri @ 2022-04-20 14:15 UTC (permalink / raw)
  To: Benson Leung, Guenter Roeck
  Cc: Thierry Reding, Uwe Kleine-König, Lee Jones, Rob Herring,
	chrome-platform, linux-pwm, devicetree, linux-kernel,
	Fabio Baltieri, Rob Herring

Add a dt-bindings include file for cros_ec devicetree definition, define
a pair of special purpose PWM channels in it.

Signed-off-by: Fabio Baltieri <fabiobaltieri@chromium.org>
Acked-by: Rob Herring <robh@kernel.org>
---
 include/dt-bindings/mfd/cros_ec.h | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
 create mode 100644 include/dt-bindings/mfd/cros_ec.h

diff --git a/include/dt-bindings/mfd/cros_ec.h b/include/dt-bindings/mfd/cros_ec.h
new file mode 100644
index 000000000000..3b29cd049578
--- /dev/null
+++ b/include/dt-bindings/mfd/cros_ec.h
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
+/*
+ * DTS binding definitions used for the Chromium OS Embedded Controller.
+ *
+ * Copyright (c) 2022 The Chromium OS Authors. All rights reserved.
+ */
+
+#ifndef _DT_BINDINGS_MFD_CROS_EC_H
+#define _DT_BINDINGS_MFD_CROS_EC_H
+
+/* Typed channel for keyboard backlight. */
+#define CROS_EC_PWM_DT_KB_LIGHT		0
+/* Typed channel for display backlight. */
+#define CROS_EC_PWM_DT_DISPLAY_LIGHT	1
+/* Number of typed channels. */
+#define CROS_EC_PWM_DT_COUNT		2
+
+#endif
-- 
2.36.0.rc0.470.gd361397f0d-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v5 2/4] pwm: pwm-cros-ec: add channel type support
  2022-04-20 14:15 [PATCH v5 0/4] Add channel type support to pwm-cros-ec Fabio Baltieri
  2022-04-20 14:15 ` [PATCH v5 1/4] dt-bindings: add mfd/cros_ec definitions Fabio Baltieri
@ 2022-04-20 14:15 ` Fabio Baltieri
  2022-04-20 17:55   ` Prashant Malani
  2022-04-20 14:15 ` [PATCH v5 3/4] dt-bindings: update google,cros-ec-pwm documentation Fabio Baltieri
  2022-04-20 14:15 ` [PATCH v5 4/4] arm64: dts: address cros-ec-pwm channels by type Fabio Baltieri
  3 siblings, 1 reply; 11+ messages in thread
From: Fabio Baltieri @ 2022-04-20 14:15 UTC (permalink / raw)
  To: Benson Leung, Guenter Roeck
  Cc: Thierry Reding, Uwe Kleine-König, Lee Jones, Rob Herring,
	chrome-platform, linux-pwm, devicetree, linux-kernel,
	Fabio Baltieri, Tzung-Bi Shih

Add support for EC_PWM_TYPE_DISPLAY_LIGHT and EC_PWM_TYPE_KB_LIGHT pwm
types to the PWM cros_ec_pwm driver. This allows specifying one of these
PWM channel by functionality, and let the EC firmware pick the correct
channel, thus abstracting the hardware implementation from the kernel
driver.

To use it, define the node with the "google,cros-ec-pwm-type"
compatible.

Signed-off-by: Fabio Baltieri <fabiobaltieri@chromium.org>
Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org>
---
 drivers/pwm/pwm-cros-ec.c | 82 ++++++++++++++++++++++++++++++++-------
 1 file changed, 67 insertions(+), 15 deletions(-)

diff --git a/drivers/pwm/pwm-cros-ec.c b/drivers/pwm/pwm-cros-ec.c
index 5e29d9c682c3..7f10f56c3eb6 100644
--- a/drivers/pwm/pwm-cros-ec.c
+++ b/drivers/pwm/pwm-cros-ec.c
@@ -12,17 +12,21 @@
 #include <linux/pwm.h>
 #include <linux/slab.h>
 
+#include <dt-bindings/mfd/cros_ec.h>
+
 /**
  * struct cros_ec_pwm_device - Driver data for EC PWM
  *
  * @dev: Device node
  * @ec: Pointer to EC device
  * @chip: PWM controller chip
+ * @use_pwm_type: Use PWM types instead of generic channels
  */
 struct cros_ec_pwm_device {
 	struct device *dev;
 	struct cros_ec_device *ec;
 	struct pwm_chip chip;
+	bool use_pwm_type;
 };
 
 /**
@@ -58,14 +62,31 @@ static void cros_ec_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
 	kfree(channel);
 }
 
-static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
+static int cros_ec_dt_type_to_pwm_type(u8 dt_index, u8 *pwm_type)
 {
+	switch (dt_index) {
+	case CROS_EC_PWM_DT_KB_LIGHT:
+		*pwm_type = EC_PWM_TYPE_KB_LIGHT;
+		return 0;
+	case CROS_EC_PWM_DT_DISPLAY_LIGHT:
+		*pwm_type = EC_PWM_TYPE_DISPLAY_LIGHT;
+		return 0;
+	default:
+		return -EINVAL;
+	}
+}
+
+static int cros_ec_pwm_set_duty(struct cros_ec_pwm_device *ec_pwm, u8 index,
+				u16 duty)
+{
+	struct cros_ec_device *ec = ec_pwm->ec;
 	struct {
 		struct cros_ec_command msg;
 		struct ec_params_pwm_set_duty params;
 	} __packed buf;
 	struct ec_params_pwm_set_duty *params = &buf.params;
 	struct cros_ec_command *msg = &buf.msg;
+	int ret;
 
 	memset(&buf, 0, sizeof(buf));
 
@@ -75,14 +96,25 @@ static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
 	msg->outsize = sizeof(*params);
 
 	params->duty = duty;
-	params->pwm_type = EC_PWM_TYPE_GENERIC;
-	params->index = index;
+
+	if (ec_pwm->use_pwm_type) {
+		ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type);
+		if (ret) {
+			dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
+			return ret;
+		}
+		params->index = 0;
+	} else {
+		params->pwm_type = EC_PWM_TYPE_GENERIC;
+		params->index = index;
+	}
 
 	return cros_ec_cmd_xfer_status(ec, msg);
 }
 
-static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
+static int cros_ec_pwm_get_duty(struct cros_ec_pwm_device *ec_pwm, u8 index)
 {
+	struct cros_ec_device *ec = ec_pwm->ec;
 	struct {
 		struct cros_ec_command msg;
 		union {
@@ -102,8 +134,17 @@ static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
 	msg->insize = sizeof(*resp);
 	msg->outsize = sizeof(*params);
 
-	params->pwm_type = EC_PWM_TYPE_GENERIC;
-	params->index = index;
+	if (ec_pwm->use_pwm_type) {
+		ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type);
+		if (ret) {
+			dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
+			return ret;
+		}
+		params->index = 0;
+	} else {
+		params->pwm_type = EC_PWM_TYPE_GENERIC;
+		params->index = index;
+	}
 
 	ret = cros_ec_cmd_xfer_status(ec, msg);
 	if (ret < 0)
@@ -133,7 +174,7 @@ static int cros_ec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
 	 */
 	duty_cycle = state->enabled ? state->duty_cycle : 0;
 
-	ret = cros_ec_pwm_set_duty(ec_pwm->ec, pwm->hwpwm, duty_cycle);
+	ret = cros_ec_pwm_set_duty(ec_pwm, pwm->hwpwm, duty_cycle);
 	if (ret < 0)
 		return ret;
 
@@ -149,7 +190,7 @@ static void cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
 	struct cros_ec_pwm *channel = pwm_get_chip_data(pwm);
 	int ret;
 
-	ret = cros_ec_pwm_get_duty(ec_pwm->ec, pwm->hwpwm);
+	ret = cros_ec_pwm_get_duty(ec_pwm, pwm->hwpwm);
 	if (ret < 0) {
 		dev_err(chip->dev, "error getting initial duty: %d\n", ret);
 		return;
@@ -204,13 +245,13 @@ static const struct pwm_ops cros_ec_pwm_ops = {
  * of PWMs it supports directly, so we have to read the pwm duty cycle for
  * subsequent channels until we get an error.
  */
-static int cros_ec_num_pwms(struct cros_ec_device *ec)
+static int cros_ec_num_pwms(struct cros_ec_pwm_device *ec_pwm)
 {
 	int i, ret;
 
 	/* The index field is only 8 bits */
 	for (i = 0; i <= U8_MAX; i++) {
-		ret = cros_ec_pwm_get_duty(ec, i);
+		ret = cros_ec_pwm_get_duty(ec_pwm, i);
 		/*
 		 * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM
 		 * responses; everything else is treated as an error.
@@ -236,6 +277,7 @@ static int cros_ec_pwm_probe(struct platform_device *pdev)
 {
 	struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
 	struct device *dev = &pdev->dev;
+	struct device_node *np = pdev->dev.of_node;
 	struct cros_ec_pwm_device *ec_pwm;
 	struct pwm_chip *chip;
 	int ret;
@@ -251,17 +293,26 @@ static int cros_ec_pwm_probe(struct platform_device *pdev)
 	chip = &ec_pwm->chip;
 	ec_pwm->ec = ec;
 
+	if (of_device_is_compatible(np, "google,cros-ec-pwm-type"))
+		ec_pwm->use_pwm_type = true;
+
 	/* PWM chip */
 	chip->dev = dev;
 	chip->ops = &cros_ec_pwm_ops;
 	chip->of_xlate = cros_ec_pwm_xlate;
 	chip->of_pwm_n_cells = 1;
-	ret = cros_ec_num_pwms(ec);
-	if (ret < 0) {
-		dev_err(dev, "Couldn't find PWMs: %d\n", ret);
-		return ret;
+
+	if (ec_pwm->use_pwm_type) {
+		chip->npwm = CROS_EC_PWM_DT_COUNT;
+	} else {
+		ret = cros_ec_num_pwms(ec_pwm);
+		if (ret < 0) {
+			dev_err(dev, "Couldn't find PWMs: %d\n", ret);
+			return ret;
+		}
+		chip->npwm = ret;
 	}
-	chip->npwm = ret;
+
 	dev_dbg(dev, "Probed %u PWMs\n", chip->npwm);
 
 	ret = pwmchip_add(chip);
@@ -288,6 +339,7 @@ static int cros_ec_pwm_remove(struct platform_device *dev)
 #ifdef CONFIG_OF
 static const struct of_device_id cros_ec_pwm_of_match[] = {
 	{ .compatible = "google,cros-ec-pwm" },
+	{ .compatible = "google,cros-ec-pwm-type" },
 	{},
 };
 MODULE_DEVICE_TABLE(of, cros_ec_pwm_of_match);
-- 
2.36.0.rc0.470.gd361397f0d-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v5 3/4] dt-bindings: update google,cros-ec-pwm documentation
  2022-04-20 14:15 [PATCH v5 0/4] Add channel type support to pwm-cros-ec Fabio Baltieri
  2022-04-20 14:15 ` [PATCH v5 1/4] dt-bindings: add mfd/cros_ec definitions Fabio Baltieri
  2022-04-20 14:15 ` [PATCH v5 2/4] pwm: pwm-cros-ec: add channel type support Fabio Baltieri
@ 2022-04-20 14:15 ` Fabio Baltieri
  2022-04-25 21:58   ` Rob Herring
  2022-04-20 14:15 ` [PATCH v5 4/4] arm64: dts: address cros-ec-pwm channels by type Fabio Baltieri
  3 siblings, 1 reply; 11+ messages in thread
From: Fabio Baltieri @ 2022-04-20 14:15 UTC (permalink / raw)
  To: Benson Leung, Guenter Roeck
  Cc: Thierry Reding, Uwe Kleine-König, Lee Jones, Rob Herring,
	chrome-platform, linux-pwm, devicetree, linux-kernel,
	Fabio Baltieri

Update google,cros-ec-pwm node documentation to mention the
google,cros-ec-pwm-type compatible.

Signed-off-by: Fabio Baltieri <fabiobaltieri@chromium.org>
---
 .../devicetree/bindings/pwm/google,cros-ec-pwm.yaml      | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/Documentation/devicetree/bindings/pwm/google,cros-ec-pwm.yaml b/Documentation/devicetree/bindings/pwm/google,cros-ec-pwm.yaml
index 7ab6912a845f..c8577bdf6c94 100644
--- a/Documentation/devicetree/bindings/pwm/google,cros-ec-pwm.yaml
+++ b/Documentation/devicetree/bindings/pwm/google,cros-ec-pwm.yaml
@@ -21,7 +21,14 @@ allOf:
 
 properties:
   compatible:
-    const: google,cros-ec-pwm
+    oneOf:
+      - description: PWM controlled using EC_PWM_TYPE_GENERIC channels.
+        items:
+          - const: google,cros-ec-pwm
+      - description: PWM controlled using CROS_EC_PWM_DT_<...> types.
+        items:
+          - const: google,cros-ec-pwm-type
+
   "#pwm-cells":
     description: The cell specifies the PWM index.
     const: 1
-- 
2.36.0.rc0.470.gd361397f0d-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v5 4/4] arm64: dts: address cros-ec-pwm channels by type
  2022-04-20 14:15 [PATCH v5 0/4] Add channel type support to pwm-cros-ec Fabio Baltieri
                   ` (2 preceding siblings ...)
  2022-04-20 14:15 ` [PATCH v5 3/4] dt-bindings: update google,cros-ec-pwm documentation Fabio Baltieri
@ 2022-04-20 14:15 ` Fabio Baltieri
  3 siblings, 0 replies; 11+ messages in thread
From: Fabio Baltieri @ 2022-04-20 14:15 UTC (permalink / raw)
  To: Benson Leung, Guenter Roeck
  Cc: Thierry Reding, Uwe Kleine-König, Lee Jones, Rob Herring,
	chrome-platform, linux-pwm, devicetree, linux-kernel,
	Fabio Baltieri

Update various cros-ec-pwm board definitions to address the keyboard and
screen backlight PWM channels by type rather than channel number. This
makes the instance independent by the actual hardware configuration,
relying on the EC firmware to pick the right channel, and allows
dropping few dtsi overrides as a consequence.

Changed the node label used to cros_ec_pwm_type to avoid ambiguity about
the pwm cell meaning.

Signed-off-by: Fabio Baltieri <fabiobaltieri@chromium.org>
---
 .../dts/mediatek/mt8183-kukui-jacuzzi-fennel-sku1.dts    | 4 ++--
 arch/arm64/boot/dts/mediatek/mt8183-kukui-jacuzzi.dtsi   | 4 ++--
 arch/arm64/boot/dts/mediatek/mt8183-kukui.dtsi           | 1 +
 arch/arm64/boot/dts/qcom/sc7180-trogdor-coachz.dtsi      | 4 ----
 arch/arm64/boot/dts/qcom/sc7180-trogdor.dtsi             | 9 +++++----
 .../boot/dts/qcom/sc7280-herobrine-herobrine-r0.dts      | 7 ++++---
 arch/arm64/boot/dts/qcom/sc7280-herobrine.dtsi           | 7 ++++---
 arch/arm64/boot/dts/qcom/sc7280-idp-ec-h1.dtsi           | 4 ++--
 arch/arm64/boot/dts/qcom/sdm845-cheza.dtsi               | 7 ++++---
 arch/arm64/boot/dts/rockchip/rk3399-gru-bob.dts          | 4 ----
 arch/arm64/boot/dts/rockchip/rk3399-gru-chromebook.dtsi  | 5 +++--
 arch/arm64/boot/dts/rockchip/rk3399-gru-kevin.dts        | 4 ----
 arch/arm64/boot/dts/rockchip/rk3399-gru.dtsi             | 1 +
 13 files changed, 28 insertions(+), 33 deletions(-)

diff --git a/arch/arm64/boot/dts/mediatek/mt8183-kukui-jacuzzi-fennel-sku1.dts b/arch/arm64/boot/dts/mediatek/mt8183-kukui-jacuzzi-fennel-sku1.dts
index dec11a4eb59e..e2554a313deb 100644
--- a/arch/arm64/boot/dts/mediatek/mt8183-kukui-jacuzzi-fennel-sku1.dts
+++ b/arch/arm64/boot/dts/mediatek/mt8183-kukui-jacuzzi-fennel-sku1.dts
@@ -15,13 +15,13 @@ pwmleds {
 		compatible = "pwm-leds";
 		keyboard_backlight: keyboard-backlight {
 			label = "cros_ec::kbd_backlight";
-			pwms = <&cros_ec_pwm 0>;
+			pwms = <&cros_ec_pwm_type CROS_EC_PWM_DT_KB_LIGHT>;
 			max-brightness = <1023>;
 		};
 	};
 };
 
-&cros_ec_pwm {
+&cros_ec_pwm_type {
 	status = "okay";
 };
 
diff --git a/arch/arm64/boot/dts/mediatek/mt8183-kukui-jacuzzi.dtsi b/arch/arm64/boot/dts/mediatek/mt8183-kukui-jacuzzi.dtsi
index 8f7bf33f607d..8474bd3af6eb 100644
--- a/arch/arm64/boot/dts/mediatek/mt8183-kukui-jacuzzi.dtsi
+++ b/arch/arm64/boot/dts/mediatek/mt8183-kukui-jacuzzi.dtsi
@@ -92,8 +92,8 @@ volume_up {
 };
 
 &cros_ec {
-	cros_ec_pwm: ec-pwm {
-		compatible = "google,cros-ec-pwm";
+	cros_ec_pwm_type: ec-pwm {
+		compatible = "google,cros-ec-pwm-type";
 		#pwm-cells = <1>;
 		status = "disabled";
 	};
diff --git a/arch/arm64/boot/dts/mediatek/mt8183-kukui.dtsi b/arch/arm64/boot/dts/mediatek/mt8183-kukui.dtsi
index 0f9480f91261..ff54687ab8bf 100644
--- a/arch/arm64/boot/dts/mediatek/mt8183-kukui.dtsi
+++ b/arch/arm64/boot/dts/mediatek/mt8183-kukui.dtsi
@@ -7,6 +7,7 @@
 
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mfd/cros_ec.h>
 #include "mt8183.dtsi"
 #include "mt6358.dtsi"
 
diff --git a/arch/arm64/boot/dts/qcom/sc7180-trogdor-coachz.dtsi b/arch/arm64/boot/dts/qcom/sc7180-trogdor-coachz.dtsi
index c81805ef2250..aea7c66d95e0 100644
--- a/arch/arm64/boot/dts/qcom/sc7180-trogdor-coachz.dtsi
+++ b/arch/arm64/boot/dts/qcom/sc7180-trogdor-coachz.dtsi
@@ -77,10 +77,6 @@ &ap_spi_fp {
 	status = "okay";
 };
 
-&backlight {
-	pwms = <&cros_ec_pwm 0>;
-};
-
 &camcc {
 	status = "okay";
 };
diff --git a/arch/arm64/boot/dts/qcom/sc7180-trogdor.dtsi b/arch/arm64/boot/dts/qcom/sc7180-trogdor.dtsi
index 732e1181af48..6552e0025f84 100644
--- a/arch/arm64/boot/dts/qcom/sc7180-trogdor.dtsi
+++ b/arch/arm64/boot/dts/qcom/sc7180-trogdor.dtsi
@@ -8,6 +8,7 @@
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/gpio-keys.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mfd/cros_ec.h>
 #include <dt-bindings/regulator/qcom,rpmh-regulator.h>
 #include <dt-bindings/sound/sc7180-lpass.h>
 
@@ -316,7 +317,7 @@ backlight: backlight {
 		num-interpolated-steps = <64>;
 		default-brightness-level = <951>;
 
-		pwms = <&cros_ec_pwm 1>;
+		pwms = <&cros_ec_pwm_type CROS_EC_PWM_DT_DISPLAY_LIGHT>;
 		enable-gpios = <&tlmm 12 GPIO_ACTIVE_HIGH>;
 		power-supply = <&ppvar_sys>;
 		pinctrl-names = "default";
@@ -354,7 +355,7 @@ pwmleds {
 		keyboard_backlight: keyboard-backlight {
 			status = "disabled";
 			label = "cros_ec::kbd_backlight";
-			pwms = <&cros_ec_pwm 0>;
+			pwms = <&cros_ec_pwm_type CROS_EC_PWM_DT_KB_LIGHT>;
 			max-brightness = <1023>;
 		};
 	};
@@ -637,8 +638,8 @@ cros_ec: ec@0 {
 		pinctrl-0 = <&ap_ec_int_l>;
 		spi-max-frequency = <3000000>;
 
-		cros_ec_pwm: pwm {
-			compatible = "google,cros-ec-pwm";
+		cros_ec_pwm_type: pwm {
+			compatible = "google,cros-ec-pwm-type";
 			#pwm-cells = <1>;
 		};
 
diff --git a/arch/arm64/boot/dts/qcom/sc7280-herobrine-herobrine-r0.dts b/arch/arm64/boot/dts/qcom/sc7280-herobrine-herobrine-r0.dts
index 1779d96c30f6..628ef990433b 100644
--- a/arch/arm64/boot/dts/qcom/sc7280-herobrine-herobrine-r0.dts
+++ b/arch/arm64/boot/dts/qcom/sc7280-herobrine-herobrine-r0.dts
@@ -11,6 +11,7 @@
 #include <dt-bindings/iio/qcom,spmi-adc7-pmr735a.h>
 #include <dt-bindings/input/gpio-keys.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mfd/cros_ec.h>
 #include <dt-bindings/pinctrl/qcom,pmic-gpio.h>
 #include <dt-bindings/regulator/qcom,rpmh-regulator.h>
 
@@ -336,7 +337,7 @@ pwmleds {
 		keyboard_backlight: keyboard-backlight {
 			status = "disabled";
 			label = "cros_ec::kbd_backlight";
-			pwms = <&cros_ec_pwm 0>;
+			pwms = <&cros_ec_pwm_type CROS_EC_PWM_DT_KB_LIGHT>;
 			max-brightness = <1023>;
 		};
 	};
@@ -705,8 +706,8 @@ cros_ec: ec@0 {
 		pinctrl-0 = <&ap_ec_int_l>;
 		spi-max-frequency = <3000000>;
 
-		cros_ec_pwm: pwm {
-			compatible = "google,cros-ec-pwm";
+		cros_ec_pwm_type: pwm {
+			compatible = "google,cros-ec-pwm-type";
 			#pwm-cells = <1>;
 		};
 
diff --git a/arch/arm64/boot/dts/qcom/sc7280-herobrine.dtsi b/arch/arm64/boot/dts/qcom/sc7280-herobrine.dtsi
index dc17f2079695..eb4b0e17adec 100644
--- a/arch/arm64/boot/dts/qcom/sc7280-herobrine.dtsi
+++ b/arch/arm64/boot/dts/qcom/sc7280-herobrine.dtsi
@@ -15,6 +15,7 @@
 
 #include <dt-bindings/input/gpio-keys.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mfd/cros_ec.h>
 
 #include "sc7280-qcard.dtsi"
 #include "sc7280-chrome-common.dtsi"
@@ -288,7 +289,7 @@ pwmleds {
 		keyboard_backlight: keyboard-backlight {
 			status = "disabled";
 			label = "cros_ec::kbd_backlight";
-			pwms = <&cros_ec_pwm 0>;
+			pwms = <&cros_ec_pwm_type CROS_EC_PWM_DT_KB_LIGHT>;
 			max-brightness = <1023>;
 		};
 	};
@@ -421,8 +422,8 @@ cros_ec: ec@0 {
 		pinctrl-0 = <&ap_ec_int_l>;
 		spi-max-frequency = <3000000>;
 
-		cros_ec_pwm: pwm {
-			compatible = "google,cros-ec-pwm";
+		cros_ec_pwm_type: pwm {
+			compatible = "google,cros-ec-pwm-type";
 			#pwm-cells = <1>;
 		};
 
diff --git a/arch/arm64/boot/dts/qcom/sc7280-idp-ec-h1.dtsi b/arch/arm64/boot/dts/qcom/sc7280-idp-ec-h1.dtsi
index a7c346aa3b02..a797f09e1328 100644
--- a/arch/arm64/boot/dts/qcom/sc7280-idp-ec-h1.dtsi
+++ b/arch/arm64/boot/dts/qcom/sc7280-idp-ec-h1.dtsi
@@ -20,8 +20,8 @@ cros_ec: ec@0 {
 		pinctrl-0 = <&ap_ec_int_l>;
 		spi-max-frequency = <3000000>;
 
-		cros_ec_pwm: pwm {
-			compatible = "google,cros-ec-pwm";
+		cros_ec_pwm_type: pwm {
+			compatible = "google,cros-ec-pwm-type";
 			#pwm-cells = <1>;
 		};
 
diff --git a/arch/arm64/boot/dts/qcom/sdm845-cheza.dtsi b/arch/arm64/boot/dts/qcom/sdm845-cheza.dtsi
index e7e4cc5936aa..a57951a50cd6 100644
--- a/arch/arm64/boot/dts/qcom/sdm845-cheza.dtsi
+++ b/arch/arm64/boot/dts/qcom/sdm845-cheza.dtsi
@@ -6,6 +6,7 @@
  */
 
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mfd/cros_ec.h>
 #include <dt-bindings/regulator/qcom,rpmh-regulator.h>
 #include "sdm845.dtsi"
 
@@ -27,7 +28,7 @@ chosen {
 
 	backlight: backlight {
 		compatible = "pwm-backlight";
-		pwms = <&cros_ec_pwm 0>;
+		pwms = <&cros_ec_pwm_type CROS_EC_PWM_DT_DISPLAY_LIGHT>;
 		enable-gpios = <&tlmm 37 GPIO_ACTIVE_HIGH>;
 		power-supply = <&ppvar_sys>;
 		pinctrl-names = "default";
@@ -708,8 +709,8 @@ cros_ec: ec@0 {
 		pinctrl-0 = <&ec_ap_int_l>;
 		spi-max-frequency = <3000000>;
 
-		cros_ec_pwm: pwm {
-			compatible = "google,cros-ec-pwm";
+		cros_ec_pwm_type: pwm {
+			compatible = "google,cros-ec-pwm-type";
 			#pwm-cells = <1>;
 		};
 
diff --git a/arch/arm64/boot/dts/rockchip/rk3399-gru-bob.dts b/arch/arm64/boot/dts/rockchip/rk3399-gru-bob.dts
index 31ebb4e5fd33..5a076c2564f6 100644
--- a/arch/arm64/boot/dts/rockchip/rk3399-gru-bob.dts
+++ b/arch/arm64/boot/dts/rockchip/rk3399-gru-bob.dts
@@ -55,10 +55,6 @@ trackpad: trackpad@15 {
 	};
 };
 
-&backlight {
-	pwms = <&cros_ec_pwm 0>;
-};
-
 &cpu_alert0 {
 	temperature = <65000>;
 };
diff --git a/arch/arm64/boot/dts/rockchip/rk3399-gru-chromebook.dtsi b/arch/arm64/boot/dts/rockchip/rk3399-gru-chromebook.dtsi
index 3355fb90fa54..28eda361dfe1 100644
--- a/arch/arm64/boot/dts/rockchip/rk3399-gru-chromebook.dtsi
+++ b/arch/arm64/boot/dts/rockchip/rk3399-gru-chromebook.dtsi
@@ -198,6 +198,7 @@ backlight: backlight {
 		power-supply = <&pp3300_disp>;
 		pinctrl-names = "default";
 		pinctrl-0 = <&bl_en>;
+		pwms = <&cros_ec_pwm_type CROS_EC_PWM_DT_DISPLAY_LIGHT>;
 		pwm-delay-us = <10000>;
 	};
 
@@ -462,8 +463,8 @@ ap_i2c_tp: &i2c5 {
 };
 
 &cros_ec {
-	cros_ec_pwm: pwm {
-		compatible = "google,cros-ec-pwm";
+	cros_ec_pwm_type: pwm {
+		compatible = "google,cros-ec-pwm-type";
 		#pwm-cells = <1>;
 	};
 
diff --git a/arch/arm64/boot/dts/rockchip/rk3399-gru-kevin.dts b/arch/arm64/boot/dts/rockchip/rk3399-gru-kevin.dts
index 6863689df06f..e959a33af34b 100644
--- a/arch/arm64/boot/dts/rockchip/rk3399-gru-kevin.dts
+++ b/arch/arm64/boot/dts/rockchip/rk3399-gru-kevin.dts
@@ -84,10 +84,6 @@ thermistor_ppvar_litcpu: thermistor-ppvar-litcpu {
 	};
 };
 
-&backlight {
-	pwms = <&cros_ec_pwm 1>;
-};
-
 &gpio_keys {
 	pinctrl-names = "default";
 	pinctrl-0 = <&bt_host_wake_l>, <&cpu1_pen_eject>;
diff --git a/arch/arm64/boot/dts/rockchip/rk3399-gru.dtsi b/arch/arm64/boot/dts/rockchip/rk3399-gru.dtsi
index 162f08bca0d4..181159e9982d 100644
--- a/arch/arm64/boot/dts/rockchip/rk3399-gru.dtsi
+++ b/arch/arm64/boot/dts/rockchip/rk3399-gru.dtsi
@@ -6,6 +6,7 @@
  */
 
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mfd/cros_ec.h>
 #include "rk3399.dtsi"
 #include "rk3399-op1-opp.dtsi"
 
-- 
2.36.0.rc0.470.gd361397f0d-goog


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 2/4] pwm: pwm-cros-ec: add channel type support
  2022-04-20 14:15 ` [PATCH v5 2/4] pwm: pwm-cros-ec: add channel type support Fabio Baltieri
@ 2022-04-20 17:55   ` Prashant Malani
  2022-04-21  8:34     ` Fabio Baltieri
  0 siblings, 1 reply; 11+ messages in thread
From: Prashant Malani @ 2022-04-20 17:55 UTC (permalink / raw)
  To: Fabio Baltieri
  Cc: Benson Leung, Guenter Roeck, Thierry Reding,
	Uwe Kleine-König, Lee Jones, Rob Herring, chrome-platform,
	linux-pwm, devicetree, linux-kernel, Tzung-Bi Shih

On Apr 20 14:15, Fabio Baltieri wrote:
> Add support for EC_PWM_TYPE_DISPLAY_LIGHT and EC_PWM_TYPE_KB_LIGHT pwm
> types to the PWM cros_ec_pwm driver. This allows specifying one of these
> PWM channel by functionality, and let the EC firmware pick the correct
> channel, thus abstracting the hardware implementation from the kernel
> driver.
> 
> To use it, define the node with the "google,cros-ec-pwm-type"
> compatible.
> 
> Signed-off-by: Fabio Baltieri <fabiobaltieri@chromium.org>
> Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org>
> ---
>  drivers/pwm/pwm-cros-ec.c | 82 ++++++++++++++++++++++++++++++++-------
>  1 file changed, 67 insertions(+), 15 deletions(-)
> 
> diff --git a/drivers/pwm/pwm-cros-ec.c b/drivers/pwm/pwm-cros-ec.c
> index 5e29d9c682c3..7f10f56c3eb6 100644
> --- a/drivers/pwm/pwm-cros-ec.c
> +++ b/drivers/pwm/pwm-cros-ec.c
> @@ -12,17 +12,21 @@
>  #include <linux/pwm.h>
>  #include <linux/slab.h>
>  
> +#include <dt-bindings/mfd/cros_ec.h>
> +
>  /**
>   * struct cros_ec_pwm_device - Driver data for EC PWM
>   *
>   * @dev: Device node
>   * @ec: Pointer to EC device
>   * @chip: PWM controller chip
> + * @use_pwm_type: Use PWM types instead of generic channels
>   */
>  struct cros_ec_pwm_device {
>  	struct device *dev;
>  	struct cros_ec_device *ec;
>  	struct pwm_chip chip;
> +	bool use_pwm_type;
>  };
>  
>  /**
> @@ -58,14 +62,31 @@ static void cros_ec_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
>  	kfree(channel);
>  }
>  
> -static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
> +static int cros_ec_dt_type_to_pwm_type(u8 dt_index, u8 *pwm_type)
>  {
> +	switch (dt_index) {
> +	case CROS_EC_PWM_DT_KB_LIGHT:
> +		*pwm_type = EC_PWM_TYPE_KB_LIGHT;
> +		return 0;
> +	case CROS_EC_PWM_DT_DISPLAY_LIGHT:
> +		*pwm_type = EC_PWM_TYPE_DISPLAY_LIGHT;
> +		return 0;
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +static int cros_ec_pwm_set_duty(struct cros_ec_pwm_device *ec_pwm, u8 index,
> +				u16 duty)
> +{
> +	struct cros_ec_device *ec = ec_pwm->ec;
>  	struct {
>  		struct cros_ec_command msg;
>  		struct ec_params_pwm_set_duty params;
>  	} __packed buf;
>  	struct ec_params_pwm_set_duty *params = &buf.params;
>  	struct cros_ec_command *msg = &buf.msg;
> +	int ret;
>  
>  	memset(&buf, 0, sizeof(buf));
>  
> @@ -75,14 +96,25 @@ static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
>  	msg->outsize = sizeof(*params);
>  
>  	params->duty = duty;
> -	params->pwm_type = EC_PWM_TYPE_GENERIC;
> -	params->index = index;
> +
> +	if (ec_pwm->use_pwm_type) {
> +		ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type);
> +		if (ret) {
> +			dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
> +			return ret;
> +		}
> +		params->index = 0;
> +	} else {
> +		params->pwm_type = EC_PWM_TYPE_GENERIC;
> +		params->index = index;
> +	}
>  
>  	return cros_ec_cmd_xfer_status(ec, msg);
>  }
>  
> -static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
> +static int cros_ec_pwm_get_duty(struct cros_ec_pwm_device *ec_pwm, u8 index)
>  {
> +	struct cros_ec_device *ec = ec_pwm->ec;
>  	struct {
>  		struct cros_ec_command msg;
>  		union {
> @@ -102,8 +134,17 @@ static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
>  	msg->insize = sizeof(*resp);
>  	msg->outsize = sizeof(*params);
>  
> -	params->pwm_type = EC_PWM_TYPE_GENERIC;
> -	params->index = index;
> +	if (ec_pwm->use_pwm_type) {
> +		ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type);
> +		if (ret) {
> +			dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
> +			return ret;
> +		}
> +		params->index = 0;
> +	} else {
> +		params->pwm_type = EC_PWM_TYPE_GENERIC;
> +		params->index = index;
> +	}
>  
>  	ret = cros_ec_cmd_xfer_status(ec, msg);
>  	if (ret < 0)
> @@ -133,7 +174,7 @@ static int cros_ec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
>  	 */
>  	duty_cycle = state->enabled ? state->duty_cycle : 0;
>  
> -	ret = cros_ec_pwm_set_duty(ec_pwm->ec, pwm->hwpwm, duty_cycle);
> +	ret = cros_ec_pwm_set_duty(ec_pwm, pwm->hwpwm, duty_cycle);
>  	if (ret < 0)
>  		return ret;
>  
> @@ -149,7 +190,7 @@ static void cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
>  	struct cros_ec_pwm *channel = pwm_get_chip_data(pwm);
>  	int ret;
>  
> -	ret = cros_ec_pwm_get_duty(ec_pwm->ec, pwm->hwpwm);
> +	ret = cros_ec_pwm_get_duty(ec_pwm, pwm->hwpwm);
>  	if (ret < 0) {
>  		dev_err(chip->dev, "error getting initial duty: %d\n", ret);
>  		return;
> @@ -204,13 +245,13 @@ static const struct pwm_ops cros_ec_pwm_ops = {
>   * of PWMs it supports directly, so we have to read the pwm duty cycle for
>   * subsequent channels until we get an error.
>   */
> -static int cros_ec_num_pwms(struct cros_ec_device *ec)
> +static int cros_ec_num_pwms(struct cros_ec_pwm_device *ec_pwm)
>  {
>  	int i, ret;
>  
>  	/* The index field is only 8 bits */
>  	for (i = 0; i <= U8_MAX; i++) {
> -		ret = cros_ec_pwm_get_duty(ec, i);
> +		ret = cros_ec_pwm_get_duty(ec_pwm, i);
>  		/*
>  		 * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM
>  		 * responses; everything else is treated as an error.
> @@ -236,6 +277,7 @@ static int cros_ec_pwm_probe(struct platform_device *pdev)
>  {
>  	struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
>  	struct device *dev = &pdev->dev;
> +	struct device_node *np = pdev->dev.of_node;
>  	struct cros_ec_pwm_device *ec_pwm;
>  	struct pwm_chip *chip;
>  	int ret;
> @@ -251,17 +293,26 @@ static int cros_ec_pwm_probe(struct platform_device *pdev)
>  	chip = &ec_pwm->chip;
>  	ec_pwm->ec = ec;
>  
> +	if (of_device_is_compatible(np, "google,cros-ec-pwm-type"))
> +		ec_pwm->use_pwm_type = true;

Isn't it possible to just use an optional boolean property
(for example: "use-pwm-type") instead of defining a new compatible
string?

> +
>  	/* PWM chip */
>  	chip->dev = dev;
>  	chip->ops = &cros_ec_pwm_ops;
>  	chip->of_xlate = cros_ec_pwm_xlate;
>  	chip->of_pwm_n_cells = 1;
> -	ret = cros_ec_num_pwms(ec);
> -	if (ret < 0) {
> -		dev_err(dev, "Couldn't find PWMs: %d\n", ret);
> -		return ret;
> +
> +	if (ec_pwm->use_pwm_type) {
> +		chip->npwm = CROS_EC_PWM_DT_COUNT;
> +	} else {
> +		ret = cros_ec_num_pwms(ec_pwm);
> +		if (ret < 0) {
> +			dev_err(dev, "Couldn't find PWMs: %d\n", ret);
> +			return ret;
> +		}
> +		chip->npwm = ret;
>  	}
> -	chip->npwm = ret;
> +
>  	dev_dbg(dev, "Probed %u PWMs\n", chip->npwm);
>  
>  	ret = pwmchip_add(chip);
> @@ -288,6 +339,7 @@ static int cros_ec_pwm_remove(struct platform_device *dev)
>  #ifdef CONFIG_OF
>  static const struct of_device_id cros_ec_pwm_of_match[] = {
>  	{ .compatible = "google,cros-ec-pwm" },
> +	{ .compatible = "google,cros-ec-pwm-type" },
>  	{},
>  };
>  MODULE_DEVICE_TABLE(of, cros_ec_pwm_of_match);
> -- 
> 2.36.0.rc0.470.gd361397f0d-goog
> 
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 2/4] pwm: pwm-cros-ec: add channel type support
  2022-04-20 17:55   ` Prashant Malani
@ 2022-04-21  8:34     ` Fabio Baltieri
  2022-04-21  8:52       ` Prashant Malani
  0 siblings, 1 reply; 11+ messages in thread
From: Fabio Baltieri @ 2022-04-21  8:34 UTC (permalink / raw)
  To: Prashant Malani
  Cc: Benson Leung, Guenter Roeck, Thierry Reding,
	Uwe Kleine-König, Lee Jones, Rob Herring, chrome-platform,
	linux-pwm, devicetree, linux-kernel, Tzung-Bi Shih

On Wed, Apr 20, 2022 at 05:55:35PM +0000, Prashant Malani wrote:
> On Apr 20 14:15, Fabio Baltieri wrote:
> > Add support for EC_PWM_TYPE_DISPLAY_LIGHT and EC_PWM_TYPE_KB_LIGHT pwm
> > types to the PWM cros_ec_pwm driver. This allows specifying one of these
> > PWM channel by functionality, and let the EC firmware pick the correct
> > channel, thus abstracting the hardware implementation from the kernel
> > driver.
> > 
> > To use it, define the node with the "google,cros-ec-pwm-type"
> > compatible.
> > 
> > Signed-off-by: Fabio Baltieri <fabiobaltieri@chromium.org>
> > Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org>
> > ---
> >  drivers/pwm/pwm-cros-ec.c | 82 ++++++++++++++++++++++++++++++++-------
> >  1 file changed, 67 insertions(+), 15 deletions(-)
> > 
> > diff --git a/drivers/pwm/pwm-cros-ec.c b/drivers/pwm/pwm-cros-ec.c
> > index 5e29d9c682c3..7f10f56c3eb6 100644
> > --- a/drivers/pwm/pwm-cros-ec.c
> > +++ b/drivers/pwm/pwm-cros-ec.c
> > @@ -12,17 +12,21 @@
> >  #include <linux/pwm.h>
> >  #include <linux/slab.h>
> >  
> > +#include <dt-bindings/mfd/cros_ec.h>
> > +
> >  /**
> >   * struct cros_ec_pwm_device - Driver data for EC PWM
> >   *
> >   * @dev: Device node
> >   * @ec: Pointer to EC device
> >   * @chip: PWM controller chip
> > + * @use_pwm_type: Use PWM types instead of generic channels
> >   */
> >  struct cros_ec_pwm_device {
> >  	struct device *dev;
> >  	struct cros_ec_device *ec;
> >  	struct pwm_chip chip;
> > +	bool use_pwm_type;
> >  };
> >  
> >  /**
> > @@ -58,14 +62,31 @@ static void cros_ec_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
> >  	kfree(channel);
> >  }
> >  
> > -static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
> > +static int cros_ec_dt_type_to_pwm_type(u8 dt_index, u8 *pwm_type)
> >  {
> > +	switch (dt_index) {
> > +	case CROS_EC_PWM_DT_KB_LIGHT:
> > +		*pwm_type = EC_PWM_TYPE_KB_LIGHT;
> > +		return 0;
> > +	case CROS_EC_PWM_DT_DISPLAY_LIGHT:
> > +		*pwm_type = EC_PWM_TYPE_DISPLAY_LIGHT;
> > +		return 0;
> > +	default:
> > +		return -EINVAL;
> > +	}
> > +}
> > +
> > +static int cros_ec_pwm_set_duty(struct cros_ec_pwm_device *ec_pwm, u8 index,
> > +				u16 duty)
> > +{
> > +	struct cros_ec_device *ec = ec_pwm->ec;
> >  	struct {
> >  		struct cros_ec_command msg;
> >  		struct ec_params_pwm_set_duty params;
> >  	} __packed buf;
> >  	struct ec_params_pwm_set_duty *params = &buf.params;
> >  	struct cros_ec_command *msg = &buf.msg;
> > +	int ret;
> >  
> >  	memset(&buf, 0, sizeof(buf));
> >  
> > @@ -75,14 +96,25 @@ static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
> >  	msg->outsize = sizeof(*params);
> >  
> >  	params->duty = duty;
> > -	params->pwm_type = EC_PWM_TYPE_GENERIC;
> > -	params->index = index;
> > +
> > +	if (ec_pwm->use_pwm_type) {
> > +		ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type);
> > +		if (ret) {
> > +			dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
> > +			return ret;
> > +		}
> > +		params->index = 0;
> > +	} else {
> > +		params->pwm_type = EC_PWM_TYPE_GENERIC;
> > +		params->index = index;
> > +	}
> >  
> >  	return cros_ec_cmd_xfer_status(ec, msg);
> >  }
> >  
> > -static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
> > +static int cros_ec_pwm_get_duty(struct cros_ec_pwm_device *ec_pwm, u8 index)
> >  {
> > +	struct cros_ec_device *ec = ec_pwm->ec;
> >  	struct {
> >  		struct cros_ec_command msg;
> >  		union {
> > @@ -102,8 +134,17 @@ static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
> >  	msg->insize = sizeof(*resp);
> >  	msg->outsize = sizeof(*params);
> >  
> > -	params->pwm_type = EC_PWM_TYPE_GENERIC;
> > -	params->index = index;
> > +	if (ec_pwm->use_pwm_type) {
> > +		ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type);
> > +		if (ret) {
> > +			dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
> > +			return ret;
> > +		}
> > +		params->index = 0;
> > +	} else {
> > +		params->pwm_type = EC_PWM_TYPE_GENERIC;
> > +		params->index = index;
> > +	}
> >  
> >  	ret = cros_ec_cmd_xfer_status(ec, msg);
> >  	if (ret < 0)
> > @@ -133,7 +174,7 @@ static int cros_ec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> >  	 */
> >  	duty_cycle = state->enabled ? state->duty_cycle : 0;
> >  
> > -	ret = cros_ec_pwm_set_duty(ec_pwm->ec, pwm->hwpwm, duty_cycle);
> > +	ret = cros_ec_pwm_set_duty(ec_pwm, pwm->hwpwm, duty_cycle);
> >  	if (ret < 0)
> >  		return ret;
> >  
> > @@ -149,7 +190,7 @@ static void cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
> >  	struct cros_ec_pwm *channel = pwm_get_chip_data(pwm);
> >  	int ret;
> >  
> > -	ret = cros_ec_pwm_get_duty(ec_pwm->ec, pwm->hwpwm);
> > +	ret = cros_ec_pwm_get_duty(ec_pwm, pwm->hwpwm);
> >  	if (ret < 0) {
> >  		dev_err(chip->dev, "error getting initial duty: %d\n", ret);
> >  		return;
> > @@ -204,13 +245,13 @@ static const struct pwm_ops cros_ec_pwm_ops = {
> >   * of PWMs it supports directly, so we have to read the pwm duty cycle for
> >   * subsequent channels until we get an error.
> >   */
> > -static int cros_ec_num_pwms(struct cros_ec_device *ec)
> > +static int cros_ec_num_pwms(struct cros_ec_pwm_device *ec_pwm)
> >  {
> >  	int i, ret;
> >  
> >  	/* The index field is only 8 bits */
> >  	for (i = 0; i <= U8_MAX; i++) {
> > -		ret = cros_ec_pwm_get_duty(ec, i);
> > +		ret = cros_ec_pwm_get_duty(ec_pwm, i);
> >  		/*
> >  		 * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM
> >  		 * responses; everything else is treated as an error.
> > @@ -236,6 +277,7 @@ static int cros_ec_pwm_probe(struct platform_device *pdev)
> >  {
> >  	struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
> >  	struct device *dev = &pdev->dev;
> > +	struct device_node *np = pdev->dev.of_node;
> >  	struct cros_ec_pwm_device *ec_pwm;
> >  	struct pwm_chip *chip;
> >  	int ret;
> > @@ -251,17 +293,26 @@ static int cros_ec_pwm_probe(struct platform_device *pdev)
> >  	chip = &ec_pwm->chip;
> >  	ec_pwm->ec = ec;
> >  
> > +	if (of_device_is_compatible(np, "google,cros-ec-pwm-type"))
> > +		ec_pwm->use_pwm_type = true;
> 
> Isn't it possible to just use an optional boolean property
> (for example: "use-pwm-type") instead of defining a new compatible
> string?

Yeah that's what I did originally but Rob suggested to use a new
compatible instead:

https://lore.kernel.org/chrome-platform/Yk20uTE%2FVdm2c6jI@robh.at.kernel.org/

> > +
> >  	/* PWM chip */
> >  	chip->dev = dev;
> >  	chip->ops = &cros_ec_pwm_ops;
> >  	chip->of_xlate = cros_ec_pwm_xlate;
> >  	chip->of_pwm_n_cells = 1;
> > -	ret = cros_ec_num_pwms(ec);
> > -	if (ret < 0) {
> > -		dev_err(dev, "Couldn't find PWMs: %d\n", ret);
> > -		return ret;
> > +
> > +	if (ec_pwm->use_pwm_type) {
> > +		chip->npwm = CROS_EC_PWM_DT_COUNT;
> > +	} else {
> > +		ret = cros_ec_num_pwms(ec_pwm);
> > +		if (ret < 0) {
> > +			dev_err(dev, "Couldn't find PWMs: %d\n", ret);
> > +			return ret;
> > +		}
> > +		chip->npwm = ret;
> >  	}
> > -	chip->npwm = ret;
> > +
> >  	dev_dbg(dev, "Probed %u PWMs\n", chip->npwm);
> >  
> >  	ret = pwmchip_add(chip);
> > @@ -288,6 +339,7 @@ static int cros_ec_pwm_remove(struct platform_device *dev)
> >  #ifdef CONFIG_OF
> >  static const struct of_device_id cros_ec_pwm_of_match[] = {
> >  	{ .compatible = "google,cros-ec-pwm" },
> > +	{ .compatible = "google,cros-ec-pwm-type" },
> >  	{},
> >  };
> >  MODULE_DEVICE_TABLE(of, cros_ec_pwm_of_match);
> > -- 
> > 2.36.0.rc0.470.gd361397f0d-goog
> > 
> > 

-- 
Fabio Baltieri

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 2/4] pwm: pwm-cros-ec: add channel type support
  2022-04-21  8:34     ` Fabio Baltieri
@ 2022-04-21  8:52       ` Prashant Malani
  2022-04-21  9:42         ` Fabio Baltieri
  0 siblings, 1 reply; 11+ messages in thread
From: Prashant Malani @ 2022-04-21  8:52 UTC (permalink / raw)
  To: Fabio Baltieri
  Cc: Benson Leung, Guenter Roeck, Thierry Reding,
	Uwe Kleine-König, Lee Jones, Rob Herring, chrome-platform,
	linux-pwm, devicetree, linux-kernel, Tzung-Bi Shih

On Thu, Apr 21, 2022 at 1:34 AM Fabio Baltieri
<fabiobaltieri@chromium.org> wrote:
>
> On Wed, Apr 20, 2022 at 05:55:35PM +0000, Prashant Malani wrote:
> > On Apr 20 14:15, Fabio Baltieri wrote:
> > > Add support for EC_PWM_TYPE_DISPLAY_LIGHT and EC_PWM_TYPE_KB_LIGHT pwm
> > > types to the PWM cros_ec_pwm driver. This allows specifying one of these
> > > PWM channel by functionality, and let the EC firmware pick the correct
> > > channel, thus abstracting the hardware implementation from the kernel
> > > driver.
> > >
> > > To use it, define the node with the "google,cros-ec-pwm-type"
> > > compatible.
> > >
> > > Signed-off-by: Fabio Baltieri <fabiobaltieri@chromium.org>
> > > Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org>
> > > ---
> > >  drivers/pwm/pwm-cros-ec.c | 82 ++++++++++++++++++++++++++++++++-------
> > >  1 file changed, 67 insertions(+), 15 deletions(-)
> > >
> > > diff --git a/drivers/pwm/pwm-cros-ec.c b/drivers/pwm/pwm-cros-ec.c
> > > index 5e29d9c682c3..7f10f56c3eb6 100644
> > > --- a/drivers/pwm/pwm-cros-ec.c
> > > +++ b/drivers/pwm/pwm-cros-ec.c
> > > @@ -12,17 +12,21 @@
> > >  #include <linux/pwm.h>
> > >  #include <linux/slab.h>
> > >
> > > +#include <dt-bindings/mfd/cros_ec.h>
> > > +
> > >  /**
> > >   * struct cros_ec_pwm_device - Driver data for EC PWM
> > >   *
> > >   * @dev: Device node
> > >   * @ec: Pointer to EC device
> > >   * @chip: PWM controller chip
> > > + * @use_pwm_type: Use PWM types instead of generic channels
> > >   */
> > >  struct cros_ec_pwm_device {
> > >     struct device *dev;
> > >     struct cros_ec_device *ec;
> > >     struct pwm_chip chip;
> > > +   bool use_pwm_type;
> > >  };
> > >
> > >  /**
> > > @@ -58,14 +62,31 @@ static void cros_ec_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
> > >     kfree(channel);
> > >  }
> > >
> > > -static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
> > > +static int cros_ec_dt_type_to_pwm_type(u8 dt_index, u8 *pwm_type)
> > >  {
> > > +   switch (dt_index) {
> > > +   case CROS_EC_PWM_DT_KB_LIGHT:
> > > +           *pwm_type = EC_PWM_TYPE_KB_LIGHT;
> > > +           return 0;
> > > +   case CROS_EC_PWM_DT_DISPLAY_LIGHT:
> > > +           *pwm_type = EC_PWM_TYPE_DISPLAY_LIGHT;
> > > +           return 0;
> > > +   default:
> > > +           return -EINVAL;
> > > +   }
> > > +}
> > > +
> > > +static int cros_ec_pwm_set_duty(struct cros_ec_pwm_device *ec_pwm, u8 index,
> > > +                           u16 duty)
> > > +{
> > > +   struct cros_ec_device *ec = ec_pwm->ec;
> > >     struct {
> > >             struct cros_ec_command msg;
> > >             struct ec_params_pwm_set_duty params;
> > >     } __packed buf;
> > >     struct ec_params_pwm_set_duty *params = &buf.params;
> > >     struct cros_ec_command *msg = &buf.msg;
> > > +   int ret;
> > >
> > >     memset(&buf, 0, sizeof(buf));
> > >
> > > @@ -75,14 +96,25 @@ static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
> > >     msg->outsize = sizeof(*params);
> > >
> > >     params->duty = duty;
> > > -   params->pwm_type = EC_PWM_TYPE_GENERIC;
> > > -   params->index = index;
> > > +
> > > +   if (ec_pwm->use_pwm_type) {
> > > +           ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type);
> > > +           if (ret) {
> > > +                   dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
> > > +                   return ret;
> > > +           }
> > > +           params->index = 0;
> > > +   } else {
> > > +           params->pwm_type = EC_PWM_TYPE_GENERIC;
> > > +           params->index = index;
> > > +   }
> > >
> > >     return cros_ec_cmd_xfer_status(ec, msg);
> > >  }
> > >
> > > -static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
> > > +static int cros_ec_pwm_get_duty(struct cros_ec_pwm_device *ec_pwm, u8 index)
> > >  {
> > > +   struct cros_ec_device *ec = ec_pwm->ec;
> > >     struct {
> > >             struct cros_ec_command msg;
> > >             union {
> > > @@ -102,8 +134,17 @@ static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
> > >     msg->insize = sizeof(*resp);
> > >     msg->outsize = sizeof(*params);
> > >
> > > -   params->pwm_type = EC_PWM_TYPE_GENERIC;
> > > -   params->index = index;
> > > +   if (ec_pwm->use_pwm_type) {
> > > +           ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type);
> > > +           if (ret) {
> > > +                   dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
> > > +                   return ret;
> > > +           }
> > > +           params->index = 0;
> > > +   } else {
> > > +           params->pwm_type = EC_PWM_TYPE_GENERIC;
> > > +           params->index = index;
> > > +   }
> > >
> > >     ret = cros_ec_cmd_xfer_status(ec, msg);
> > >     if (ret < 0)
> > > @@ -133,7 +174,7 @@ static int cros_ec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> > >      */
> > >     duty_cycle = state->enabled ? state->duty_cycle : 0;
> > >
> > > -   ret = cros_ec_pwm_set_duty(ec_pwm->ec, pwm->hwpwm, duty_cycle);
> > > +   ret = cros_ec_pwm_set_duty(ec_pwm, pwm->hwpwm, duty_cycle);
> > >     if (ret < 0)
> > >             return ret;
> > >
> > > @@ -149,7 +190,7 @@ static void cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
> > >     struct cros_ec_pwm *channel = pwm_get_chip_data(pwm);
> > >     int ret;
> > >
> > > -   ret = cros_ec_pwm_get_duty(ec_pwm->ec, pwm->hwpwm);
> > > +   ret = cros_ec_pwm_get_duty(ec_pwm, pwm->hwpwm);
> > >     if (ret < 0) {
> > >             dev_err(chip->dev, "error getting initial duty: %d\n", ret);
> > >             return;
> > > @@ -204,13 +245,13 @@ static const struct pwm_ops cros_ec_pwm_ops = {
> > >   * of PWMs it supports directly, so we have to read the pwm duty cycle for
> > >   * subsequent channels until we get an error.
> > >   */
> > > -static int cros_ec_num_pwms(struct cros_ec_device *ec)
> > > +static int cros_ec_num_pwms(struct cros_ec_pwm_device *ec_pwm)
> > >  {
> > >     int i, ret;
> > >
> > >     /* The index field is only 8 bits */
> > >     for (i = 0; i <= U8_MAX; i++) {
> > > -           ret = cros_ec_pwm_get_duty(ec, i);
> > > +           ret = cros_ec_pwm_get_duty(ec_pwm, i);
> > >             /*
> > >              * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM
> > >              * responses; everything else is treated as an error.
> > > @@ -236,6 +277,7 @@ static int cros_ec_pwm_probe(struct platform_device *pdev)
> > >  {
> > >     struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
> > >     struct device *dev = &pdev->dev;
> > > +   struct device_node *np = pdev->dev.of_node;
> > >     struct cros_ec_pwm_device *ec_pwm;
> > >     struct pwm_chip *chip;
> > >     int ret;
> > > @@ -251,17 +293,26 @@ static int cros_ec_pwm_probe(struct platform_device *pdev)
> > >     chip = &ec_pwm->chip;
> > >     ec_pwm->ec = ec;
> > >
> > > +   if (of_device_is_compatible(np, "google,cros-ec-pwm-type"))
> > > +           ec_pwm->use_pwm_type = true;
> >
> > Isn't it possible to just use an optional boolean property
> > (for example: "use-pwm-type") instead of defining a new compatible
> > string?
>
> Yeah that's what I did originally but Rob suggested to use a new
> compatible instead:
>
> https://lore.kernel.org/chrome-platform/Yk20uTE%2FVdm2c6jI@robh.at.kernel.org/

Thanks for the context. I suppose one could use the "split number
space" suggestion mentioned there,
and still be able to supply the right arguments for
pwm_request_from_chip() (since we're the ones
supplying the argument via of_xlate). But, if the DT maintainer is
alright with a new compatible, then
of course you should go with whatever works for you.

>
> > > +
> > >     /* PWM chip */
> > >     chip->dev = dev;
> > >     chip->ops = &cros_ec_pwm_ops;
> > >     chip->of_xlate = cros_ec_pwm_xlate;
> > >     chip->of_pwm_n_cells = 1;
> > > -   ret = cros_ec_num_pwms(ec);
> > > -   if (ret < 0) {
> > > -           dev_err(dev, "Couldn't find PWMs: %d\n", ret);
> > > -           return ret;
> > > +
> > > +   if (ec_pwm->use_pwm_type) {
> > > +           chip->npwm = CROS_EC_PWM_DT_COUNT;
> > > +   } else {
> > > +           ret = cros_ec_num_pwms(ec_pwm);
> > > +           if (ret < 0) {
> > > +                   dev_err(dev, "Couldn't find PWMs: %d\n", ret);
> > > +                   return ret;
> > > +           }
> > > +           chip->npwm = ret;
> > >     }
> > > -   chip->npwm = ret;
> > > +
> > >     dev_dbg(dev, "Probed %u PWMs\n", chip->npwm);
> > >
> > >     ret = pwmchip_add(chip);
> > > @@ -288,6 +339,7 @@ static int cros_ec_pwm_remove(struct platform_device *dev)
> > >  #ifdef CONFIG_OF
> > >  static const struct of_device_id cros_ec_pwm_of_match[] = {
> > >     { .compatible = "google,cros-ec-pwm" },
> > > +   { .compatible = "google,cros-ec-pwm-type" },
> > >     {},
> > >  };
> > >  MODULE_DEVICE_TABLE(of, cros_ec_pwm_of_match);
> > > --
> > > 2.36.0.rc0.470.gd361397f0d-goog
> > >
> > >
>
> --
> Fabio Baltieri

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 2/4] pwm: pwm-cros-ec: add channel type support
  2022-04-21  8:52       ` Prashant Malani
@ 2022-04-21  9:42         ` Fabio Baltieri
  0 siblings, 0 replies; 11+ messages in thread
From: Fabio Baltieri @ 2022-04-21  9:42 UTC (permalink / raw)
  To: Prashant Malani
  Cc: Benson Leung, Guenter Roeck, Thierry Reding,
	Uwe Kleine-König, Lee Jones, Rob Herring, chrome-platform,
	linux-pwm, devicetree, linux-kernel, Tzung-Bi Shih

On Thu, Apr 21, 2022 at 01:52:02AM -0700, Prashant Malani wrote:
> On Thu, Apr 21, 2022 at 1:34 AM Fabio Baltieri
> <fabiobaltieri@chromium.org> wrote:
> >
> > On Wed, Apr 20, 2022 at 05:55:35PM +0000, Prashant Malani wrote:
> > > On Apr 20 14:15, Fabio Baltieri wrote:
> > > > Add support for EC_PWM_TYPE_DISPLAY_LIGHT and EC_PWM_TYPE_KB_LIGHT pwm
> > > > types to the PWM cros_ec_pwm driver. This allows specifying one of these
> > > > PWM channel by functionality, and let the EC firmware pick the correct
> > > > channel, thus abstracting the hardware implementation from the kernel
> > > > driver.
> > > >
> > > > To use it, define the node with the "google,cros-ec-pwm-type"
> > > > compatible.
> > > >
> > > > Signed-off-by: Fabio Baltieri <fabiobaltieri@chromium.org>
> > > > Reviewed-by: Tzung-Bi Shih <tzungbi@kernel.org>
> > > > ---
> > > >  drivers/pwm/pwm-cros-ec.c | 82 ++++++++++++++++++++++++++++++++-------
> > > >  1 file changed, 67 insertions(+), 15 deletions(-)
> > > >
> > > > diff --git a/drivers/pwm/pwm-cros-ec.c b/drivers/pwm/pwm-cros-ec.c
> > > > index 5e29d9c682c3..7f10f56c3eb6 100644
> > > > --- a/drivers/pwm/pwm-cros-ec.c
> > > > +++ b/drivers/pwm/pwm-cros-ec.c
> > > > @@ -12,17 +12,21 @@
> > > >  #include <linux/pwm.h>
> > > >  #include <linux/slab.h>
> > > >
> > > > +#include <dt-bindings/mfd/cros_ec.h>
> > > > +
> > > >  /**
> > > >   * struct cros_ec_pwm_device - Driver data for EC PWM
> > > >   *
> > > >   * @dev: Device node
> > > >   * @ec: Pointer to EC device
> > > >   * @chip: PWM controller chip
> > > > + * @use_pwm_type: Use PWM types instead of generic channels
> > > >   */
> > > >  struct cros_ec_pwm_device {
> > > >     struct device *dev;
> > > >     struct cros_ec_device *ec;
> > > >     struct pwm_chip chip;
> > > > +   bool use_pwm_type;
> > > >  };
> > > >
> > > >  /**
> > > > @@ -58,14 +62,31 @@ static void cros_ec_pwm_free(struct pwm_chip *chip, struct pwm_device *pwm)
> > > >     kfree(channel);
> > > >  }
> > > >
> > > > -static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
> > > > +static int cros_ec_dt_type_to_pwm_type(u8 dt_index, u8 *pwm_type)
> > > >  {
> > > > +   switch (dt_index) {
> > > > +   case CROS_EC_PWM_DT_KB_LIGHT:
> > > > +           *pwm_type = EC_PWM_TYPE_KB_LIGHT;
> > > > +           return 0;
> > > > +   case CROS_EC_PWM_DT_DISPLAY_LIGHT:
> > > > +           *pwm_type = EC_PWM_TYPE_DISPLAY_LIGHT;
> > > > +           return 0;
> > > > +   default:
> > > > +           return -EINVAL;
> > > > +   }
> > > > +}
> > > > +
> > > > +static int cros_ec_pwm_set_duty(struct cros_ec_pwm_device *ec_pwm, u8 index,
> > > > +                           u16 duty)
> > > > +{
> > > > +   struct cros_ec_device *ec = ec_pwm->ec;
> > > >     struct {
> > > >             struct cros_ec_command msg;
> > > >             struct ec_params_pwm_set_duty params;
> > > >     } __packed buf;
> > > >     struct ec_params_pwm_set_duty *params = &buf.params;
> > > >     struct cros_ec_command *msg = &buf.msg;
> > > > +   int ret;
> > > >
> > > >     memset(&buf, 0, sizeof(buf));
> > > >
> > > > @@ -75,14 +96,25 @@ static int cros_ec_pwm_set_duty(struct cros_ec_device *ec, u8 index, u16 duty)
> > > >     msg->outsize = sizeof(*params);
> > > >
> > > >     params->duty = duty;
> > > > -   params->pwm_type = EC_PWM_TYPE_GENERIC;
> > > > -   params->index = index;
> > > > +
> > > > +   if (ec_pwm->use_pwm_type) {
> > > > +           ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type);
> > > > +           if (ret) {
> > > > +                   dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
> > > > +                   return ret;
> > > > +           }
> > > > +           params->index = 0;
> > > > +   } else {
> > > > +           params->pwm_type = EC_PWM_TYPE_GENERIC;
> > > > +           params->index = index;
> > > > +   }
> > > >
> > > >     return cros_ec_cmd_xfer_status(ec, msg);
> > > >  }
> > > >
> > > > -static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
> > > > +static int cros_ec_pwm_get_duty(struct cros_ec_pwm_device *ec_pwm, u8 index)
> > > >  {
> > > > +   struct cros_ec_device *ec = ec_pwm->ec;
> > > >     struct {
> > > >             struct cros_ec_command msg;
> > > >             union {
> > > > @@ -102,8 +134,17 @@ static int cros_ec_pwm_get_duty(struct cros_ec_device *ec, u8 index)
> > > >     msg->insize = sizeof(*resp);
> > > >     msg->outsize = sizeof(*params);
> > > >
> > > > -   params->pwm_type = EC_PWM_TYPE_GENERIC;
> > > > -   params->index = index;
> > > > +   if (ec_pwm->use_pwm_type) {
> > > > +           ret = cros_ec_dt_type_to_pwm_type(index, &params->pwm_type);
> > > > +           if (ret) {
> > > > +                   dev_err(ec->dev, "Invalid PWM type index: %d\n", index);
> > > > +                   return ret;
> > > > +           }
> > > > +           params->index = 0;
> > > > +   } else {
> > > > +           params->pwm_type = EC_PWM_TYPE_GENERIC;
> > > > +           params->index = index;
> > > > +   }
> > > >
> > > >     ret = cros_ec_cmd_xfer_status(ec, msg);
> > > >     if (ret < 0)
> > > > @@ -133,7 +174,7 @@ static int cros_ec_pwm_apply(struct pwm_chip *chip, struct pwm_device *pwm,
> > > >      */
> > > >     duty_cycle = state->enabled ? state->duty_cycle : 0;
> > > >
> > > > -   ret = cros_ec_pwm_set_duty(ec_pwm->ec, pwm->hwpwm, duty_cycle);
> > > > +   ret = cros_ec_pwm_set_duty(ec_pwm, pwm->hwpwm, duty_cycle);
> > > >     if (ret < 0)
> > > >             return ret;
> > > >
> > > > @@ -149,7 +190,7 @@ static void cros_ec_pwm_get_state(struct pwm_chip *chip, struct pwm_device *pwm,
> > > >     struct cros_ec_pwm *channel = pwm_get_chip_data(pwm);
> > > >     int ret;
> > > >
> > > > -   ret = cros_ec_pwm_get_duty(ec_pwm->ec, pwm->hwpwm);
> > > > +   ret = cros_ec_pwm_get_duty(ec_pwm, pwm->hwpwm);
> > > >     if (ret < 0) {
> > > >             dev_err(chip->dev, "error getting initial duty: %d\n", ret);
> > > >             return;
> > > > @@ -204,13 +245,13 @@ static const struct pwm_ops cros_ec_pwm_ops = {
> > > >   * of PWMs it supports directly, so we have to read the pwm duty cycle for
> > > >   * subsequent channels until we get an error.
> > > >   */
> > > > -static int cros_ec_num_pwms(struct cros_ec_device *ec)
> > > > +static int cros_ec_num_pwms(struct cros_ec_pwm_device *ec_pwm)
> > > >  {
> > > >     int i, ret;
> > > >
> > > >     /* The index field is only 8 bits */
> > > >     for (i = 0; i <= U8_MAX; i++) {
> > > > -           ret = cros_ec_pwm_get_duty(ec, i);
> > > > +           ret = cros_ec_pwm_get_duty(ec_pwm, i);
> > > >             /*
> > > >              * We look for SUCCESS, INVALID_COMMAND, or INVALID_PARAM
> > > >              * responses; everything else is treated as an error.
> > > > @@ -236,6 +277,7 @@ static int cros_ec_pwm_probe(struct platform_device *pdev)
> > > >  {
> > > >     struct cros_ec_device *ec = dev_get_drvdata(pdev->dev.parent);
> > > >     struct device *dev = &pdev->dev;
> > > > +   struct device_node *np = pdev->dev.of_node;
> > > >     struct cros_ec_pwm_device *ec_pwm;
> > > >     struct pwm_chip *chip;
> > > >     int ret;
> > > > @@ -251,17 +293,26 @@ static int cros_ec_pwm_probe(struct platform_device *pdev)
> > > >     chip = &ec_pwm->chip;
> > > >     ec_pwm->ec = ec;
> > > >
> > > > +   if (of_device_is_compatible(np, "google,cros-ec-pwm-type"))
> > > > +           ec_pwm->use_pwm_type = true;
> > >
> > > Isn't it possible to just use an optional boolean property
> > > (for example: "use-pwm-type") instead of defining a new compatible
> > > string?
> >
> > Yeah that's what I did originally but Rob suggested to use a new
> > compatible instead:
> >
> > https://lore.kernel.org/chrome-platform/Yk20uTE%2FVdm2c6jI@robh.at.kernel.org/
> 
> Thanks for the context. I suppose one could use the "split number
> space" suggestion mentioned there,
> and still be able to supply the right arguments for
> pwm_request_from_chip() (since we're the ones
> supplying the argument via of_xlate). But, if the DT maintainer is
> alright with a new compatible, then
> of course you should go with whatever works for you.

Right, that would work for masking out the number space, but then you'd
end up with an incoherent chips->npwm which is used all over the core
pwm code (including the debugfs stuff), so to be coherent there I think
we have to have something at probe time and can't mix it up.

> > > > +
> > > >     /* PWM chip */
> > > >     chip->dev = dev;
> > > >     chip->ops = &cros_ec_pwm_ops;
> > > >     chip->of_xlate = cros_ec_pwm_xlate;
> > > >     chip->of_pwm_n_cells = 1;
> > > > -   ret = cros_ec_num_pwms(ec);
> > > > -   if (ret < 0) {
> > > > -           dev_err(dev, "Couldn't find PWMs: %d\n", ret);
> > > > -           return ret;
> > > > +
> > > > +   if (ec_pwm->use_pwm_type) {
> > > > +           chip->npwm = CROS_EC_PWM_DT_COUNT;
> > > > +   } else {
> > > > +           ret = cros_ec_num_pwms(ec_pwm);
> > > > +           if (ret < 0) {
> > > > +                   dev_err(dev, "Couldn't find PWMs: %d\n", ret);
> > > > +                   return ret;
> > > > +           }
> > > > +           chip->npwm = ret;
> > > >     }
> > > > -   chip->npwm = ret;
> > > > +
> > > >     dev_dbg(dev, "Probed %u PWMs\n", chip->npwm);
> > > >
> > > >     ret = pwmchip_add(chip);
> > > > @@ -288,6 +339,7 @@ static int cros_ec_pwm_remove(struct platform_device *dev)
> > > >  #ifdef CONFIG_OF
> > > >  static const struct of_device_id cros_ec_pwm_of_match[] = {
> > > >     { .compatible = "google,cros-ec-pwm" },
> > > > +   { .compatible = "google,cros-ec-pwm-type" },
> > > >     {},
> > > >  };
> > > >  MODULE_DEVICE_TABLE(of, cros_ec_pwm_of_match);
> > > > --
> > > > 2.36.0.rc0.470.gd361397f0d-goog
> > > >
> > > >
> >
> > --
> > Fabio Baltieri

-- 
Fabio Baltieri

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 3/4] dt-bindings: update google,cros-ec-pwm documentation
  2022-04-20 14:15 ` [PATCH v5 3/4] dt-bindings: update google,cros-ec-pwm documentation Fabio Baltieri
@ 2022-04-25 21:58   ` Rob Herring
  2022-04-26 14:02     ` Fabio Baltieri
  0 siblings, 1 reply; 11+ messages in thread
From: Rob Herring @ 2022-04-25 21:58 UTC (permalink / raw)
  To: Fabio Baltieri
  Cc: Benson Leung, Guenter Roeck, Thierry Reding,
	Uwe Kleine-König, Lee Jones, chrome-platform, linux-pwm,
	devicetree, linux-kernel

On Wed, Apr 20, 2022 at 02:15:55PM +0000, Fabio Baltieri wrote:
> Update google,cros-ec-pwm node documentation to mention the
> google,cros-ec-pwm-type compatible.

It would be good if the subject provided some clue what the update is. 
Every change is an update. And all bindings are 'documentation' so 
that can be dropped.

> 
> Signed-off-by: Fabio Baltieri <fabiobaltieri@chromium.org>
> ---
>  .../devicetree/bindings/pwm/google,cros-ec-pwm.yaml      | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)

In any case,

Reviewed-by: Rob Herring <robh@kernel.org>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v5 3/4] dt-bindings: update google,cros-ec-pwm documentation
  2022-04-25 21:58   ` Rob Herring
@ 2022-04-26 14:02     ` Fabio Baltieri
  0 siblings, 0 replies; 11+ messages in thread
From: Fabio Baltieri @ 2022-04-26 14:02 UTC (permalink / raw)
  To: Rob Herring
  Cc: Benson Leung, Guenter Roeck, Thierry Reding,
	Uwe Kleine-König, Lee Jones, chrome-platform, linux-pwm,
	devicetree, linux-kernel

On Mon, Apr 25, 2022 at 04:58:55PM -0500, Rob Herring wrote:
> On Wed, Apr 20, 2022 at 02:15:55PM +0000, Fabio Baltieri wrote:
> > Update google,cros-ec-pwm node documentation to mention the
> > google,cros-ec-pwm-type compatible.
> 
> It would be good if the subject provided some clue what the update is. 
> Every change is an update. And all bindings are 'documentation' so 
> that can be dropped.

Fair enough, I'll reword the subject and send a v6.

> 
> > 
> > Signed-off-by: Fabio Baltieri <fabiobaltieri@chromium.org>
> > ---
> >  .../devicetree/bindings/pwm/google,cros-ec-pwm.yaml      | 9 ++++++++-
> >  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> In any case,
> 
> Reviewed-by: Rob Herring <robh@kernel.org>
> 

Thanks!

-- 
Fabio Baltieri

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2022-04-26 14:02 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-20 14:15 [PATCH v5 0/4] Add channel type support to pwm-cros-ec Fabio Baltieri
2022-04-20 14:15 ` [PATCH v5 1/4] dt-bindings: add mfd/cros_ec definitions Fabio Baltieri
2022-04-20 14:15 ` [PATCH v5 2/4] pwm: pwm-cros-ec: add channel type support Fabio Baltieri
2022-04-20 17:55   ` Prashant Malani
2022-04-21  8:34     ` Fabio Baltieri
2022-04-21  8:52       ` Prashant Malani
2022-04-21  9:42         ` Fabio Baltieri
2022-04-20 14:15 ` [PATCH v5 3/4] dt-bindings: update google,cros-ec-pwm documentation Fabio Baltieri
2022-04-25 21:58   ` Rob Herring
2022-04-26 14:02     ` Fabio Baltieri
2022-04-20 14:15 ` [PATCH v5 4/4] arm64: dts: address cros-ec-pwm channels by type Fabio Baltieri

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.