Skip to contentSkip to conversation

: clk: qcom: support qpnp clock divider configuration

PATCH In review Tirupathi Reddy 2 participants 6 messages Jul 13, 2017 – Jul 18, 2017

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 : clk: qcom: support qpnp clock divider configuration

This patch adds a new device driver for configuring the clkdiv module presents on the Qualcomm QPNP PMIC. The driver configures the frequency of clkdiv outputs. The driver provides callback functions for different clock operations and register the clkdiv module to clock framework.

Tirupathi Reddy (1):
clk: qcom: Add qpnp clock divider support

 .../devicetree/bindings/clock/clk-qpnp-div.txt     |  52 +++
 drivers/clk/qcom/Kconfig                           |   9 +
 drivers/clk/qcom/Makefile                          |   1 +
 drivers/clk/qcom/clk-qpnp-div.c                    | 422 +++++++++++++++++++++
 4 files changed, 484 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/clk-qpnp-div.txt
 create mode 100644 drivers/clk/qcom/clk-qpnp-div.c
Signature · 6 lines
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#1 lore raw

PATCH 1/1 clk: qcom: Add qpnp clock divider support

Clkdiv module provides a clock output on the PMIC with CXO as the source. This clock can be routed through PMIC GPIOs. Add a device driver to configure this clkdiv module.

---
 .../devicetree/bindings/clock/clk-qpnp-div.txt     |  52 +++
 drivers/clk/qcom/Kconfig                           |   9 +
 drivers/clk/qcom/Makefile                          |   1 +
 drivers/clk/qcom/clk-qpnp-div.c                    | 422 +++++++++++++++++++++
 4 files changed, 484 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/clk-qpnp-div.txt
 create mode 100644 drivers/clk/qcom/clk-qpnp-div.c
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +52 -0
new file mode 100644
index 0000000..03b7b70
@@ -0,0 +1,52 @@
+Qualcomm Technologies, Inc. QPNP clock divider (clkdiv)
+
+clkdiv configures the clock frequency of a set of outputs on the PMIC.
+These clocks are typically wired through alternate functions on
+gpio pins.
+
+=======================
+Supported Properties
+=======================
+
+- compatible
+	Usage:      required
+	Value type: <string>
+	Definition: should be "qcom,qpnp-clkdiv".
+
+- reg
+	Usage:      required
+	Value type: <prop-encoded-array>
+	Definition: Addresses and sizes for the memory of this CLKDIV
+		    peripheral.
+
+- qcom,cxo-freq
+	Usage:      required
+	Value type: <u32>
+	Definition: The frequency of the crystal oscillator (CXO) clock in Hz.
+
+- qcom,clkdiv-id
+	Usage:      required
+	Value type: <u32>
+	Definition: Integer value specifies the hardware identifier of this
+		    CLKDIV peripheral.
+
+- qcom,clkdiv-init-freq
+	Usage:      optional
+	Value type: <u32>
+	Definition: Initial output frequency in Hz to configure for the CLKDIV
+		    peripheral. The initial frequency value should be less than
+		    or equal to CXO clock frequency and greater than or equal to
+		    CXO_freq/64.
+
+=======
+Example
+=======
+
+qcom,clkdiv@5b00 {
+	compatible = "qcom,qpnp-clkdiv";
+	reg = <0x5b00 0x100>;
+
+	qcom,cxo-freq = <19200000>;
+	qcom,clkdiv-id = <1>;
+	qcom,clkdiv-init-freq = <9600000>;
+};
drivers/clk/qcom/Kconfig +9 -0
index 9f6c278..c68ae96 100644
@@ -196,3 +196,12 @@ config MSM_MMCC_8996
 	  Support for the multimedia clock controller on msm8996 devices.
 	  Say Y if you want to support multimedia devices such as display,
 	  graphics, video encode/decode, camera, etc.
+
+config CLOCK_QPNP_DIV
+	tristate "QPNP PMIC clkdiv driver"
+	depends on COMMON_CLK_QCOM && SPMI
+	help
+	  This driver supports the clkdiv functionality on the Qualcomm
+	  Technologies, Inc. QPNP PMIC. It configures the frequency of
+	  clkdiv outputs on the PMIC. These clocks are typically wired
+	  through alternate functions on gpio pins.
drivers/clk/qcom/Makefile +1 -0
index 3f3aff2..cca8840 100644
@@ -33,3 +33,4 @@ obj-$(CONFIG_MSM_MMCC_8974) += mmcc-msm8974.o
 obj-$(CONFIG_MSM_MMCC_8996) += mmcc-msm8996.o
 obj-$(CONFIG_QCOM_CLK_RPM) += clk-rpm.o
 obj-$(CONFIG_QCOM_CLK_SMD_RPM) += clk-smd-rpm.o
+obj-$(CONFIG_CLOCK_QPNP_DIV) += clk-qpnp-div.o
drivers/clk/qcom/clk-qpnp-div.c new file +422 -0

422 lines not on this page · raw message

Signature · 1 line
QUALCOMM INDIA, on behalf of Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation
#5 lore raw
In reply to Tirupathi Reddy
Quoted 5 lines · Tirupathi Reddy

This patch adds a new device driver for configuring the clkdiv module presents on the Qualcomm QPNP PMIC. The driver configures the frequency of clkdiv outputs. The driver provides callback functions for different clock operations and register the clkdiv module to clock framework.

Please don't send cover letters for single patches.

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project
#2 lore raw

Re: [PATCH V1] clk: qcom: Add qpnp clock divider support

In reply to Tirupathi Reddy
Quoted 21 lines · Tirupathi Reddy
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +15 -0
new file mode 100644
index 0000000..03b7b70
@@ -0,0 +1,52 @@
+Qualcomm Technologies, Inc. QPNP clock divider (clkdiv)
+
+clkdiv configures the clock frequency of a set of outputs on the PMIC.
+These clocks are typically wired through alternate functions on
+gpio pins.
+
+=======================
+Supported Properties
+=======================
+
+- compatible
+	Usage:      required
+	Value type: <string>
+	Definition: should be "qcom,qpnp-clkdiv".
+

Documentation/devicetree/bindings/clock/clk-qpnp-div.txt line 25

Quoted 10 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +10 -0
+- reg
+	Usage:      required
+	Value type: <prop-encoded-array>
+	Definition: Addresses and sizes for the memory of this CLKDIV
+		    peripheral.
+
+- qcom,cxo-freq
+	Usage:      required
+	Value type: <u32>
+	Definition: The frequency of the crystal oscillator (CXO) clock in Hz.

CXO should be a parent clk then. This could have clocks and clock-names properties for that and then be hooked up to xo_board.

Documentation/devicetree/bindings/clock/clk-qpnp-div.txt line 31

Quoted 6 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +6 -0
+
+- qcom,clkdiv-id
+	Usage:      required
+	Value type: <u32>
+	Definition: Integer value specifies the hardware identifier of this
+		    CLKDIV peripheral.

This is to name the clk? You could use clock-output-names as that's more standard. But this is also sort of confusing. If there are multiple clkdivs then it would be good to combine them into one device node assuming they're all next to each other. This would be similar to how we handle gpios and regulators. Then the naming is simple enough to be an incrementing number.

Documentation/devicetree/bindings/clock/clk-qpnp-div.txt line 39

Quoted 8 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +8 -0
+
+- qcom,clkdiv-init-freq
+	Usage:      optional
+	Value type: <u32>
+	Definition: Initial output frequency in Hz to configure for the CLKDIV
+		    peripheral. The initial frequency value should be less than
+		    or equal to CXO clock frequency and greater than or equal to
+		    CXO_freq/64.

Use assigned-clock-rates instead.

Quoted 18 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +13 -0
+
+=======
+Example
+=======
+
+qcom,clkdiv@5b00 {
+	compatible = "qcom,qpnp-clkdiv";
+	reg = <0x5b00 0x100>;
+
+	qcom,cxo-freq = <19200000>;
+	qcom,clkdiv-id = <1>;
+	qcom,clkdiv-init-freq = <9600000>;
+};
drivers/clk/qcom/Kconfig +0 -0
index 9f6c278..c68ae96 100644

drivers/clk/qcom/Kconfig config MSM_MMCC_8996 · line 202

Quoted 7 lines
drivers/clk/qcom/Kconfig +4 -0
@@ -196,3 +196,12 @@ config MSM_MMCC_8996
 	  Support for the multimedia clock controller on msm8996 devices.
 	  Say Y if you want to support multimedia devices such as display,
 	  graphics, video encode/decode, camera, etc.
+
+config CLOCK_QPNP_DIV
+	tristate "QPNP PMIC clkdiv driver"
+	depends on COMMON_CLK_QCOM && SPMI

COMPILE_TEST?

drivers/clk/qcom/Kconfig config MSM_MMCC_8996 · line 205

Quoted 3 lines
drivers/clk/qcom/Kconfig +3 -0
+	help
+	  This driver supports the clkdiv functionality on the Qualcomm
+	  Technologies, Inc. QPNP PMIC. It configures the frequency of

I'm not sure we ever really call it QPNP PMIC. I see one hit from grep for the thermal driver. Perhaps SPMI PMIC is more appropriate.

Quoted 20 lines
drivers/clk/qcom/Kconfig +2 -0
+	  clkdiv outputs on the PMIC. These clocks are typically wired
+	  through alternate functions on gpio pins.
drivers/clk/qcom/clk-qpnp-div.c new file +12 -0
new file mode 100644
index 0000000..416c20f
@@ -0,0 +1,422 @@
+/* Copyright (c) 2017, The Linux Foundation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+

drivers/clk/qcom/clk-qpnp-div.c line 22

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+#include <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/delay.h>
+#include <linux/err.h>
+#include <linux/log2.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/types.h>

bitops.h?

Quoted 17 lines
drivers/clk/qcom/clk-qpnp-div.c new file +17 -0
+
+#define Q_REG_DIV_CTL1			0x43
+#define Q_DIV_CTL1_DIV_FACTOR_MASK	GENMASK(2, 0)
+
+#define Q_REG_EN_CTL			0x46
+#define Q_REG_EN_MASK			BIT(7)
+#define Q_SET_EN			BIT(7)
+
+#define Q_CXO_PERIOD_NS(cxo_clk)	(NSEC_PER_SEC / cxo_clk)
+#define Q_DIV_PERIOD_NS(cxo_clk, div)	(NSEC_PER_SEC / (cxo_clk / div))
+#define Q_ENABLE_DELAY_NS(cxo_clk, div)	(2 * Q_CXO_PERIOD_NS(cxo_clk) + \
+					(3 * Q_DIV_PERIOD_NS(cxo_clk, div)))
+#define Q_DISABLE_DELAY_NS(cxo_clk, div) \
+					(3 * Q_DIV_PERIOD_NS(cxo_clk, div))
+
+#define CLK_QPNP_DIV_OFFSET		1
+

drivers/clk/qcom/clk-qpnp-div.c line 49

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+enum q_clk_div_factor {
+	Q_CLKDIV_XO_DIV_1_0 = 0,
+	Q_CLKDIV_XO_DIV_1,
+	Q_CLKDIV_XO_DIV_2,
+	Q_CLKDIV_XO_DIV_4,
+	Q_CLKDIV_XO_DIV_8,
+	Q_CLKDIV_XO_DIV_16,
+	Q_CLKDIV_XO_DIV_32,
+	Q_CLKDIV_XO_DIV_64,
+	Q_CLKDIV_MAX_ALLOWED,

Please make #defines for these instead of enum.

drivers/clk/qcom/clk-qpnp-div.c line 55

Quoted 6 lines
drivers/clk/qcom/clk-qpnp-div.c new file +6 -0
+};
+
+enum q_clkdiv_state {
+	DISABLE = false,
+	ENABLE = true,
+};

Uh no. Just use bool.

drivers/clk/qcom/clk-qpnp-div.c line 66

Quoted 11 lines
drivers/clk/qcom/clk-qpnp-div.c new file +11 -0
+
+struct q_clkdiv {
+	struct regmap		*regmap;
+	struct device		*dev;
+
+	u16			base;
+	spinlock_t		lock;
+
+	/* clock properties */
+	struct clk_hw		hw;
+	unsigned int		cxo_hz;

Drop.

drivers/clk/qcom/clk-qpnp-div.c line 68

Quoted 2 lines
drivers/clk/qcom/clk-qpnp-div.c new file +2 -0
+	enum q_clk_div_factor	div_factor;
+	bool			enabled;

Shouldn't be needed. Read the hardware instead.

drivers/clk/qcom/clk-qpnp-div.c line 71

Quoted 3 lines
drivers/clk/qcom/clk-qpnp-div.c new file +3 -0
+};
+
+static inline struct q_clkdiv *to_clkdiv(struct clk_hw *_hw)

_hw can just be hw?

drivers/clk/qcom/clk-qpnp-div.c line 82

Quoted 11 lines
drivers/clk/qcom/clk-qpnp-div.c new file +11 -0
+{
+	return container_of(_hw, struct q_clkdiv, hw);
+}
+
+static inline unsigned int div_factor_to_div(unsigned int div_factor)
+{
+	if (div_factor == Q_CLKDIV_XO_DIV_1_0)
+		return 1;
+
+	return 1 << (div_factor - CLK_QPNP_DIV_OFFSET);
+}

Not sure, but it may be possible to reuse the code in clk-divider.c and treat this as a CLK_DIVIDER_POWER_OF_TWO?

drivers/clk/qcom/clk-qpnp-div.c line 89

Quoted 7 lines
drivers/clk/qcom/clk-qpnp-div.c new file +7 -0
+
+static inline unsigned int div_to_div_factor(unsigned int div)
+{
+	return ilog2(div) + CLK_QPNP_DIV_OFFSET;
+}
+
+static int qpnp_clkdiv_masked_write(struct q_clkdiv *q_clkdiv, u16 offset,

Please replace instances of qpnp with spmi_pmic throughout this driver.

Also, why do we need a wrapper around regmap APIs? Please just use the APIs directly.

Quoted 17 lines
drivers/clk/qcom/clk-qpnp-div.c new file +17 -0
+			u8 mask, u8 val)
+{
+	int rc;
+
+	rc = regmap_update_bits(q_clkdiv->regmap, q_clkdiv->base + offset, mask,
+				val);
+	if (rc)
+		dev_err(q_clkdiv->dev,
+			"Unable to regmap_update_bits to addr=%hx, rc(%d)\n",
+			q_clkdiv->base + offset, rc);
+
+	return rc;
+}
+
+static int qpnp_clkdiv_set_enable_state(struct q_clkdiv *q_clkdiv,
+			enum q_clkdiv_state enable_state)
+{

drivers/clk/qcom/clk-qpnp-div.c line 116

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+	int rc;
+
+	rc = qpnp_clkdiv_masked_write(q_clkdiv, Q_REG_EN_CTL, Q_REG_EN_MASK,
+				(enable_state == ENABLE) ? Q_SET_EN : 0);
+	if (rc)
+		return rc;
+
+	if (enable_state == ENABLE)
+		ndelay(Q_ENABLE_DELAY_NS(q_clkdiv->cxo_hz,
+				div_factor_to_div(q_clkdiv->div_factor)));

Can this factor can be precalculated at probe time based on XO rate? And then we just multiply that factor with the divider value to figure out how long to wait?

Quoted 43 lines
drivers/clk/qcom/clk-qpnp-div.c new file +43 -0
+	else
+		ndelay(Q_DISABLE_DELAY_NS(q_clkdiv->cxo_hz,
+				div_factor_to_div(q_clkdiv->div_factor)));
+
+	return rc;
+}
+
+static int qpnp_clkdiv_config_freq_div(struct q_clkdiv *q_clkdiv,
+			unsigned int div)
+{
+	unsigned int div_factor;
+	int rc;
+
+	div_factor = div_to_div_factor(div);
+	if (div_factor <= 0 || div_factor >= Q_CLKDIV_MAX_ALLOWED)
+		return -EINVAL;
+
+	if (q_clkdiv->enabled) {
+		rc = qpnp_clkdiv_set_enable_state(q_clkdiv, DISABLE);
+		if (rc) {
+			dev_err(q_clkdiv->dev, "unable to disable clock, rc = %d\n",
+				rc);
+			return rc;
+		}
+	}
+
+	rc = qpnp_clkdiv_masked_write(q_clkdiv, Q_REG_DIV_CTL1,
+				Q_DIV_CTL1_DIV_FACTOR_MASK, div_factor);
+	if (rc) {
+		dev_err(q_clkdiv->dev, "config divider failed, rc=%d\n",
+			rc);
+		return rc;
+	}
+
+	q_clkdiv->div_factor = div_factor;
+
+	if (q_clkdiv->enabled) {
+		rc = qpnp_clkdiv_set_enable_state(q_clkdiv, ENABLE);
+		if (rc)
+			dev_err(q_clkdiv->dev, "unable to re-enable clock, rc = %d\n",
+				rc);
+	}
+

drivers/clk/qcom/clk-qpnp-div.c line 169

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+	return rc;
+}
+
+static int clk_qpnp_div_enable(struct clk_hw *hw)
+{
+	struct q_clkdiv *q_clkdiv = to_clkdiv(hw);
+	unsigned long flags;
+	int rc;
+
+	spin_lock_irqsave(&q_clkdiv->lock, flags);

What is this locking against? Rate change?

Quoted 33 lines
drivers/clk/qcom/clk-qpnp-div.c new file +33 -0
+
+	rc =  qpnp_clkdiv_set_enable_state(q_clkdiv, ENABLE);
+	if (rc) {
+		dev_err(q_clkdiv->dev, "clk enable failed, rc=%d\n", rc);
+		goto fail;
+	}
+
+	q_clkdiv->enabled = true;
+
+fail:
+	spin_unlock_irqrestore(&q_clkdiv->lock, flags);
+	return rc;
+}
+
+static void clk_qpnp_div_disable(struct clk_hw *hw)
+{
+	struct q_clkdiv *q_clkdiv = to_clkdiv(hw);
+	unsigned long flags;
+	int rc;
+
+	spin_lock_irqsave(&q_clkdiv->lock, flags);
+
+	rc = qpnp_clkdiv_set_enable_state(q_clkdiv, DISABLE);
+	if (rc) {
+		dev_err(q_clkdiv->dev, "clk disable failed, rc=%d\n", rc);
+		goto fail;
+	}
+
+	q_clkdiv->enabled = false;
+
+fail:
+	spin_unlock_irqrestore(&q_clkdiv->lock, flags);
+}

drivers/clk/qcom/clk-qpnp-div.c line 212

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+
+static long clk_qpnp_div_round_rate(struct clk_hw *hw, unsigned long rate,
+			unsigned long *parent_rate)
+{
+	struct q_clkdiv *q_clkdiv = to_clkdiv(hw);
+	unsigned long flags, new_rate;
+	unsigned int div, div_factor;
+
+	spin_lock_irqsave(&q_clkdiv->lock, flags);
+	if (rate <= 0 || rate > q_clkdiv->cxo_hz) {

How can rate be less than zero? It's unsigned! And if it's greater than some parent rate, round_rate() should return the largest rate it can support (I guess parent_rate?)

drivers/clk/qcom/clk-qpnp-div.c line 215

Quoted 3 lines
drivers/clk/qcom/clk-qpnp-div.c new file +3 -0
+		dev_err(q_clkdiv->dev, "invalid rate requested, rate = %lu\n",
+			rate);
+		spin_unlock_irqrestore(&q_clkdiv->lock, flags);

Also what are we spinlocking for?

drivers/clk/qcom/clk-qpnp-div.c line 225

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+		return -EINVAL;
+	}
+
+	div = DIV_ROUND_UP(q_clkdiv->cxo_hz, rate);
+	div_factor = div_to_div_factor(div);
+	if (div_factor >= Q_CLKDIV_MAX_ALLOWED)
+		div_factor = Q_CLKDIV_MAX_ALLOWED - 1;
+	new_rate = q_clkdiv->cxo_hz / div_factor_to_div(div_factor);
+
+	spin_unlock_irqrestore(&q_clkdiv->lock, flags);

Shouldn't need any spinlock here...

Quoted 4 lines
drivers/clk/qcom/clk-qpnp-div.c new file +4 -0
+	return new_rate;
+}
+
+static unsigned long clk_qpnp_div_recalc_rate(struct clk_hw *hw,

drivers/clk/qcom/clk-qpnp-div.c line 239

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+			unsigned long parent_rate)
+{
+	struct q_clkdiv *q_clkdiv = to_clkdiv(hw);
+	unsigned long rate, flags;
+
+	spin_lock_irqsave(&q_clkdiv->lock, flags);
+
+	rate = q_clkdiv->cxo_hz / div_factor_to_div(q_clkdiv->div_factor);
+
+	spin_unlock_irqrestore(&q_clkdiv->lock, flags);

Also confused about spinlock usage here.

Quoted 7 lines
drivers/clk/qcom/clk-qpnp-div.c new file +7 -0
+	return rate;
+}
+
+static int clk_qpnp_div_set_rate(struct clk_hw *hw, unsigned long rate,
+			unsigned long parent_rate)
+{
+	struct q_clkdiv *q_clkdiv = to_clkdiv(hw);

drivers/clk/qcom/clk-qpnp-div.c line 256

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+	unsigned long flags;
+	int rc = 0;
+
+	spin_lock_irqsave(&q_clkdiv->lock, flags);
+	if (rate <= 0 || rate > q_clkdiv->cxo_hz) {
+		dev_err(q_clkdiv->dev, "invalid rate requested, rate = %lu\n",
+			rate);
+		rc = -EINVAL;
+		goto fail;
+	}

Seems useless. Trust the framework won't call this op with a rate that's invalid.

drivers/clk/qcom/clk-qpnp-div.c line 268

Quoted 12 lines
drivers/clk/qcom/clk-qpnp-div.c new file +12 -0
+
+	rc = qpnp_clkdiv_config_freq_div(q_clkdiv, q_clkdiv->cxo_hz / rate);
+	if (rc)
+		dev_err(q_clkdiv->dev, "clkdiv set rate(%lu) failed, rc = %d\n",
+			rate, rc);
+
+fail:
+	spin_unlock_irqrestore(&q_clkdiv->lock, flags);
+	return rc;
+}
+
+const struct clk_ops clk_qpnp_div_ops = {

static?

Quoted 9 lines
drivers/clk/qcom/clk-qpnp-div.c new file +9 -0
+	.enable = clk_qpnp_div_enable,
+	.disable = clk_qpnp_div_disable,
+	.set_rate = clk_qpnp_div_set_rate,
+	.recalc_rate = clk_qpnp_div_recalc_rate,
+	.round_rate = clk_qpnp_div_round_rate,
+};
+
+#define QPNP_CLKDIV_MAX_NAME_LEN	16
+

drivers/clk/qcom/clk-qpnp-div.c line 287

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+static int qpnp_clkdiv_probe(struct platform_device *pdev)
+{
+	struct q_clkdiv *q_clkdiv;
+	struct device *dev = &pdev->dev;
+	struct device_node *of_node = dev->of_node;
+	struct clk_init_data *init;
+	struct clk_onecell_data *clk_data;
+	struct clk *clk;
+	unsigned int base, div, init_freq;
+	int rc = 0, id;

Leave rc unassigned.

Quoted 46 lines
drivers/clk/qcom/clk-qpnp-div.c new file +46 -0
+	char *clk_name;
+
+	q_clkdiv = devm_kzalloc(dev, sizeof(*q_clkdiv), GFP_KERNEL);
+	if (!q_clkdiv)
+		return -ENOMEM;
+
+	q_clkdiv->regmap = dev_get_regmap(dev->parent, NULL);
+	if (!q_clkdiv->regmap) {
+		dev_err(dev, "Couldn't get parent's regmap\n");
+		return -EINVAL;
+	}
+	q_clkdiv->dev = dev;
+
+	rc = of_property_read_u32(of_node, "reg", &base);
+	if (rc < 0) {
+		dev_err(dev, "Couldn't find reg in node = %s, rc = %d\n",
+			of_node->full_name, rc);
+		return rc;
+	}
+	q_clkdiv->base = base;
+
+	/* init clock properties */
+	rc = of_property_read_u32(of_node, "qcom,cxo-freq", &q_clkdiv->cxo_hz);
+	if (rc) {
+		dev_err(dev, "unable to get qcom,cxo-freq property, rc = %d\n",
+			rc);
+		return rc;
+	}
+
+	q_clkdiv->div_factor = Q_CLKDIV_XO_DIV_1_0;
+	rc = of_property_read_u32(of_node, "qcom,clkdiv-init-freq", &init_freq);
+	if (rc) {
+		if (rc != -EINVAL) {
+			dev_err(dev, "Unable to read initial frequency value, rc=%d\n",
+				rc);
+			return rc;
+		}
+	} else {
+		if (init_freq <= 0 || init_freq > q_clkdiv->cxo_hz) {
+			dev_err(dev, "invalid initial frequency specified, rate = %u\n",
+				init_freq);
+			return -EINVAL;
+		}
+
+		div = DIV_ROUND_UP(q_clkdiv->cxo_hz, init_freq);
+		q_clkdiv->div_factor = div_to_div_factor(div);

drivers/clk/qcom/clk-qpnp-div.c line 343

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+		if (q_clkdiv->div_factor >= Q_CLKDIV_MAX_ALLOWED)
+			q_clkdiv->div_factor = Q_CLKDIV_MAX_ALLOWED - 1;
+		rc = qpnp_clkdiv_config_freq_div(q_clkdiv,
+				div_factor_to_div(q_clkdiv->div_factor));
+		if (rc) {
+			dev_err(dev, "Config initial frequency failed, rc = %d\n",
+				rc);
+			return rc;
+		}
+	}

Hopefully a bunch of this code goes away.

Quoted 6 lines
drivers/clk/qcom/clk-qpnp-div.c new file +6 -0
+
+	init = devm_kzalloc(dev, sizeof(*init), GFP_KERNEL);
+	if (!init)
+		return -ENOMEM;
+
+	rc = of_property_read_u32(of_node, "qcom,clkdiv-id", &id);

drivers/clk/qcom/clk-qpnp-div.c line 359

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+	if (rc) {
+		dev_err(dev, "Unable to read clkdiv node id, rc = %d\n", rc);
+		return rc;
+	}
+
+	clk_name = devm_kcalloc(dev, QPNP_CLKDIV_MAX_NAME_LEN,
+				sizeof(*clk_name), GFP_KERNEL);
+	if (!clk_name)
+		return -ENOMEM;
+	snprintf(clk_name, QPNP_CLKDIV_MAX_NAME_LEN, "qpnp_clkdiv_%d", id);

devm_kasprintf? Also make sure to free the name after registration because we copy it over in the framework.

Quoted 14 lines
drivers/clk/qcom/clk-qpnp-div.c new file +14 -0
+
+	init->name = clk_name;
+	init->ops = &clk_qpnp_div_ops;
+	q_clkdiv->hw.init = init;
+	spin_lock_init(&q_clkdiv->lock);
+
+	clk_data = devm_kzalloc(dev, sizeof(*clk_data), GFP_KERNEL);
+	if (!clk_data)
+		return -ENOMEM;
+
+	clk_data->clks = devm_kzalloc(dev, sizeof(*clk_data->clks), GFP_KERNEL);
+	if (!clk_data->clks)
+		return -ENOMEM;
+

drivers/clk/qcom/clk-qpnp-div.c line 383

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+	clk = devm_clk_register(dev, &q_clkdiv->hw);
+	if (IS_ERR(clk)) {
+		dev_err(dev, "Unable to register qpnp div clock\n");
+		return PTR_ERR(clk);
+	}
+
+	clk_data->clk_num = 1;
+	clk_data->clks[0] = clk;
+
+	rc = of_clk_add_provider(of_node, of_clk_src_onecell_get, clk_data);

Please use the clk_hw_register() and of_clk_add_hw_provider() APIs. Also if we have only one clk it should be of_clk_hw_simple_get() and have no cells in the binding. But, if there are multiple of these clks, then it will be onecell.

drivers/clk/qcom/clk-qpnp-div.c line 390

Quoted 7 lines
drivers/clk/qcom/clk-qpnp-div.c new file +7 -0
+	if (rc) {
+		dev_err(dev, "Unable to register qpnp div clock provider, rc = %d\n",
+			rc);
+		return rc;
+	}
+
+	dev_set_drvdata(dev, q_clkdiv);

Unused? Remove?

drivers/clk/qcom/clk-qpnp-div.c line 391

Quoted 1 line
drivers/clk/qcom/clk-qpnp-div.c new file +1 -0
+	dev_info(dev, "Registered %s successfully\n", clk_name);

No noise please.

drivers/clk/qcom/clk-qpnp-div.c line 399

Quoted 8 lines
drivers/clk/qcom/clk-qpnp-div.c new file +8 -0
+
+	return rc;
+}
+
+static const struct of_device_id qpnp_clkdiv_match_table[] = {
+	{ .compatible = "qcom,qpnp-clkdiv" },
+	{}
+};

Add a MODULE_DEVICE_TABLE() please.

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+
+static struct platform_driver qpnp_clkdiv_driver = {
+	.driver		= {
+		.name	= "qcom,qpnp-clkdiv",
+		.of_match_table = qpnp_clkdiv_match_table,
+	},
+	.probe		= qpnp_clkdiv_probe,
+};
+
+static int __init qpnp_clkdiv_init(void)

drivers/clk/qcom/clk-qpnp-div.c line 419

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+{
+	return platform_driver_register(&qpnp_clkdiv_driver);
+}
+module_init(qpnp_clkdiv_init);
+
+static void __exit qpnp_clkdiv_exit(void)
+{
+	return platform_driver_unregister(&qpnp_clkdiv_driver);
+}
+module_exit(qpnp_clkdiv_exit);

Use module_platform_driver() macro.

Signature · 6 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Co
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw

Re: [PATCH V1] clk: qcom: Add qpnp clock divider support

Quoted 27 lines · Stephen Boyd
Quoted 26 lines · Tirupathi Reddy
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +20 -0
new file mode 100644
index 0000000..03b7b70
@@ -0,0 +1,52 @@
+Qualcomm Technologies, Inc. QPNP clock divider (clkdiv)
+
+clkdiv configures the clock frequency of a set of outputs on the PMIC.
+These clocks are typically wired through alternate functions on
+gpio pins.
+
+=======================
+Supported Properties
+=======================
+
+- compatible
+	Usage:      required
+	Value type: <string>
+	Definition: should be "qcom,qpnp-clkdiv".
+
+- reg
+	Usage:      required
+	Value type: <prop-encoded-array>
+	Definition: Addresses and sizes for the memory of this CLKDIV
+		    peripheral.
Quoted 8 lines

Documentation/devicetree/bindings/clock/clk-qpnp-div.txt line 25

Quoted 5 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +5 -0
+
+- qcom,cxo-freq
+	Usage:      required
+	Value type: <u32>
+	Definition: The frequency of the crystal oscillator (CXO) clock in Hz.

CXO should be a parent clk then. This could have clocks and clock-names properties for that and then be hooked up to xo_board.

Sure. I will address in the next patch set.

Quoted 4 lines
Quoted 4 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +4 -0
+
+- qcom,clkdiv-id
+	Usage:      required
+	Value type: <u32>
Quoted 8 lines

Documentation/devicetree/bindings/clock/clk-qpnp-div.txt line 31

Quoted 2 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +2 -0
+	Definition: Integer value specifies the hardware identifier of this
+		    CLKDIV peripheral.

This is to name the clk? You could use clock-output-names as that's more standard. But this is also sort of confusing. If there are multiple clkdivs then it would be good to combine them into one device node assuming they're all next to each other. This would be similar to how we handle gpios and regulators. Then the naming is simple enough to be an incrementing number.

Yes, multiple clkdivs present. We add sub-nodes for each clkdiv and parse them inside driver as separate clock.

Quoted 9 lines

Documentation/devicetree/bindings/clock/clk-qpnp-div.txt line 39

Quoted 8 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +8 -0
+
+- qcom,clkdiv-init-freq
+	Usage:      optional
+	Value type: <u32>
+	Definition: Initial output frequency in Hz to configure for the CLKDIV
+		    peripheral. The initial frequency value should be less than
+		    or equal to CXO clock frequency and greater than or equal to
+		    CXO_freq/64.

Use assigned-clock-rates instead.

Sure. I will address in the next patch set.

Quoted 18 lines
Quoted 18 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +13 -0
+
+=======
+Example
+=======
+
+qcom,clkdiv@5b00 {
+	compatible = "qcom,qpnp-clkdiv";
+	reg = <0x5b00 0x100>;
+
+	qcom,cxo-freq = <19200000>;
+	qcom,clkdiv-id = <1>;
+	qcom,clkdiv-init-freq = <9600000>;
+};
drivers/clk/qcom/Kconfig +0 -0
index 9f6c278..c68ae96 100644
@@ -196,3 +196,12 @@ config MSM_MMCC_8996
Quoted 8 lines

drivers/clk/qcom/Kconfig config MSM_MMCC_8996 · line 202

Quoted 7 lines
drivers/clk/qcom/Kconfig +4 -0
  	  Support for the multimedia clock controller on msm8996 devices.
  	  Say Y if you want to support multimedia devices such as display,
  	  graphics, video encode/decode, camera, etc.
+
+config CLOCK_QPNP_DIV
+	tristate "QPNP PMIC clkdiv driver"
+	depends on COMMON_CLK_QCOM && SPMI

COMPILE_TEST?

Sure. I will address in the next patch set.

Quoted 7 lines

drivers/clk/qcom/Kconfig config MSM_MMCC_8996 · line 205

Quoted 4 lines
drivers/clk/qcom/Kconfig +3 -0
+	help
+	  This driver supports the clkdiv functionality on the Qualcomm
+	  Technologies, Inc. QPNP PMIC. It configures the frequency of

I'm not sure we ever really call it QPNP PMIC. I see one hit from grep for the thermal driver. Perhaps SPMI PMIC is more appropriate.

Sure. I will address in the next patch set.

Quoted 24 lines
Quoted 24 lines
drivers/clk/qcom/Kconfig +2 -0
+	  clkdiv outputs on the PMIC. These clocks are typically wired
+	  through alternate functions on gpio pins.
drivers/clk/qcom/clk-qpnp-div.c new file +15 -0
new file mode 100644
index 0000000..416c20f
@@ -0,0 +1,422 @@
+/* Copyright (c) 2017, The Linux Foundation. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 and
+ * only version 2 as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <linux/clk.h>
+#include <linux/clk-provider.h>
+#include <linux/delay.h>
Quoted 8 lines

drivers/clk/qcom/clk-qpnp-div.c line 22

Quoted 7 lines
drivers/clk/qcom/clk-qpnp-div.c new file +7 -0
+#include <linux/err.h>
+#include <linux/log2.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/platform_device.h>
+#include <linux/regmap.h>
+#include <linux/types.h>

bitops.h?

Sure. I will add it in the next patch set.

Quoted 21 lines
Quoted 21 lines
drivers/clk/qcom/clk-qpnp-div.c new file +20 -0
+
+#define Q_REG_DIV_CTL1			0x43
+#define Q_DIV_CTL1_DIV_FACTOR_MASK	GENMASK(2, 0)
+
+#define Q_REG_EN_CTL			0x46
+#define Q_REG_EN_MASK			BIT(7)
+#define Q_SET_EN			BIT(7)
+
+#define Q_CXO_PERIOD_NS(cxo_clk)	(NSEC_PER_SEC / cxo_clk)
+#define Q_DIV_PERIOD_NS(cxo_clk, div)	(NSEC_PER_SEC / (cxo_clk / div))
+#define Q_ENABLE_DELAY_NS(cxo_clk, div)	(2 * Q_CXO_PERIOD_NS(cxo_clk) + \
+					(3 * Q_DIV_PERIOD_NS(cxo_clk, div)))
+#define Q_DISABLE_DELAY_NS(cxo_clk, div) \
+					(3 * Q_DIV_PERIOD_NS(cxo_clk, div))
+
+#define CLK_QPNP_DIV_OFFSET		1
+
+enum q_clk_div_factor {
+	Q_CLKDIV_XO_DIV_1_0 = 0,
+	Q_CLKDIV_XO_DIV_1,
Quoted 8 lines

drivers/clk/qcom/clk-qpnp-div.c line 49

Quoted 7 lines
drivers/clk/qcom/clk-qpnp-div.c new file +7 -0
+	Q_CLKDIV_XO_DIV_2,
+	Q_CLKDIV_XO_DIV_4,
+	Q_CLKDIV_XO_DIV_8,
+	Q_CLKDIV_XO_DIV_16,
+	Q_CLKDIV_XO_DIV_32,
+	Q_CLKDIV_XO_DIV_64,
+	Q_CLKDIV_MAX_ALLOWED,

Please make #defines for these instead of enum.

We used enum primarily for easy debug at runtime. For an example, The "div_factor" field of "struct q_clkdiv" would always show the enumerated value which would help in determining the configured absolute divider value rather than spending time in mapping register bit values to absolute divider value.

Quoted 8 lines

drivers/clk/qcom/clk-qpnp-div.c line 55

Quoted 7 lines
drivers/clk/qcom/clk-qpnp-div.c new file +6 -0
+};
+
+enum q_clkdiv_state {
+	DISABLE = false,
+	ENABLE = true,
+};

Uh no. Just use bool.

Sure. I will address in the next patch set.

Quoted 13 lines

drivers/clk/qcom/clk-qpnp-div.c line 66

Quoted 12 lines
drivers/clk/qcom/clk-qpnp-div.c new file +11 -0
+
+struct q_clkdiv {
+	struct regmap		*regmap;
+	struct device		*dev;
+
+	u16			base;
+	spinlock_t		lock;
+
+	/* clock properties */
+	struct clk_hw		hw;
+	unsigned int		cxo_hz;

Drop.

cxo_hz is required to be populated at driver initialization and being used at runtime for calculating the div value to be applied.

Quoted 4 lines

drivers/clk/qcom/clk-qpnp-div.c line 68

Quoted 3 lines
drivers/clk/qcom/clk-qpnp-div.c new file +2 -0
+	enum q_clk_div_factor	div_factor;
+	bool			enabled;

Shouldn't be needed. Read the hardware instead.

This enabled field is required for the typical handling of set_rate in qpnp_clkdiv_config_freq_div(). **

Quoted 5 lines

drivers/clk/qcom/clk-qpnp-div.c line 71

Quoted 4 lines
drivers/clk/qcom/clk-qpnp-div.c new file +3 -0
+};
+
+static inline struct q_clkdiv *to_clkdiv(struct clk_hw *_hw)

_hw can just be hw?

Sure. I will address it in next patch set.

Quoted 14 lines

drivers/clk/qcom/clk-qpnp-div.c line 82

Quoted 12 lines
drivers/clk/qcom/clk-qpnp-div.c new file +11 -0
+{
+	return container_of(_hw, struct q_clkdiv, hw);
+}
+
+static inline unsigned int div_factor_to_div(unsigned int div_factor)
+{
+	if (div_factor == Q_CLKDIV_XO_DIV_1_0)
+		return 1;
+
+	return 1 << (div_factor - CLK_QPNP_DIV_OFFSET);
+}

Not sure, but it may be possible to reuse the code in clk-divider.c and treat this as a CLK_DIVIDER_POWER_OF_TWO?

Sure. I will check more on this.

Quoted 10 lines

drivers/clk/qcom/clk-qpnp-div.c line 89

Quoted 8 lines
drivers/clk/qcom/clk-qpnp-div.c new file +7 -0
+
+static inline unsigned int div_to_div_factor(unsigned int div)
+{
+	return ilog2(div) + CLK_QPNP_DIV_OFFSET;
+}
+
+static int qpnp_clkdiv_masked_write(struct q_clkdiv *q_clkdiv, u16 offset,

Please replace instances of qpnp with spmi_pmic throughout this driver.

Sure. I will address it in next patch set.

Quoted 2 lines

Also, why do we need a wrapper around regmap APIs? Please just use the APIs directly.

Sure. I will address it in next patch set.

Quoted 22 lines
Quoted 22 lines
drivers/clk/qcom/clk-qpnp-div.c new file +22 -0
+			u8 mask, u8 val)
+{
+	int rc;
+
+	rc = regmap_update_bits(q_clkdiv->regmap, q_clkdiv->base + offset, mask,
+				val);
+	if (rc)
+		dev_err(q_clkdiv->dev,
+			"Unable to regmap_update_bits to addr=%hx, rc(%d)\n",
+			q_clkdiv->base + offset, rc);
+
+	return rc;
+}
+
+static int qpnp_clkdiv_set_enable_state(struct q_clkdiv *q_clkdiv,
+			enum q_clkdiv_state enable_state)
+{
+	int rc;
+
+	rc = qpnp_clkdiv_masked_write(q_clkdiv, Q_REG_EN_CTL, Q_REG_EN_MASK,
+				(enable_state == ENABLE) ? Q_SET_EN : 0);
+	if (rc)
Quoted 8 lines

drivers/clk/qcom/clk-qpnp-div.c line 116

Quoted 5 lines
drivers/clk/qcom/clk-qpnp-div.c new file +5 -0
+		return rc;
+
+	if (enable_state == ENABLE)
+		ndelay(Q_ENABLE_DELAY_NS(q_clkdiv->cxo_hz,
+				div_factor_to_div(q_clkdiv->div_factor)));

Can this factor can be precalculated at probe time based on XO rate? And then we just multiply that factor with the divider value to figure out how long to wait?

Sure. I will check more on this.

Quoted 47 lines
Quoted 47 lines
drivers/clk/qcom/clk-qpnp-div.c new file +46 -0
+	else
+		ndelay(Q_DISABLE_DELAY_NS(q_clkdiv->cxo_hz,
+				div_factor_to_div(q_clkdiv->div_factor)));
+
+	return rc;
+}
+
+static int qpnp_clkdiv_config_freq_div(struct q_clkdiv *q_clkdiv,
+			unsigned int div)
+{
+	unsigned int div_factor;
+	int rc;
+
+	div_factor = div_to_div_factor(div);
+	if (div_factor <= 0 || div_factor >= Q_CLKDIV_MAX_ALLOWED)
+		return -EINVAL;
+
+	if (q_clkdiv->enabled) {
+		rc = qpnp_clkdiv_set_enable_state(q_clkdiv, DISABLE);
+		if (rc) {
+			dev_err(q_clkdiv->dev, "unable to disable clock, rc = %d\n",
+				rc);
+			return rc;
+		}
+	}
+
+	rc = qpnp_clkdiv_masked_write(q_clkdiv, Q_REG_DIV_CTL1,
+				Q_DIV_CTL1_DIV_FACTOR_MASK, div_factor);
+	if (rc) {
+		dev_err(q_clkdiv->dev, "config divider failed, rc=%d\n",
+			rc);
+		return rc;
+	}
+
+	q_clkdiv->div_factor = div_factor;
+
+	if (q_clkdiv->enabled) {
+		rc = qpnp_clkdiv_set_enable_state(q_clkdiv, ENABLE);
+		if (rc)
+			dev_err(q_clkdiv->dev, "unable to re-enable clock, rc = %d\n",
+				rc);
+	}
+
+	return rc;
+}
+
Quoted 8 lines

drivers/clk/qcom/clk-qpnp-div.c line 169

Quoted 7 lines
drivers/clk/qcom/clk-qpnp-div.c new file +7 -0
+static int clk_qpnp_div_enable(struct clk_hw *hw)
+{
+	struct q_clkdiv *q_clkdiv = to_clkdiv(hw);
+	unsigned long flags;
+	int rc;
+
+	spin_lock_irqsave(&q_clkdiv->lock, flags);

What is this locking against? Rate change?

Yes, locking against "q_clkdiv->enabled".

Quoted 39 lines
Quoted 39 lines
drivers/clk/qcom/clk-qpnp-div.c new file +38 -0
+
+	rc =  qpnp_clkdiv_set_enable_state(q_clkdiv, ENABLE);
+	if (rc) {
+		dev_err(q_clkdiv->dev, "clk enable failed, rc=%d\n", rc);
+		goto fail;
+	}
+
+	q_clkdiv->enabled = true;
+
+fail:
+	spin_unlock_irqrestore(&q_clkdiv->lock, flags);
+	return rc;
+}
+
+static void clk_qpnp_div_disable(struct clk_hw *hw)
+{
+	struct q_clkdiv *q_clkdiv = to_clkdiv(hw);
+	unsigned long flags;
+	int rc;
+
+	spin_lock_irqsave(&q_clkdiv->lock, flags);
+
+	rc = qpnp_clkdiv_set_enable_state(q_clkdiv, DISABLE);
+	if (rc) {
+		dev_err(q_clkdiv->dev, "clk disable failed, rc=%d\n", rc);
+		goto fail;
+	}
+
+	q_clkdiv->enabled = false;
+
+fail:
+	spin_unlock_irqrestore(&q_clkdiv->lock, flags);
+}
+
+static long clk_qpnp_div_round_rate(struct clk_hw *hw, unsigned long rate,
+			unsigned long *parent_rate)
+{
+	struct q_clkdiv *q_clkdiv = to_clkdiv(hw);
Quoted 8 lines

drivers/clk/qcom/clk-qpnp-div.c line 212

Quoted 5 lines
drivers/clk/qcom/clk-qpnp-div.c new file +5 -0
+	unsigned long flags, new_rate;
+	unsigned int div, div_factor;
+
+	spin_lock_irqsave(&q_clkdiv->lock, flags);
+	if (rate <= 0 || rate > q_clkdiv->cxo_hz) {

How can rate be less than zero? It's unsigned! And if it's greater than some parent rate, round_rate() should return the largest rate it can support (I guess parent_rate?)

Sure. I will address it in next patch set.

Quoted 5 lines

drivers/clk/qcom/clk-qpnp-div.c line 215

Quoted 4 lines
drivers/clk/qcom/clk-qpnp-div.c new file +3 -0
+		dev_err(q_clkdiv->dev, "invalid rate requested, rate = %lu\n",
+			rate);
+		spin_unlock_irqrestore(&q_clkdiv->lock, flags);

Also what are we spinlocking for?

Seems locking is not required here. I will address it in next patch set.

Quoted 12 lines

drivers/clk/qcom/clk-qpnp-div.c line 225

Quoted 11 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+		return -EINVAL;
+	}
+
+	div = DIV_ROUND_UP(q_clkdiv->cxo_hz, rate);
+	div_factor = div_to_div_factor(div);
+	if (div_factor >= Q_CLKDIV_MAX_ALLOWED)
+		div_factor = Q_CLKDIV_MAX_ALLOWED - 1;
+	new_rate = q_clkdiv->cxo_hz / div_factor_to_div(div_factor);
+
+	spin_unlock_irqrestore(&q_clkdiv->lock, flags);

Shouldn't need any spinlock here...

Seems locking is not required here. I will address it in next patch set.

Quoted 16 lines
Quoted 5 lines
drivers/clk/qcom/clk-qpnp-div.c new file +4 -0
+	return new_rate;
+}
+
+static unsigned long clk_qpnp_div_recalc_rate(struct clk_hw *hw,

drivers/clk/qcom/clk-qpnp-div.c line 239

Quoted 10 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+			unsigned long parent_rate)
+{
+	struct q_clkdiv *q_clkdiv = to_clkdiv(hw);
+	unsigned long rate, flags;
+
+	spin_lock_irqsave(&q_clkdiv->lock, flags);
+
+	rate = q_clkdiv->cxo_hz / div_factor_to_div(q_clkdiv->div_factor);
+
+	spin_unlock_irqrestore(&q_clkdiv->lock, flags);

Also confused about spinlock usage here.

Seems locking is not required here. I will address it in next patch set

Quoted 12 lines
Quoted 12 lines
drivers/clk/qcom/clk-qpnp-div.c new file +11 -0
+	return rate;
+}
+
+static int clk_qpnp_div_set_rate(struct clk_hw *hw, unsigned long rate,
+			unsigned long parent_rate)
+{
+	struct q_clkdiv *q_clkdiv = to_clkdiv(hw);
+	unsigned long flags;
+	int rc = 0;
+
+	spin_lock_irqsave(&q_clkdiv->lock, flags);
Quoted 8 lines

drivers/clk/qcom/clk-qpnp-div.c line 256

Quoted 6 lines
drivers/clk/qcom/clk-qpnp-div.c new file +6 -0
+	if (rate <= 0 || rate > q_clkdiv->cxo_hz) {
+		dev_err(q_clkdiv->dev, "invalid rate requested, rate = %lu\n",
+			rate);
+		rc = -EINVAL;
+		goto fail;
+	}

Seems useless. Trust the framework won't call this op with a rate that's invalid.

I will check more on this and address in next patch set.

Quoted 14 lines

drivers/clk/qcom/clk-qpnp-div.c line 268

Quoted 13 lines
drivers/clk/qcom/clk-qpnp-div.c new file +12 -0
+
+	rc = qpnp_clkdiv_config_freq_div(q_clkdiv, q_clkdiv->cxo_hz / rate);
+	if (rc)
+		dev_err(q_clkdiv->dev, "clkdiv set rate(%lu) failed, rc = %d\n",
+			rate, rc);
+
+fail:
+	spin_unlock_irqrestore(&q_clkdiv->lock, flags);
+	return rc;
+}
+
+const struct clk_ops clk_qpnp_div_ops = {

static?

Sure. I will address it in next patch set.

Quoted 13 lines
Quoted 13 lines
drivers/clk/qcom/clk-qpnp-div.c new file +12 -0
+	.enable = clk_qpnp_div_enable,
+	.disable = clk_qpnp_div_disable,
+	.set_rate = clk_qpnp_div_set_rate,
+	.recalc_rate = clk_qpnp_div_recalc_rate,
+	.round_rate = clk_qpnp_div_round_rate,
+};
+
+#define QPNP_CLKDIV_MAX_NAME_LEN	16
+
+static int qpnp_clkdiv_probe(struct platform_device *pdev)
+{
+	struct q_clkdiv *q_clkdiv;
Quoted 8 lines

drivers/clk/qcom/clk-qpnp-div.c line 287

Quoted 7 lines
drivers/clk/qcom/clk-qpnp-div.c new file +7 -0
+	struct device *dev = &pdev->dev;
+	struct device_node *of_node = dev->of_node;
+	struct clk_init_data *init;
+	struct clk_onecell_data *clk_data;
+	struct clk *clk;
+	unsigned int base, div, init_freq;
+	int rc = 0, id;

Leave rc unassigned.

Sure. I will address it in next patch set

Quoted 50 lines
Quoted 50 lines
drivers/clk/qcom/clk-qpnp-div.c new file +49 -0
+	char *clk_name;
+
+	q_clkdiv = devm_kzalloc(dev, sizeof(*q_clkdiv), GFP_KERNEL);
+	if (!q_clkdiv)
+		return -ENOMEM;
+
+	q_clkdiv->regmap = dev_get_regmap(dev->parent, NULL);
+	if (!q_clkdiv->regmap) {
+		dev_err(dev, "Couldn't get parent's regmap\n");
+		return -EINVAL;
+	}
+	q_clkdiv->dev = dev;
+
+	rc = of_property_read_u32(of_node, "reg", &base);
+	if (rc < 0) {
+		dev_err(dev, "Couldn't find reg in node = %s, rc = %d\n",
+			of_node->full_name, rc);
+		return rc;
+	}
+	q_clkdiv->base = base;
+
+	/* init clock properties */
+	rc = of_property_read_u32(of_node, "qcom,cxo-freq", &q_clkdiv->cxo_hz);
+	if (rc) {
+		dev_err(dev, "unable to get qcom,cxo-freq property, rc = %d\n",
+			rc);
+		return rc;
+	}
+
+	q_clkdiv->div_factor = Q_CLKDIV_XO_DIV_1_0;
+	rc = of_property_read_u32(of_node, "qcom,clkdiv-init-freq", &init_freq);
+	if (rc) {
+		if (rc != -EINVAL) {
+			dev_err(dev, "Unable to read initial frequency value, rc=%d\n",
+				rc);
+			return rc;
+		}
+	} else {
+		if (init_freq <= 0 || init_freq > q_clkdiv->cxo_hz) {
+			dev_err(dev, "invalid initial frequency specified, rate = %u\n",
+				init_freq);
+			return -EINVAL;
+		}
+
+		div = DIV_ROUND_UP(q_clkdiv->cxo_hz, init_freq);
+		q_clkdiv->div_factor = div_to_div_factor(div);
+		if (q_clkdiv->div_factor >= Q_CLKDIV_MAX_ALLOWED)
+			q_clkdiv->div_factor = Q_CLKDIV_MAX_ALLOWED - 1;
+		rc = qpnp_clkdiv_config_freq_div(q_clkdiv,
Quoted 8 lines

drivers/clk/qcom/clk-qpnp-div.c line 343

Quoted 7 lines
drivers/clk/qcom/clk-qpnp-div.c new file +7 -0
+				div_factor_to_div(q_clkdiv->div_factor));
+		if (rc) {
+			dev_err(dev, "Config initial frequency failed, rc = %d\n",
+				rc);
+			return rc;
+		}
+	}

Hopefully a bunch of this code goes away.

I will check more on this and address in next patch set.

Quoted 11 lines
Quoted 11 lines
drivers/clk/qcom/clk-qpnp-div.c new file +10 -0
+
+	init = devm_kzalloc(dev, sizeof(*init), GFP_KERNEL);
+	if (!init)
+		return -ENOMEM;
+
+	rc = of_property_read_u32(of_node, "qcom,clkdiv-id", &id);
+	if (rc) {
+		dev_err(dev, "Unable to read clkdiv node id, rc = %d\n", rc);
+		return rc;
+	}
Quoted 8 lines

drivers/clk/qcom/clk-qpnp-div.c line 359

Quoted 6 lines
drivers/clk/qcom/clk-qpnp-div.c new file +6 -0
+
+	clk_name = devm_kcalloc(dev, QPNP_CLKDIV_MAX_NAME_LEN,
+				sizeof(*clk_name), GFP_KERNEL);
+	if (!clk_name)
+		return -ENOMEM;
+	snprintf(clk_name, QPNP_CLKDIV_MAX_NAME_LEN, "qpnp_clkdiv_%d", id);

devm_kasprintf? Also make sure to free the name after registration because we copy it over in the framework.

Sure. I will address it in next patch set

Quoted 21 lines
Quoted 21 lines
drivers/clk/qcom/clk-qpnp-div.c new file +20 -0
+
+	init->name = clk_name;
+	init->ops = &clk_qpnp_div_ops;
+	q_clkdiv->hw.init = init;
+	spin_lock_init(&q_clkdiv->lock);
+
+	clk_data = devm_kzalloc(dev, sizeof(*clk_data), GFP_KERNEL);
+	if (!clk_data)
+		return -ENOMEM;
+
+	clk_data->clks = devm_kzalloc(dev, sizeof(*clk_data->clks), GFP_KERNEL);
+	if (!clk_data->clks)
+		return -ENOMEM;
+
+	clk = devm_clk_register(dev, &q_clkdiv->hw);
+	if (IS_ERR(clk)) {
+		dev_err(dev, "Unable to register qpnp div clock\n");
+		return PTR_ERR(clk);
+	}
+
Quoted 8 lines

drivers/clk/qcom/clk-qpnp-div.c line 383

Quoted 4 lines
drivers/clk/qcom/clk-qpnp-div.c new file +4 -0
+	clk_data->clk_num = 1;
+	clk_data->clks[0] = clk;
+
+	rc = of_clk_add_provider(of_node, of_clk_src_onecell_get, clk_data);

Please use the clk_hw_register() and of_clk_add_hw_provider() APIs. Also if we have only one clk it should be of_clk_hw_simple_get() and have no cells in the binding. But, if there are multiple of these clks, then it will be onecell.

Sure. I will address it in next patch set

Quoted 9 lines

drivers/clk/qcom/clk-qpnp-div.c line 390

Quoted 8 lines
drivers/clk/qcom/clk-qpnp-div.c new file +7 -0
+	if (rc) {
+		dev_err(dev, "Unable to register qpnp div clock provider, rc = %d\n",
+			rc);
+		return rc;
+	}
+
+	dev_set_drvdata(dev, q_clkdiv);

Unused? Remove?

Sure. I will address it in next patch set

Quoted 3 lines

drivers/clk/qcom/clk-qpnp-div.c line 391

Quoted 2 lines
drivers/clk/qcom/clk-qpnp-div.c new file +1 -0
+	dev_info(dev, "Registered %s successfully\n", clk_name);

No noise please.

Sure. I will address it in next patch set

Quoted 10 lines

drivers/clk/qcom/clk-qpnp-div.c line 399

Quoted 9 lines
drivers/clk/qcom/clk-qpnp-div.c new file +8 -0
+
+	return rc;
+}
+
+static const struct of_device_id qpnp_clkdiv_match_table[] = {
+	{ .compatible = "qcom,qpnp-clkdiv" },
+	{}
+};

Add a MODULE_DEVICE_TABLE() please.

Sure. I will address it in next patch set

Quoted 14 lines
Quoted 14 lines
drivers/clk/qcom/clk-qpnp-div.c new file +13 -0
+
+static struct platform_driver qpnp_clkdiv_driver = {
+	.driver		= {
+		.name	= "qcom,qpnp-clkdiv",
+		.of_match_table = qpnp_clkdiv_match_table,
+	},
+	.probe		= qpnp_clkdiv_probe,
+};
+
+static int __init qpnp_clkdiv_init(void)
+{
+	return platform_driver_register(&qpnp_clkdiv_driver);
+}
Quoted 8 lines

drivers/clk/qcom/clk-qpnp-div.c line 419

Quoted 7 lines
drivers/clk/qcom/clk-qpnp-div.c new file +7 -0
+module_init(qpnp_clkdiv_init);
+
+static void __exit qpnp_clkdiv_exit(void)
+{
+	return platform_driver_unregister(&qpnp_clkdiv_driver);
+}
+module_exit(qpnp_clkdiv_exit);

Use module_platform_driver() macro.

Sure. I will address it in next patch set

Quoted 1 line
#4 lore raw

Re: [PATCH V1] clk: qcom: Add qpnp clock divider support

Quoted 50 lines · Tirupathi Reddy T
Quoted 27 lines · Stephen Boyd
Quoted 26 lines · Tirupathi Reddy
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +20 -0
new file mode 100644
index 0000000..03b7b70
@@ -0,0 +1,52 @@
+Qualcomm Technologies, Inc. QPNP clock divider (clkdiv)
+
+clkdiv configures the clock frequency of a set of outputs on the PMIC.
+These clocks are typically wired through alternate functions on
+gpio pins.
+
+=======================
+Supported Properties
+=======================
+
+- compatible
+	Usage:      required
+	Value type: <string>
+	Definition: should be "qcom,qpnp-clkdiv".
+
+- reg
+	Usage:      required
+	Value type: <prop-encoded-array>
+	Definition: Addresses and sizes for the memory of this CLKDIV
+		    peripheral.
Quoted 8 lines

Documentation/devicetree/bindings/clock/clk-qpnp-div.txt line 25

Quoted 5 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +5 -0
+
+- qcom,cxo-freq
+	Usage:      required
+	Value type: <u32>
+	Definition: The frequency of the crystal oscillator (CXO) clock in Hz.

CXO should be a parent clk then. This could have clocks and clock-names properties for that and then be hooked up to xo_board.

Sure. I will address in the next patch set.

Quoted 4 lines
Quoted 4 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +4 -0
+
+- qcom,clkdiv-id
+	Usage:      required
+	Value type: <u32>
Quoted 8 lines

Documentation/devicetree/bindings/clock/clk-qpnp-div.txt line 31

Quoted 2 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +2 -0
+	Definition: Integer value specifies the hardware identifier of this
+		    CLKDIV peripheral.

This is to name the clk? You could use clock-output-names as that's more standard. But this is also sort of confusing. If there are multiple clkdivs then it would be good to combine them into one device node assuming they're all next to each other. This would be similar to how we handle gpios and regulators. Then the naming is simple enough to be an incrementing number.

Quoted 2 lines

Yes, multiple clkdivs present. We add sub-nodes for each clkdiv and parse them inside driver as separate clock.

Please just roll them all into one node instead of a node per clk on the PMIC.

Quoted 19 lines
Quoted 11 lines
Quoted 11 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +11 -0
+#define Q_DIV_PERIOD_NS(cxo_clk, div)	(NSEC_PER_SEC / (cxo_clk / div))
+#define Q_ENABLE_DELAY_NS(cxo_clk, div)	(2 * Q_CXO_PERIOD_NS(cxo_clk) + \
+					(3 * Q_DIV_PERIOD_NS(cxo_clk, div)))
+#define Q_DISABLE_DELAY_NS(cxo_clk, div) \
+					(3 * Q_DIV_PERIOD_NS(cxo_clk, div))
+
+#define CLK_QPNP_DIV_OFFSET		1
+
+enum q_clk_div_factor {
+	Q_CLKDIV_XO_DIV_1_0 = 0,
+	Q_CLKDIV_XO_DIV_1,
Quoted 8 lines

Documentation/devicetree/bindings/clock/clk-qpnp-div.txt line 49

Quoted 7 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +7 -0
+	Q_CLKDIV_XO_DIV_2,
+	Q_CLKDIV_XO_DIV_4,
+	Q_CLKDIV_XO_DIV_8,
+	Q_CLKDIV_XO_DIV_16,
+	Q_CLKDIV_XO_DIV_32,
+	Q_CLKDIV_XO_DIV_64,
+	Q_CLKDIV_MAX_ALLOWED,

Please make #defines for these instead of enum.

Quoted 7 lines

We used enum primarily for easy debug at runtime. For an example, The "div_factor" field
of "struct q_clkdiv" would always show the enumerated value which would help in
determining the configured absolute divider value rather than spending time in mapping
register bit values to absolute divider value.

Ok. I can't find a good reference, but typically we don't do this in kernel drivers and just use #defines instead. Please just do that. It seems simple enough to know to translate the number to a power of 2 after reading it.

Quoted 26 lines
Quoted 8 lines

Documentation/devicetree/bindings/clock/clk-qpnp-div.txt

Quoted 7 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +6 -0
+};
+
+enum q_clkdiv_state {
+	DISABLE = false,
+	ENABLE = true,
+};

Uh no. Just use bool.

Sure. I will address in the next patch set.

Quoted 13 lines

Documentation/devicetree/bindings/clock/clk-qpnp-div.txt

Quoted 12 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +11 -0
+
+struct q_clkdiv {
+	struct regmap		*regmap;
+	struct device		*dev;
+
+	u16			base;
+	spinlock_t		lock;
+
+	/* clock properties */
+	struct clk_hw		hw;
+	unsigned int		cxo_hz;

Drop.

cxo_hz is required to be populated at driver initialization and being used at runtime
for calculating the div value to be applied.

Quoted 1 line
Quoted 5 lines
Quoted 3 lines

Documentation/devicetree/bindings/clock/clk-qpnp-div.txt

Quoted 2 lines
Documentation/devicetree/bindings/clock/clk-qpnp-div.txt new file +2 -0
+	enum q_clk_div_factor	div_factor;
+	bool			enabled;

Shouldn't be needed. Read the hardware instead.

This enabled field is required for the typical handling of set_rate in qpnp_clkdiv_config_freq_div().

You can't read the hardware in set_rate op?

Signature · 2 lines
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
a Linux Foundation Collaborative Project