Skip to contentSkip to conversation

dt-bindings: add binding for i.MX8MQ CCM

PATCH Superseded Abel Vesa 2 participants 8 messages Sep 20, 2018 – Sep 21, 2018 1 message not in the archive

Subsystems (4)

v7 of 15 Superseded Latest is v15 →

lore
Share & tools
lore

Abel Vesa

author
#1 lore raw

PATCH v7 1/5 dt-bindings: add binding for i.MX8MQ CCM

In reply to a message not in the archive

From: Lucas Stach <[email protected]>

This adds the binding for the i.MX8MQ Clock Controller Module.

---
 .../devicetree/bindings/clock/imx8mq-clock.txt     |  20 ++
 include/dt-bindings/clock/imx8mq-clock.h           | 395 +++++++++++++++++++++
 2 files changed, 415 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/clock/imx8mq-clock.txt
 create mode 100644 include/dt-bindings/clock/imx8mq-clock.h
Documentation/devicetree/bindings/clock/imx8mq-clock.txt new file +20 -0
new file mode 100644
index 0000000..52de826
@@ -0,0 +1,20 @@
+* Clock bindings for NXP i.MX8M Quad
+
+Required properties:
+- compatible: Should be "fsl,imx8mq-ccm"
+- reg: Address and length of the register set
+- #clock-cells: Should be <1>
+- clocks: list of clock specifiers, must contain an entry for each required
+          entry in clock-names
+- clock-names: should include the following entries:
+    - "ckil"
+    - "osc_25m"
+    - "osc_27m"
+    - "clk_ext1"
+    - "clk_ext2"
+    - "clk_ext3"
+    - "clk_ext4"
+
+The clock consumer should specify the desired clock by having the clock
+ID in its "clocks" phandle cell.  See include/dt-bindings/clock/imx8mq-clock.h
+for the full list of i.MX8M Quad clock IDs.
include/dt-bindings/clock/imx8mq-clock.h new file +395 -0

395 lines not on this page · raw message

Signature · 1 line
2.7.4
author
#2 lore raw

PATCH v7 2/5 clk: imx: add fractional PLL output clock

In reply to a message not in the archive

From: Lucas Stach <[email protected]>

This is a new clock type introduced on i.MX8.

---
 drivers/clk/imx/Makefile       |   1 +
 drivers/clk/imx/clk-frac-pll.c | 230 +++++++++++++++++++++++++++++++++++++++++
 drivers/clk/imx/clk.h          |   3 +
 3 files changed, 234 insertions(+)
 create mode 100644 drivers/clk/imx/clk-frac-pll.c
drivers/clk/imx/Makefile +1 -0
index 8c3baa7..4893c1f 100644
@@ -6,6 +6,7 @@ obj-y += \
 	clk-cpu.o \
 	clk-fixup-div.o \
 	clk-fixup-mux.o \
+	clk-frac-pll.o \
 	clk-gate-exclusive.o \
 	clk-gate2.o \
 	clk-pllv1.o \
drivers/clk/imx/clk-frac-pll.c new file +230 -0

230 lines not on this page · raw message

drivers/clk/imx/clk.h +3 -0
index 8076ec0..13daf1c 100644
@@ -27,6 +27,9 @@ struct clk *imx_clk_pllv1(enum imx_pllv1_type type, const char *name,
 struct clk *imx_clk_pllv2(const char *name, const char *parent,
 		void __iomem *base);
 
+struct clk *imx_clk_frac_pll(const char *name, const char *parent_name,
+			     void __iomem *base);
+
 enum imx_pllv3_type {
 	IMX_PLLV3_GENERIC,
 	IMX_PLLV3_SYS,
Signature · 1 line
2.7.4
author
#4 lore raw

PATCH v7 3/5 clk: imx: add SCCG PLL type

In reply to a message not in the archive

From: Lucas Stach <[email protected]>

The SCCG is a new PLL type introduced on i.MX8. Add support for this. The driver currently misses the PLL lock check, as the preliminary documentation mentions lock configurations, but is quiet about where to find the actual lock status signal.

---
 drivers/clk/imx/Makefile       |   3 +-
 drivers/clk/imx/clk-sccg-pll.c | 246 +++++++++++++++++++++++++++++++++++++++++
 drivers/clk/imx/clk.h          |   9 ++
 3 files changed, 257 insertions(+), 1 deletion(-)
 create mode 100644 drivers/clk/imx/clk-sccg-pll.c
drivers/clk/imx/Makefile +2 -1
index 4893c1f..b87513c 100644
@@ -12,7 +12,8 @@ obj-y += \
 	clk-pllv1.o \
 	clk-pllv2.o \
 	clk-pllv3.o \
-	clk-pfd.o
+	clk-pfd.o \
+	clk-sccg-pll.o
 
 obj-$(CONFIG_SOC_IMX1)   += clk-imx1.o
 obj-$(CONFIG_SOC_IMX21)  += clk-imx21.o
drivers/clk/imx/clk-sccg-pll.c new file +246 -0

246 lines not on this page · raw message

drivers/clk/imx/clk.h +9 -0
index 13daf1c..12b3fd6 100644
@@ -21,6 +21,11 @@ enum imx_pllv1_type {
 	IMX_PLLV1_IMX35,
 };
 
+enum imx_sccg_pll_type {
+	SCCG_PLL1,
+	SCCG_PLL2,
+};
+
 struct clk *imx_clk_pllv1(enum imx_pllv1_type type, const char *name,
 		const char *parent, void __iomem *base);
 
@@ -30,6 +35,10 @@ struct clk *imx_clk_pllv2(const char *name, const char *parent,
 struct clk *imx_clk_frac_pll(const char *name, const char *parent_name,
 			     void __iomem *base);
 
+struct clk *imx_clk_sccg_pll(const char *name, const char *parent_name,
+			     void __iomem *base,
+			     enum imx_sccg_pll_type pll_type);
+
 enum imx_pllv3_type {
 	IMX_PLLV3_GENERIC,
 	IMX_PLLV3_SYS,
Signature · 1 line
2.7.4
author
#6 lore raw

PATCH v7 4/5 clk: imx: add imx composite clock

In reply to a message not in the archive

Since a lot of clocks on imx8 are formed by a mux, gate, predivider and divider, the idea here is to combine all of those into one composite clock, but we need to deal with both predivider and divider at the same time and therefore we add the imx_clk_composite_divider_ops and register the composite clock with those.

---
 drivers/clk/imx/Makefile        |   1 +
 drivers/clk/imx/clk-composite.c | 155 ++++++++++++++++++++++++++++++++++++++++
 drivers/clk/imx/clk.h           |  11 +++
 3 files changed, 167 insertions(+)
 create mode 100644 drivers/clk/imx/clk-composite.c
drivers/clk/imx/Makefile +1 -0
index b87513c..4fabb0a 100644
@@ -3,6 +3,7 @@
 obj-y += \
 	clk.o \
 	clk-busy.o \
+	clk-composite.o \
 	clk-cpu.o \
 	clk-fixup-div.o \
 	clk-fixup-mux.o \
drivers/clk/imx/clk-composite.c new file +155 -0

155 lines not on this page · raw message

drivers/clk/imx/clk.h +11 -0
index 12b3fd6..74d8d46 100644
@@ -232,4 +232,15 @@ struct clk *imx_clk_cpu(const char *name, const char *parent_name,
 		struct clk *div, struct clk *mux, struct clk *pll,
 		struct clk *step);
 
+struct clk *imx_clk_composite_flags(const char *name, const char **parent_names,
+		int num_parents, void __iomem *reg, unsigned long flags);
+
+#define imx_clk_composite(name, parent_names, reg) \
+	imx_clk_composite_flags(name, parent_names, ARRAY_SIZE(parent_names), reg, \
+					CLK_SET_RATE_NO_REPARENT | CLK_OPS_PARENT_ENABLE)
+
+#define imx_clk_composite_critical(name, parent_names, reg) \
+	imx_clk_composite_flags(name, parent_names, ARRAY_SIZE(parent_names), reg, \
+				CLK_IS_CRITICAL | CLK_SET_RATE_NO_REPARENT | CLK_OPS_PARENT_ENABLE)
+
 #endif
Signature · 1 line
2.7.4
author
#8 lore raw

PATCH v7 5/5 clk: imx: add clock driver for i.MX8MQ CCM

In reply to a message not in the archive

From: Lucas Stach <[email protected]>

Add driver for the Clock Control Module found on i.MX8MQ.

This is largely based on the downstream driver from Anson Huang and Bai Ping at NXP, plus the imx composite clock from Abel Vesa at NXP, with only some small adaptions to mainline from me.

---
 drivers/clk/imx/Makefile     |   1 +
 drivers/clk/imx/clk-imx8mq.c | 602 +++++++++++++++++++++++++++++++++++++++++++
 drivers/clk/imx/clk.h        |  36 +++
 3 files changed, 639 insertions(+)
 create mode 100644 drivers/clk/imx/clk-imx8mq.c
drivers/clk/imx/Makefile +1 -0
index 4fabb0a..64e695c 100644
@@ -30,3 +30,4 @@ obj-$(CONFIG_SOC_IMX6SX) += clk-imx6sx.o
 obj-$(CONFIG_SOC_IMX6UL) += clk-imx6ul.o
 obj-$(CONFIG_SOC_IMX7D)  += clk-imx7d.o
 obj-$(CONFIG_SOC_VF610)  += clk-vf610.o
+obj-$(CONFIG_SOC_IMX8MQ) += clk-imx8mq.o
drivers/clk/imx/clk-imx8mq.c new file +602 -0

602 lines not on this page · raw message

drivers/clk/imx/clk.h +36 -0
index 74d8d46..c389f1c 100644
@@ -128,6 +128,15 @@ static inline struct clk *imx_clk_divider2(const char *name, const char *parent,
 			reg, shift, width, 0, &imx_ccm_lock);
 }
 
+static inline struct clk *imx_clk_divider2_flags(const char *name,
+		const char *parent, void __iomem *reg, u8 shift, u8 width,
+		unsigned long flags)
+{
+	return clk_register_divider(NULL, name, parent,
+			flags | CLK_SET_RATE_PARENT | CLK_OPS_PARENT_ENABLE,
+			reg, shift, width, 0, &imx_ccm_lock);
+}
+
 static inline struct clk *imx_clk_gate(const char *name, const char *parent,
 		void __iomem *reg, u8 shift)
 {
@@ -195,6 +204,15 @@ static inline struct clk *imx_clk_gate3(const char *name, const char *parent,
 			reg, shift, 0, &imx_ccm_lock);
 }
 
+static inline struct clk *imx_clk_gate3_flags(const char *name,
+		const char *parent, void __iomem *reg, u8 shift,
+		unsigned long flags)
+{
+	return clk_register_gate(NULL, name, parent,
+			flags | CLK_SET_RATE_PARENT | CLK_OPS_PARENT_ENABLE,
+			reg, shift, 0, &imx_ccm_lock);
+}
+
 static inline struct clk *imx_clk_gate4(const char *name, const char *parent,
 		void __iomem *reg, u8 shift)
 {
@@ -203,6 +221,15 @@ static inline struct clk *imx_clk_gate4(const char *name, const char *parent,
 			reg, shift, 0x3, 0, &imx_ccm_lock, NULL);
 }
 
+static inline struct clk *imx_clk_gate4_flags(const char *name,
+		const char *parent, void __iomem *reg, u8 shift,
+		unsigned long flags)
+{
+	return clk_register_gate2(NULL, name, parent,
+			flags | CLK_SET_RATE_PARENT | CLK_OPS_PARENT_ENABLE,
+			reg, shift, 0x3, 0, &imx_ccm_lock, NULL);
+}
+
 static inline struct clk *imx_clk_mux(const char *name, void __iomem *reg,
 		u8 shift, u8 width, const char **parents, int num_parents)
 {
@@ -228,6 +255,15 @@ static inline struct clk *imx_clk_mux_flags(const char *name,
 			&imx_ccm_lock);
 }
 
+static inline struct clk *imx_clk_mux2_flags(const char *name,
+		void __iomem *reg, u8 shift, u8 width, const char **parents,
+		int num_parents, unsigned long flags)
+{
+	return clk_register_mux(NULL, name, parents, num_parents,
+			flags | CLK_SET_RATE_NO_REPARENT | CLK_OPS_PARENT_ENABLE,
+			reg, shift, width, 0, &imx_ccm_lock);
+}
+
 struct clk *imx_clk_cpu(const char *name, const char *parent_name,
 		struct clk *div, struct clk *mux, struct clk *pll,
 		struct clk *step);
Signature · 1 line
2.7.4
#3 lore raw

Re: [PATCH v7 2/5] clk: imx: add fractional PLL output clock

In reply to Abel Vesa
Quoted 73 lines · Abel Vesa

From: Lucas Stach <[email protected]>

This is a new clock type introduced on i.MX8.

---
 drivers/clk/imx/Makefile       |   1 +
 drivers/clk/imx/clk-frac-pll.c | 230 +++++++++++++++++++++++++++++++++++++++++
 drivers/clk/imx/clk.h          |   3 +
 3 files changed, 234 insertions(+)
 create mode 100644 drivers/clk/imx/clk-frac-pll.c
drivers/clk/imx/Makefile +1 -0
index 8c3baa7..4893c1f 100644
@@ -6,6 +6,7 @@ obj-y += \
        clk-cpu.o \
        clk-fixup-div.o \
        clk-fixup-mux.o \
+       clk-frac-pll.o \
        clk-gate-exclusive.o \
        clk-gate2.o \
        clk-pllv1.o \
drivers/clk/imx/clk-frac-pll.c new file +41 -0
new file mode 100644
index 0000000..c80c6ed
@@ -0,0 +1,230 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018 NXP.
+ */
+
+#include <linux/clk-provider.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/jiffies.h>
+#include <linux/slab.h>
+
+#include "clk.h"
+
+#define PLL_CFG0               0x0
+#define PLL_CFG1               0x4
+
+#define PLL_LOCK_STATUS                BIT(31)
+#define PLL_PD                 19
+#define PLL_PD_MASK            BIT(PLL_PD)
+#define PLL_BYPASS             14
+#define PLL_BYPASS_MASK                BIT(PLL_BYPASS)
+#define PLL_NEWDIV_VAL         BIT(12)
+#define PLL_NEWDIV_ACK         BIT(11)
+#define PLL_FRAC_DIV_MASK      0xffffff
+#define PLL_INT_DIV_MASK       0x7f
+#define PLL_OUTPUT_DIV_MASK    0x1f
+#define PLL_FRAC_DENOM         0x1000000
+
+struct clk_frac_pll {
+       struct clk_hw   hw;
+       void __iomem    *base;
+};
+
+#define to_clk_frac_pll(_hw) container_of(_hw, struct clk_frac_pll, hw)
+
+static int clk_wait_lock(struct clk_frac_pll *pll)
+{
+       unsigned long timeout = jiffies + msecs_to_jiffies(10);
+       u32 val;
+

drivers/clk/imx/clk-frac-pll.c line 51

Quoted 10 lines
drivers/clk/imx/clk-frac-pll.c new file +10 -0
+       /* Wait for PLL to lock */
+       do {
+               if (readl_relaxed(pll->base) & PLL_LOCK_STATUS)
+                       break;
+               if (time_after(jiffies, timeout))
+                       break;
+       } while (1);
+
+       return readl_poll_timeout(pll->base, val,
+                                       val & PLL_LOCK_STATUS, 0, 1000);

Is this code intentional? It seems to me those two are almost identical wait loops, so it's a bit confusing to see without at least a comment why it is done that way. If it is intentional, can the first loop be replaced with readx_poll_timeout(readl_relaxed, ...) ?

Quoted 12 lines
drivers/clk/imx/clk-frac-pll.c new file +12 -0
+}
+
+static int clk_wait_ack(struct clk_frac_pll *pll)
+{
+       unsigned long timeout = jiffies + msecs_to_jiffies(50);
+       u32 val;
+
+       /* return directly if the pll is in powerdown or in bypass */
+       if (readl_relaxed(pll->base) & (PLL_PD_MASK | PLL_BYPASS_MASK))
+               return 0;
+
+       /* Wait for the pll's divfi and divff to be reloaded */

drivers/clk/imx/clk-frac-pll.c line 73

Quoted 10 lines
drivers/clk/imx/clk-frac-pll.c new file +10 -0
+       do {
+               if (readl_relaxed(pll->base) & PLL_NEWDIV_ACK)
+                       break;
+               if (time_after(jiffies, timeout))
+                       break;
+       } while (1);
+
+       return readl_poll_timeout(pll->base, val,
+                                       val & PLL_NEWDIV_ACK, 0, 1000);
+}

Ditto.

Quoted 33 lines
drivers/clk/imx/clk-frac-pll.c new file +33 -0
+
+static int clk_pll_prepare(struct clk_hw *hw)
+{
+       struct clk_frac_pll *pll = to_clk_frac_pll(hw);
+       u32 val;
+
+       val = readl_relaxed(pll->base + PLL_CFG0);
+       val &= ~PLL_PD_MASK;
+       writel_relaxed(val, pll->base + PLL_CFG0);
+
+       return clk_wait_lock(pll);
+}
+
+static void clk_pll_unprepare(struct clk_hw *hw)
+{
+       struct clk_frac_pll *pll = to_clk_frac_pll(hw);
+       u32 val;
+
+       val = readl_relaxed(pll->base + PLL_CFG0);
+       val |= PLL_PD_MASK;
+       writel_relaxed(val, pll->base + PLL_CFG0);
+}
+
+static int clk_pll_is_prepared(struct clk_hw *hw)
+{
+       struct clk_frac_pll *pll = to_clk_frac_pll(hw);
+       u32 val;
+
+       val = readl_relaxed(pll->base + PLL_CFG0);
+       return (val & PLL_PD_MASK) ? 0 : 1;
+}
+
+static unsigned long clk_pll_recalc_rate(struct clk_hw *hw,

drivers/clk/imx/clk-frac-pll.c line 116

Quoted 10 lines
drivers/clk/imx/clk-frac-pll.c new file +10 -0
+                                        unsigned long parent_rate)
+{
+       struct clk_frac_pll *pll = to_clk_frac_pll(hw);
+       u32 val, divff, divfi, divq;
+       u64 temp64;
+
+       val = readl_relaxed(pll->base + PLL_CFG0);
+       divq = ((val & PLL_OUTPUT_DIV_MASK) + 1) * 2;
+       val = readl_relaxed(pll->base + PLL_CFG1);
+       divff = (val >> 7) & PLL_FRAC_DIV_MASK;

Just as a suggestion you can do:

#define PLL_FRAC_DIV GENMASK(30, 7)

divff = FIELD_GET(PLL_FRAC_DIV, val)

it's a bit nicer (IMHO, of course) because it allows you to avoid having to encode shit and mask separately and mask definition can be easily verified against the datasheet.

Thanks,
Andrey Smirnov

#5 lore raw

Re: [PATCH v7 3/5] clk: imx: add SCCG PLL type

In reply to Abel Vesa
Quoted 56 lines · Abel Vesa

From: Lucas Stach <[email protected]>

The SCCG is a new PLL type introduced on i.MX8. Add support for this. The driver currently misses the PLL lock check, as the preliminary documentation mentions lock configurations, but is quiet about where to find the actual lock status signal.

---
 drivers/clk/imx/Makefile       |   3 +-
 drivers/clk/imx/clk-sccg-pll.c | 246 +++++++++++++++++++++++++++++++++++++++++
 drivers/clk/imx/clk.h          |   9 ++
 3 files changed, 257 insertions(+), 1 deletion(-)
 create mode 100644 drivers/clk/imx/clk-sccg-pll.c
drivers/clk/imx/Makefile +2 -1
index 4893c1f..b87513c 100644
@@ -12,7 +12,8 @@ obj-y += \
        clk-pllv1.o \
        clk-pllv2.o \
        clk-pllv3.o \
-       clk-pfd.o
+       clk-pfd.o \
+       clk-sccg-pll.o
 
 obj-$(CONFIG_SOC_IMX1)   += clk-imx1.o
 obj-$(CONFIG_SOC_IMX21)  += clk-imx21.o
drivers/clk/imx/clk-sccg-pll.c new file +19 -0
new file mode 100644
index 0000000..8d87ba5
@@ -0,0 +1,246 @@
+// SPDX-License-Identifier: (GPL-2.0 OR MIT)
+/*
+ * Copyright 2018 NXP.
+ */
+
+#include <linux/clk.h>
+#include <linux/err.h>
+#include <linux/io.h>
+#include <linux/slab.h>
+#include <linux/delay.h>
+
+#include "clk.h"
+
+/* PLL CFGs */
+#define PLL_CFG0       0x0
+#define PLL_CFG1       0x4
+#define PLL_CFG2       0x8
+
+#define PLL_DIVF1_SHIFT        13

drivers/clk/imx/clk-sccg-pll.c line 29

Quoted 10 lines
drivers/clk/imx/clk-sccg-pll.c new file +10 -0
+#define PLL_DIVF2_SHIFT        7
+#define PLL_DIVF_MASK  0x3f
+
+#define PLL_DIVR1_SHIFT        25
+#define PLL_DIVR2_SHIFT        19
+#define PLL_DIVR1_MASK 0x3
+#define PLL_DIVR2_MASK 0x3f
+#define PLL_REF_SHIFT  0
+#define PLL_REF_MASK   0x3
+

Similar to previous patch, I'd suggest using FILED_GET()/GENMAKS for the above fields

Quoted 16 lines
drivers/clk/imx/clk-sccg-pll.c new file +16 -0
+#define PLL_LOCK_MASK  BIT(31)
+#define PLL_PD_MASK    BIT(7)
+
+#define OSC_25M                25000000
+#define OSC_27M                27000000
+
+struct clk_sccg_pll {
+       struct clk_hw   hw;
+       void __iomem    *base;
+};
+
+#define to_clk_sccg_pll(_hw) container_of(_hw, struct clk_sccg_pll, hw)
+
+static int clk_pll_wait_lock(struct clk_sccg_pll *pll)
+{
+       /* max lock time is 70us */

drivers/clk/imx/clk-sccg-pll.c line 55

Quoted 10 lines
drivers/clk/imx/clk-sccg-pll.c new file +10 -0
+       int retry = 7;
+
+       /* Wait for PLL to lock */
+       do {
+               if (readl_relaxed(pll->base) & PLL_LOCK_MASK)
+                       break;
+               udelay(10);
+               retry--;
+       } while (retry);
+

Can readx_poll_timeout_atomic() be used here instead?

Thanks,
Andrey Smirnov

#7 lore raw

Re: [PATCH v7 4/5] clk: imx: add imx composite clock

In reply to Abel Vesa
Quoted 175 lines · Abel Vesa

Since a lot of clocks on imx8 are formed by a mux, gate, predivider and divider, the idea here is to combine all of those into one composite clock, but we need to deal with both predivider and divider at the same time and therefore we add the imx_clk_composite_divider_ops and register the composite clock with those.

---
 drivers/clk/imx/Makefile        |   1 +
 drivers/clk/imx/clk-composite.c | 155 ++++++++++++++++++++++++++++++++++++++++
 drivers/clk/imx/clk.h           |  11 +++
 3 files changed, 167 insertions(+)
 create mode 100644 drivers/clk/imx/clk-composite.c
drivers/clk/imx/Makefile +1 -0
index b87513c..4fabb0a 100644
@@ -3,6 +3,7 @@
 obj-y += \
        clk.o \
        clk-busy.o \
+       clk-composite.o \
        clk-cpu.o \
        clk-fixup-div.o \
        clk-fixup-mux.o \
drivers/clk/imx/clk-composite.c new file +141 -0
new file mode 100644
index 0000000..40c2aa8
@@ -0,0 +1,155 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright 2018 NXP
+ */
+
+#include <linux/errno.h>
+#include <linux/slab.h>
+#include <linux/clk-provider.h>
+#include <linux/clk.h>
+
+#include "clk.h"
+
+#define PCG_PREDIV_SHIFT       16
+#define PCG_PREDIV_WIDTH       3
+
+#define PCG_DIV_SHIFT          0
+#define PCG_DIV_WIDTH          6
+
+#define PCG_PCS_SHIFT          24
+#define PCG_PCS_MASK           0x7
+
+#define PCG_CGC_SHIFT          28
+
+static unsigned long imx_clk_composite_divider_recalc_rate(struct clk_hw *hw,
+                                               unsigned long parent_rate)
+{
+       struct clk_divider *divider = to_clk_divider(hw);
+       unsigned long prediv_rate;
+       unsigned int prediv_value;
+       unsigned int div_value;
+
+       prediv_value = clk_readl(divider->reg) >> divider->shift;
+       prediv_value &= clk_div_mask(divider->width);
+
+       prediv_rate = divider_recalc_rate(hw, parent_rate, prediv_value,
+                                               NULL, divider->flags,
+                                               divider->width);
+
+       div_value = clk_readl(divider->reg) >> PCG_DIV_SHIFT;
+       div_value &= clk_div_mask(PCG_DIV_WIDTH);
+
+       return divider_recalc_rate(hw, prediv_rate, div_value, NULL,
+                                  divider->flags, PCG_DIV_WIDTH);
+}
+
+static long imx_clk_composite_divider_round_rate(struct clk_hw *hw,
+                                               unsigned long rate,
+                                               unsigned long *prate)
+{
+       struct clk_divider *divider = to_clk_divider(hw);
+       unsigned long prediv_rate;
+
+       prediv_rate = divider_round_rate(hw, rate, prate, divider->table,
+                                 divider->width, divider->flags);
+       return divider_round_rate(hw, rate, &prediv_rate, divider->table,
+                                 PCG_DIV_WIDTH, divider->flags);
+}
+
+static int imx_clk_composite_divider_set_rate(struct clk_hw *hw,
+                                       unsigned long rate,
+                                       unsigned long parent_rate)
+{
+       struct clk_divider *divider = to_clk_divider(hw);
+       unsigned long prediv_rate;
+       unsigned long flags = 0;
+       int prediv_value;
+       int div_value;
+       u32 val;
+
+       prediv_value = divider_get_val(rate, parent_rate, NULL,
+                               PCG_PREDIV_WIDTH, CLK_DIVIDER_ROUND_CLOSEST);
+       if (prediv_value < 0)
+               return prediv_value;
+
+       prediv_rate = DIV_ROUND_UP_ULL((u64)parent_rate, prediv_value + 1);
+
+       div_value = divider_get_val(rate, prediv_rate, NULL,
+                               PCG_DIV_WIDTH, CLK_DIVIDER_ROUND_CLOSEST);
+       if (div_value < 0)
+               return div_value;
+
+       spin_lock_irqsave(divider->lock, flags);
+
+       val = clk_readl(divider->reg);
+       val &= ~((clk_div_mask(divider->width) << divider->shift) |
+                       (clk_div_mask(PCG_DIV_WIDTH) << PCG_DIV_SHIFT));
+
+       val |= (u32)prediv_value << divider->shift;
+       val |= (u32)div_value << PCG_DIV_SHIFT;
+       clk_writel(val, divider->reg);
+
+       spin_unlock_irqrestore(divider->lock, flags);
+
+       return 0;
+}
+
+static const struct clk_ops imx_clk_composite_divider_ops = {
+       .recalc_rate = imx_clk_composite_divider_recalc_rate,
+       .round_rate = imx_clk_composite_divider_round_rate,
+       .set_rate = imx_clk_composite_divider_set_rate,
+};
+
+struct clk *imx_clk_composite_flags(const char *name,
+                                       const char **parent_names,
+                                       int num_parents, void __iomem *reg,
+                                       unsigned long flags)
+{
+       struct clk_hw *mux_hw = NULL, *div_hw = NULL, *gate_hw = NULL;
+       struct clk_divider *div = NULL;
+       struct clk_gate *gate = NULL;
+       struct clk_mux *mux = NULL;
+       struct clk *clk;
+
+       mux = kzalloc(sizeof(*mux), GFP_KERNEL);
+       if (!mux)
+               return ERR_PTR(-ENOMEM);
+       mux_hw = &mux->hw;
+       mux->reg = reg;
+       mux->shift = PCG_PCS_SHIFT;
+       mux->mask = PCG_PCS_MASK;
+
+       div = kzalloc(sizeof(*div), GFP_KERNEL);
+       if (!div) {
+               kfree(mux);
+               return ERR_PTR(-ENOMEM);
+       }
+       div_hw = &div->hw;
+       div->reg = reg;
+       div->shift = PCG_PREDIV_SHIFT;
+       div->width = PCG_PREDIV_WIDTH;
+       div->lock = &imx_ccm_lock;
+       div->flags = CLK_DIVIDER_ROUND_CLOSEST;
+
+       gate = kzalloc(sizeof(*gate), GFP_KERNEL);
+       if (!gate) {
+               kfree(mux);
+               kfree(div);
+               return ERR_PTR(-ENOMEM);
+       }
+       gate_hw = &gate->hw;
+       gate->reg = reg;

drivers/clk/imx/clk-composite.c line 151

Quoted 10 lines
drivers/clk/imx/clk-composite.c new file +10 -0
+       gate->bit_idx = PCG_CGC_SHIFT;
+
+       clk = clk_register_composite(NULL, name, parent_names, num_parents,
+                                       mux_hw, &clk_mux_ops, div_hw,
+                                       &imx_clk_composite_divider_ops, gate_hw,
+                                       &clk_gate_ops, flags);
+       if (IS_ERR(clk)) {
+               kfree(mux);
+               kfree(div);
+               kfree(gate);

Minor suggestion: using goto to free resource in this case might be more straightforward and require less repeatition.

Quoted 12 lines
drivers/clk/imx/clk-composite.c new file +4 -0
+       }
+
+       return clk;
+}
drivers/clk/imx/clk.h +0 -0
index 12b3fd6..74d8d46 100644
@@ -232,4 +232,15 @@ struct clk *imx_clk_cpu(const char *name, const char *parent_name,
                struct clk *div, struct clk *mux, struct clk *pll,
                struct clk *step);
 

drivers/clk/imx/clk.h struct clk *imx_clk_cpu(const char *name, const char *parent_name, · line 244

Quoted 10 lines
drivers/clk/imx/clk.h +10 -0
+struct clk *imx_clk_composite_flags(const char *name, const char **parent_names,
+               int num_parents, void __iomem *reg, unsigned long flags);
+
+#define imx_clk_composite(name, parent_names, reg) \
+       imx_clk_composite_flags(name, parent_names, ARRAY_SIZE(parent_names), reg, \
+                                       CLK_SET_RATE_NO_REPARENT | CLK_OPS_PARENT_ENABLE)
+
+#define imx_clk_composite_critical(name, parent_names, reg) \
+       imx_clk_composite_flags(name, parent_names, ARRAY_SIZE(parent_names), reg, \
+                               CLK_IS_CRITICAL | CLK_SET_RATE_NO_REPARENT | CLK_OPS_PARENT_ENABLE)

Another minor suggestion is to define a intermediary macro and define both imx_clk_composite_critical() and imx_clk_composite() in terms of it. Say:

#define __imx_clk_composite(name, parent_names, reg, flags) \
    imx_clk_composite_flags(name, parent_names, ARRAY_SIZE(parent_names), reg, \
                                             flags |

CLK_SET_RATE_NO_REPARENT | CLK_OPS_PARENT_ENABLE)

#define imx_clk_composite(name, parent_names, reg) __imx_clk_composite_flags(name, parent_names, reg, 0) #define imx_clk_composite_critical(name, parent_names, reg) __imx_clk_composite_flags(name, parent_names, reg, CLK_IS_CRITICAL)

this way it might be a bit easier to spot that the only difference between the two is CLK_IS_CRITICAL.

Thanks,
Andrey Smirnov