2022-01-23 14:55:09

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 00/31] clk: mediatek: Cleanups and Improvements - Part 1

Hi everyone,

This is part 1 of planned work to cleanup, improve, and modernize the
MediaTek clk drivers. The full proposal for the work is included below.

Part 1 involves cleaning up various parts of the MediaTek common clk
driver library:

- Code style cleanups in places touched by other changes
- Code for a given clk type that happened to be split between different
files is consolidated
- Parts of the implementation, such as the internal data structures and
registration/unregistration of a single clk, are internalized and no
longer exported to other parts of the library
- Unregister APIs for all clk types in the library are added
- Remove function added for the simple driver case
- Error handling is added for all clk types
- MT8195 clk drivers cleaned up with error handling and removal support
added

Adding error handling to the clk driver library results is a change in
its behavior, and might cause some breakage where the affected system
was originally somewhat working, but not completely. The failure would
result from a duplicated clk, either in name or ID. Old behavior was to
skip over these and ignore the error, whereas the new behavior is to
restore changes and error out.

To detect duplicate IDs, an additional warning will now be printed when
such occurrences happen. In a correct driver such things should not
happen though, so maybe we could consider wrapping that in some debug
Kconfig option. That's why I put it as the last patch in this series.

This series was tested on MT8195 with the series applied on the downstream
ChromeOS v5.10 kernel. Some other patches were applied to make this
series apply cleanly, but nothing The cpumux clk type changes were
not tested due to lack of related hardware. However the changes are
very similar to the other types, and I am confident there are no issues.


Full proposal follows:

MediaTek Kernel Clk Driver Improvement

Objective

Modernize the MediaTek clk drivers to support "struct clk_hw" based
registration and local parenting. Also address any deficiencies, such
as lack of proper cleanup and removal of clocks.

Requirements

- Clks should be registered with clk_hw_* APIs
- Clk parents within the same IP block / driver should be described with
clk_hw pointers, not global clock names
- All clk registration APIs should have corresponding unregister APIs
for proper cleanup
- All clk drivers should have proper error path cleanup
- Essential clk drivers should not be allowed to be unbound
- Non-essential modular clk drivers should have removal paths

Background

The clk API in the Linux kernel is split into per-user "struct clk"
consumer and per-provider "struct clk_hw" provider APIs. Upstream has
deprecated the use of clk_register() and co APIs that return "struct clk",
and encourages everyone to migrate to the provider APIs.

The MediaTek clk driver is still using the deprecated clk_register() API
to register clocks. As the driver family is based on a common library
shared by all MediaTek platforms, this presents a huge obstacle in the
deprecation and removal of the old API. This also precludes the MediaTek
driver from fully utilizing new features and improvements in the Common
Clock Framework.

One such feature is the ability to reference clock parents directly with
pointers to their respective "struct clk_hw", instead of looking up clock
names in the global clock table. Newer platforms have hundreds of
individual clocks, split amongst a handful of IP blocks. Most of them are
internal nodes of each IP block's clock tree. Reducing the amount of time
spent on global clock name lookups would make clk drivers probe faster
and boost overall boot time.

Indeed, there has already been an attempt by MediaTek to improve this by
hashing the clock names [1] to get around the slow string comparison.
However this doesn't address the primary deficiency of the current driver
implementation.

Design ideas

As the requirements section lists, many improvements can be made to the
driver library and platform drivers. Given the amount of existing
platforms supported by the driver library, and lack of hardware within
ChromeOS for some of them, the most likely approach is to target one
specific platform to do the improvements, and let MediaTek & the broader
kernel community handle the rest.

1. Add proper cleanup code
- Add "unregister" APIs to each type of clock supported by the driver
library
- Add calls to these new APIs to the error paths of existing drivers
- Add driver removal code for non-essential modular drivers
2. clk_hw_register() support
- Add matching clk_hw_register_* API variants
- This should be made as generic as possible (like the clk_hw_register()
API) to make step 3 easier to implement.
- Migrate existing platform driver to new APIs
3. Pointer based local parenting
- Add macros to handle "struct clk_hw" and "struct clk_parent_data"
ways of describing clock parents
- Add clock parent descriptions based on "struct clk_hw" to existing
platform drivers
- For clocks on the input-side hardware boundary,
"struct clk_parent_data" should be used to leave room for device
tree based clock lookups

4. At this point, we should require MediaTek and other parties to use the
new APIs to implement drivers for new platforms

5. Once all platform drivers have been converted, the old clk_register()
based APIs can be removed

Alternatives considered

Hashing the clock names and comparing the hash first [1] has been proposed
by MediaTek. However while this does improve performance, the hashing
itself still incurs a cost that could be avoided entirely for most
instances. The only instances where local clock parenting is unusable
and global clock name matching is needed is for clock parents between
different clock provider IP blocks. The number of clocks wired between
blocks is quite large, and MediaTek has avoided describing them in the
device tree.

[1] https://lore.kernel.org/linux-mediatek/[email protected]/


Chen-Yu Tsai (31):
clk: mediatek: Use %pe to print errors
clk: mediatek: gate: Consolidate gate type clk related code
clk: mediatek: gate: Internalize clk implementation
clk: mediatek: gate: Implement unregister API
clk: mediatek: gate: Clean up included headers
clk: mediatek: cpumux: Implement unregister API
clk: mediatek: cpumux: Internalize struct mtk_clk_cpumux
clk: mediatek: cpumux: Clean up included headers
clk: mediatek: mux: Implement unregister API
clk: mediatek: mux: Internalize struct mtk_clk_mux
clk: mediatek: mux: Clean up included headers
clk: mediatek: pll: Split definitions into separate header file
clk: mediatek: pll: Implement unregister API
clk: mediatek: pll: Clean up included headers
clk: mediatek: Implement mtk_clk_unregister_fixed_clks() API
clk: mediatek: Implement mtk_clk_unregister_factors() API
clk: mediatek: Implement mtk_clk_unregister_divider_clks() API
clk: mediatek: Implement mtk_clk_unregister_composites() API
clk: mediatek: Add mtk_clk_simple_remove()
clk: mediatek: mtk: Clean up included headers
clk: mediatek: cpumux: Implement error handling in register API
clk: mediatek: gate: Implement error handling in register API
clk: mediatek: mux: Reverse check for existing clk to reduce nesting
level
clk: mediatek: mux: Implement error handling in register API
clk: mediatek: pll: Implement error handling in register API
clk: mediatek: mtk: Implement error handling in register APIs
clk: mediatek: Unregister clks in mtk_clk_simple_probe() error path
clk: mediatek: mt8195: Hook up mtk_clk_simple_remove()
clk: mediatek: mt8195: Implement error handling in probe functions
clk: mediatek: mt8195: Implement remove functions
clk: mediatek: Warn if clk IDs are duplicated

drivers/clk/mediatek/clk-apmixed.c | 2 +-
drivers/clk/mediatek/clk-cpumux.c | 69 +++-
drivers/clk/mediatek/clk-cpumux.h | 13 +-
drivers/clk/mediatek/clk-gate.c | 148 +++++++--
drivers/clk/mediatek/clk-gate.h | 59 ++--
drivers/clk/mediatek/clk-mt2701.c | 5 +-
drivers/clk/mediatek/clk-mt2712.c | 3 +-
drivers/clk/mediatek/clk-mt6765.c | 3 +-
drivers/clk/mediatek/clk-mt6779.c | 3 +-
drivers/clk/mediatek/clk-mt6797.c | 3 +-
drivers/clk/mediatek/clk-mt7622.c | 5 +-
drivers/clk/mediatek/clk-mt7629.c | 5 +-
drivers/clk/mediatek/clk-mt7986-apmixed.c | 4 +-
drivers/clk/mediatek/clk-mt8135.c | 3 +-
drivers/clk/mediatek/clk-mt8167.c | 3 +-
drivers/clk/mediatek/clk-mt8173.c | 5 +-
drivers/clk/mediatek/clk-mt8183.c | 3 +-
drivers/clk/mediatek/clk-mt8192.c | 3 +-
drivers/clk/mediatek/clk-mt8195-apmixedsys.c | 30 +-
drivers/clk/mediatek/clk-mt8195-apusys_pll.c | 25 +-
drivers/clk/mediatek/clk-mt8195-cam.c | 1 +
drivers/clk/mediatek/clk-mt8195-ccu.c | 1 +
drivers/clk/mediatek/clk-mt8195-img.c | 1 +
.../clk/mediatek/clk-mt8195-imp_iic_wrap.c | 1 +
drivers/clk/mediatek/clk-mt8195-infra_ao.c | 1 +
drivers/clk/mediatek/clk-mt8195-ipe.c | 1 +
drivers/clk/mediatek/clk-mt8195-mfg.c | 1 +
drivers/clk/mediatek/clk-mt8195-peri_ao.c | 1 +
drivers/clk/mediatek/clk-mt8195-scp_adsp.c | 1 +
drivers/clk/mediatek/clk-mt8195-topckgen.c | 69 +++-
drivers/clk/mediatek/clk-mt8195-vdec.c | 1 +
drivers/clk/mediatek/clk-mt8195-vdo0.c | 20 +-
drivers/clk/mediatek/clk-mt8195-vdo1.c | 20 +-
drivers/clk/mediatek/clk-mt8195-venc.c | 1 +
drivers/clk/mediatek/clk-mt8195-vpp0.c | 1 +
drivers/clk/mediatek/clk-mt8195-vpp1.c | 1 +
drivers/clk/mediatek/clk-mt8195-wpe.c | 1 +
drivers/clk/mediatek/clk-mt8516.c | 3 +-
drivers/clk/mediatek/clk-mtk.c | 299 +++++++++++++-----
drivers/clk/mediatek/clk-mtk.h | 105 ++----
drivers/clk/mediatek/clk-mux.c | 89 +++++-
drivers/clk/mediatek/clk-mux.h | 17 +-
drivers/clk/mediatek/clk-pll.c | 94 +++++-
drivers/clk/mediatek/clk-pll.h | 57 ++++
drivers/clk/mediatek/reset.c | 3 +-
45 files changed, 891 insertions(+), 293 deletions(-)
create mode 100644 drivers/clk/mediatek/clk-pll.h

--
2.35.0.rc0.227.g00780c9af4-goog


2022-01-23 14:55:15

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 01/31] clk: mediatek: Use %pe to print errors

If %pe is used to print errors, a string representation of the error
would be printed instead of a number as with %ld. Also, all the sites
printing errors are deriving the error code from a pointer. Using %pe
is more straightforward.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-apmixed.c | 2 +-
drivers/clk/mediatek/clk-cpumux.c | 6 ++----
drivers/clk/mediatek/clk-mtk.c | 18 ++++++------------
drivers/clk/mediatek/clk-mux.c | 6 ++----
drivers/clk/mediatek/clk-pll.c | 3 +--
drivers/clk/mediatek/reset.c | 3 +--
6 files changed, 13 insertions(+), 25 deletions(-)

diff --git a/drivers/clk/mediatek/clk-apmixed.c b/drivers/clk/mediatek/clk-apmixed.c
index caa9119413f1..a29339cc26c4 100644
--- a/drivers/clk/mediatek/clk-apmixed.c
+++ b/drivers/clk/mediatek/clk-apmixed.c
@@ -92,7 +92,7 @@ struct clk * __init mtk_clk_register_ref2usb_tx(const char *name,
clk = clk_register(NULL, &tx->hw);

if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n", name, PTR_ERR(clk));
+ pr_err("Failed to register clk %s: %pe\n", name, clk);
kfree(tx);
}

diff --git a/drivers/clk/mediatek/clk-cpumux.c b/drivers/clk/mediatek/clk-cpumux.c
index e188018bc906..cab5095416b6 100644
--- a/drivers/clk/mediatek/clk-cpumux.c
+++ b/drivers/clk/mediatek/clk-cpumux.c
@@ -87,8 +87,7 @@ int mtk_clk_register_cpumuxes(struct device_node *node,

regmap = device_node_to_regmap(node);
if (IS_ERR(regmap)) {
- pr_err("Cannot find regmap for %pOF: %ld\n", node,
- PTR_ERR(regmap));
+ pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
return PTR_ERR(regmap);
}

@@ -97,8 +96,7 @@ int mtk_clk_register_cpumuxes(struct device_node *node,

clk = mtk_clk_register_cpumux(mux, regmap);
if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- mux->name, PTR_ERR(clk));
+ pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
continue;
}

diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index 8d5791b3f460..519a461cbb6f 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -70,8 +70,7 @@ void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
rc->rate);

if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- rc->name, PTR_ERR(clk));
+ pr_err("Failed to register clk %s: %pe\n", rc->name, clk);
continue;
}

@@ -97,8 +96,7 @@ void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
CLK_SET_RATE_PARENT, ff->mult, ff->div);

if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- ff->name, PTR_ERR(clk));
+ pr_err("Failed to register clk %s: %pe\n", ff->name, clk);
continue;
}

@@ -122,8 +120,7 @@ int mtk_clk_register_gates_with_dev(struct device_node *node,

regmap = device_node_to_regmap(node);
if (IS_ERR(regmap)) {
- pr_err("Cannot find regmap for %pOF: %ld\n", node,
- PTR_ERR(regmap));
+ pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
return PTR_ERR(regmap);
}

@@ -141,8 +138,7 @@ int mtk_clk_register_gates_with_dev(struct device_node *node,
gate->shift, gate->ops, gate->flags, dev);

if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- gate->name, PTR_ERR(clk));
+ pr_err("Failed to register clk %s: %pe\n", gate->name, clk);
continue;
}

@@ -264,8 +260,7 @@ void mtk_clk_register_composites(const struct mtk_composite *mcs,
clk = mtk_clk_register_composite(mc, base, lock);

if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- mc->name, PTR_ERR(clk));
+ pr_err("Failed to register clk %s: %pe\n", mc->name, clk);
continue;
}

@@ -293,8 +288,7 @@ void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
mcd->div_width, mcd->clk_divider_flags, lock);

if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- mcd->name, PTR_ERR(clk));
+ pr_err("Failed to register clk %s: %pe\n", mcd->name, clk);
continue;
}

diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
index 6d3a50eb7d6f..89f23e111d91 100644
--- a/drivers/clk/mediatek/clk-mux.c
+++ b/drivers/clk/mediatek/clk-mux.c
@@ -175,8 +175,7 @@ int mtk_clk_register_muxes(const struct mtk_mux *muxes,

regmap = device_node_to_regmap(node);
if (IS_ERR(regmap)) {
- pr_err("Cannot find regmap for %pOF: %ld\n", node,
- PTR_ERR(regmap));
+ pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
return PTR_ERR(regmap);
}

@@ -187,8 +186,7 @@ int mtk_clk_register_muxes(const struct mtk_mux *muxes,
clk = mtk_clk_register_mux(mux, regmap, lock);

if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- mux->name, PTR_ERR(clk));
+ pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
continue;
}

diff --git a/drivers/clk/mediatek/clk-pll.c b/drivers/clk/mediatek/clk-pll.c
index 60d7ffa0b924..f04f724e12e5 100644
--- a/drivers/clk/mediatek/clk-pll.c
+++ b/drivers/clk/mediatek/clk-pll.c
@@ -378,8 +378,7 @@ void mtk_clk_register_plls(struct device_node *node,
clk = mtk_clk_register_pll(pll, base);

if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %ld\n",
- pll->name, PTR_ERR(clk));
+ pr_err("Failed to register clk %s: %pe\n", pll->name, clk);
continue;
}

diff --git a/drivers/clk/mediatek/reset.c b/drivers/clk/mediatek/reset.c
index ffe464ce7ff8..bcec4b89f449 100644
--- a/drivers/clk/mediatek/reset.c
+++ b/drivers/clk/mediatek/reset.c
@@ -100,8 +100,7 @@ static void mtk_register_reset_controller_common(struct device_node *np,

regmap = device_node_to_regmap(np);
if (IS_ERR(regmap)) {
- pr_err("Cannot find regmap for %pOF: %ld\n", np,
- PTR_ERR(regmap));
+ pr_err("Cannot find regmap for %pOF: %pe\n", np, regmap);
return;
}

--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:55:23

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 03/31] clk: mediatek: gate: Internalize clk implementation

struct mtk_clk_gate and mtk_clk_register_gate() are not used outside of
the gate clk library. Only the API that handles a list of clks is used
by the individual platform clk drivers.

Internalize the parts that aren't used outside of the implementation.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-gate.c | 35 ++++++++++++++++++++++-----------
drivers/clk/mediatek/clk-gate.h | 31 ++---------------------------
2 files changed, 25 insertions(+), 41 deletions(-)

diff --git a/drivers/clk/mediatek/clk-gate.c b/drivers/clk/mediatek/clk-gate.c
index 54921768bfba..e51cfd8c653c 100644
--- a/drivers/clk/mediatek/clk-gate.c
+++ b/drivers/clk/mediatek/clk-gate.c
@@ -11,11 +11,28 @@
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/clkdev.h>
+#include <linux/clk-provider.h>
+#include <linux/container_of.h>
#include <linux/mfd/syscon.h>
#include <linux/module.h>
+#include <linux/regmap.h>

#include "clk-gate.h"

+struct mtk_clk_gate {
+ struct clk_hw hw;
+ struct regmap *regmap;
+ int set_ofs;
+ int clr_ofs;
+ int sta_ofs;
+ u8 bit;
+};
+
+static inline struct mtk_clk_gate *to_mtk_clk_gate(struct clk_hw *hw)
+{
+ return container_of(hw, struct mtk_clk_gate, hw);
+}
+
static u32 mtk_get_clockgating(struct clk_hw *hw)
{
struct mtk_clk_gate *cg = to_mtk_clk_gate(hw);
@@ -140,17 +157,12 @@ const struct clk_ops mtk_clk_gate_ops_no_setclr_inv = {
};
EXPORT_SYMBOL_GPL(mtk_clk_gate_ops_no_setclr_inv);

-struct clk *mtk_clk_register_gate(
- const char *name,
- const char *parent_name,
- struct regmap *regmap,
- int set_ofs,
- int clr_ofs,
- int sta_ofs,
- u8 bit,
- const struct clk_ops *ops,
- unsigned long flags,
- struct device *dev)
+static struct clk *mtk_clk_register_gate(const char *name,
+ const char *parent_name,
+ struct regmap *regmap, int set_ofs,
+ int clr_ofs, int sta_ofs, u8 bit,
+ const struct clk_ops *ops,
+ unsigned long flags, struct device *dev)
{
struct mtk_clk_gate *cg;
struct clk *clk;
@@ -180,7 +192,6 @@ struct clk *mtk_clk_register_gate(

return clk;
}
-EXPORT_SYMBOL_GPL(mtk_clk_register_gate);

int mtk_clk_register_gates_with_dev(struct device_node *node,
const struct mtk_gate *clks, int num,
diff --git a/drivers/clk/mediatek/clk-gate.h b/drivers/clk/mediatek/clk-gate.h
index 432b571d23b3..3d75521ba20c 100644
--- a/drivers/clk/mediatek/clk-gate.h
+++ b/drivers/clk/mediatek/clk-gate.h
@@ -7,42 +7,15 @@
#ifndef __DRV_CLK_GATE_H
#define __DRV_CLK_GATE_H

-#include <linux/regmap.h>
#include <linux/clk-provider.h>
-
-struct clk;
-
-struct mtk_clk_gate {
- struct clk_hw hw;
- struct regmap *regmap;
- int set_ofs;
- int clr_ofs;
- int sta_ofs;
- u8 bit;
-};
-
-static inline struct mtk_clk_gate *to_mtk_clk_gate(struct clk_hw *hw)
-{
- return container_of(hw, struct mtk_clk_gate, hw);
-}
+#include <linux/device.h>
+#include <linux/of.h>

extern const struct clk_ops mtk_clk_gate_ops_setclr;
extern const struct clk_ops mtk_clk_gate_ops_setclr_inv;
extern const struct clk_ops mtk_clk_gate_ops_no_setclr;
extern const struct clk_ops mtk_clk_gate_ops_no_setclr_inv;

-struct clk *mtk_clk_register_gate(
- const char *name,
- const char *parent_name,
- struct regmap *regmap,
- int set_ofs,
- int clr_ofs,
- int sta_ofs,
- u8 bit,
- const struct clk_ops *ops,
- unsigned long flags,
- struct device *dev);
-
struct mtk_gate_regs {
u32 sta_ofs;
u32 clr_ofs;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:55:31

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 04/31] clk: mediatek: gate: Implement unregister API

The gate clk type within the MediaTek clk driver library only has a
register function, and no corresponding unregister function. This
means there is no way for its users to properly implement cleanup
and removal.

Add a matching unregister function for the gate type clk.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-gate.c | 35 +++++++++++++++++++++++++++++++++
drivers/clk/mediatek/clk-gate.h | 3 +++
2 files changed, 38 insertions(+)

diff --git a/drivers/clk/mediatek/clk-gate.c b/drivers/clk/mediatek/clk-gate.c
index e51cfd8c653c..ed1ad5535d41 100644
--- a/drivers/clk/mediatek/clk-gate.c
+++ b/drivers/clk/mediatek/clk-gate.c
@@ -193,6 +193,21 @@ static struct clk *mtk_clk_register_gate(const char *name,
return clk;
}

+static void mtk_clk_unregister_gate(struct clk *clk)
+{
+ struct mtk_clk_gate *cg;
+ struct clk_hw *hw;
+
+ hw = __clk_get_hw(clk);
+ if (!hw)
+ return;
+
+ cg = to_mtk_clk_gate(hw);
+
+ clk_unregister(clk);
+ kfree(cg);
+}
+
int mtk_clk_register_gates_with_dev(struct device_node *node,
const struct mtk_gate *clks, int num,
struct clk_onecell_data *clk_data,
@@ -244,4 +259,24 @@ int mtk_clk_register_gates(struct device_node *node,
}
EXPORT_SYMBOL_GPL(mtk_clk_register_gates);

+void mtk_clk_unregister_gates(const struct mtk_gate *clks, int num,
+ struct clk_onecell_data *clk_data)
+{
+ int i;
+
+ if (!clk_data)
+ return;
+
+ for (i = num; i > 0; i--) {
+ const struct mtk_gate *gate = &clks[i - 1];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[gate->id]))
+ continue;
+
+ mtk_clk_unregister_gate(clk_data->clks[gate->id]);
+ clk_data->clks[gate->id] = ERR_PTR(-ENOENT);
+ }
+}
+EXPORT_SYMBOL_GPL(mtk_clk_unregister_gates);
+
MODULE_LICENSE("GPL");
diff --git a/drivers/clk/mediatek/clk-gate.h b/drivers/clk/mediatek/clk-gate.h
index 3d75521ba20c..344adffb6578 100644
--- a/drivers/clk/mediatek/clk-gate.h
+++ b/drivers/clk/mediatek/clk-gate.h
@@ -55,4 +55,7 @@ int mtk_clk_register_gates_with_dev(struct device_node *node,
struct clk_onecell_data *clk_data,
struct device *dev);

+void mtk_clk_unregister_gates(const struct mtk_gate *clks, int num,
+ struct clk_onecell_data *clk_data);
+
#endif /* __DRV_CLK_GATE_H */
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:55:31

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 02/31] clk: mediatek: gate: Consolidate gate type clk related code

Right now some bits of the gate type clk code are in clk-gate.[ch], but
other bits are in clk-mtk.[ch]. This is different from the cpumux and
mux type clks, for which all of the code are found in the same files.

Move the functions that register multiple clks from a given list,
mtk_clk_register_gates_with_dev() and mtk_clk_register_gates(), to
clk-gate.[ch] to consolidate all the code for the gate type clks.

This commit only moves code with minor whitespace fixups to correct
the code style. Further improvements, such as internalizing various
functions and structures will be done in later commits.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-gate.c | 53 ++++++++++++++++++++++++++++++++-
drivers/clk/mediatek/clk-gate.h | 25 ++++++++++++++++
drivers/clk/mediatek/clk-mtk.c | 51 -------------------------------
drivers/clk/mediatek/clk-mtk.h | 25 ----------------
4 files changed, 77 insertions(+), 77 deletions(-)

diff --git a/drivers/clk/mediatek/clk-gate.c b/drivers/clk/mediatek/clk-gate.c
index 5d88b428565b..54921768bfba 100644
--- a/drivers/clk/mediatek/clk-gate.c
+++ b/drivers/clk/mediatek/clk-gate.c
@@ -11,9 +11,9 @@
#include <linux/slab.h>
#include <linux/delay.h>
#include <linux/clkdev.h>
+#include <linux/mfd/syscon.h>
#include <linux/module.h>

-#include "clk-mtk.h"
#include "clk-gate.h"

static u32 mtk_get_clockgating(struct clk_hw *hw)
@@ -182,4 +182,55 @@ struct clk *mtk_clk_register_gate(
}
EXPORT_SYMBOL_GPL(mtk_clk_register_gate);

+int mtk_clk_register_gates_with_dev(struct device_node *node,
+ const struct mtk_gate *clks, int num,
+ struct clk_onecell_data *clk_data,
+ struct device *dev)
+{
+ int i;
+ struct clk *clk;
+ struct regmap *regmap;
+
+ if (!clk_data)
+ return -ENOMEM;
+
+ regmap = device_node_to_regmap(node);
+ if (IS_ERR(regmap)) {
+ pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
+ return PTR_ERR(regmap);
+ }
+
+ for (i = 0; i < num; i++) {
+ const struct mtk_gate *gate = &clks[i];
+
+ if (!IS_ERR_OR_NULL(clk_data->clks[gate->id]))
+ continue;
+
+ clk = mtk_clk_register_gate(gate->name, gate->parent_name,
+ regmap,
+ gate->regs->set_ofs,
+ gate->regs->clr_ofs,
+ gate->regs->sta_ofs,
+ gate->shift, gate->ops,
+ gate->flags, dev);
+
+ if (IS_ERR(clk)) {
+ pr_err("Failed to register clk %s: %pe\n", gate->name, clk);
+ continue;
+ }
+
+ clk_data->clks[gate->id] = clk;
+ }
+
+ return 0;
+}
+
+int mtk_clk_register_gates(struct device_node *node,
+ const struct mtk_gate *clks, int num,
+ struct clk_onecell_data *clk_data)
+{
+ return mtk_clk_register_gates_with_dev(node, clks, num, clk_data, NULL);
+}
+EXPORT_SYMBOL_GPL(mtk_clk_register_gates);
+
MODULE_LICENSE("GPL");
diff --git a/drivers/clk/mediatek/clk-gate.h b/drivers/clk/mediatek/clk-gate.h
index 3c3329ec54b7..432b571d23b3 100644
--- a/drivers/clk/mediatek/clk-gate.h
+++ b/drivers/clk/mediatek/clk-gate.h
@@ -43,6 +43,22 @@ struct clk *mtk_clk_register_gate(
unsigned long flags,
struct device *dev);

+struct mtk_gate_regs {
+ u32 sta_ofs;
+ u32 clr_ofs;
+ u32 set_ofs;
+};
+
+struct mtk_gate {
+ int id;
+ const char *name;
+ const char *parent_name;
+ const struct mtk_gate_regs *regs;
+ int shift;
+ const struct clk_ops *ops;
+ unsigned long flags;
+};
+
#define GATE_MTK_FLAGS(_id, _name, _parent, _regs, _shift, \
_ops, _flags) { \
.id = _id, \
@@ -57,4 +73,13 @@ struct clk *mtk_clk_register_gate(
#define GATE_MTK(_id, _name, _parent, _regs, _shift, _ops) \
GATE_MTK_FLAGS(_id, _name, _parent, _regs, _shift, _ops, 0)

+int mtk_clk_register_gates(struct device_node *node,
+ const struct mtk_gate *clks, int num,
+ struct clk_onecell_data *clk_data);
+
+int mtk_clk_register_gates_with_dev(struct device_node *node,
+ const struct mtk_gate *clks, int num,
+ struct clk_onecell_data *clk_data,
+ struct device *dev);
+
#endif /* __DRV_CLK_GATE_H */
diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index 519a461cbb6f..0c5db3c71fdd 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -106,57 +106,6 @@ void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
}
EXPORT_SYMBOL_GPL(mtk_clk_register_factors);

-int mtk_clk_register_gates_with_dev(struct device_node *node,
- const struct mtk_gate *clks,
- int num, struct clk_onecell_data *clk_data,
- struct device *dev)
-{
- int i;
- struct clk *clk;
- struct regmap *regmap;
-
- if (!clk_data)
- return -ENOMEM;
-
- regmap = device_node_to_regmap(node);
- if (IS_ERR(regmap)) {
- pr_err("Cannot find regmap for %pOF: %pe\n", node, regmap);
- return PTR_ERR(regmap);
- }
-
- for (i = 0; i < num; i++) {
- const struct mtk_gate *gate = &clks[i];
-
- if (!IS_ERR_OR_NULL(clk_data->clks[gate->id]))
- continue;
-
- clk = mtk_clk_register_gate(gate->name, gate->parent_name,
- regmap,
- gate->regs->set_ofs,
- gate->regs->clr_ofs,
- gate->regs->sta_ofs,
- gate->shift, gate->ops, gate->flags, dev);
-
- if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %pe\n", gate->name, clk);
- continue;
- }
-
- clk_data->clks[gate->id] = clk;
- }
-
- return 0;
-}
-
-int mtk_clk_register_gates(struct device_node *node,
- const struct mtk_gate *clks,
- int num, struct clk_onecell_data *clk_data)
-{
- return mtk_clk_register_gates_with_dev(node,
- clks, num, clk_data, NULL);
-}
-EXPORT_SYMBOL_GPL(mtk_clk_register_gates);
-
struct clk *mtk_clk_register_composite(const struct mtk_composite *mc,
void __iomem *base, spinlock_t *lock)
{
diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
index 0ff289d93452..bdec7dc5e07a 100644
--- a/drivers/clk/mediatek/clk-mtk.h
+++ b/drivers/clk/mediatek/clk-mtk.h
@@ -150,31 +150,6 @@ void mtk_clk_register_composites(const struct mtk_composite *mcs,
int num, void __iomem *base, spinlock_t *lock,
struct clk_onecell_data *clk_data);

-struct mtk_gate_regs {
- u32 sta_ofs;
- u32 clr_ofs;
- u32 set_ofs;
-};
-
-struct mtk_gate {
- int id;
- const char *name;
- const char *parent_name;
- const struct mtk_gate_regs *regs;
- int shift;
- const struct clk_ops *ops;
- unsigned long flags;
-};
-
-int mtk_clk_register_gates(struct device_node *node,
- const struct mtk_gate *clks, int num,
- struct clk_onecell_data *clk_data);
-
-int mtk_clk_register_gates_with_dev(struct device_node *node,
- const struct mtk_gate *clks,
- int num, struct clk_onecell_data *clk_data,
- struct device *dev);
-
struct mtk_clk_divider {
int id;
const char *name;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:55:38

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 05/31] clk: mediatek: gate: Clean up included headers

Some included headers aren't actually used anywhere, while other headers
with the declaration of functions and structures aren't directly
included.

Get rid of the unused ones, and add the ones that should be included
directly.

On the header side, replace headers that are included purely for data
structure definitions with forward declarations. This decreases the
amount of preprocessing and compilation effort required for each
inclusion.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-gate.c | 11 +++--------
drivers/clk/mediatek/clk-gate.h | 10 +++++++---
2 files changed, 10 insertions(+), 11 deletions(-)

diff --git a/drivers/clk/mediatek/clk-gate.c b/drivers/clk/mediatek/clk-gate.c
index ed1ad5535d41..e8881ae1489a 100644
--- a/drivers/clk/mediatek/clk-gate.c
+++ b/drivers/clk/mediatek/clk-gate.c
@@ -4,18 +4,13 @@
* Author: James Liao <[email protected]>
*/

-#include <linux/of.h>
-#include <linux/of_address.h>
-
-#include <linux/io.h>
-#include <linux/slab.h>
-#include <linux/delay.h>
-#include <linux/clkdev.h>
#include <linux/clk-provider.h>
-#include <linux/container_of.h>
#include <linux/mfd/syscon.h>
#include <linux/module.h>
+#include <linux/printk.h>
#include <linux/regmap.h>
+#include <linux/slab.h>
+#include <linux/types.h>

#include "clk-gate.h"

diff --git a/drivers/clk/mediatek/clk-gate.h b/drivers/clk/mediatek/clk-gate.h
index 344adffb6578..6b5738826a22 100644
--- a/drivers/clk/mediatek/clk-gate.h
+++ b/drivers/clk/mediatek/clk-gate.h
@@ -7,9 +7,13 @@
#ifndef __DRV_CLK_GATE_H
#define __DRV_CLK_GATE_H

-#include <linux/clk-provider.h>
-#include <linux/device.h>
-#include <linux/of.h>
+#include <linux/types.h>
+
+struct clk;
+struct clk_onecell_data;
+struct clk_ops;
+struct device;
+struct device_node;

extern const struct clk_ops mtk_clk_gate_ops_setclr;
extern const struct clk_ops mtk_clk_gate_ops_setclr_inv;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:55:48

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 07/31] clk: mediatek: cpumux: Internalize struct mtk_clk_cpumux

struct mtk_clk_cpumux is an implementation detail of the cpumux clk
type, and is not used outside of the implementation.

Internalize the definition to minimize leakage of details and shrink
the header file.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-cpumux.c | 8 ++++++++
drivers/clk/mediatek/clk-cpumux.h | 8 --------
2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/clk/mediatek/clk-cpumux.c b/drivers/clk/mediatek/clk-cpumux.c
index 99a819e3673f..344c6399b22f 100644
--- a/drivers/clk/mediatek/clk-cpumux.c
+++ b/drivers/clk/mediatek/clk-cpumux.c
@@ -12,6 +12,14 @@
#include "clk-mtk.h"
#include "clk-cpumux.h"

+struct mtk_clk_cpumux {
+ struct clk_hw hw;
+ struct regmap *regmap;
+ u32 reg;
+ u32 mask;
+ u8 shift;
+};
+
static inline struct mtk_clk_cpumux *to_mtk_clk_cpumux(struct clk_hw *_hw)
{
return container_of(_hw, struct mtk_clk_cpumux, hw);
diff --git a/drivers/clk/mediatek/clk-cpumux.h b/drivers/clk/mediatek/clk-cpumux.h
index fda7770fd803..a538f2bbef0d 100644
--- a/drivers/clk/mediatek/clk-cpumux.h
+++ b/drivers/clk/mediatek/clk-cpumux.h
@@ -7,14 +7,6 @@
#ifndef __DRV_CLK_CPUMUX_H
#define __DRV_CLK_CPUMUX_H

-struct mtk_clk_cpumux {
- struct clk_hw hw;
- struct regmap *regmap;
- u32 reg;
- u32 mask;
- u8 shift;
-};
-
int mtk_clk_register_cpumuxes(struct device_node *node,
const struct mtk_composite *clks, int num,
struct clk_onecell_data *clk_data);
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:56:00

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 11/31] clk: mediatek: mux: Clean up included headers

Some included headers aren't actually used anywhere, while other headers
with the declaration of functions and structures aren't directly
included.

Get rid of the unused ones, and add the ones that should be included
directly.

On the header side, replace headers that are included purely for data
structure definitions with forward declarations. This decreases the
amount of preprocessing and compilation effort required for each
inclusion.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mux.c | 11 +++++++----
drivers/clk/mediatek/clk-mux.h | 8 +++++++-
2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
index 2d4d8dc0120a..01af6a52711a 100644
--- a/drivers/clk/mediatek/clk-mux.c
+++ b/drivers/clk/mediatek/clk-mux.c
@@ -4,13 +4,16 @@
* Author: Owen Chen <[email protected]>
*/

-#include <linux/of.h>
-#include <linux/of_address.h>
-#include <linux/slab.h>
+#include <linux/clk-provider.h>
+#include <linux/compiler_types.h>
+#include <linux/container_of.h>
+#include <linux/err.h>
#include <linux/mfd/syscon.h>
#include <linux/module.h>
+#include <linux/regmap.h>
+#include <linux/spinlock.h>
+#include <linux/slab.h>

-#include "clk-mtk.h"
#include "clk-mux.h"

struct mtk_clk_mux {
diff --git a/drivers/clk/mediatek/clk-mux.h b/drivers/clk/mediatek/clk-mux.h
index 38a2b6014b08..903a3c937959 100644
--- a/drivers/clk/mediatek/clk-mux.h
+++ b/drivers/clk/mediatek/clk-mux.h
@@ -7,7 +7,13 @@
#ifndef __DRV_CLK_MTK_MUX_H
#define __DRV_CLK_MTK_MUX_H

-#include <linux/clk-provider.h>
+#include <linux/spinlock.h>
+#include <linux/types.h>
+
+struct clk;
+struct clk_onecell_data;
+struct clk_ops;
+struct device_node;

struct mtk_mux {
int id;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:56:04

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 10/31] clk: mediatek: mux: Internalize struct mtk_clk_mux

struct mtk_clk_mux is an implementation detail of the mux clk type,
and is not used outside of the implementation.

Internalize the definition to minimize leakage of details and shrink
the header file.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mux.c | 8 ++++++++
drivers/clk/mediatek/clk-mux.h | 8 --------
2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
index 6f0c22a699c3..2d4d8dc0120a 100644
--- a/drivers/clk/mediatek/clk-mux.c
+++ b/drivers/clk/mediatek/clk-mux.c
@@ -13,6 +13,14 @@
#include "clk-mtk.h"
#include "clk-mux.h"

+struct mtk_clk_mux {
+ struct clk_hw hw;
+ struct regmap *regmap;
+ const struct mtk_mux *data;
+ spinlock_t *lock;
+ bool reparent;
+};
+
static inline struct mtk_clk_mux *to_mtk_clk_mux(struct clk_hw *hw)
{
return container_of(hw, struct mtk_clk_mux, hw);
diff --git a/drivers/clk/mediatek/clk-mux.h b/drivers/clk/mediatek/clk-mux.h
index cb2ac4f04c58..38a2b6014b08 100644
--- a/drivers/clk/mediatek/clk-mux.h
+++ b/drivers/clk/mediatek/clk-mux.h
@@ -9,14 +9,6 @@

#include <linux/clk-provider.h>

-struct mtk_clk_mux {
- struct clk_hw hw;
- struct regmap *regmap;
- const struct mtk_mux *data;
- spinlock_t *lock;
- bool reparent;
-};
-
struct mtk_mux {
int id;
const char *name;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:56:06

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 09/31] clk: mediatek: mux: Implement unregister API

The mux clk type within the MediaTek clk driver library only has a
register function, and no corresponding unregister function. This
means there is no way for its users to properly implement cleanup
and removal.

Add a matching unregister function for the mux type clk.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mux.c | 35 ++++++++++++++++++++++++++++++++++
drivers/clk/mediatek/clk-mux.h | 3 +++
2 files changed, 38 insertions(+)

diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
index 89f23e111d91..6f0c22a699c3 100644
--- a/drivers/clk/mediatek/clk-mux.c
+++ b/drivers/clk/mediatek/clk-mux.c
@@ -164,6 +164,21 @@ static struct clk *mtk_clk_register_mux(const struct mtk_mux *mux,
return clk;
}

+static void mtk_clk_unregister_mux(struct clk *clk)
+{
+ struct mtk_clk_mux *mux;
+ struct clk_hw *hw;
+
+ hw = __clk_get_hw(clk);
+ if (!hw)
+ return;
+
+ mux = to_mtk_clk_mux(hw);
+
+ clk_unregister(clk);
+ kfree(mux);
+}
+
int mtk_clk_register_muxes(const struct mtk_mux *muxes,
int num, struct device_node *node,
spinlock_t *lock,
@@ -198,4 +213,24 @@ int mtk_clk_register_muxes(const struct mtk_mux *muxes,
}
EXPORT_SYMBOL_GPL(mtk_clk_register_muxes);

+void mtk_clk_unregister_muxes(const struct mtk_mux *muxes, int num,
+ struct clk_onecell_data *clk_data)
+{
+ int i;
+
+ if (!clk_data)
+ return;
+
+ for (i = num; i > 0; i--) {
+ const struct mtk_mux *mux = &muxes[i - 1];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[mux->id]))
+ continue;
+
+ mtk_clk_unregister_mux(clk_data->clks[mux->id]);
+ clk_data->clks[mux->id] = ERR_PTR(-ENOENT);
+ }
+}
+EXPORT_SYMBOL_GPL(mtk_clk_unregister_muxes);
+
MODULE_LICENSE("GPL");
diff --git a/drivers/clk/mediatek/clk-mux.h b/drivers/clk/mediatek/clk-mux.h
index 27841d649118..cb2ac4f04c58 100644
--- a/drivers/clk/mediatek/clk-mux.h
+++ b/drivers/clk/mediatek/clk-mux.h
@@ -88,4 +88,7 @@ int mtk_clk_register_muxes(const struct mtk_mux *muxes,
spinlock_t *lock,
struct clk_onecell_data *clk_data);

+void mtk_clk_unregister_muxes(const struct mtk_mux *muxes, int num,
+ struct clk_onecell_data *clk_data);
+
#endif /* __DRV_CLK_MTK_MUX_H */
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:56:10

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 12/31] clk: mediatek: pll: Split definitions into separate header file

When the PLL type clk was implemented in the MediaTek clk driver
library, the data structure definitions and function declaration
were put in the common header file.

Since it is its own type of clk, and not all platform clk drivers
utilize it, having the definitions in the common header results
in wasted cycles during compilation.

Split out the related definitions and declarations into its own
header file, and include that only in the platform clk drivers that
need it.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mt2701.c | 5 +-
drivers/clk/mediatek/clk-mt2712.c | 3 +-
drivers/clk/mediatek/clk-mt6765.c | 3 +-
drivers/clk/mediatek/clk-mt6779.c | 3 +-
drivers/clk/mediatek/clk-mt6797.c | 3 +-
drivers/clk/mediatek/clk-mt7622.c | 5 +-
drivers/clk/mediatek/clk-mt7629.c | 5 +-
drivers/clk/mediatek/clk-mt7986-apmixed.c | 4 +-
drivers/clk/mediatek/clk-mt8135.c | 3 +-
drivers/clk/mediatek/clk-mt8167.c | 3 +-
drivers/clk/mediatek/clk-mt8173.c | 5 +-
drivers/clk/mediatek/clk-mt8183.c | 3 +-
drivers/clk/mediatek/clk-mt8192.c | 3 +-
drivers/clk/mediatek/clk-mt8195-apmixedsys.c | 1 +
drivers/clk/mediatek/clk-mt8195-apusys_pll.c | 1 +
drivers/clk/mediatek/clk-mt8516.c | 3 +-
drivers/clk/mediatek/clk-mtk.h | 39 --------------
drivers/clk/mediatek/clk-pll.c | 1 +
drivers/clk/mediatek/clk-pll.h | 55 ++++++++++++++++++++
19 files changed, 91 insertions(+), 57 deletions(-)
create mode 100644 drivers/clk/mediatek/clk-pll.h

diff --git a/drivers/clk/mediatek/clk-mt2701.c b/drivers/clk/mediatek/clk-mt2701.c
index 695be0f77427..1eb3e4563c3f 100644
--- a/drivers/clk/mediatek/clk-mt2701.c
+++ b/drivers/clk/mediatek/clk-mt2701.c
@@ -10,9 +10,10 @@
#include <linux/of_device.h>
#include <linux/platform_device.h>

-#include "clk-mtk.h"
-#include "clk-gate.h"
#include "clk-cpumux.h"
+#include "clk-gate.h"
+#include "clk-mtk.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt2701-clk.h>

diff --git a/drivers/clk/mediatek/clk-mt2712.c b/drivers/clk/mediatek/clk-mt2712.c
index a3bd9a107209..ff72b9ab945b 100644
--- a/drivers/clk/mediatek/clk-mt2712.c
+++ b/drivers/clk/mediatek/clk-mt2712.c
@@ -13,8 +13,9 @@
#include <linux/platform_device.h>
#include <linux/slab.h>

-#include "clk-mtk.h"
#include "clk-gate.h"
+#include "clk-pll.h"
+#include "clk-mtk.h"

#include <dt-bindings/clock/mt2712-clk.h>

diff --git a/drivers/clk/mediatek/clk-mt6765.c b/drivers/clk/mediatek/clk-mt6765.c
index d77ea5aff292..24829ca3bd1f 100644
--- a/drivers/clk/mediatek/clk-mt6765.c
+++ b/drivers/clk/mediatek/clk-mt6765.c
@@ -12,9 +12,10 @@
#include <linux/of_device.h>
#include <linux/platform_device.h>

-#include "clk-mtk.h"
#include "clk-gate.h"
+#include "clk-mtk.h"
#include "clk-mux.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt6765-clk.h>

diff --git a/drivers/clk/mediatek/clk-mt6779.c b/drivers/clk/mediatek/clk-mt6779.c
index 9825385c9f94..7b61664da18f 100644
--- a/drivers/clk/mediatek/clk-mt6779.c
+++ b/drivers/clk/mediatek/clk-mt6779.c
@@ -10,9 +10,10 @@
#include <linux/of_device.h>
#include <linux/platform_device.h>

+#include "clk-gate.h"
#include "clk-mtk.h"
#include "clk-mux.h"
-#include "clk-gate.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt6779-clk.h>

diff --git a/drivers/clk/mediatek/clk-mt6797.c b/drivers/clk/mediatek/clk-mt6797.c
index 428eb24ffec5..02259e81625a 100644
--- a/drivers/clk/mediatek/clk-mt6797.c
+++ b/drivers/clk/mediatek/clk-mt6797.c
@@ -9,8 +9,9 @@
#include <linux/of_device.h>
#include <linux/platform_device.h>

-#include "clk-mtk.h"
#include "clk-gate.h"
+#include "clk-mtk.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt6797-clk.h>

diff --git a/drivers/clk/mediatek/clk-mt7622.c b/drivers/clk/mediatek/clk-mt7622.c
index ef5947e15c75..0e1fb30a1e98 100644
--- a/drivers/clk/mediatek/clk-mt7622.c
+++ b/drivers/clk/mediatek/clk-mt7622.c
@@ -11,9 +11,10 @@
#include <linux/of_device.h>
#include <linux/platform_device.h>

-#include "clk-mtk.h"
-#include "clk-gate.h"
#include "clk-cpumux.h"
+#include "clk-gate.h"
+#include "clk-mtk.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt7622-clk.h>
#include <linux/clk.h> /* for consumer */
diff --git a/drivers/clk/mediatek/clk-mt7629.c b/drivers/clk/mediatek/clk-mt7629.c
index a0ee079670c7..c0e023bf31eb 100644
--- a/drivers/clk/mediatek/clk-mt7629.c
+++ b/drivers/clk/mediatek/clk-mt7629.c
@@ -12,9 +12,10 @@
#include <linux/of_device.h>
#include <linux/platform_device.h>

-#include "clk-mtk.h"
-#include "clk-gate.h"
#include "clk-cpumux.h"
+#include "clk-gate.h"
+#include "clk-mtk.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt7629-clk.h>

diff --git a/drivers/clk/mediatek/clk-mt7986-apmixed.c b/drivers/clk/mediatek/clk-mt7986-apmixed.c
index 98ec3887585f..21d4c82e782a 100644
--- a/drivers/clk/mediatek/clk-mt7986-apmixed.c
+++ b/drivers/clk/mediatek/clk-mt7986-apmixed.c
@@ -10,9 +10,11 @@
#include <linux/of_address.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
-#include "clk-mtk.h"
+
#include "clk-gate.h"
+#include "clk-mtk.h"
#include "clk-mux.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt7986-clk.h>
#include <linux/clk.h>
diff --git a/drivers/clk/mediatek/clk-mt8135.c b/drivers/clk/mediatek/clk-mt8135.c
index 9b4b645aea99..09ad272d51f1 100644
--- a/drivers/clk/mediatek/clk-mt8135.c
+++ b/drivers/clk/mediatek/clk-mt8135.c
@@ -11,8 +11,9 @@
#include <linux/mfd/syscon.h>
#include <dt-bindings/clock/mt8135-clk.h>

-#include "clk-mtk.h"
#include "clk-gate.h"
+#include "clk-mtk.h"
+#include "clk-pll.h"

static DEFINE_SPINLOCK(mt8135_clk_lock);

diff --git a/drivers/clk/mediatek/clk-mt8167.c b/drivers/clk/mediatek/clk-mt8167.c
index e5ea10e31799..812b33a57530 100644
--- a/drivers/clk/mediatek/clk-mt8167.c
+++ b/drivers/clk/mediatek/clk-mt8167.c
@@ -12,8 +12,9 @@
#include <linux/slab.h>
#include <linux/mfd/syscon.h>

-#include "clk-mtk.h"
#include "clk-gate.h"
+#include "clk-mtk.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt8167-clk.h>

diff --git a/drivers/clk/mediatek/clk-mt8173.c b/drivers/clk/mediatek/clk-mt8173.c
index 8f898ac476c0..46b7655feeaa 100644
--- a/drivers/clk/mediatek/clk-mt8173.c
+++ b/drivers/clk/mediatek/clk-mt8173.c
@@ -8,9 +8,10 @@
#include <linux/of.h>
#include <linux/of_address.h>

-#include "clk-mtk.h"
-#include "clk-gate.h"
#include "clk-cpumux.h"
+#include "clk-gate.h"
+#include "clk-mtk.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt8173-clk.h>

diff --git a/drivers/clk/mediatek/clk-mt8183.c b/drivers/clk/mediatek/clk-mt8183.c
index 5046852eb0fd..68496554dd3d 100644
--- a/drivers/clk/mediatek/clk-mt8183.c
+++ b/drivers/clk/mediatek/clk-mt8183.c
@@ -11,9 +11,10 @@
#include <linux/platform_device.h>
#include <linux/slab.h>

+#include "clk-gate.h"
#include "clk-mtk.h"
#include "clk-mux.h"
-#include "clk-gate.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt8183-clk.h>

diff --git a/drivers/clk/mediatek/clk-mt8192.c b/drivers/clk/mediatek/clk-mt8192.c
index cbc7c6dbe0f4..5f998aab3bfd 100644
--- a/drivers/clk/mediatek/clk-mt8192.c
+++ b/drivers/clk/mediatek/clk-mt8192.c
@@ -12,9 +12,10 @@
#include <linux/platform_device.h>
#include <linux/slab.h>

+#include "clk-gate.h"
#include "clk-mtk.h"
#include "clk-mux.h"
-#include "clk-gate.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt8192-clk.h>

diff --git a/drivers/clk/mediatek/clk-mt8195-apmixedsys.c b/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
index 6156ceeed71e..5b1b7dc447eb 100644
--- a/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
+++ b/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
@@ -5,6 +5,7 @@

#include "clk-gate.h"
#include "clk-mtk.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt8195-clk.h>
#include <linux/of_device.h>
diff --git a/drivers/clk/mediatek/clk-mt8195-apusys_pll.c b/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
index f1c84186346e..db449ff877d7 100644
--- a/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
+++ b/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
@@ -4,6 +4,7 @@
// Author: Chun-Jie Chen <[email protected]>

#include "clk-mtk.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt8195-clk.h>
#include <linux/clk-provider.h>
diff --git a/drivers/clk/mediatek/clk-mt8516.c b/drivers/clk/mediatek/clk-mt8516.c
index 9d4261ecc760..a37143f920ce 100644
--- a/drivers/clk/mediatek/clk-mt8516.c
+++ b/drivers/clk/mediatek/clk-mt8516.c
@@ -11,8 +11,9 @@
#include <linux/slab.h>
#include <linux/mfd/syscon.h>

-#include "clk-mtk.h"
#include "clk-gate.h"
+#include "clk-mtk.h"
+#include "clk-pll.h"

#include <dt-bindings/clock/mt8516-clk.h>

diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
index bdec7dc5e07a..168220f85489 100644
--- a/drivers/clk/mediatek/clk-mtk.h
+++ b/drivers/clk/mediatek/clk-mtk.h
@@ -179,45 +179,6 @@ void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
struct clk_onecell_data *mtk_alloc_clk_data(unsigned int clk_num);
void mtk_free_clk_data(struct clk_onecell_data *clk_data);

-#define HAVE_RST_BAR BIT(0)
-#define PLL_AO BIT(1)
-
-struct mtk_pll_div_table {
- u32 div;
- unsigned long freq;
-};
-
-struct mtk_pll_data {
- int id;
- const char *name;
- u32 reg;
- u32 pwr_reg;
- u32 en_mask;
- u32 pd_reg;
- u32 tuner_reg;
- u32 tuner_en_reg;
- u8 tuner_en_bit;
- int pd_shift;
- unsigned int flags;
- const struct clk_ops *ops;
- u32 rst_bar_mask;
- unsigned long fmin;
- unsigned long fmax;
- int pcwbits;
- int pcwibits;
- u32 pcw_reg;
- int pcw_shift;
- u32 pcw_chg_reg;
- const struct mtk_pll_div_table *div_table;
- const char *parent_name;
- u32 en_reg;
- u8 pll_en_bit; /* Assume 0, indicates BIT(0) by default */
-};
-
-void mtk_clk_register_plls(struct device_node *node,
- const struct mtk_pll_data *plls, int num_plls,
- struct clk_onecell_data *clk_data);
-
struct clk *mtk_clk_register_ref2usb_tx(const char *name,
const char *parent_name, void __iomem *reg);

diff --git a/drivers/clk/mediatek/clk-pll.c b/drivers/clk/mediatek/clk-pll.c
index f04f724e12e5..64f59554bc9b 100644
--- a/drivers/clk/mediatek/clk-pll.c
+++ b/drivers/clk/mediatek/clk-pll.c
@@ -13,6 +13,7 @@
#include <linux/delay.h>

#include "clk-mtk.h"
+#include "clk-pll.h"

#define REG_CON0 0
#define REG_CON1 4
diff --git a/drivers/clk/mediatek/clk-pll.h b/drivers/clk/mediatek/clk-pll.h
new file mode 100644
index 000000000000..d01b0c38311d
--- /dev/null
+++ b/drivers/clk/mediatek/clk-pll.h
@@ -0,0 +1,55 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2014 MediaTek Inc.
+ * Author: James Liao <[email protected]>
+ */
+
+#ifndef __DRV_CLK_MTK_PLL_H
+#define __DRV_CLK_MTK_PLL_H
+
+#include <linux/types.h>
+
+struct clk_ops;
+struct clk_onecell_data;
+struct device_node;
+
+struct mtk_pll_div_table {
+ u32 div;
+ unsigned long freq;
+};
+
+#define HAVE_RST_BAR BIT(0)
+#define PLL_AO BIT(1)
+
+struct mtk_pll_data {
+ int id;
+ const char *name;
+ u32 reg;
+ u32 pwr_reg;
+ u32 en_mask;
+ u32 pd_reg;
+ u32 tuner_reg;
+ u32 tuner_en_reg;
+ u8 tuner_en_bit;
+ int pd_shift;
+ unsigned int flags;
+ const struct clk_ops *ops;
+ u32 rst_bar_mask;
+ unsigned long fmin;
+ unsigned long fmax;
+ int pcwbits;
+ int pcwibits;
+ u32 pcw_reg;
+ int pcw_shift;
+ u32 pcw_chg_reg;
+ const struct mtk_pll_div_table *div_table;
+ const char *parent_name;
+ u32 en_reg;
+ u8 pll_en_bit; /* Assume 0, indicates BIT(0) by default */
+};
+
+void mtk_clk_register_plls(struct device_node *node,
+ const struct mtk_pll_data *plls, int num_plls,
+ struct clk_onecell_data *clk_data);
+
+#endif /* __DRV_CLK_MTK_PLL_H */
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:56:15

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 13/31] clk: mediatek: pll: Implement unregister API

The PLL clk type within the MediaTek clk driver library only has a
register function, and no corresponding unregister function. This
means there is no way for its users to properly implement cleanup
and removal.

Add a matching unregister function for the PLL type clk.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-pll.c | 49 ++++++++++++++++++++++++++++++++++
drivers/clk/mediatek/clk-pll.h | 2 ++
2 files changed, 51 insertions(+)

diff --git a/drivers/clk/mediatek/clk-pll.c b/drivers/clk/mediatek/clk-pll.c
index 64f59554bc9b..081e0df8203e 100644
--- a/drivers/clk/mediatek/clk-pll.c
+++ b/drivers/clk/mediatek/clk-pll.c
@@ -360,6 +360,15 @@ static struct clk *mtk_clk_register_pll(const struct mtk_pll_data *data,
return clk;
}

+static void mtk_clk_unregister_pll(struct clk *clk)
+{
+ struct clk_hw *hw = __clk_get_hw(clk);
+ struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
+
+ clk_unregister(clk);
+ kfree(pll);
+}
+
void mtk_clk_register_plls(struct device_node *node,
const struct mtk_pll_data *plls, int num_plls, struct clk_onecell_data *clk_data)
{
@@ -388,4 +397,44 @@ void mtk_clk_register_plls(struct device_node *node,
}
EXPORT_SYMBOL_GPL(mtk_clk_register_plls);

+static __iomem void *mtk_clk_pll_get_base(struct clk *clk,
+ const struct mtk_pll_data *data)
+{
+ struct clk_hw *hw = __clk_get_hw(clk);
+ struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
+
+ return pll->base_addr - data->reg;
+}
+
+void mtk_clk_unregister_plls(const struct mtk_pll_data *plls, int num_plls,
+ struct clk_onecell_data *clk_data)
+{
+ __iomem void *base = NULL;
+ int i;
+
+ if (!clk_data)
+ return;
+
+ for (i = num_plls; i > 0; i--) {
+ const struct mtk_pll_data *pll = &plls[i - 1];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[pll->id]))
+ continue;
+
+ /*
+ * This is quite ugly but unfortunately the clks don't have
+ * any device tied to them, so there's no place to store the
+ * pointer to the I/O region base address. We have to fetch
+ * it from one of the registered clks.
+ */
+ base = mtk_clk_pll_get_base(clk_data->clks[pll->id], pll);
+
+ mtk_clk_unregister_pll(clk_data->clks[pll->id]);
+ clk_data->clks[pll->id] = ERR_PTR(-ENOENT);
+ }
+
+ iounmap(base);
+}
+EXPORT_SYMBOL_GPL(mtk_clk_unregister_plls);
+
MODULE_LICENSE("GPL");
diff --git a/drivers/clk/mediatek/clk-pll.h b/drivers/clk/mediatek/clk-pll.h
index d01b0c38311d..a889b1e472e7 100644
--- a/drivers/clk/mediatek/clk-pll.h
+++ b/drivers/clk/mediatek/clk-pll.h
@@ -51,5 +51,7 @@ struct mtk_pll_data {
void mtk_clk_register_plls(struct device_node *node,
const struct mtk_pll_data *plls, int num_plls,
struct clk_onecell_data *clk_data);
+void mtk_clk_unregister_plls(const struct mtk_pll_data *plls, int num_plls,
+ struct clk_onecell_data *clk_data);

#endif /* __DRV_CLK_MTK_PLL_H */
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:56:30

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 14/31] clk: mediatek: pll: Clean up included headers

Some included headers aren't actually used anywhere, while other headers
with the declaration of functions and structures aren't directly
included.

Get rid of the unused ones, and add the ones that should be included
directly.

Also, expand the MHZ macro with spelled-out "1000 * 1000" to be able
to not include clk-mtk.h. The existing ternary operator is rewritten
in a shortened form to accommodate the expanded macro.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-pll.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/clk/mediatek/clk-pll.c b/drivers/clk/mediatek/clk-pll.c
index 081e0df8203e..9698d1c97cd6 100644
--- a/drivers/clk/mediatek/clk-pll.c
+++ b/drivers/clk/mediatek/clk-pll.c
@@ -4,15 +4,15 @@
* Author: James Liao <[email protected]>
*/

-#include <linux/of.h>
-#include <linux/of_address.h>
+#include <linux/clk-provider.h>
+#include <linux/container_of.h>
+#include <linux/delay.h>
+#include <linux/err.h>
#include <linux/io.h>
#include <linux/module.h>
+#include <linux/of_address.h>
#include <linux/slab.h>
-#include <linux/clkdev.h>
-#include <linux/delay.h>

-#include "clk-mtk.h"
#include "clk-pll.h"

#define REG_CON0 0
@@ -162,7 +162,7 @@ static void mtk_pll_set_rate_regs(struct mtk_clk_pll *pll, u32 pcw,
static void mtk_pll_calc_values(struct mtk_clk_pll *pll, u32 *pcw, u32 *postdiv,
u32 freq, u32 fin)
{
- unsigned long fmin = pll->data->fmin ? pll->data->fmin : (1000 * MHZ);
+ unsigned long fmin = pll->data->fmin ?: (1000 * 1000 * 1000);
const struct mtk_pll_div_table *div_table = pll->data->div_table;
u64 _pcw;
int ibits;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:56:40

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 15/31] clk: mediatek: Implement mtk_clk_unregister_fixed_clks() API

mtk_clk_register_fixed_clks(), as the name suggests, is used to register
a given list of fixed rate clks. However it is lacking a counterpart
unregister API.

Implement said unregister API so that the various clock platform drivers
can utilize it to do proper unregistration, cleanup and removal.

In the header file, the register function's declaration is also
reformatted to fit code style guidelines.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mtk.c | 20 ++++++++++++++++++++
drivers/clk/mediatek/clk-mtk.h | 6 ++++--
2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index 0c5db3c71fdd..7c0d5706eed7 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -80,6 +80,26 @@ void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
}
EXPORT_SYMBOL_GPL(mtk_clk_register_fixed_clks);

+void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
+ struct clk_onecell_data *clk_data)
+{
+ int i;
+
+ if (!clk_data)
+ return;
+
+ for (i = num; i > 0; i--) {
+ const struct mtk_fixed_clk *rc = &clks[i - 1];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[rc->id]))
+ continue;
+
+ clk_unregister_fixed_rate(clk_data->clks[rc->id]);
+ clk_data->clks[rc->id] = ERR_PTR(-ENOENT);
+ }
+}
+EXPORT_SYMBOL_GPL(mtk_clk_unregister_fixed_clks);
+
void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
int num, struct clk_onecell_data *clk_data)
{
diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
index 168220f85489..cc7f920eabb4 100644
--- a/drivers/clk/mediatek/clk-mtk.h
+++ b/drivers/clk/mediatek/clk-mtk.h
@@ -34,8 +34,10 @@ struct mtk_fixed_clk {
.rate = _rate, \
}

-void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
- int num, struct clk_onecell_data *clk_data);
+void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
+ struct clk_onecell_data *clk_data);
+void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
+ struct clk_onecell_data *clk_data);

struct mtk_fixed_factor {
int id;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:56:52

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 06/31] clk: mediatek: cpumux: Implement unregister API

The cpumux clk type within the MediaTek clk driver library only has
a register function, and no corresponding unregister function. This
means there is no way for its users to properly implement cleanup
and removal.

Add a matching unregister function for the cpumux type clk.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-cpumux.c | 31 +++++++++++++++++++++++++++++++
drivers/clk/mediatek/clk-cpumux.h | 3 +++
2 files changed, 34 insertions(+)

diff --git a/drivers/clk/mediatek/clk-cpumux.c b/drivers/clk/mediatek/clk-cpumux.c
index cab5095416b6..99a819e3673f 100644
--- a/drivers/clk/mediatek/clk-cpumux.c
+++ b/drivers/clk/mediatek/clk-cpumux.c
@@ -77,6 +77,21 @@ mtk_clk_register_cpumux(const struct mtk_composite *mux,
return clk;
}

+static void mtk_clk_unregister_cpumux(struct clk *clk)
+{
+ struct mtk_clk_cpumux *cpumux;
+ struct clk_hw *hw;
+
+ hw = __clk_get_hw(clk);
+ if (!hw)
+ return;
+
+ cpumux = to_mtk_clk_cpumux(hw);
+
+ clk_unregister(clk);
+ kfree(cpumux);
+}
+
int mtk_clk_register_cpumuxes(struct device_node *node,
const struct mtk_composite *clks, int num,
struct clk_onecell_data *clk_data)
@@ -106,4 +121,20 @@ int mtk_clk_register_cpumuxes(struct device_node *node,
return 0;
}

+void mtk_clk_unregister_cpumuxes(const struct mtk_composite *clks, int num,
+ struct clk_onecell_data *clk_data)
+{
+ int i;
+
+ for (i = num; i > 0; i--) {
+ const struct mtk_composite *mux = &clks[i - 1];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[mux->id]))
+ continue;
+
+ mtk_clk_unregister_cpumux(clk_data->clks[mux->id]);
+ clk_data->clks[mux->id] = ERR_PTR(-ENOENT);
+ }
+}
+
MODULE_LICENSE("GPL");
diff --git a/drivers/clk/mediatek/clk-cpumux.h b/drivers/clk/mediatek/clk-cpumux.h
index 2aaf1afd4e5f..fda7770fd803 100644
--- a/drivers/clk/mediatek/clk-cpumux.h
+++ b/drivers/clk/mediatek/clk-cpumux.h
@@ -19,4 +19,7 @@ int mtk_clk_register_cpumuxes(struct device_node *node,
const struct mtk_composite *clks, int num,
struct clk_onecell_data *clk_data);

+void mtk_clk_unregister_cpumuxes(const struct mtk_composite *clks, int num,
+ struct clk_onecell_data *clk_data);
+
#endif /* __DRV_CLK_CPUMUX_H */
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:56:59

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 16/31] clk: mediatek: Implement mtk_clk_unregister_factors() API

mtk_clk_register_factors(), as the name suggests, is used to register
a given list of fixed factor clks. However it is lacking a counterpart
unregister API.

Implement said unregister API so that the various clock platform drivers
can utilize it to do proper unregistration, cleanup and removal.

In the header file, the register function's declaration is also
reformatted to fit code style guidelines.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mtk.c | 20 ++++++++++++++++++++
drivers/clk/mediatek/clk-mtk.h | 6 ++++--
2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index 7c0d5706eed7..b267b2f04b84 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -126,6 +126,26 @@ void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
}
EXPORT_SYMBOL_GPL(mtk_clk_register_factors);

+void mtk_clk_unregister_factors(const struct mtk_fixed_factor *clks, int num,
+ struct clk_onecell_data *clk_data)
+{
+ int i;
+
+ if (!clk_data)
+ return;
+
+ for (i = num; i > 0; i--) {
+ const struct mtk_fixed_factor *ff = &clks[i - 1];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[ff->id]))
+ continue;
+
+ clk_unregister_fixed_factor(clk_data->clks[ff->id]);
+ clk_data->clks[ff->id] = ERR_PTR(-ENOENT);
+ }
+}
+EXPORT_SYMBOL_GPL(mtk_clk_unregister_factors);
+
struct clk *mtk_clk_register_composite(const struct mtk_composite *mc,
void __iomem *base, spinlock_t *lock)
{
diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
index cc7f920eabb4..4db1a97c1250 100644
--- a/drivers/clk/mediatek/clk-mtk.h
+++ b/drivers/clk/mediatek/clk-mtk.h
@@ -55,8 +55,10 @@ struct mtk_fixed_factor {
.div = _div, \
}

-void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
- int num, struct clk_onecell_data *clk_data);
+void mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
+ struct clk_onecell_data *clk_data);
+void mtk_clk_unregister_factors(const struct mtk_fixed_factor *clks, int num,
+ struct clk_onecell_data *clk_data);

struct mtk_composite {
int id;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:57:00

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 08/31] clk: mediatek: cpumux: Clean up included headers

Some headers with the declaration of functions and structures aren't
directly included. Explicitly include them so that future changes to
other headers would not result in an unexpected build break.

On the header side, add forward declarations for any data structures
whose pointers are used in function signatures. No headers are
required.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-cpumux.c | 3 +++
drivers/clk/mediatek/clk-cpumux.h | 4 ++++
2 files changed, 7 insertions(+)

diff --git a/drivers/clk/mediatek/clk-cpumux.c b/drivers/clk/mediatek/clk-cpumux.c
index 344c6399b22f..658aee789f44 100644
--- a/drivers/clk/mediatek/clk-cpumux.c
+++ b/drivers/clk/mediatek/clk-cpumux.c
@@ -5,8 +5,11 @@
*/

#include <linux/clk-provider.h>
+#include <linux/container_of.h>
+#include <linux/err.h>
#include <linux/mfd/syscon.h>
#include <linux/module.h>
+#include <linux/regmap.h>
#include <linux/slab.h>

#include "clk-mtk.h"
diff --git a/drivers/clk/mediatek/clk-cpumux.h b/drivers/clk/mediatek/clk-cpumux.h
index a538f2bbef0d..b07e89f7c283 100644
--- a/drivers/clk/mediatek/clk-cpumux.h
+++ b/drivers/clk/mediatek/clk-cpumux.h
@@ -7,6 +7,10 @@
#ifndef __DRV_CLK_CPUMUX_H
#define __DRV_CLK_CPUMUX_H

+struct clk_onecell_data;
+struct device_node;
+struct mtk_composite;
+
int mtk_clk_register_cpumuxes(struct device_node *node,
const struct mtk_composite *clks, int num,
struct clk_onecell_data *clk_data);
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:57:07

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 17/31] clk: mediatek: Implement mtk_clk_unregister_divider_clks() API

mtk_clk_register_divider_clks(), as the name suggests, is used to register
a given list of divider clks. However it is lacking a counterpart
unregister API.

Implement said unregister API so that the various clock platform drivers
can utilize it to do proper unregistration, cleanup and removal.

In the header file, the register function's declaration is also
reformatted to fit code style guidelines.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mtk.c | 19 +++++++++++++++++++
drivers/clk/mediatek/clk-mtk.h | 8 +++++---
2 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index b267b2f04b84..3a6dfe445e63 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -286,6 +286,25 @@ void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
}
}

+void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
+ struct clk_onecell_data *clk_data)
+{
+ int i;
+
+ if (!clk_data)
+ return;
+
+ for (i = num; i > 0; i--) {
+ const struct mtk_clk_divider *mcd = &mcds[i - 1];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
+ continue;
+
+ clk_unregister_divider(clk_data->clks[mcd->id]);
+ clk_data->clks[mcd->id] = ERR_PTR(-ENOENT);
+ }
+}
+
int mtk_clk_simple_probe(struct platform_device *pdev)
{
const struct mtk_clk_desc *mcd;
diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
index 4db1a97c1250..e3ae22fb0334 100644
--- a/drivers/clk/mediatek/clk-mtk.h
+++ b/drivers/clk/mediatek/clk-mtk.h
@@ -176,9 +176,11 @@ struct mtk_clk_divider {
.div_width = _width, \
}

-void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
- int num, void __iomem *base, spinlock_t *lock,
- struct clk_onecell_data *clk_data);
+void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
+ void __iomem *base, spinlock_t *lock,
+ struct clk_onecell_data *clk_data);
+void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
+ struct clk_onecell_data *clk_data);

struct clk_onecell_data *mtk_alloc_clk_data(unsigned int clk_num);
void mtk_free_clk_data(struct clk_onecell_data *clk_data);
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:57:08

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 18/31] clk: mediatek: Implement mtk_clk_unregister_composites() API

mtk_clk_register_composites(), as the name suggests, is used to register
a given list of composite clks. However it is lacking a counterpart
unregister API.

Implement said unregister API so that the various clock platform drivers
can utilize it to do proper unregistration, cleanup and removal.

In the header file, the register function's declaration is also
reformatted to fit code style guidelines.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mtk.c | 41 ++++++++++++++++++++++++++++++++++
drivers/clk/mediatek/clk-mtk.h | 2 ++
2 files changed, 43 insertions(+)

diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index 3a6dfe445e63..2150ed8678e0 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -233,6 +233,27 @@ struct clk *mtk_clk_register_composite(const struct mtk_composite *mc,
return ERR_PTR(ret);
}

+static void mtk_clk_unregister_composite(struct clk *clk)
+{
+ struct clk_hw *hw = __clk_get_hw(clk);
+ struct clk_composite *composite = to_clk_composite(hw);
+ struct clk_mux *mux = NULL;
+ struct clk_gate *gate = NULL;
+ struct clk_divider *div = NULL;
+
+ if (composite->mux_hw)
+ mux = to_clk_mux(composite->mux_hw);
+ if (composite->gate_hw)
+ gate = to_clk_gate(composite->gate_hw);
+ if (composite->rate_hw)
+ div = to_clk_divider(composite->rate_hw);
+
+ clk_unregister_composite(clk);
+ kfree(div);
+ kfree(gate);
+ kfree(mux);
+}
+
void mtk_clk_register_composites(const struct mtk_composite *mcs,
int num, void __iomem *base, spinlock_t *lock,
struct clk_onecell_data *clk_data)
@@ -259,6 +280,26 @@ void mtk_clk_register_composites(const struct mtk_composite *mcs,
}
EXPORT_SYMBOL_GPL(mtk_clk_register_composites);

+void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
+ struct clk_onecell_data *clk_data)
+{
+ int i;
+
+ if (!clk_data)
+ return;
+
+ for (i = num; i > 0; i--) {
+ const struct mtk_composite *mc = &mcs[i - 1];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[mc->id]))
+ continue;
+
+ mtk_clk_unregister_composite(clk_data->clks[mc->id]);
+ clk_data->clks[mc->id] = ERR_PTR(-ENOENT);
+ }
+}
+EXPORT_SYMBOL_GPL(mtk_clk_unregister_composites);
+
void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
int num, void __iomem *base, spinlock_t *lock,
struct clk_onecell_data *clk_data)
diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
index e3ae22fb0334..3c3a934f53cd 100644
--- a/drivers/clk/mediatek/clk-mtk.h
+++ b/drivers/clk/mediatek/clk-mtk.h
@@ -153,6 +153,8 @@ struct clk *mtk_clk_register_composite(const struct mtk_composite *mc,
void mtk_clk_register_composites(const struct mtk_composite *mcs,
int num, void __iomem *base, spinlock_t *lock,
struct clk_onecell_data *clk_data);
+void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
+ struct clk_onecell_data *clk_data);

struct mtk_clk_divider {
int id;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:57:16

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 19/31] clk: mediatek: Add mtk_clk_simple_remove()

In commit c58cd0e40ffa ("clk: mediatek: Add mtk_clk_simple_probe() to
simplify clock providers"), a generic probe function was added to
simplify clk drivers that only needed to support clk gates. However due
to the lack of unregister APIs, a corresponding remove function was not
added.

Now that the unregister APIs have been implemented, add aforementioned
remove function to make it complete.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mtk.c | 15 +++++++++++++++
drivers/clk/mediatek/clk-mtk.h | 1 +
2 files changed, 16 insertions(+)

diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index 2150ed8678e0..cd76e1d80242 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -369,6 +369,8 @@ int mtk_clk_simple_probe(struct platform_device *pdev)
if (r)
goto free_data;

+ platform_set_drvdata(pdev, clk_data);
+
return r;

free_data:
@@ -376,4 +378,17 @@ int mtk_clk_simple_probe(struct platform_device *pdev)
return r;
}

+int mtk_clk_simple_remove(struct platform_device *pdev)
+{
+ const struct mtk_clk_desc *mcd = of_device_get_match_data(&pdev->dev);
+ struct clk_onecell_data *clk_data = platform_get_drvdata(pdev);
+ struct device_node *node = pdev->dev.of_node;
+
+ of_clk_del_provider(node);
+ mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
+ mtk_free_clk_data(clk_data);
+
+ return 0;
+}
+
MODULE_LICENSE("GPL");
diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
index 3c3a934f53cd..4fa658f5d934 100644
--- a/drivers/clk/mediatek/clk-mtk.h
+++ b/drivers/clk/mediatek/clk-mtk.h
@@ -202,5 +202,6 @@ struct mtk_clk_desc {
};

int mtk_clk_simple_probe(struct platform_device *pdev);
+int mtk_clk_simple_remove(struct platform_device *pdev);

#endif /* __DRV_CLK_MTK_H */
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:57:32

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 20/31] clk: mediatek: mtk: Clean up included headers

Some included headers aren't actually used anywhere, while other headers
with the declaration of functions and structures aren't directly
included.

Get rid of the unused ones, and add the ones that should be included
directly.

On the header side, replace headers that are included purely for data
structure definitions with forward declarations. This decreases the
amount of preprocessing and compilation effort required for each
inclusion.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mtk.c | 13 ++++++-------
drivers/clk/mediatek/clk-mtk.h | 12 ++++++------
2 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index cd76e1d80242..d2c50186cceb 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -4,17 +4,16 @@
* Author: James Liao <[email protected]>
*/

-#include <linux/of.h>
-#include <linux/of_address.h>
+#include <linux/bitops.h>
+#include <linux/clk-provider.h>
#include <linux/err.h>
#include <linux/io.h>
-#include <linux/slab.h>
-#include <linux/delay.h>
-#include <linux/clkdev.h>
-#include <linux/module.h>
#include <linux/mfd/syscon.h>
-#include <linux/device.h>
+#include <linux/module.h>
+#include <linux/of.h>
#include <linux/of_device.h>
+#include <linux/platform_device.h>
+#include <linux/slab.h>

#include "clk-mtk.h"
#include "clk-gate.h"
diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
index 4fa658f5d934..7f902581a115 100644
--- a/drivers/clk/mediatek/clk-mtk.h
+++ b/drivers/clk/mediatek/clk-mtk.h
@@ -7,19 +7,19 @@
#ifndef __DRV_CLK_MTK_H
#define __DRV_CLK_MTK_H

-#include <linux/regmap.h>
-#include <linux/bitops.h>
#include <linux/clk-provider.h>
-#include <linux/platform_device.h>
-
-struct clk;
-struct clk_onecell_data;
+#include <linux/io.h>
+#include <linux/kernel.h>
+#include <linux/spinlock.h>
+#include <linux/types.h>

#define MAX_MUX_GATE_BIT 31
#define INVALID_MUX_GATE_BIT (MAX_MUX_GATE_BIT + 1)

#define MHZ (1000 * 1000)

+struct platform_device;
+
struct mtk_fixed_clk {
int id;
const char *name;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:57:55

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 21/31] clk: mediatek: cpumux: Implement error handling in register API

The cpumux clk type registration function does not stop or return errors
if any clk failed to be registered, nor does it implement an error
handling path. This may result in a partially working device if any
step failed.

Make the register function return proper error codes, and bail out if
errors occur. Proper cleanup, i.e. unregister any clks that were
successfully registered, is done in the new error path.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-cpumux.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/mediatek/clk-cpumux.c b/drivers/clk/mediatek/clk-cpumux.c
index 658aee789f44..499c60432280 100644
--- a/drivers/clk/mediatek/clk-cpumux.c
+++ b/drivers/clk/mediatek/clk-cpumux.c
@@ -123,13 +123,26 @@ int mtk_clk_register_cpumuxes(struct device_node *node,
clk = mtk_clk_register_cpumux(mux, regmap);
if (IS_ERR(clk)) {
pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
- continue;
+ goto err;
}

clk_data->clks[mux->id] = clk;
}

return 0;
+
+err:
+ while (--i >= 0) {
+ const struct mtk_composite *mux = &clks[i];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[mux->id]))
+ continue;
+
+ mtk_clk_unregister_cpumux(clk_data->clks[mux->id]);
+ clk_data->clks[mux->id] = ERR_PTR(-ENOENT);
+ }
+
+ return PTR_ERR(clk);
}

void mtk_clk_unregister_cpumuxes(const struct mtk_composite *clks, int num,
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:58:06

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 22/31] clk: mediatek: gate: Implement error handling in register API

The gate clk type registration function does not stop or return errors
if any clk failed to be registered, nor does it implement an error
handling path. This may result in a partially working device if any
step failed.

Make the register function return proper error codes, and bail out if
errors occur. Proper cleanup, i.e. unregister any clks that were
successfully registered, is done in the new error path.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-gate.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/mediatek/clk-gate.c b/drivers/clk/mediatek/clk-gate.c
index e8881ae1489a..631ff170b7b9 100644
--- a/drivers/clk/mediatek/clk-gate.c
+++ b/drivers/clk/mediatek/clk-gate.c
@@ -237,13 +237,26 @@ int mtk_clk_register_gates_with_dev(struct device_node *node,

if (IS_ERR(clk)) {
pr_err("Failed to register clk %s: %pe\n", gate->name, clk);
- continue;
+ goto err;
}

clk_data->clks[gate->id] = clk;
}

return 0;
+
+err:
+ while (--i >= 0) {
+ const struct mtk_gate *gate = &clks[i];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[gate->id]))
+ continue;
+
+ mtk_clk_unregister_gate(clk_data->clks[gate->id]);
+ clk_data->clks[gate->id] = ERR_PTR(-ENOENT);
+ }
+
+ return PTR_ERR(clk);
}

int mtk_clk_register_gates(struct device_node *node,
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:58:09

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 23/31] clk: mediatek: mux: Reverse check for existing clk to reduce nesting level

The clk registration code here currently does:

if (IS_ERR_OR_NULL(clk_data->clks[mux->id])) {
... do clk registration ...
}

This extra level of nesting wastes screen real estate.

Reduce the nesting level by reversing the conditional shown above.
Other than that, functionality is not changed.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mux.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
index 01af6a52711a..70aa42144632 100644
--- a/drivers/clk/mediatek/clk-mux.c
+++ b/drivers/clk/mediatek/clk-mux.c
@@ -208,16 +208,17 @@ int mtk_clk_register_muxes(const struct mtk_mux *muxes,
for (i = 0; i < num; i++) {
const struct mtk_mux *mux = &muxes[i];

- if (IS_ERR_OR_NULL(clk_data->clks[mux->id])) {
- clk = mtk_clk_register_mux(mux, regmap, lock);
+ if (!IS_ERR_OR_NULL(clk_data->clks[mux->id]))
+ continue;

- if (IS_ERR(clk)) {
- pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
- continue;
- }
+ clk = mtk_clk_register_mux(mux, regmap, lock);

- clk_data->clks[mux->id] = clk;
+ if (IS_ERR(clk)) {
+ pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
+ continue;
}
+
+ clk_data->clks[mux->id] = clk;
}

return 0;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:58:16

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 24/31] clk: mediatek: mux: Implement error handling in register API

The mux clk type registration function does not stop or return errors
if any clk failed to be registered, nor does it implement an error
handling path. This may result in a partially working device if any
step failed.

Make the register function return proper error codes, and bail out if
errors occur. Proper cleanup, i.e. unregister any clks that were
successfully registered, is done in the new error path.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mux.c | 15 ++++++++++++++-
1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
index 70aa42144632..f51e67650f03 100644
--- a/drivers/clk/mediatek/clk-mux.c
+++ b/drivers/clk/mediatek/clk-mux.c
@@ -215,13 +215,26 @@ int mtk_clk_register_muxes(const struct mtk_mux *muxes,

if (IS_ERR(clk)) {
pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
- continue;
+ goto err;
}

clk_data->clks[mux->id] = clk;
}

return 0;
+
+err:
+ while (--i >= 0) {
+ const struct mtk_mux *mux = &muxes[i];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[mux->id]))
+ continue;
+
+ mtk_clk_unregister_mux(clk_data->clks[mux->id]);
+ clk_data->clks[mux->id] = ERR_PTR(-ENOENT);
+ }
+
+ return PTR_ERR(clk);
}
EXPORT_SYMBOL_GPL(mtk_clk_register_muxes);

--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:58:23

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 25/31] clk: mediatek: pll: Implement error handling in register API

The pll clk type registration function does not stop or return errors
if any clk failed to be registered, nor does it implement an error
handling path. This may result in a partially working device if any
step failed.

Make the register function return proper error codes, and bail out if
errors occur. Proper cleanup, i.e. unregister any clks that were
successfully registered, and unmap the I/O space, is done in the new
error path.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-pll.c | 23 +++++++++++++++++++----
drivers/clk/mediatek/clk-pll.h | 6 +++---
2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/drivers/clk/mediatek/clk-pll.c b/drivers/clk/mediatek/clk-pll.c
index 9698d1c97cd6..1dd15f560659 100644
--- a/drivers/clk/mediatek/clk-pll.c
+++ b/drivers/clk/mediatek/clk-pll.c
@@ -369,8 +369,9 @@ static void mtk_clk_unregister_pll(struct clk *clk)
kfree(pll);
}

-void mtk_clk_register_plls(struct device_node *node,
- const struct mtk_pll_data *plls, int num_plls, struct clk_onecell_data *clk_data)
+int mtk_clk_register_plls(struct device_node *node,
+ const struct mtk_pll_data *plls, int num_plls,
+ struct clk_onecell_data *clk_data)
{
void __iomem *base;
int i;
@@ -379,7 +380,7 @@ void mtk_clk_register_plls(struct device_node *node,
base = of_iomap(node, 0);
if (!base) {
pr_err("%s(): ioremap failed\n", __func__);
- return;
+ return -EINVAL;
}

for (i = 0; i < num_plls; i++) {
@@ -389,11 +390,25 @@ void mtk_clk_register_plls(struct device_node *node,

if (IS_ERR(clk)) {
pr_err("Failed to register clk %s: %pe\n", pll->name, clk);
- continue;
+ goto err;
}

clk_data->clks[pll->id] = clk;
}
+
+ return 0;
+
+err:
+ while (--i >= 0) {
+ const struct mtk_pll_data *pll = &plls[i];
+
+ mtk_clk_unregister_pll(clk_data->clks[pll->id]);
+ clk_data->clks[pll->id] = ERR_PTR(-ENOENT);
+ };
+
+ iounmap(base);
+
+ return PTR_ERR(clk);
}
EXPORT_SYMBOL_GPL(mtk_clk_register_plls);

diff --git a/drivers/clk/mediatek/clk-pll.h b/drivers/clk/mediatek/clk-pll.h
index a889b1e472e7..bf06e44caef9 100644
--- a/drivers/clk/mediatek/clk-pll.h
+++ b/drivers/clk/mediatek/clk-pll.h
@@ -48,9 +48,9 @@ struct mtk_pll_data {
u8 pll_en_bit; /* Assume 0, indicates BIT(0) by default */
};

-void mtk_clk_register_plls(struct device_node *node,
- const struct mtk_pll_data *plls, int num_plls,
- struct clk_onecell_data *clk_data);
+int mtk_clk_register_plls(struct device_node *node,
+ const struct mtk_pll_data *plls, int num_plls,
+ struct clk_onecell_data *clk_data);
void mtk_clk_unregister_plls(const struct mtk_pll_data *plls, int num_plls,
struct clk_onecell_data *clk_data);

--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:58:30

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 26/31] clk: mediatek: mtk: Implement error handling in register APIs

The remaining clk registration functions do not stop or return errors
if any clk failed to be registered, nor do they implement error
handling paths. This may result in a partially working device if any
step fails.

Make the register functions return proper error codes, and bail out if
errors occur. Proper cleanup, i.e. unregister any clks that were
successfully registered, is done in the new error path.

This also makes the |struct clk_data *| argument mandatory, as it is
used to track the list of clks registered.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mtk.c | 118 ++++++++++++++++++++++++++-------
drivers/clk/mediatek/clk-mtk.h | 20 +++---
2 files changed, 103 insertions(+), 35 deletions(-)

diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index d2c50186cceb..e1977c8e130a 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -53,16 +53,19 @@ void mtk_free_clk_data(struct clk_onecell_data *clk_data)
kfree(clk_data);
}

-void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
- int num, struct clk_onecell_data *clk_data)
+int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
+ struct clk_onecell_data *clk_data)
{
int i;
struct clk *clk;

+ if (!clk_data)
+ return -ENOMEM;
+
for (i = 0; i < num; i++) {
const struct mtk_fixed_clk *rc = &clks[i];

- if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[rc->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[rc->id]))
continue;

clk = clk_register_fixed_rate(NULL, rc->name, rc->parent, 0,
@@ -70,12 +73,26 @@ void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,

if (IS_ERR(clk)) {
pr_err("Failed to register clk %s: %pe\n", rc->name, clk);
- continue;
+ goto err;
}

- if (clk_data)
- clk_data->clks[rc->id] = clk;
+ clk_data->clks[rc->id] = clk;
}
+
+ return 0;
+
+err:
+ while (--i >= 0) {
+ const struct mtk_fixed_clk *rc = &clks[i];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[rc->id]))
+ continue;
+
+ clk_unregister_fixed_rate(clk_data->clks[rc->id]);
+ clk_data->clks[rc->id] = ERR_PTR(-ENOENT);
+ }
+
+ return PTR_ERR(clk);
}
EXPORT_SYMBOL_GPL(mtk_clk_register_fixed_clks);

@@ -99,16 +116,19 @@ void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
}
EXPORT_SYMBOL_GPL(mtk_clk_unregister_fixed_clks);

-void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
- int num, struct clk_onecell_data *clk_data)
+int mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
+ struct clk_onecell_data *clk_data)
{
int i;
struct clk *clk;

+ if (!clk_data)
+ return -ENOMEM;
+
for (i = 0; i < num; i++) {
const struct mtk_fixed_factor *ff = &clks[i];

- if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[ff->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[ff->id]))
continue;

clk = clk_register_fixed_factor(NULL, ff->name, ff->parent_name,
@@ -116,12 +136,26 @@ void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,

if (IS_ERR(clk)) {
pr_err("Failed to register clk %s: %pe\n", ff->name, clk);
- continue;
+ goto err;
}

- if (clk_data)
- clk_data->clks[ff->id] = clk;
+ clk_data->clks[ff->id] = clk;
+ }
+
+ return 0;
+
+err:
+ while (--i >= 0) {
+ const struct mtk_fixed_factor *ff = &clks[i];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[ff->id]))
+ continue;
+
+ clk_unregister_fixed_factor(clk_data->clks[ff->id]);
+ clk_data->clks[ff->id] = ERR_PTR(-ENOENT);
}
+
+ return PTR_ERR(clk);
}
EXPORT_SYMBOL_GPL(mtk_clk_register_factors);

@@ -253,13 +287,16 @@ static void mtk_clk_unregister_composite(struct clk *clk)
kfree(mux);
}

-void mtk_clk_register_composites(const struct mtk_composite *mcs,
- int num, void __iomem *base, spinlock_t *lock,
- struct clk_onecell_data *clk_data)
+int mtk_clk_register_composites(const struct mtk_composite *mcs, int num,
+ void __iomem *base, spinlock_t *lock,
+ struct clk_onecell_data *clk_data)
{
struct clk *clk;
int i;

+ if (!clk_data)
+ return -ENOMEM;
+
for (i = 0; i < num; i++) {
const struct mtk_composite *mc = &mcs[i];

@@ -270,12 +307,26 @@ void mtk_clk_register_composites(const struct mtk_composite *mcs,

if (IS_ERR(clk)) {
pr_err("Failed to register clk %s: %pe\n", mc->name, clk);
- continue;
+ goto err;
}

- if (clk_data)
- clk_data->clks[mc->id] = clk;
+ clk_data->clks[mc->id] = clk;
+ }
+
+ return 0;
+
+err:
+ while (--i >= 0) {
+ const struct mtk_composite *mc = &mcs[i];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[mcs->id]))
+ continue;
+
+ mtk_clk_unregister_composite(clk_data->clks[mc->id]);
+ clk_data->clks[mc->id] = ERR_PTR(-ENOENT);
}
+
+ return PTR_ERR(clk);
}
EXPORT_SYMBOL_GPL(mtk_clk_register_composites);

@@ -299,17 +350,20 @@ void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
}
EXPORT_SYMBOL_GPL(mtk_clk_unregister_composites);

-void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
- int num, void __iomem *base, spinlock_t *lock,
- struct clk_onecell_data *clk_data)
+int mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
+ void __iomem *base, spinlock_t *lock,
+ struct clk_onecell_data *clk_data)
{
struct clk *clk;
int i;

+ if (!clk_data)
+ return -ENOMEM;
+
for (i = 0; i < num; i++) {
const struct mtk_clk_divider *mcd = &mcds[i];

- if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
continue;

clk = clk_register_divider(NULL, mcd->name, mcd->parent_name,
@@ -318,12 +372,26 @@ void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,

if (IS_ERR(clk)) {
pr_err("Failed to register clk %s: %pe\n", mcd->name, clk);
- continue;
+ goto err;
}

- if (clk_data)
- clk_data->clks[mcd->id] = clk;
+ clk_data->clks[mcd->id] = clk;
+ }
+
+ return 0;
+
+err:
+ while (--i >= 0) {
+ const struct mtk_clk_divider *mcd = &mcds[i];
+
+ if (IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
+ continue;
+
+ mtk_clk_unregister_composite(clk_data->clks[mcd->id]);
+ clk_data->clks[mcd->id] = ERR_PTR(-ENOENT);
}
+
+ return PTR_ERR(clk);
}

void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
index 7f902581a115..bf6565aa7319 100644
--- a/drivers/clk/mediatek/clk-mtk.h
+++ b/drivers/clk/mediatek/clk-mtk.h
@@ -34,8 +34,8 @@ struct mtk_fixed_clk {
.rate = _rate, \
}

-void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
- struct clk_onecell_data *clk_data);
+int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
+ struct clk_onecell_data *clk_data);
void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
struct clk_onecell_data *clk_data);

@@ -55,8 +55,8 @@ struct mtk_fixed_factor {
.div = _div, \
}

-void mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
- struct clk_onecell_data *clk_data);
+int mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
+ struct clk_onecell_data *clk_data);
void mtk_clk_unregister_factors(const struct mtk_fixed_factor *clks, int num,
struct clk_onecell_data *clk_data);

@@ -150,9 +150,9 @@ struct mtk_composite {
struct clk *mtk_clk_register_composite(const struct mtk_composite *mc,
void __iomem *base, spinlock_t *lock);

-void mtk_clk_register_composites(const struct mtk_composite *mcs,
- int num, void __iomem *base, spinlock_t *lock,
- struct clk_onecell_data *clk_data);
+int mtk_clk_register_composites(const struct mtk_composite *mcs, int num,
+ void __iomem *base, spinlock_t *lock,
+ struct clk_onecell_data *clk_data);
void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
struct clk_onecell_data *clk_data);

@@ -178,9 +178,9 @@ struct mtk_clk_divider {
.div_width = _width, \
}

-void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
- void __iomem *base, spinlock_t *lock,
- struct clk_onecell_data *clk_data);
+int mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
+ void __iomem *base, spinlock_t *lock,
+ struct clk_onecell_data *clk_data);
void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
struct clk_onecell_data *clk_data);

--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:58:42

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 28/31] clk: mediatek: mt8195: Hook up mtk_clk_simple_remove()

Various small clock controllers only have clock gates, and utilize
mtk_clk_simple_probe() as their driver probe function.

Now that we have a matching remove function, hook it up for the relevant
drivers. This was done with the following command:

sed -i -e '/mtk_clk_simple_probe/a \
.remove = mtk_clk_simple_remove,' drivers/clk/mediatek/clk-mt8195-*.c

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mt8195-cam.c | 1 +
drivers/clk/mediatek/clk-mt8195-ccu.c | 1 +
drivers/clk/mediatek/clk-mt8195-img.c | 1 +
drivers/clk/mediatek/clk-mt8195-imp_iic_wrap.c | 1 +
drivers/clk/mediatek/clk-mt8195-infra_ao.c | 1 +
drivers/clk/mediatek/clk-mt8195-ipe.c | 1 +
drivers/clk/mediatek/clk-mt8195-mfg.c | 1 +
drivers/clk/mediatek/clk-mt8195-peri_ao.c | 1 +
drivers/clk/mediatek/clk-mt8195-scp_adsp.c | 1 +
drivers/clk/mediatek/clk-mt8195-vdec.c | 1 +
drivers/clk/mediatek/clk-mt8195-venc.c | 1 +
drivers/clk/mediatek/clk-mt8195-vpp0.c | 1 +
drivers/clk/mediatek/clk-mt8195-vpp1.c | 1 +
drivers/clk/mediatek/clk-mt8195-wpe.c | 1 +
14 files changed, 14 insertions(+)

diff --git a/drivers/clk/mediatek/clk-mt8195-cam.c b/drivers/clk/mediatek/clk-mt8195-cam.c
index 3d261fc3848e..e4d00fe6e757 100644
--- a/drivers/clk/mediatek/clk-mt8195-cam.c
+++ b/drivers/clk/mediatek/clk-mt8195-cam.c
@@ -134,6 +134,7 @@ static const struct of_device_id of_match_clk_mt8195_cam[] = {

static struct platform_driver clk_mt8195_cam_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-cam",
.of_match_table = of_match_clk_mt8195_cam,
diff --git a/drivers/clk/mediatek/clk-mt8195-ccu.c b/drivers/clk/mediatek/clk-mt8195-ccu.c
index f846f1d73605..4e326b6301ba 100644
--- a/drivers/clk/mediatek/clk-mt8195-ccu.c
+++ b/drivers/clk/mediatek/clk-mt8195-ccu.c
@@ -42,6 +42,7 @@ static const struct of_device_id of_match_clk_mt8195_ccu[] = {

static struct platform_driver clk_mt8195_ccu_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-ccu",
.of_match_table = of_match_clk_mt8195_ccu,
diff --git a/drivers/clk/mediatek/clk-mt8195-img.c b/drivers/clk/mediatek/clk-mt8195-img.c
index 22b52a8f15fe..12f5c436d075 100644
--- a/drivers/clk/mediatek/clk-mt8195-img.c
+++ b/drivers/clk/mediatek/clk-mt8195-img.c
@@ -88,6 +88,7 @@ static const struct of_device_id of_match_clk_mt8195_img[] = {

static struct platform_driver clk_mt8195_img_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-img",
.of_match_table = of_match_clk_mt8195_img,
diff --git a/drivers/clk/mediatek/clk-mt8195-imp_iic_wrap.c b/drivers/clk/mediatek/clk-mt8195-imp_iic_wrap.c
index 4ab312eb26a5..fbc809d05072 100644
--- a/drivers/clk/mediatek/clk-mt8195-imp_iic_wrap.c
+++ b/drivers/clk/mediatek/clk-mt8195-imp_iic_wrap.c
@@ -58,6 +58,7 @@ static const struct of_device_id of_match_clk_mt8195_imp_iic_wrap[] = {

static struct platform_driver clk_mt8195_imp_iic_wrap_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-imp_iic_wrap",
.of_match_table = of_match_clk_mt8195_imp_iic_wrap,
diff --git a/drivers/clk/mediatek/clk-mt8195-infra_ao.c b/drivers/clk/mediatek/clk-mt8195-infra_ao.c
index 5f9b69967459..8ebe3b9415c4 100644
--- a/drivers/clk/mediatek/clk-mt8195-infra_ao.c
+++ b/drivers/clk/mediatek/clk-mt8195-infra_ao.c
@@ -198,6 +198,7 @@ static const struct of_device_id of_match_clk_mt8195_infra_ao[] = {

static struct platform_driver clk_mt8195_infra_ao_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-infra_ao",
.of_match_table = of_match_clk_mt8195_infra_ao,
diff --git a/drivers/clk/mediatek/clk-mt8195-ipe.c b/drivers/clk/mediatek/clk-mt8195-ipe.c
index fc1d42b6ac84..b0d745cf7752 100644
--- a/drivers/clk/mediatek/clk-mt8195-ipe.c
+++ b/drivers/clk/mediatek/clk-mt8195-ipe.c
@@ -43,6 +43,7 @@ static const struct of_device_id of_match_clk_mt8195_ipe[] = {

static struct platform_driver clk_mt8195_ipe_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-ipe",
.of_match_table = of_match_clk_mt8195_ipe,
diff --git a/drivers/clk/mediatek/clk-mt8195-mfg.c b/drivers/clk/mediatek/clk-mt8195-mfg.c
index aca6d9c0837c..9411c556a5a9 100644
--- a/drivers/clk/mediatek/clk-mt8195-mfg.c
+++ b/drivers/clk/mediatek/clk-mt8195-mfg.c
@@ -39,6 +39,7 @@ static const struct of_device_id of_match_clk_mt8195_mfg[] = {

static struct platform_driver clk_mt8195_mfg_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-mfg",
.of_match_table = of_match_clk_mt8195_mfg,
diff --git a/drivers/clk/mediatek/clk-mt8195-peri_ao.c b/drivers/clk/mediatek/clk-mt8195-peri_ao.c
index 907a92b22de8..2f6b3bb657db 100644
--- a/drivers/clk/mediatek/clk-mt8195-peri_ao.c
+++ b/drivers/clk/mediatek/clk-mt8195-peri_ao.c
@@ -54,6 +54,7 @@ static const struct of_device_id of_match_clk_mt8195_peri_ao[] = {

static struct platform_driver clk_mt8195_peri_ao_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-peri_ao",
.of_match_table = of_match_clk_mt8195_peri_ao,
diff --git a/drivers/clk/mediatek/clk-mt8195-scp_adsp.c b/drivers/clk/mediatek/clk-mt8195-scp_adsp.c
index 26b4846c5894..e16c383f631b 100644
--- a/drivers/clk/mediatek/clk-mt8195-scp_adsp.c
+++ b/drivers/clk/mediatek/clk-mt8195-scp_adsp.c
@@ -39,6 +39,7 @@ static const struct of_device_id of_match_clk_mt8195_scp_adsp[] = {

static struct platform_driver clk_mt8195_scp_adsp_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-scp_adsp",
.of_match_table = of_match_clk_mt8195_scp_adsp,
diff --git a/drivers/clk/mediatek/clk-mt8195-vdec.c b/drivers/clk/mediatek/clk-mt8195-vdec.c
index a1df04f42a90..a1446b666385 100644
--- a/drivers/clk/mediatek/clk-mt8195-vdec.c
+++ b/drivers/clk/mediatek/clk-mt8195-vdec.c
@@ -96,6 +96,7 @@ static const struct of_device_id of_match_clk_mt8195_vdec[] = {

static struct platform_driver clk_mt8195_vdec_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-vdec",
.of_match_table = of_match_clk_mt8195_vdec,
diff --git a/drivers/clk/mediatek/clk-mt8195-venc.c b/drivers/clk/mediatek/clk-mt8195-venc.c
index 7339851a0856..622f57804f96 100644
--- a/drivers/clk/mediatek/clk-mt8195-venc.c
+++ b/drivers/clk/mediatek/clk-mt8195-venc.c
@@ -61,6 +61,7 @@ static const struct of_device_id of_match_clk_mt8195_venc[] = {

static struct platform_driver clk_mt8195_venc_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-venc",
.of_match_table = of_match_clk_mt8195_venc,
diff --git a/drivers/clk/mediatek/clk-mt8195-vpp0.c b/drivers/clk/mediatek/clk-mt8195-vpp0.c
index c3241466a8d0..bf2939c3a023 100644
--- a/drivers/clk/mediatek/clk-mt8195-vpp0.c
+++ b/drivers/clk/mediatek/clk-mt8195-vpp0.c
@@ -102,6 +102,7 @@ static const struct of_device_id of_match_clk_mt8195_vpp0[] = {

static struct platform_driver clk_mt8195_vpp0_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-vpp0",
.of_match_table = of_match_clk_mt8195_vpp0,
diff --git a/drivers/clk/mediatek/clk-mt8195-vpp1.c b/drivers/clk/mediatek/clk-mt8195-vpp1.c
index ce0b9a40a179..ffd52c762890 100644
--- a/drivers/clk/mediatek/clk-mt8195-vpp1.c
+++ b/drivers/clk/mediatek/clk-mt8195-vpp1.c
@@ -100,6 +100,7 @@ static const struct of_device_id of_match_clk_mt8195_vpp1[] = {

static struct platform_driver clk_mt8195_vpp1_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-vpp1",
.of_match_table = of_match_clk_mt8195_vpp1,
diff --git a/drivers/clk/mediatek/clk-mt8195-wpe.c b/drivers/clk/mediatek/clk-mt8195-wpe.c
index 274d60838d8e..b483fab10e18 100644
--- a/drivers/clk/mediatek/clk-mt8195-wpe.c
+++ b/drivers/clk/mediatek/clk-mt8195-wpe.c
@@ -135,6 +135,7 @@ static const struct of_device_id of_match_clk_mt8195_wpe[] = {

static struct platform_driver clk_mt8195_wpe_drv = {
.probe = mtk_clk_simple_probe,
+ .remove = mtk_clk_simple_remove,
.driver = {
.name = "clk-mt8195-wpe",
.of_match_table = of_match_clk_mt8195_wpe,
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:58:47

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 29/31] clk: mediatek: mt8195: Implement error handling in probe functions

Until now the mediatek clk driver library did not have any way to
unregister clks, and so all drivers did not do proper cleanup in
their error paths.

Now that the library does have APIs to unregister clks, use them
in the error path of the probe functions for the mt8195 clk drivers
to do proper cleanup.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mt8195-apmixedsys.c | 13 ++++--
drivers/clk/mediatek/clk-mt8195-apusys_pll.c | 11 ++++-
drivers/clk/mediatek/clk-mt8195-topckgen.c | 49 +++++++++++++++-----
drivers/clk/mediatek/clk-mt8195-vdo0.c | 4 +-
drivers/clk/mediatek/clk-mt8195-vdo1.c | 4 +-
5 files changed, 63 insertions(+), 18 deletions(-)

diff --git a/drivers/clk/mediatek/clk-mt8195-apmixedsys.c b/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
index 5b1b7dc447eb..af8d80f25f30 100644
--- a/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
+++ b/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
@@ -120,17 +120,24 @@ static int clk_mt8195_apmixed_probe(struct platform_device *pdev)
if (!clk_data)
return -ENOMEM;

- mtk_clk_register_plls(node, plls, ARRAY_SIZE(plls), clk_data);
- r = mtk_clk_register_gates(node, apmixed_clks, ARRAY_SIZE(apmixed_clks), clk_data);
+ r = mtk_clk_register_plls(node, plls, ARRAY_SIZE(plls), clk_data);
if (r)
goto free_apmixed_data;

+ r = mtk_clk_register_gates(node, apmixed_clks, ARRAY_SIZE(apmixed_clks), clk_data);
+ if (r)
+ goto unregister_plls;
+
r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
if (r)
- goto free_apmixed_data;
+ goto unregister_gates;

return r;

+unregister_gates:
+ mtk_clk_register_gates(node, apmixed_clks, ARRAY_SIZE(apmixed_clks), clk_data);
+unregister_plls:
+ mtk_clk_register_plls(node, plls, ARRAY_SIZE(plls), clk_data);
free_apmixed_data:
mtk_free_clk_data(clk_data);
return r;
diff --git a/drivers/clk/mediatek/clk-mt8195-apusys_pll.c b/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
index db449ff877d7..1fff6f3d2dc7 100644
--- a/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
+++ b/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
@@ -66,13 +66,20 @@ static int clk_mt8195_apusys_pll_probe(struct platform_device *pdev)
if (!clk_data)
return -ENOMEM;

- mtk_clk_register_plls(node, apusys_plls, ARRAY_SIZE(apusys_plls), clk_data);
- r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
+ r = mtk_clk_register_plls(node, apusys_plls, ARRAY_SIZE(apusys_plls), clk_data);
if (r)
goto free_apusys_pll_data;

+ r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
+ if (r)
+ goto unregister_plls;
+
+ platform_set_drvdata(pdev, clk_data);
+
return r;

+unregister_plls:
+ mtk_clk_unregister_plls(apusys_plls, ARRAY_SIZE(apusys_plls), clk_data);
free_apusys_pll_data:
mtk_free_clk_data(clk_data);
return r;
diff --git a/drivers/clk/mediatek/clk-mt8195-topckgen.c b/drivers/clk/mediatek/clk-mt8195-topckgen.c
index 3e2aba9c40bb..3631f49a5e5a 100644
--- a/drivers/clk/mediatek/clk-mt8195-topckgen.c
+++ b/drivers/clk/mediatek/clk-mt8195-topckgen.c
@@ -1239,25 +1239,52 @@ static int clk_mt8195_topck_probe(struct platform_device *pdev)
goto free_top_data;
}

- mtk_clk_register_fixed_clks(top_fixed_clks, ARRAY_SIZE(top_fixed_clks),
- top_clk_data);
- mtk_clk_register_factors(top_divs, ARRAY_SIZE(top_divs), top_clk_data);
- mtk_clk_register_muxes(top_mtk_muxes, ARRAY_SIZE(top_mtk_muxes), node,
- &mt8195_clk_lock, top_clk_data);
- mtk_clk_register_composites(top_muxes, ARRAY_SIZE(top_muxes), base,
- &mt8195_clk_lock, top_clk_data);
- mtk_clk_register_composites(top_adj_divs, ARRAY_SIZE(top_adj_divs), base,
- &mt8195_clk_lock, top_clk_data);
- r = mtk_clk_register_gates(node, top_clks, ARRAY_SIZE(top_clks), top_clk_data);
+ r = mtk_clk_register_fixed_clks(top_fixed_clks, ARRAY_SIZE(top_fixed_clks),
+ top_clk_data);
if (r)
goto free_top_data;

+ r = mtk_clk_register_factors(top_divs, ARRAY_SIZE(top_divs), top_clk_data);
+ if (r)
+ goto unregister_fixed_clks;
+
+ r = mtk_clk_register_muxes(top_mtk_muxes, ARRAY_SIZE(top_mtk_muxes), node,
+ &mt8195_clk_lock, top_clk_data);
+ if (r)
+ goto unregister_factors;
+
+ r = mtk_clk_register_composites(top_muxes, ARRAY_SIZE(top_muxes), base,
+ &mt8195_clk_lock, top_clk_data);
+ if (r)
+ goto unregister_muxes;
+
+ r = mtk_clk_register_composites(top_adj_divs, ARRAY_SIZE(top_adj_divs), base,
+ &mt8195_clk_lock, top_clk_data);
+ if (r)
+ goto unregister_composite_muxes;
+
+ r = mtk_clk_register_gates(node, top_clks, ARRAY_SIZE(top_clks), top_clk_data);
+ if (r)
+ goto unregister_composite_divs;
+
r = of_clk_add_provider(node, of_clk_src_onecell_get, top_clk_data);
if (r)
- goto free_top_data;
+ goto unregister_gates;

return r;

+unregister_gates:
+ mtk_clk_unregister_gates(top_clks, ARRAY_SIZE(top_clks), top_clk_data);
+unregister_composite_divs:
+ mtk_clk_unregister_composites(top_adj_divs, ARRAY_SIZE(top_adj_divs), top_clk_data);
+unregister_composite_muxes:
+ mtk_clk_unregister_composites(top_muxes, ARRAY_SIZE(top_muxes), top_clk_data);
+unregister_muxes:
+ mtk_clk_unregister_muxes(top_mtk_muxes, ARRAY_SIZE(top_mtk_muxes), top_clk_data);
+unregister_factors:
+ mtk_clk_unregister_factors(top_divs, ARRAY_SIZE(top_divs), top_clk_data);
+unregister_fixed_clks:
+ mtk_clk_unregister_fixed_clks(top_fixed_clks, ARRAY_SIZE(top_fixed_clks), top_clk_data);
free_top_data:
mtk_free_clk_data(top_clk_data);
return r;
diff --git a/drivers/clk/mediatek/clk-mt8195-vdo0.c b/drivers/clk/mediatek/clk-mt8195-vdo0.c
index f7ff7618c714..af34eb564b1d 100644
--- a/drivers/clk/mediatek/clk-mt8195-vdo0.c
+++ b/drivers/clk/mediatek/clk-mt8195-vdo0.c
@@ -105,10 +105,12 @@ static int clk_mt8195_vdo0_probe(struct platform_device *pdev)

r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
if (r)
- goto free_vdo0_data;
+ goto unregister_gates;

return r;

+unregister_gates:
+ mtk_clk_unregister_gates(vdo0_clks, ARRAY_SIZE(vdo0_clks), clk_data);
free_vdo0_data:
mtk_free_clk_data(clk_data);
return r;
diff --git a/drivers/clk/mediatek/clk-mt8195-vdo1.c b/drivers/clk/mediatek/clk-mt8195-vdo1.c
index 03df8eae8838..6b502bbc730c 100644
--- a/drivers/clk/mediatek/clk-mt8195-vdo1.c
+++ b/drivers/clk/mediatek/clk-mt8195-vdo1.c
@@ -122,10 +122,12 @@ static int clk_mt8195_vdo1_probe(struct platform_device *pdev)

r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
if (r)
- goto free_vdo1_data;
+ goto unregister_gates;

return r;

+unregister_gates:
+ mtk_clk_unregister_gates(vdo1_clks, ARRAY_SIZE(vdo1_clks), clk_data);
free_vdo1_data:
mtk_free_clk_data(clk_data);
return r;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:58:48

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 27/31] clk: mediatek: Unregister clks in mtk_clk_simple_probe() error path

Until now the mediatek clk driver library did not have any way to
unregister clks, and so all drivers did not do proper cleanup in
their error paths.

Now that the library does have APIs to unregister clks, use them
in the error path of mtk_clk_simple_probe() to do proper cleanup.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mtk.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index e1977c8e130a..6d0b8842971b 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -434,12 +434,14 @@ int mtk_clk_simple_probe(struct platform_device *pdev)

r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
if (r)
- goto free_data;
+ goto unregister_clks;

platform_set_drvdata(pdev, clk_data);

return r;

+unregister_clks:
+ mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
free_data:
mtk_free_clk_data(clk_data);
return r;
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:58:55

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 30/31] clk: mediatek: mt8195: Implement remove functions

Until now the mediatek clk driver library did not have any way to
unregister clks, and so none of the drivers implemented remove
functions.

Now that the library does have APIs to unregister clks, use them
to implement remove functions for the mt8195 clk drivers.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-mt8195-apmixedsys.c | 16 ++++++++++++++++
drivers/clk/mediatek/clk-mt8195-apusys_pll.c | 13 +++++++++++++
drivers/clk/mediatek/clk-mt8195-topckgen.c | 20 ++++++++++++++++++++
drivers/clk/mediatek/clk-mt8195-vdo0.c | 16 ++++++++++++++++
drivers/clk/mediatek/clk-mt8195-vdo1.c | 16 ++++++++++++++++
5 files changed, 81 insertions(+)

diff --git a/drivers/clk/mediatek/clk-mt8195-apmixedsys.c b/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
index af8d80f25f30..29cac3cf5f53 100644
--- a/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
+++ b/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
@@ -132,6 +132,8 @@ static int clk_mt8195_apmixed_probe(struct platform_device *pdev)
if (r)
goto unregister_gates;

+ platform_set_drvdata(pdev, clk_data);
+
return r;

unregister_gates:
@@ -143,8 +145,22 @@ static int clk_mt8195_apmixed_probe(struct platform_device *pdev)
return r;
}

+static int clk_mt8195_apmixed_remove(struct platform_device *pdev)
+{
+ struct device_node *node = pdev->dev.of_node;
+ struct clk_onecell_data *clk_data = platform_get_drvdata(pdev);
+
+ of_clk_del_provider(node);
+ mtk_clk_unregister_gates(apmixed_clks, ARRAY_SIZE(apmixed_clks), clk_data);
+ mtk_clk_unregister_plls(plls, ARRAY_SIZE(plls), clk_data);
+ mtk_free_clk_data(clk_data);
+
+ return 0;
+}
+
static struct platform_driver clk_mt8195_apmixed_drv = {
.probe = clk_mt8195_apmixed_probe,
+ .remove = clk_mt8195_apmixed_remove,
.driver = {
.name = "clk-mt8195-apmixed",
.of_match_table = of_match_clk_mt8195_apmixed,
diff --git a/drivers/clk/mediatek/clk-mt8195-apusys_pll.c b/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
index 1fff6f3d2dc7..8cd88dfc3283 100644
--- a/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
+++ b/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
@@ -85,6 +85,18 @@ static int clk_mt8195_apusys_pll_probe(struct platform_device *pdev)
return r;
}

+static int clk_mt8195_apusys_pll_remove(struct platform_device *pdev)
+{
+ struct clk_onecell_data *clk_data = platform_get_drvdata(pdev);
+ struct device_node *node = pdev->dev.of_node;
+
+ of_clk_del_provider(node);
+ mtk_clk_unregister_plls(apusys_plls, ARRAY_SIZE(apusys_plls), clk_data);
+ mtk_free_clk_data(clk_data);
+
+ return 0;
+}
+
static const struct of_device_id of_match_clk_mt8195_apusys_pll[] = {
{ .compatible = "mediatek,mt8195-apusys_pll", },
{}
@@ -92,6 +104,7 @@ static const struct of_device_id of_match_clk_mt8195_apusys_pll[] = {

static struct platform_driver clk_mt8195_apusys_pll_drv = {
.probe = clk_mt8195_apusys_pll_probe,
+ .remove = clk_mt8195_apusys_pll_remove,
.driver = {
.name = "clk-mt8195-apusys_pll",
.of_match_table = of_match_clk_mt8195_apusys_pll,
diff --git a/drivers/clk/mediatek/clk-mt8195-topckgen.c b/drivers/clk/mediatek/clk-mt8195-topckgen.c
index 3631f49a5e5a..b602fcd7f1d1 100644
--- a/drivers/clk/mediatek/clk-mt8195-topckgen.c
+++ b/drivers/clk/mediatek/clk-mt8195-topckgen.c
@@ -1271,6 +1271,8 @@ static int clk_mt8195_topck_probe(struct platform_device *pdev)
if (r)
goto unregister_gates;

+ platform_set_drvdata(pdev, top_clk_data);
+
return r;

unregister_gates:
@@ -1290,8 +1292,26 @@ static int clk_mt8195_topck_probe(struct platform_device *pdev)
return r;
}

+static int clk_mt8195_topck_remove(struct platform_device *pdev)
+{
+ struct clk_onecell_data *top_clk_data = platform_get_drvdata(pdev);
+ struct device_node *node = pdev->dev.of_node;
+
+ of_clk_del_provider(node);
+ mtk_clk_unregister_gates(top_clks, ARRAY_SIZE(top_clks), top_clk_data);
+ mtk_clk_unregister_composites(top_adj_divs, ARRAY_SIZE(top_adj_divs), top_clk_data);
+ mtk_clk_unregister_composites(top_muxes, ARRAY_SIZE(top_muxes), top_clk_data);
+ mtk_clk_unregister_muxes(top_mtk_muxes, ARRAY_SIZE(top_mtk_muxes), top_clk_data);
+ mtk_clk_unregister_factors(top_divs, ARRAY_SIZE(top_divs), top_clk_data);
+ mtk_clk_unregister_fixed_clks(top_fixed_clks, ARRAY_SIZE(top_fixed_clks), top_clk_data);
+ mtk_free_clk_data(top_clk_data);
+
+ return 0;
+}
+
static struct platform_driver clk_mt8195_topck_drv = {
.probe = clk_mt8195_topck_probe,
+ .remove = clk_mt8195_topck_remove,
.driver = {
.name = "clk-mt8195-topck",
.of_match_table = of_match_clk_mt8195_topck,
diff --git a/drivers/clk/mediatek/clk-mt8195-vdo0.c b/drivers/clk/mediatek/clk-mt8195-vdo0.c
index af34eb564b1d..3bc7ed19d550 100644
--- a/drivers/clk/mediatek/clk-mt8195-vdo0.c
+++ b/drivers/clk/mediatek/clk-mt8195-vdo0.c
@@ -107,6 +107,8 @@ static int clk_mt8195_vdo0_probe(struct platform_device *pdev)
if (r)
goto unregister_gates;

+ platform_set_drvdata(pdev, clk_data);
+
return r;

unregister_gates:
@@ -116,8 +118,22 @@ static int clk_mt8195_vdo0_probe(struct platform_device *pdev)
return r;
}

+static int clk_mt8195_vdo0_remove(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct device_node *node = dev->parent->of_node;
+ struct clk_onecell_data *clk_data = platform_get_drvdata(pdev);
+
+ of_clk_del_provider(node);
+ mtk_clk_unregister_gates(vdo0_clks, ARRAY_SIZE(vdo0_clks), clk_data);
+ mtk_free_clk_data(clk_data);
+
+ return 0;
+}
+
static struct platform_driver clk_mt8195_vdo0_drv = {
.probe = clk_mt8195_vdo0_probe,
+ .remove = clk_mt8195_vdo0_remove,
.driver = {
.name = "clk-mt8195-vdo0",
},
diff --git a/drivers/clk/mediatek/clk-mt8195-vdo1.c b/drivers/clk/mediatek/clk-mt8195-vdo1.c
index 6b502bbc730c..90c738a85ff1 100644
--- a/drivers/clk/mediatek/clk-mt8195-vdo1.c
+++ b/drivers/clk/mediatek/clk-mt8195-vdo1.c
@@ -124,6 +124,8 @@ static int clk_mt8195_vdo1_probe(struct platform_device *pdev)
if (r)
goto unregister_gates;

+ platform_set_drvdata(pdev, clk_data);
+
return r;

unregister_gates:
@@ -133,8 +135,22 @@ static int clk_mt8195_vdo1_probe(struct platform_device *pdev)
return r;
}

+static int clk_mt8195_vdo1_remove(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct device_node *node = dev->parent->of_node;
+ struct clk_onecell_data *clk_data = platform_get_drvdata(pdev);
+
+ of_clk_del_provider(node);
+ mtk_clk_unregister_gates(vdo1_clks, ARRAY_SIZE(vdo1_clks), clk_data);
+ mtk_free_clk_data(clk_data);
+
+ return 0;
+}
+
static struct platform_driver clk_mt8195_vdo1_drv = {
.probe = clk_mt8195_vdo1_probe,
+ .remove = clk_mt8195_vdo1_remove,
.driver = {
.name = "clk-mt8195-vdo1",
},
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-23 14:58:58

by Chen-Yu Tsai

[permalink] [raw]
Subject: [PATCH 31/31] clk: mediatek: Warn if clk IDs are duplicated

The Mediatek clk driver library handles duplicate clock IDs in two
different ways: either ignoring the duplicate entry, or overwriting
the old clk. Either way may cause unexpected behavior, and the latter
also causes an orphan clk that cannot be cleaned up.

Align the behavior so that later duplicate entries are ignored, and
a warning printed. The warning will also aid in making the issue
noticeable.

Signed-off-by: Chen-Yu Tsai <[email protected]>
---
drivers/clk/mediatek/clk-cpumux.c | 6 ++++++
drivers/clk/mediatek/clk-gate.c | 5 ++++-
drivers/clk/mediatek/clk-mtk.c | 18 ++++++++++++++----
drivers/clk/mediatek/clk-mux.c | 5 ++++-
drivers/clk/mediatek/clk-pll.c | 6 ++++++
5 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/drivers/clk/mediatek/clk-cpumux.c b/drivers/clk/mediatek/clk-cpumux.c
index 499c60432280..c11b3fae622e 100644
--- a/drivers/clk/mediatek/clk-cpumux.c
+++ b/drivers/clk/mediatek/clk-cpumux.c
@@ -120,6 +120,12 @@ int mtk_clk_register_cpumuxes(struct device_node *node,
for (i = 0; i < num; i++) {
const struct mtk_composite *mux = &clks[i];

+ if (!IS_ERR_OR_NULL(clk_data->clks[mux->id])) {
+ pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
+ node, mux->id);
+ continue;
+ }
+
clk = mtk_clk_register_cpumux(mux, regmap);
if (IS_ERR(clk)) {
pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
diff --git a/drivers/clk/mediatek/clk-gate.c b/drivers/clk/mediatek/clk-gate.c
index 631ff170b7b9..da52023f8455 100644
--- a/drivers/clk/mediatek/clk-gate.c
+++ b/drivers/clk/mediatek/clk-gate.c
@@ -224,8 +224,11 @@ int mtk_clk_register_gates_with_dev(struct device_node *node,
for (i = 0; i < num; i++) {
const struct mtk_gate *gate = &clks[i];

- if (!IS_ERR_OR_NULL(clk_data->clks[gate->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[gate->id])) {
+ pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
+ node, gate->id);
continue;
+ }

clk = mtk_clk_register_gate(gate->name, gate->parent_name,
regmap,
diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
index 6d0b8842971b..b2a3568922b2 100644
--- a/drivers/clk/mediatek/clk-mtk.c
+++ b/drivers/clk/mediatek/clk-mtk.c
@@ -65,8 +65,10 @@ int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
for (i = 0; i < num; i++) {
const struct mtk_fixed_clk *rc = &clks[i];

- if (!IS_ERR_OR_NULL(clk_data->clks[rc->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[rc->id])) {
+ pr_warn("Trying to register duplicate clock ID: %d\n", rc->id);
continue;
+ }

clk = clk_register_fixed_rate(NULL, rc->name, rc->parent, 0,
rc->rate);
@@ -128,8 +130,10 @@ int mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
for (i = 0; i < num; i++) {
const struct mtk_fixed_factor *ff = &clks[i];

- if (!IS_ERR_OR_NULL(clk_data->clks[ff->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[ff->id])) {
+ pr_warn("Trying to register duplicate clock ID: %d\n", ff->id);
continue;
+ }

clk = clk_register_fixed_factor(NULL, ff->name, ff->parent_name,
CLK_SET_RATE_PARENT, ff->mult, ff->div);
@@ -300,8 +304,11 @@ int mtk_clk_register_composites(const struct mtk_composite *mcs, int num,
for (i = 0; i < num; i++) {
const struct mtk_composite *mc = &mcs[i];

- if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[mc->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[mc->id])) {
+ pr_warn("Trying to register duplicate clock ID: %d\n",
+ mc->id);
continue;
+ }

clk = mtk_clk_register_composite(mc, base, lock);

@@ -363,8 +370,11 @@ int mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
for (i = 0; i < num; i++) {
const struct mtk_clk_divider *mcd = &mcds[i];

- if (!IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[mcd->id])) {
+ pr_warn("Trying to register duplicate clock ID: %d\n",
+ mcd->id);
continue;
+ }

clk = clk_register_divider(NULL, mcd->name, mcd->parent_name,
mcd->flags, base + mcd->div_reg, mcd->div_shift,
diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
index f51e67650f03..21ad5a4afd65 100644
--- a/drivers/clk/mediatek/clk-mux.c
+++ b/drivers/clk/mediatek/clk-mux.c
@@ -208,8 +208,11 @@ int mtk_clk_register_muxes(const struct mtk_mux *muxes,
for (i = 0; i < num; i++) {
const struct mtk_mux *mux = &muxes[i];

- if (!IS_ERR_OR_NULL(clk_data->clks[mux->id]))
+ if (!IS_ERR_OR_NULL(clk_data->clks[mux->id])) {
+ pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
+ node, mux->id);
continue;
+ }

clk = mtk_clk_register_mux(mux, regmap, lock);

diff --git a/drivers/clk/mediatek/clk-pll.c b/drivers/clk/mediatek/clk-pll.c
index 1dd15f560659..e5e9c188be99 100644
--- a/drivers/clk/mediatek/clk-pll.c
+++ b/drivers/clk/mediatek/clk-pll.c
@@ -386,6 +386,12 @@ int mtk_clk_register_plls(struct device_node *node,
for (i = 0; i < num_plls; i++) {
const struct mtk_pll_data *pll = &plls[i];

+ if (!IS_ERR_OR_NULL(clk_data->clks[pll->id])) {
+ pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
+ node, pll->id);
+ continue;
+ }
+
clk = mtk_clk_register_pll(pll, base);

if (IS_ERR(clk)) {
--
2.35.0.rc0.227.g00780c9af4-goog

2022-01-24 19:07:09

by kernel test robot

[permalink] [raw]
Subject: [PATCH] clk: mediatek: pll: fix semicolon.cocci warnings

From: kernel test robot <[email protected]>

drivers/clk/mediatek/clk-pll.c:407:2-3: Unneeded semicolon


Remove unneeded semicolon.

Generated by: scripts/coccinelle/misc/semicolon.cocci

CC: Chen-Yu Tsai <[email protected]>
Reported-by: kernel test robot <[email protected]>
Signed-off-by: kernel test robot <[email protected]>
---

url: https://github.com/0day-ci/linux/commits/Chen-Yu-Tsai/clk-mediatek-Cleanups-and-Improvements-Part-1/20220122-172158
base: https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git clk-next
:::::: branch date: 2 days ago
:::::: commit date: 2 days ago

clk-pll.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

--- a/drivers/clk/mediatek/clk-pll.c
+++ b/drivers/clk/mediatek/clk-pll.c
@@ -404,7 +404,7 @@ err:

mtk_clk_unregister_pll(clk_data->clks[pll->id]);
clk_data->clks[pll->id] = ERR_PTR(-ENOENT);
- };
+ }

iounmap(base);

2022-01-24 19:11:32

by kernel test robot

[permalink] [raw]
Subject: Re: [PATCH 25/31] clk: mediatek: pll: Implement error handling in register API

Hi Chen-Yu,

I love your patch! Perhaps something to improve:

[auto build test WARNING on clk/clk-next]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url: https://github.com/0day-ci/linux/commits/Chen-Yu-Tsai/clk-mediatek-Cleanups-and-Improvements-Part-1/20220122-172158
base: https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git clk-next
config: h8300-randconfig-c003-20220124 (https://download.01.org/0day-ci/archive/20220124/[email protected]/config)
compiler: h8300-linux-gcc (GCC) 11.2.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <[email protected]>


cocci warnings: (new ones prefixed by >>)
>> drivers/clk/mediatek/clk-pll.c:407:2-3: Unneeded semicolon

Please review and possibly fold the followup patch.

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/[email protected]

2022-01-24 19:32:07

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 02/31] clk: mediatek: gate: Consolidate gate type clk related code

> Right now some bits of the gate type clk code are in clk-gate.[ch], but
> other bits are in clk-mtk.[ch]. This is different from the cpumux and
> mux type clks, for which all of the code are found in the same files.
>
> Move the functions that register multiple clks from a given list,
> mtk_clk_register_gates_with_dev() and mtk_clk_register_gates(), to
> clk-gate.[ch] to consolidate all the code for the gate type clks.
>
> This commit only moves code with minor whitespace fixups to correct
> the code style. Further improvements, such as internalizing various
> functions and structures will be done in later commits.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>
>
Reviewed-by: Miles Chen <[email protected]>

2022-01-24 19:32:09

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 03/31] clk: mediatek: gate: Internalize clk implementation

> struct mtk_clk_gate and mtk_clk_register_gate() are not used outside of
> the gate clk library. Only the API that handles a list of clks is used
> by the individual platform clk drivers.
>
> Internalize the parts that aren't used outside of the implementation.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>
>
Reviewed-by: Miles Chen <[email protected]>

2022-01-24 19:33:11

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 04/31] clk: mediatek: gate: Implement unregister API

> The gate clk type within the MediaTek clk driver library only has a
> register function, and no corresponding unregister function. This
> means there is no way for its users to properly implement cleanup
> and removal.
>
> Add a matching unregister function for the gate type clk.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>

2022-01-24 19:34:32

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 05/31] clk: mediatek: gate: Clean up included headers

> Some included headers aren't actually used anywhere, while other headers
> with the declaration of functions and structures aren't directly
> included.
>
> Get rid of the unused ones, and add the ones that should be included
> directly.
>
> On the header side, replace headers that are included purely for data
> structure definitions with forward declarations. This decreases the
> amount of preprocessing and compilation effort required for each
> inclusion.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>
>
Reviewed-by: Miles Chen <[email protected]>

2022-01-26 01:32:32

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 06/31] clk: mediatek: cpumux: Implement unregister API

> The cpumux clk type within the MediaTek clk driver library only has
> a register function, and no corresponding unregister function. This
> means there is no way for its users to properly implement cleanup
> and removal.
>
> Add a matching unregister function for the cpumux type clk.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>
>
Reviewed-by: Miles Chen <[email protected]>
> ---
> drivers/clk/mediatek/clk-cpumux.c | 31 +++++++++++++++++++++++++++++++
> drivers/clk/mediatek/clk-cpumux.h | 3 +++
> 2 files changed, 34 insertions(+)
>
> diff --git a/drivers/clk/mediatek/clk-cpumux.c b/drivers/clk/mediatek/clk-cpumux.c
> index cab5095416b6..99a819e3673f 100644
> --- a/drivers/clk/mediatek/clk-cpumux.c
> +++ b/drivers/clk/mediatek/clk-cpumux.c
> @@ -77,6 +77,21 @@ mtk_clk_register_cpumux(const struct mtk_composite *mux,
> return clk;
> }
>
> +static void mtk_clk_unregister_cpumux(struct clk *clk)
> +{
> + struct mtk_clk_cpumux *cpumux;
> + struct clk_hw *hw;
> +
> + hw = __clk_get_hw(clk);
> + if (!hw)
> + return;
> +
> + cpumux = to_mtk_clk_cpumux(hw);
> +
> + clk_unregister(clk);
> + kfree(cpumux);
> +}
> +
> int mtk_clk_register_cpumuxes(struct device_node *node,
> const struct mtk_composite *clks, int num,
> struct clk_onecell_data *clk_data)
> @@ -106,4 +121,20 @@ int mtk_clk_register_cpumuxes(struct device_node *node,
> return 0;
> }
>
> +void mtk_clk_unregister_cpumuxes(const struct mtk_composite *clks, int num,
> + struct clk_onecell_data *clk_data)
> +{
> + int i;
> +
> + for (i = num; i > 0; i--) {
> + const struct mtk_composite *mux = &clks[i - 1];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[mux->id]))
> + continue;
> +
> + mtk_clk_unregister_cpumux(clk_data->clks[mux->id]);
> + clk_data->clks[mux->id] = ERR_PTR(-ENOENT);
> + }
> +}
> +
> MODULE_LICENSE("GPL");
> diff --git a/drivers/clk/mediatek/clk-cpumux.h b/drivers/clk/mediatek/clk-cpumux.h
> index 2aaf1afd4e5f..fda7770fd803 100644
> --- a/drivers/clk/mediatek/clk-cpumux.h
> +++ b/drivers/clk/mediatek/clk-cpumux.h
> @@ -19,4 +19,7 @@ int mtk_clk_register_cpumuxes(struct device_node *node,
> const struct mtk_composite *clks, int num,
> struct clk_onecell_data *clk_data);
>
> +void mtk_clk_unregister_cpumuxes(const struct mtk_composite *clks, int num,
> + struct clk_onecell_data *clk_data);
> +
> #endif /* __DRV_CLK_CPUMUX_H */
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 03:00:52

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 07/31] clk: mediatek: cpumux: Internalize struct mtk_clk_cpumux

> struct mtk_clk_cpumux is an implementation detail of the cpumux clk
> type, and is not used outside of the implementation.
>
> Internalize the definition to minimize leakage of details and shrink
> the header file.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>
>
Reviewed-by: Miles Chen <[email protected]>
> ---
> drivers/clk/mediatek/clk-cpumux.c | 8 ++++++++
> drivers/clk/mediatek/clk-cpumux.h | 8 --------
> 2 files changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/clk/mediatek/clk-cpumux.c b/drivers/clk/mediatek/clk-cpumux.c
> index 99a819e3673f..344c6399b22f 100644
> --- a/drivers/clk/mediatek/clk-cpumux.c
> +++ b/drivers/clk/mediatek/clk-cpumux.c
> @@ -12,6 +12,14 @@
> #include "clk-mtk.h"
> #include "clk-cpumux.h"
>
> +struct mtk_clk_cpumux {
> + struct clk_hw hw;
> + struct regmap *regmap;
> + u32 reg;
> + u32 mask;
> + u8 shift;
> +};
> +
> static inline struct mtk_clk_cpumux *to_mtk_clk_cpumux(struct clk_hw *_hw)
> {
> return container_of(_hw, struct mtk_clk_cpumux, hw);
> diff --git a/drivers/clk/mediatek/clk-cpumux.h b/drivers/clk/mediatek/clk-cpumux.h
> index fda7770fd803..a538f2bbef0d 100644
> --- a/drivers/clk/mediatek/clk-cpumux.h
> +++ b/drivers/clk/mediatek/clk-cpumux.h
> @@ -7,14 +7,6 @@
> #ifndef __DRV_CLK_CPUMUX_H
> #define __DRV_CLK_CPUMUX_H
>
> -struct mtk_clk_cpumux {
> - struct clk_hw hw;
> - struct regmap *regmap;
> - u32 reg;
> - u32 mask;
> - u8 shift;
> -};
> -
> int mtk_clk_register_cpumuxes(struct device_node *node,
> const struct mtk_composite *clks, int num,
> struct clk_onecell_data *clk_data);
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 03:01:38

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 10/31] clk: mediatek: mux: Internalize struct mtk_clk_mux

> struct mtk_clk_mux is an implementation detail of the mux clk type,
> and is not used outside of the implementation.
>
> Internalize the definition to minimize leakage of details and shrink
> the header file.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>
>
Reviewed-by: Miles Chen <[email protected]>
> ---
> drivers/clk/mediatek/clk-mux.c | 8 ++++++++
> drivers/clk/mediatek/clk-mux.h | 8 --------
> 2 files changed, 8 insertions(+), 8 deletions(-)
>
> diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
> index 6f0c22a699c3..2d4d8dc0120a 100644
> --- a/drivers/clk/mediatek/clk-mux.c
> +++ b/drivers/clk/mediatek/clk-mux.c
> @@ -13,6 +13,14 @@
> #include "clk-mtk.h"
> #include "clk-mux.h"
>
> +struct mtk_clk_mux {
> + struct clk_hw hw;
> + struct regmap *regmap;
> + const struct mtk_mux *data;
> + spinlock_t *lock;
> + bool reparent;
> +};
> +
> static inline struct mtk_clk_mux *to_mtk_clk_mux(struct clk_hw *hw)
> {
> return container_of(hw, struct mtk_clk_mux, hw);
> diff --git a/drivers/clk/mediatek/clk-mux.h b/drivers/clk/mediatek/clk-mux.h
> index cb2ac4f04c58..38a2b6014b08 100644
> --- a/drivers/clk/mediatek/clk-mux.h
> +++ b/drivers/clk/mediatek/clk-mux.h
> @@ -9,14 +9,6 @@
>
> #include <linux/clk-provider.h>
>
> -struct mtk_clk_mux {
> - struct clk_hw hw;
> - struct regmap *regmap;
> - const struct mtk_mux *data;
> - spinlock_t *lock;
> - bool reparent;
> -};
> -
> struct mtk_mux {
> int id;
> const char *name;
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 03:03:47

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 08/31] clk: mediatek: cpumux: Clean up included headers

> Some headers with the declaration of functions and structures aren't
> directly included. Explicitly include them so that future changes to
> other headers would not result in an unexpected build break.
>
> On the header side, add forward declarations for any data structures
> whose pointers are used in function signatures. No headers are
> required.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>

> ---
> drivers/clk/mediatek/clk-cpumux.c | 3 +++
> drivers/clk/mediatek/clk-cpumux.h | 4 ++++
> 2 files changed, 7 insertions(+)
>
> diff --git a/drivers/clk/mediatek/clk-cpumux.c b/drivers/clk/mediatek/clk-cpumux.c
> index 344c6399b22f..658aee789f44 100644
> --- a/drivers/clk/mediatek/clk-cpumux.c
> +++ b/drivers/clk/mediatek/clk-cpumux.c
> @@ -5,8 +5,11 @@
> */
>
> #include <linux/clk-provider.h>
> +#include <linux/container_of.h>
> +#include <linux/err.h>
> #include <linux/mfd/syscon.h>
> #include <linux/module.h>
> +#include <linux/regmap.h>
> #include <linux/slab.h>
>
> #include "clk-mtk.h"
> diff --git a/drivers/clk/mediatek/clk-cpumux.h b/drivers/clk/mediatek/clk-cpumux.h
> index a538f2bbef0d..b07e89f7c283 100644
> --- a/drivers/clk/mediatek/clk-cpumux.h
> +++ b/drivers/clk/mediatek/clk-cpumux.h
> @@ -7,6 +7,10 @@
> #ifndef __DRV_CLK_CPUMUX_H
> #define __DRV_CLK_CPUMUX_H
>
> +struct clk_onecell_data;
> +struct device_node;
> +struct mtk_composite;
> +
> int mtk_clk_register_cpumuxes(struct device_node *node,
> const struct mtk_composite *clks, int num,
> struct clk_onecell_data *clk_data);
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 03:04:02

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 09/31] clk: mediatek: mux: Implement unregister API

> The mux clk type within the MediaTek clk driver library only has a
> register function, and no corresponding unregister function. This
> means there is no way for its users to properly implement cleanup
> and removal.
>
> Add a matching unregister function for the mux type clk.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>

> ---
> drivers/clk/mediatek/clk-mux.c | 35 ++++++++++++++++++++++++++++++++++
> drivers/clk/mediatek/clk-mux.h | 3 +++
> 2 files changed, 38 insertions(+)
>
> diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
> index 89f23e111d91..6f0c22a699c3 100644
> --- a/drivers/clk/mediatek/clk-mux.c
> +++ b/drivers/clk/mediatek/clk-mux.c
> @@ -164,6 +164,21 @@ static struct clk *mtk_clk_register_mux(const struct mtk_mux *mux,
> return clk;
> }
>
> +static void mtk_clk_unregister_mux(struct clk *clk)
> +{
> + struct mtk_clk_mux *mux;
> + struct clk_hw *hw;
> +
> + hw = __clk_get_hw(clk);
> + if (!hw)
> + return;
> +
> + mux = to_mtk_clk_mux(hw);
> +
> + clk_unregister(clk);
> + kfree(mux);
> +}
> +
> int mtk_clk_register_muxes(const struct mtk_mux *muxes,
> int num, struct device_node *node,
> spinlock_t *lock,
> @@ -198,4 +213,24 @@ int mtk_clk_register_muxes(const struct mtk_mux *muxes,
> }
> EXPORT_SYMBOL_GPL(mtk_clk_register_muxes);
>
> +void mtk_clk_unregister_muxes(const struct mtk_mux *muxes, int num,
> + struct clk_onecell_data *clk_data)
> +{
> + int i;
> +
> + if (!clk_data)
> + return;
> +
> + for (i = num; i > 0; i--) {
> + const struct mtk_mux *mux = &muxes[i - 1];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[mux->id]))
> + continue;
> +
> + mtk_clk_unregister_mux(clk_data->clks[mux->id]);
> + clk_data->clks[mux->id] = ERR_PTR(-ENOENT);
> + }
> +}
> +EXPORT_SYMBOL_GPL(mtk_clk_unregister_muxes);
> +
> MODULE_LICENSE("GPL");
> diff --git a/drivers/clk/mediatek/clk-mux.h b/drivers/clk/mediatek/clk-mux.h
> index 27841d649118..cb2ac4f04c58 100644
> --- a/drivers/clk/mediatek/clk-mux.h
> +++ b/drivers/clk/mediatek/clk-mux.h
> @@ -88,4 +88,7 @@ int mtk_clk_register_muxes(const struct mtk_mux *muxes,
> spinlock_t *lock,
> struct clk_onecell_data *clk_data);
>
> +void mtk_clk_unregister_muxes(const struct mtk_mux *muxes, int num,
> + struct clk_onecell_data *clk_data);
> +
> #endif /* __DRV_CLK_MTK_MUX_H */
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 03:07:31

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 11/31] clk: mediatek: mux: Clean up included headers

> Some included headers aren't actually used anywhere, while other headers
> with the declaration of functions and structures aren't directly
> included.
>
> Get rid of the unused ones, and add the ones that should be included
> directly.
>
> On the header side, replace headers that are included purely for data
> structure definitions with forward declarations. This decreases the
> amount of preprocessing and compilation effort required for each
> inclusion.

Is this inspired by [1]?
[1] https://lore.kernel.org/lkml/[email protected]/

>
> Signed-off-by: Chen-Yu Tsai <[email protected]>
>
Reviewed-by: Miles Chen <[email protected]>
> ---
> drivers/clk/mediatek/clk-mux.c | 11 +++++++----
> drivers/clk/mediatek/clk-mux.h | 8 +++++++-
> 2 files changed, 14 insertions(+), 5 deletions(-)
>
> diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
> index 2d4d8dc0120a..01af6a52711a 100644
> --- a/drivers/clk/mediatek/clk-mux.c
> +++ b/drivers/clk/mediatek/clk-mux.c
> @@ -4,13 +4,16 @@
> * Author: Owen Chen <[email protected]>
> */
>
> -#include <linux/of.h>
> -#include <linux/of_address.h>
> -#include <linux/slab.h>
> +#include <linux/clk-provider.h>
> +#include <linux/compiler_types.h>
> +#include <linux/container_of.h>
> +#include <linux/err.h>
> #include <linux/mfd/syscon.h>
> #include <linux/module.h>
> +#include <linux/regmap.h>
> +#include <linux/spinlock.h>
> +#include <linux/slab.h>
>
> -#include "clk-mtk.h"
> #include "clk-mux.h"
>
> struct mtk_clk_mux {
> diff --git a/drivers/clk/mediatek/clk-mux.h b/drivers/clk/mediatek/clk-mux.h
> index 38a2b6014b08..903a3c937959 100644
> --- a/drivers/clk/mediatek/clk-mux.h
> +++ b/drivers/clk/mediatek/clk-mux.h
> @@ -7,7 +7,13 @@
> #ifndef __DRV_CLK_MTK_MUX_H
> #define __DRV_CLK_MTK_MUX_H
>
> -#include <linux/clk-provider.h>
> +#include <linux/spinlock.h>
> +#include <linux/types.h>
> +
> +struct clk;
> +struct clk_onecell_data;
> +struct clk_ops;
> +struct device_node;
>
> struct mtk_mux {
> int id;
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 19:51:42

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 13/31] clk: mediatek: pll: Implement unregister API

> +static void mtk_clk_unregister_pll(struct clk *clk)
> +{
> + struct clk_hw *hw = __clk_get_hw(clk);
> + struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
> +
> + clk_unregister(clk);
> + kfree(pll);
> +}
> +

mtk_clk_unregister_pll() looks different.
Do we need to check hw before passing it to to_mtk_clk_pll(hw), like
mtk_clk_unregister_mux()?

> void mtk_clk_register_plls(struct device_node *node,
> const struct mtk_pll_data *plls, int num_plls, struct clk_onecell_data *clk_data)
> {
> @@ -388,4 +397,44 @@ void mtk_clk_register_plls(struct device_node *node,
> }
> EXPORT_SYMBOL_GPL(mtk_clk_register_plls);
>
> +static __iomem void *mtk_clk_pll_get_base(struct clk *clk,
> + const struct mtk_pll_data *data)
> +{
> + struct clk_hw *hw = __clk_get_hw(clk);
> + struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
> +
> + return pll->base_addr - data->reg;
> +}
> +
> +void mtk_clk_unregister_plls(const struct mtk_pll_data *plls, int num_plls,
> + struct clk_onecell_data *clk_data)
> +{
> + __iomem void *base = NULL;
> + int i;
> +
> + if (!clk_data)
> + return;
> +
> + for (i = num_plls; i > 0; i--) {
> + const struct mtk_pll_data *pll = &plls[i - 1];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[pll->id]))
> + continue;
> +
> + /*
> + * This is quite ugly but unfortunately the clks don't have
> + * any device tied to them, so there's no place to store the
> + * pointer to the I/O region base address. We have to fetch
> + * it from one of the registered clks.
> + */
> + base = mtk_clk_pll_get_base(clk_data->clks[pll->id], pll);
> +
> + mtk_clk_unregister_pll(clk_data->clks[pll->id]);
> + clk_data->clks[pll->id] = ERR_PTR(-ENOENT);
> + }
> +
> + iounmap(base);
> +}
> +EXPORT_SYMBOL_GPL(mtk_clk_unregister_plls);
> +
> MODULE_LICENSE("GPL");
> diff --git a/drivers/clk/mediatek/clk-pll.h b/drivers/clk/mediatek/clk-pll.h
> index d01b0c38311d..a889b1e472e7 100644
> --- a/drivers/clk/mediatek/clk-pll.h
> +++ b/drivers/clk/mediatek/clk-pll.h
> @@ -51,5 +51,7 @@ struct mtk_pll_data {
> void mtk_clk_register_plls(struct device_node *node,
> const struct mtk_pll_data *plls, int num_plls,
> struct clk_onecell_data *clk_data);
> +void mtk_clk_unregister_plls(const struct mtk_pll_data *plls, int num_plls,
> + struct clk_onecell_data *clk_data);
>
> #endif /* __DRV_CLK_MTK_PLL_H */
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 19:52:51

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 12/31] clk: mediatek: pll: Split definitions into separate header file

> When the PLL type clk was implemented in the MediaTek clk driver
> library, the data structure definitions and function declaration
> were put in the common header file.
>
> Since it is its own type of clk, and not all platform clk drivers
> utilize it, having the definitions in the common header results
> in wasted cycles during compilation.
>
> Split out the related definitions and declarations into its own
> header file, and include that only in the platform clk drivers that
> need it.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>
>
Reviewed-by: Miles Chen <[email protected]>
> ---
> drivers/clk/mediatek/clk-mt2701.c | 5 +-
> drivers/clk/mediatek/clk-mt2712.c | 3 +-
> drivers/clk/mediatek/clk-mt6765.c | 3 +-
> drivers/clk/mediatek/clk-mt6779.c | 3 +-
> drivers/clk/mediatek/clk-mt6797.c | 3 +-
> drivers/clk/mediatek/clk-mt7622.c | 5 +-
> drivers/clk/mediatek/clk-mt7629.c | 5 +-
> drivers/clk/mediatek/clk-mt7986-apmixed.c | 4 +-
> drivers/clk/mediatek/clk-mt8135.c | 3 +-
> drivers/clk/mediatek/clk-mt8167.c | 3 +-
> drivers/clk/mediatek/clk-mt8173.c | 5 +-
> drivers/clk/mediatek/clk-mt8183.c | 3 +-
> drivers/clk/mediatek/clk-mt8192.c | 3 +-
> drivers/clk/mediatek/clk-mt8195-apmixedsys.c | 1 +
> drivers/clk/mediatek/clk-mt8195-apusys_pll.c | 1 +
> drivers/clk/mediatek/clk-mt8516.c | 3 +-
> drivers/clk/mediatek/clk-mtk.h | 39 --------------
> drivers/clk/mediatek/clk-pll.c | 1 +
> drivers/clk/mediatek/clk-pll.h | 55 ++++++++++++++++++++
> 19 files changed, 91 insertions(+), 57 deletions(-)
> create mode 100644 drivers/clk/mediatek/clk-pll.h
>
> diff --git a/drivers/clk/mediatek/clk-mt2701.c b/drivers/clk/mediatek/clk-mt2701.c
> index 695be0f77427..1eb3e4563c3f 100644
> --- a/drivers/clk/mediatek/clk-mt2701.c
> +++ b/drivers/clk/mediatek/clk-mt2701.c
> @@ -10,9 +10,10 @@
> #include <linux/of_device.h>
> #include <linux/platform_device.h>
>
> -#include "clk-mtk.h"
> -#include "clk-gate.h"
> #include "clk-cpumux.h"
> +#include "clk-gate.h"
> +#include "clk-mtk.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt2701-clk.h>
>
> diff --git a/drivers/clk/mediatek/clk-mt2712.c b/drivers/clk/mediatek/clk-mt2712.c
> index a3bd9a107209..ff72b9ab945b 100644
> --- a/drivers/clk/mediatek/clk-mt2712.c
> +++ b/drivers/clk/mediatek/clk-mt2712.c
> @@ -13,8 +13,9 @@
> #include <linux/platform_device.h>
> #include <linux/slab.h>
>
> -#include "clk-mtk.h"
> #include "clk-gate.h"
> +#include "clk-pll.h"
> +#include "clk-mtk.h"
>
> #include <dt-bindings/clock/mt2712-clk.h>
>
> diff --git a/drivers/clk/mediatek/clk-mt6765.c b/drivers/clk/mediatek/clk-mt6765.c
> index d77ea5aff292..24829ca3bd1f 100644
> --- a/drivers/clk/mediatek/clk-mt6765.c
> +++ b/drivers/clk/mediatek/clk-mt6765.c
> @@ -12,9 +12,10 @@
> #include <linux/of_device.h>
> #include <linux/platform_device.h>
>
> -#include "clk-mtk.h"
> #include "clk-gate.h"
> +#include "clk-mtk.h"
> #include "clk-mux.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt6765-clk.h>
>
> diff --git a/drivers/clk/mediatek/clk-mt6779.c b/drivers/clk/mediatek/clk-mt6779.c
> index 9825385c9f94..7b61664da18f 100644
> --- a/drivers/clk/mediatek/clk-mt6779.c
> +++ b/drivers/clk/mediatek/clk-mt6779.c
> @@ -10,9 +10,10 @@
> #include <linux/of_device.h>
> #include <linux/platform_device.h>
>
> +#include "clk-gate.h"
> #include "clk-mtk.h"
> #include "clk-mux.h"
> -#include "clk-gate.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt6779-clk.h>
>
> diff --git a/drivers/clk/mediatek/clk-mt6797.c b/drivers/clk/mediatek/clk-mt6797.c
> index 428eb24ffec5..02259e81625a 100644
> --- a/drivers/clk/mediatek/clk-mt6797.c
> +++ b/drivers/clk/mediatek/clk-mt6797.c
> @@ -9,8 +9,9 @@
> #include <linux/of_device.h>
> #include <linux/platform_device.h>
>
> -#include "clk-mtk.h"
> #include "clk-gate.h"
> +#include "clk-mtk.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt6797-clk.h>
>
> diff --git a/drivers/clk/mediatek/clk-mt7622.c b/drivers/clk/mediatek/clk-mt7622.c
> index ef5947e15c75..0e1fb30a1e98 100644
> --- a/drivers/clk/mediatek/clk-mt7622.c
> +++ b/drivers/clk/mediatek/clk-mt7622.c
> @@ -11,9 +11,10 @@
> #include <linux/of_device.h>
> #include <linux/platform_device.h>
>
> -#include "clk-mtk.h"
> -#include "clk-gate.h"
> #include "clk-cpumux.h"
> +#include "clk-gate.h"
> +#include "clk-mtk.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt7622-clk.h>
> #include <linux/clk.h> /* for consumer */
> diff --git a/drivers/clk/mediatek/clk-mt7629.c b/drivers/clk/mediatek/clk-mt7629.c
> index a0ee079670c7..c0e023bf31eb 100644
> --- a/drivers/clk/mediatek/clk-mt7629.c
> +++ b/drivers/clk/mediatek/clk-mt7629.c
> @@ -12,9 +12,10 @@
> #include <linux/of_device.h>
> #include <linux/platform_device.h>
>
> -#include "clk-mtk.h"
> -#include "clk-gate.h"
> #include "clk-cpumux.h"
> +#include "clk-gate.h"
> +#include "clk-mtk.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt7629-clk.h>
>
> diff --git a/drivers/clk/mediatek/clk-mt7986-apmixed.c b/drivers/clk/mediatek/clk-mt7986-apmixed.c
> index 98ec3887585f..21d4c82e782a 100644
> --- a/drivers/clk/mediatek/clk-mt7986-apmixed.c
> +++ b/drivers/clk/mediatek/clk-mt7986-apmixed.c
> @@ -10,9 +10,11 @@
> #include <linux/of_address.h>
> #include <linux/of_device.h>
> #include <linux/platform_device.h>
> -#include "clk-mtk.h"
> +
> #include "clk-gate.h"
> +#include "clk-mtk.h"
> #include "clk-mux.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt7986-clk.h>
> #include <linux/clk.h>
> diff --git a/drivers/clk/mediatek/clk-mt8135.c b/drivers/clk/mediatek/clk-mt8135.c
> index 9b4b645aea99..09ad272d51f1 100644
> --- a/drivers/clk/mediatek/clk-mt8135.c
> +++ b/drivers/clk/mediatek/clk-mt8135.c
> @@ -11,8 +11,9 @@
> #include <linux/mfd/syscon.h>
> #include <dt-bindings/clock/mt8135-clk.h>
>
> -#include "clk-mtk.h"
> #include "clk-gate.h"
> +#include "clk-mtk.h"
> +#include "clk-pll.h"
>
> static DEFINE_SPINLOCK(mt8135_clk_lock);
>
> diff --git a/drivers/clk/mediatek/clk-mt8167.c b/drivers/clk/mediatek/clk-mt8167.c
> index e5ea10e31799..812b33a57530 100644
> --- a/drivers/clk/mediatek/clk-mt8167.c
> +++ b/drivers/clk/mediatek/clk-mt8167.c
> @@ -12,8 +12,9 @@
> #include <linux/slab.h>
> #include <linux/mfd/syscon.h>
>
> -#include "clk-mtk.h"
> #include "clk-gate.h"
> +#include "clk-mtk.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt8167-clk.h>
>
> diff --git a/drivers/clk/mediatek/clk-mt8173.c b/drivers/clk/mediatek/clk-mt8173.c
> index 8f898ac476c0..46b7655feeaa 100644
> --- a/drivers/clk/mediatek/clk-mt8173.c
> +++ b/drivers/clk/mediatek/clk-mt8173.c
> @@ -8,9 +8,10 @@
> #include <linux/of.h>
> #include <linux/of_address.h>
>
> -#include "clk-mtk.h"
> -#include "clk-gate.h"
> #include "clk-cpumux.h"
> +#include "clk-gate.h"
> +#include "clk-mtk.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt8173-clk.h>
>
> diff --git a/drivers/clk/mediatek/clk-mt8183.c b/drivers/clk/mediatek/clk-mt8183.c
> index 5046852eb0fd..68496554dd3d 100644
> --- a/drivers/clk/mediatek/clk-mt8183.c
> +++ b/drivers/clk/mediatek/clk-mt8183.c
> @@ -11,9 +11,10 @@
> #include <linux/platform_device.h>
> #include <linux/slab.h>
>
> +#include "clk-gate.h"
> #include "clk-mtk.h"
> #include "clk-mux.h"
> -#include "clk-gate.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt8183-clk.h>
>
> diff --git a/drivers/clk/mediatek/clk-mt8192.c b/drivers/clk/mediatek/clk-mt8192.c
> index cbc7c6dbe0f4..5f998aab3bfd 100644
> --- a/drivers/clk/mediatek/clk-mt8192.c
> +++ b/drivers/clk/mediatek/clk-mt8192.c
> @@ -12,9 +12,10 @@
> #include <linux/platform_device.h>
> #include <linux/slab.h>
>
> +#include "clk-gate.h"
> #include "clk-mtk.h"
> #include "clk-mux.h"
> -#include "clk-gate.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt8192-clk.h>
>
> diff --git a/drivers/clk/mediatek/clk-mt8195-apmixedsys.c b/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
> index 6156ceeed71e..5b1b7dc447eb 100644
> --- a/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
> +++ b/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
> @@ -5,6 +5,7 @@
>
> #include "clk-gate.h"
> #include "clk-mtk.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt8195-clk.h>
> #include <linux/of_device.h>
> diff --git a/drivers/clk/mediatek/clk-mt8195-apusys_pll.c b/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
> index f1c84186346e..db449ff877d7 100644
> --- a/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
> +++ b/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
> @@ -4,6 +4,7 @@
> // Author: Chun-Jie Chen <[email protected]>
>
> #include "clk-mtk.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt8195-clk.h>
> #include <linux/clk-provider.h>
> diff --git a/drivers/clk/mediatek/clk-mt8516.c b/drivers/clk/mediatek/clk-mt8516.c
> index 9d4261ecc760..a37143f920ce 100644
> --- a/drivers/clk/mediatek/clk-mt8516.c
> +++ b/drivers/clk/mediatek/clk-mt8516.c
> @@ -11,8 +11,9 @@
> #include <linux/slab.h>
> #include <linux/mfd/syscon.h>
>
> -#include "clk-mtk.h"
> #include "clk-gate.h"
> +#include "clk-mtk.h"
> +#include "clk-pll.h"
>
> #include <dt-bindings/clock/mt8516-clk.h>
>
> diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
> index bdec7dc5e07a..168220f85489 100644
> --- a/drivers/clk/mediatek/clk-mtk.h
> +++ b/drivers/clk/mediatek/clk-mtk.h
> @@ -179,45 +179,6 @@ void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
> struct clk_onecell_data *mtk_alloc_clk_data(unsigned int clk_num);
> void mtk_free_clk_data(struct clk_onecell_data *clk_data);
>
> -#define HAVE_RST_BAR BIT(0)
> -#define PLL_AO BIT(1)
> -
> -struct mtk_pll_div_table {
> - u32 div;
> - unsigned long freq;
> -};
> -
> -struct mtk_pll_data {
> - int id;
> - const char *name;
> - u32 reg;
> - u32 pwr_reg;
> - u32 en_mask;
> - u32 pd_reg;
> - u32 tuner_reg;
> - u32 tuner_en_reg;
> - u8 tuner_en_bit;
> - int pd_shift;
> - unsigned int flags;
> - const struct clk_ops *ops;
> - u32 rst_bar_mask;
> - unsigned long fmin;
> - unsigned long fmax;
> - int pcwbits;
> - int pcwibits;
> - u32 pcw_reg;
> - int pcw_shift;
> - u32 pcw_chg_reg;
> - const struct mtk_pll_div_table *div_table;
> - const char *parent_name;
> - u32 en_reg;
> - u8 pll_en_bit; /* Assume 0, indicates BIT(0) by default */
> -};
> -
> -void mtk_clk_register_plls(struct device_node *node,
> - const struct mtk_pll_data *plls, int num_plls,
> - struct clk_onecell_data *clk_data);
> -
> struct clk *mtk_clk_register_ref2usb_tx(const char *name,
> const char *parent_name, void __iomem *reg);
>
> diff --git a/drivers/clk/mediatek/clk-pll.c b/drivers/clk/mediatek/clk-pll.c
> index f04f724e12e5..64f59554bc9b 100644
> --- a/drivers/clk/mediatek/clk-pll.c
> +++ b/drivers/clk/mediatek/clk-pll.c
> @@ -13,6 +13,7 @@
> #include <linux/delay.h>
>
> #include "clk-mtk.h"
> +#include "clk-pll.h"
>
> #define REG_CON0 0
> #define REG_CON1 4
> diff --git a/drivers/clk/mediatek/clk-pll.h b/drivers/clk/mediatek/clk-pll.h
> new file mode 100644
> index 000000000000..d01b0c38311d
> --- /dev/null
> +++ b/drivers/clk/mediatek/clk-pll.h
> @@ -0,0 +1,55 @@
> +/* SPDX-License-Identifier: GPL-2.0-only */
> +/*
> + * Copyright (c) 2014 MediaTek Inc.
> + * Author: James Liao <[email protected]>
> + */
> +
> +#ifndef __DRV_CLK_MTK_PLL_H
> +#define __DRV_CLK_MTK_PLL_H
> +
> +#include <linux/types.h>
> +
> +struct clk_ops;
> +struct clk_onecell_data;
> +struct device_node;
> +
> +struct mtk_pll_div_table {
> + u32 div;
> + unsigned long freq;
> +};
> +
> +#define HAVE_RST_BAR BIT(0)
> +#define PLL_AO BIT(1)
> +
> +struct mtk_pll_data {
> + int id;
> + const char *name;
> + u32 reg;
> + u32 pwr_reg;
> + u32 en_mask;
> + u32 pd_reg;
> + u32 tuner_reg;
> + u32 tuner_en_reg;
> + u8 tuner_en_bit;
> + int pd_shift;
> + unsigned int flags;
> + const struct clk_ops *ops;
> + u32 rst_bar_mask;
> + unsigned long fmin;
> + unsigned long fmax;
> + int pcwbits;
> + int pcwibits;
> + u32 pcw_reg;
> + int pcw_shift;
> + u32 pcw_chg_reg;
> + const struct mtk_pll_div_table *div_table;
> + const char *parent_name;
> + u32 en_reg;
> + u8 pll_en_bit; /* Assume 0, indicates BIT(0) by default */
> +};
> +
> +void mtk_clk_register_plls(struct device_node *node,
> + const struct mtk_pll_data *plls, int num_plls,
> + struct clk_onecell_data *clk_data);
> +
> +#endif /* __DRV_CLK_MTK_PLL_H */
> --
> 2.35.0.rc0.227.g00780c9af4-goog
>
>

2022-01-26 19:54:33

by Chen-Yu Tsai

[permalink] [raw]
Subject: Re: [PATCH 13/31] clk: mediatek: pll: Implement unregister API

On Wed, Jan 26, 2022 at 2:04 PM Miles Chen <[email protected]> wrote:
>
> > +static void mtk_clk_unregister_pll(struct clk *clk)
> > +{
> > + struct clk_hw *hw = __clk_get_hw(clk);
> > + struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
> > +
> > + clk_unregister(clk);
> > + kfree(pll);
> > +}
> > +
>
> mtk_clk_unregister_pll() looks different.
> Do we need to check hw before passing it to to_mtk_clk_pll(hw), like
> mtk_clk_unregister_mux()?

Good catch.

In theory we should, since __get_clk_hw() would return NULL if clk is NULL.
However the code already does that check before it calls
mtk_clk_unregister_pll(), so the code is safe. We should make everything
consistent though.

I might have written the code on separate days and thus introduced some
discrepancies. I'll fix it in v2.


ChenYu

> > void mtk_clk_register_plls(struct device_node *node,
> > const struct mtk_pll_data *plls, int num_plls, struct clk_onecell_data *clk_data)
> > {
> > @@ -388,4 +397,44 @@ void mtk_clk_register_plls(struct device_node *node,
> > }
> > EXPORT_SYMBOL_GPL(mtk_clk_register_plls);
> >
> > +static __iomem void *mtk_clk_pll_get_base(struct clk *clk,
> > + const struct mtk_pll_data *data)
> > +{
> > + struct clk_hw *hw = __clk_get_hw(clk);
> > + struct mtk_clk_pll *pll = to_mtk_clk_pll(hw);
> > +
> > + return pll->base_addr - data->reg;
> > +}
> > +
> > +void mtk_clk_unregister_plls(const struct mtk_pll_data *plls, int num_plls,
> > + struct clk_onecell_data *clk_data)
> > +{
> > + __iomem void *base = NULL;
> > + int i;
> > +
> > + if (!clk_data)
> > + return;
> > +
> > + for (i = num_plls; i > 0; i--) {
> > + const struct mtk_pll_data *pll = &plls[i - 1];
> > +
> > + if (IS_ERR_OR_NULL(clk_data->clks[pll->id]))
> > + continue;
> > +
> > + /*
> > + * This is quite ugly but unfortunately the clks don't have
> > + * any device tied to them, so there's no place to store the
> > + * pointer to the I/O region base address. We have to fetch
> > + * it from one of the registered clks.
> > + */
> > + base = mtk_clk_pll_get_base(clk_data->clks[pll->id], pll);
> > +
> > + mtk_clk_unregister_pll(clk_data->clks[pll->id]);
> > + clk_data->clks[pll->id] = ERR_PTR(-ENOENT);
> > + }
> > +
> > + iounmap(base);
> > +}
> > +EXPORT_SYMBOL_GPL(mtk_clk_unregister_plls);
> > +
> > MODULE_LICENSE("GPL");
> > diff --git a/drivers/clk/mediatek/clk-pll.h b/drivers/clk/mediatek/clk-pll.h
> > index d01b0c38311d..a889b1e472e7 100644
> > --- a/drivers/clk/mediatek/clk-pll.h
> > +++ b/drivers/clk/mediatek/clk-pll.h
> > @@ -51,5 +51,7 @@ struct mtk_pll_data {
> > void mtk_clk_register_plls(struct device_node *node,
> > const struct mtk_pll_data *plls, int num_plls,
> > struct clk_onecell_data *clk_data);
> > +void mtk_clk_unregister_plls(const struct mtk_pll_data *plls, int num_plls,
> > + struct clk_onecell_data *clk_data);
> >
> > #endif /* __DRV_CLK_MTK_PLL_H */
> > --
> > 2.35.0.rc0.227.g00780c9af4-goog
>
>

2022-01-26 20:04:25

by Chen-Yu Tsai

[permalink] [raw]
Subject: Re: [PATCH 11/31] clk: mediatek: mux: Clean up included headers

On Wed, Jan 26, 2022 at 1:38 AM Miles Chen <[email protected]> wrote:
>
> > Some included headers aren't actually used anywhere, while other headers
> > with the declaration of functions and structures aren't directly
> > included.
> >
> > Get rid of the unused ones, and add the ones that should be included
> > directly.
> >
> > On the header side, replace headers that are included purely for data
> > structure definitions with forward declarations. This decreases the
> > amount of preprocessing and compilation effort required for each
> > inclusion.
>
> Is this inspired by [1]?
> [1] https://lore.kernel.org/lkml/[email protected]/

Yes and no.

When clk-mtk.h was removed from the included headers, many headers had
to be added. Also, clk-mux.h did not include any headers, so if it were
to be used by itself, it probably would not compile.

Using forward declarations for the data structures instead of including the
header files inspired in part by that series though.

ChenYu

2022-01-26 20:07:53

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 14/31] clk: mediatek: pll: Clean up included headers

> Some included headers aren't actually used anywhere, while other headers
> with the declaration of functions and structures aren't directly
> included.
>
> Get rid of the unused ones, and add the ones that should be included
> directly.
>
> Also, expand the MHZ macro with spelled-out "1000 * 1000" to be able
> to not include clk-mtk.h. The existing ternary operator is rewritten
> in a shortened form to accommodate the expanded macro.

I think MHZ is more human readable than 1000*1000,
like SZ_4K in linux/sizes.h.

MHZ is also use by other clk-mtxxxx.c (by including clk-mtk.h)
, so maybe we should keep clk-mtk.h?

Miles
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>
> ---
> drivers/clk/mediatek/clk-pll.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/clk/mediatek/clk-pll.c b/drivers/clk/mediatek/clk-pll.c
> index 081e0df8203e..9698d1c97cd6 100644
> --- a/drivers/clk/mediatek/clk-pll.c
> +++ b/drivers/clk/mediatek/clk-pll.c
> @@ -4,15 +4,15 @@
> * Author: James Liao <[email protected]>
> */
>
> -#include <linux/of.h>
> -#include <linux/of_address.h>
> +#include <linux/clk-provider.h>
> +#include <linux/container_of.h>
> +#include <linux/delay.h>
> +#include <linux/err.h>
> #include <linux/io.h>
> #include <linux/module.h>
> +#include <linux/of_address.h>
> #include <linux/slab.h>
> -#include <linux/clkdev.h>
> -#include <linux/delay.h>
>
> -#include "clk-mtk.h"
> #include "clk-pll.h"
>
> #define REG_CON0 0
> @@ -162,7 +162,7 @@ static void mtk_pll_set_rate_regs(struct mtk_clk_pll *pll, u32 pcw,
> static void mtk_pll_calc_values(struct mtk_clk_pll *pll, u32 *pcw, u32 *postdiv,
> u32 freq, u32 fin)
> {
> - unsigned long fmin = pll->data->fmin ? pll->data->fmin : (1000 * MHZ);
> + unsigned long fmin = pll->data->fmin ?: (1000 * 1000 * 1000);
> const struct mtk_pll_div_table *div_table = pll->data->div_table;
> u64 _pcw;
> int ibits;
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 20:10:26

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 15/31] clk: mediatek: Implement mtk_clk_unregister_fixed_clks() API

> mtk_clk_register_fixed_clks(), as the name suggests, is used to register
> a given list of fixed rate clks. However it is lacking a counterpart
> unregister API.
>
> Implement said unregister API so that the various clock platform drivers
> can utilize it to do proper unregistration, cleanup and removal.
>
> In the header file, the register function's declaration is also
> reformatted to fit code style guidelines.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>
>

Reviewed-by: Miles Chen <[email protected]>
> ---
> drivers/clk/mediatek/clk-mtk.c | 20 ++++++++++++++++++++
> drivers/clk/mediatek/clk-mtk.h | 6 ++++--
> 2 files changed, 24 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
> index 0c5db3c71fdd..7c0d5706eed7 100644
> --- a/drivers/clk/mediatek/clk-mtk.c
> +++ b/drivers/clk/mediatek/clk-mtk.c
> @@ -80,6 +80,26 @@ void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
> }
> EXPORT_SYMBOL_GPL(mtk_clk_register_fixed_clks);
>
> +void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
> + struct clk_onecell_data *clk_data)
> +{
> + int i;
> +
> + if (!clk_data)
> + return;
> +
> + for (i = num; i > 0; i--) {
> + const struct mtk_fixed_clk *rc = &clks[i - 1];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[rc->id]))
> + continue;
> +
> + clk_unregister_fixed_rate(clk_data->clks[rc->id]);
> + clk_data->clks[rc->id] = ERR_PTR(-ENOENT);
> + }
> +}
> +EXPORT_SYMBOL_GPL(mtk_clk_unregister_fixed_clks);
> +
> void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
> int num, struct clk_onecell_data *clk_data)
> {
> diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
> index 168220f85489..cc7f920eabb4 100644
> --- a/drivers/clk/mediatek/clk-mtk.h
> +++ b/drivers/clk/mediatek/clk-mtk.h
> @@ -34,8 +34,10 @@ struct mtk_fixed_clk {
> .rate = _rate, \
> }
>
> -void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
> - int num, struct clk_onecell_data *clk_data);
> +void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
> + struct clk_onecell_data *clk_data);
> +void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
> + struct clk_onecell_data *clk_data);
>
> struct mtk_fixed_factor {
> int id;
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 20:11:02

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 16/31] clk: mediatek: Implement mtk_clk_unregister_factors() API

> mtk_clk_register_factors(), as the name suggests, is used to register
> a given list of fixed factor clks. However it is lacking a counterpart
> unregister API.
>
> Implement said unregister API so that the various clock platform drivers
> can utilize it to do proper unregistration, cleanup and removal.
>
> In the header file, the register function's declaration is also
> reformatted to fit code style guidelines.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>
>
Reviewed-by: Miles Chen <[email protected]>
> ---
> drivers/clk/mediatek/clk-mtk.c | 20 ++++++++++++++++++++
> drivers/clk/mediatek/clk-mtk.h | 6 ++++--
> 2 files changed, 24 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
> index 7c0d5706eed7..b267b2f04b84 100644
> --- a/drivers/clk/mediatek/clk-mtk.c
> +++ b/drivers/clk/mediatek/clk-mtk.c
> @@ -126,6 +126,26 @@ void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
> }
> EXPORT_SYMBOL_GPL(mtk_clk_register_factors);
>
> +void mtk_clk_unregister_factors(const struct mtk_fixed_factor *clks, int num,
> + struct clk_onecell_data *clk_data)
> +{
> + int i;
> +
> + if (!clk_data)
> + return;
> +
> + for (i = num; i > 0; i--) {
> + const struct mtk_fixed_factor *ff = &clks[i - 1];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[ff->id]))
> + continue;
> +
> + clk_unregister_fixed_factor(clk_data->clks[ff->id]);
> + clk_data->clks[ff->id] = ERR_PTR(-ENOENT);
> + }
> +}
> +EXPORT_SYMBOL_GPL(mtk_clk_unregister_factors);
> +
> struct clk *mtk_clk_register_composite(const struct mtk_composite *mc,
> void __iomem *base, spinlock_t *lock)
> {
> diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
> index cc7f920eabb4..4db1a97c1250 100644
> --- a/drivers/clk/mediatek/clk-mtk.h
> +++ b/drivers/clk/mediatek/clk-mtk.h
> @@ -55,8 +55,10 @@ struct mtk_fixed_factor {
> .div = _div, \
> }
>
> -void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
> - int num, struct clk_onecell_data *clk_data);
> +void mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
> + struct clk_onecell_data *clk_data);
> +void mtk_clk_unregister_factors(const struct mtk_fixed_factor *clks, int num,
> + struct clk_onecell_data *clk_data);
>
> struct mtk_composite {
> int id;
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 20:13:31

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 18/31] clk: mediatek: Implement mtk_clk_unregister_composites() API

> mtk_clk_register_composites(), as the name suggests, is used to register
> a given list of composite clks. However it is lacking a counterpart
> unregister API.
>
> Implement said unregister API so that the various clock platform drivers
> can utilize it to do proper unregistration, cleanup and removal.
>
> In the header file, the register function's declaration is also
> reformatted to fit code style guidelines.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>
> ---
> drivers/clk/mediatek/clk-mtk.c | 41 ++++++++++++++++++++++++++++++++++
> drivers/clk/mediatek/clk-mtk.h | 2 ++
> 2 files changed, 43 insertions(+)
>
> diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
> index 3a6dfe445e63..2150ed8678e0 100644
> --- a/drivers/clk/mediatek/clk-mtk.c
> +++ b/drivers/clk/mediatek/clk-mtk.c
> @@ -233,6 +233,27 @@ struct clk *mtk_clk_register_composite(const struct mtk_composite *mc,
> return ERR_PTR(ret);
> }
>
> +static void mtk_clk_unregister_composite(struct clk *clk)
> +{
> + struct clk_hw *hw = __clk_get_hw(clk);
> + struct clk_composite *composite = to_clk_composite(hw);
> + struct clk_mux *mux = NULL;
> + struct clk_gate *gate = NULL;
> + struct clk_divider *div = NULL;

Like mtk_clk_unregister_pll(clk), let's check !hw for consistency
of clk unregister functions.

thanks,
Miles
> +
> + if (composite->mux_hw)
> + mux = to_clk_mux(composite->mux_hw);
> + if (composite->gate_hw)
> + gate = to_clk_gate(composite->gate_hw);
> + if (composite->rate_hw)
> + div = to_clk_divider(composite->rate_hw);
> +
> + clk_unregister_composite(clk);
> + kfree(div);
> + kfree(gate);
> + kfree(mux);
> +}
> +
> void mtk_clk_register_composites(const struct mtk_composite *mcs,
> int num, void __iomem *base, spinlock_t *lock,
> struct clk_onecell_data *clk_data)
> @@ -259,6 +280,26 @@ void mtk_clk_register_composites(const struct mtk_composite *mcs,
> }
> EXPORT_SYMBOL_GPL(mtk_clk_register_composites);
>
> +void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
> + struct clk_onecell_data *clk_data)
> +{
> + int i;
> +
> + if (!clk_data)
> + return;
> +
> + for (i = num; i > 0; i--) {
> + const struct mtk_composite *mc = &mcs[i - 1];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[mc->id]))
> + continue;
> +
> + mtk_clk_unregister_composite(clk_data->clks[mc->id]);
> + clk_data->clks[mc->id] = ERR_PTR(-ENOENT);
> + }
> +}
> +EXPORT_SYMBOL_GPL(mtk_clk_unregister_composites);
> +
> void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
> int num, void __iomem *base, spinlock_t *lock,
> struct clk_onecell_data *clk_data)
> diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
> index e3ae22fb0334..3c3a934f53cd 100644
> --- a/drivers/clk/mediatek/clk-mtk.h
> +++ b/drivers/clk/mediatek/clk-mtk.h
> @@ -153,6 +153,8 @@ struct clk *mtk_clk_register_composite(const struct mtk_composite *mc,
> void mtk_clk_register_composites(const struct mtk_composite *mcs,
> int num, void __iomem *base, spinlock_t *lock,
> struct clk_onecell_data *clk_data);
> +void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
> + struct clk_onecell_data *clk_data);
>
> struct mtk_clk_divider {
> int id;
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 20:14:01

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 17/31] clk: mediatek: Implement mtk_clk_unregister_divider_clks() API

> mtk_clk_register_divider_clks(), as the name suggests, is used to register
> a given list of divider clks. However it is lacking a counterpart
> unregister API.
>
> Implement said unregister API so that the various clock platform drivers
> can utilize it to do proper unregistration, cleanup and removal.
>
> In the header file, the register function's declaration is also
> reformatted to fit code style guidelines.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>

> ---
> drivers/clk/mediatek/clk-mtk.c | 19 +++++++++++++++++++
> drivers/clk/mediatek/clk-mtk.h | 8 +++++---
> 2 files changed, 24 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
> index b267b2f04b84..3a6dfe445e63 100644
> --- a/drivers/clk/mediatek/clk-mtk.c
> +++ b/drivers/clk/mediatek/clk-mtk.c
> @@ -286,6 +286,25 @@ void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
> }
> }
>
> +void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
> + struct clk_onecell_data *clk_data)
> +{
> + int i;
> +
> + if (!clk_data)
> + return;
> +
> + for (i = num; i > 0; i--) {
> + const struct mtk_clk_divider *mcd = &mcds[i - 1];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
> + continue;
> +
> + clk_unregister_divider(clk_data->clks[mcd->id]);
> + clk_data->clks[mcd->id] = ERR_PTR(-ENOENT);
> + }
> +}
> +
> int mtk_clk_simple_probe(struct platform_device *pdev)
> {
> const struct mtk_clk_desc *mcd;
> diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
> index 4db1a97c1250..e3ae22fb0334 100644
> --- a/drivers/clk/mediatek/clk-mtk.h
> +++ b/drivers/clk/mediatek/clk-mtk.h
> @@ -176,9 +176,11 @@ struct mtk_clk_divider {
> .div_width = _width, \
> }
>
> -void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
> - int num, void __iomem *base, spinlock_t *lock,
> - struct clk_onecell_data *clk_data);
> +void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
> + void __iomem *base, spinlock_t *lock,
> + struct clk_onecell_data *clk_data);
> +void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
> + struct clk_onecell_data *clk_data);
>
> struct clk_onecell_data *mtk_alloc_clk_data(unsigned int clk_num);
> void mtk_free_clk_data(struct clk_onecell_data *clk_data);
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 20:17:32

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 19/31] clk: mediatek: Add mtk_clk_simple_remove()

> In commit c58cd0e40ffa ("clk: mediatek: Add mtk_clk_simple_probe() to
> simplify clock providers"), a generic probe function was added to
> simplify clk drivers that only needed to support clk gates. However due
> to the lack of unregister APIs, a corresponding remove function was not
> added.
>
> Now that the unregister APIs have been implemented, add aforementioned
> remove function to make it complete.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>
> ---
> drivers/clk/mediatek/clk-mtk.c | 15 +++++++++++++++
> drivers/clk/mediatek/clk-mtk.h | 1 +
> 2 files changed, 16 insertions(+)
>
> diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
> index 2150ed8678e0..cd76e1d80242 100644
> --- a/drivers/clk/mediatek/clk-mtk.c
> +++ b/drivers/clk/mediatek/clk-mtk.c
> @@ -369,6 +369,8 @@ int mtk_clk_simple_probe(struct platform_device *pdev)
> if (r)
> goto free_data;
>
> + platform_set_drvdata(pdev, clk_data);
> +
> return r;
>
> free_data:
> @@ -376,4 +378,17 @@ int mtk_clk_simple_probe(struct platform_device *pdev)
> return r;
> }
>
> +int mtk_clk_simple_remove(struct platform_device *pdev)
> +{
> + const struct mtk_clk_desc *mcd = of_device_get_match_data(&pdev->dev);
> + struct clk_onecell_data *clk_data = platform_get_drvdata(pdev);
> + struct device_node *node = pdev->dev.of_node;
> +
> + of_clk_del_provider(node);
> + mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
> + mtk_free_clk_data(clk_data);
> +
> + return 0;
> +}
> +
> MODULE_LICENSE("GPL");
> diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
> index 3c3a934f53cd..4fa658f5d934 100644
> --- a/drivers/clk/mediatek/clk-mtk.h
> +++ b/drivers/clk/mediatek/clk-mtk.h
> @@ -202,5 +202,6 @@ struct mtk_clk_desc {
> };
>
> int mtk_clk_simple_probe(struct platform_device *pdev);
> +int mtk_clk_simple_remove(struct platform_device *pdev);
>
> #endif /* __DRV_CLK_MTK_H */
> --
> 2.35.0.rc0.227.g00780c9af4-goog
>

2022-01-26 20:18:39

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 20/31] clk: mediatek: mtk: Clean up included headers

> Some included headers aren't actually used anywhere, while other headers
> with the declaration of functions and structures aren't directly
> included.
>
> Get rid of the unused ones, and add the ones that should be included
> directly.
>
> On the header side, replace headers that are included purely for data
> structure definitions with forward declarations. This decreases the
> amount of preprocessing and compilation effort required for each
> inclusion.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>
> ---
> drivers/clk/mediatek/clk-mtk.c | 13 ++++++-------
> drivers/clk/mediatek/clk-mtk.h | 12 ++++++------
> 2 files changed, 12 insertions(+), 13 deletions(-)
>
> diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
> index cd76e1d80242..d2c50186cceb 100644
> --- a/drivers/clk/mediatek/clk-mtk.c
> +++ b/drivers/clk/mediatek/clk-mtk.c
> @@ -4,17 +4,16 @@
> * Author: James Liao <[email protected]>
> */
>
> -#include <linux/of.h>
> -#include <linux/of_address.h>
> +#include <linux/bitops.h>
> +#include <linux/clk-provider.h>
> #include <linux/err.h>
> #include <linux/io.h>
> -#include <linux/slab.h>
> -#include <linux/delay.h>
> -#include <linux/clkdev.h>
> -#include <linux/module.h>
> #include <linux/mfd/syscon.h>
> -#include <linux/device.h>
> +#include <linux/module.h>
> +#include <linux/of.h>
> #include <linux/of_device.h>
> +#include <linux/platform_device.h>
> +#include <linux/slab.h>
>
> #include "clk-mtk.h"
> #include "clk-gate.h"
> diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
> index 4fa658f5d934..7f902581a115 100644
> --- a/drivers/clk/mediatek/clk-mtk.h
> +++ b/drivers/clk/mediatek/clk-mtk.h
> @@ -7,19 +7,19 @@
> #ifndef __DRV_CLK_MTK_H
> #define __DRV_CLK_MTK_H
>
> -#include <linux/regmap.h>
> -#include <linux/bitops.h>
> #include <linux/clk-provider.h>
> -#include <linux/platform_device.h>
> -
> -struct clk;
> -struct clk_onecell_data;
> +#include <linux/io.h>
> +#include <linux/kernel.h>
> +#include <linux/spinlock.h>
> +#include <linux/types.h>
>
> #define MAX_MUX_GATE_BIT 31
> #define INVALID_MUX_GATE_BIT (MAX_MUX_GATE_BIT + 1)
>
> #define MHZ (1000 * 1000)
>
> +struct platform_device;
> +
> struct mtk_fixed_clk {
> int id;
> const char *name;
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 20:20:16

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 21/31] clk: mediatek: cpumux: Implement error handling in register API

> The cpumux clk type registration function does not stop or return errors
> if any clk failed to be registered, nor does it implement an error
> handling path. This may result in a partially working device if any
> step failed.
>
> Make the register function return proper error codes, and bail out if
> errors occur. Proper cleanup, i.e. unregister any clks that were
> successfully registered, is done in the new error path.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>

> ---
> drivers/clk/mediatek/clk-cpumux.c | 15 ++++++++++++++-
> 1 file changed, 14 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/clk/mediatek/clk-cpumux.c b/drivers/clk/mediatek/clk-cpumux.c
> index 658aee789f44..499c60432280 100644
> --- a/drivers/clk/mediatek/clk-cpumux.c
> +++ b/drivers/clk/mediatek/clk-cpumux.c
> @@ -123,13 +123,26 @@ int mtk_clk_register_cpumuxes(struct device_node *node,
> clk = mtk_clk_register_cpumux(mux, regmap);
> if (IS_ERR(clk)) {
> pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
> - continue;
> + goto err;
> }
>
> clk_data->clks[mux->id] = clk;
> }
>
> return 0;
> +
> +err:
> + while (--i >= 0) {
> + const struct mtk_composite *mux = &clks[i];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[mux->id]))
> + continue;
> +
> + mtk_clk_unregister_cpumux(clk_data->clks[mux->id]);
> + clk_data->clks[mux->id] = ERR_PTR(-ENOENT);
> + }
> +
> + return PTR_ERR(clk);
> }
>
> void mtk_clk_unregister_cpumuxes(const struct mtk_composite *clks, int num,
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 20:24:37

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 22/31] clk: mediatek: gate: Implement error handling in register API

> The gate clk type registration function does not stop or return errors
> if any clk failed to be registered, nor does it implement an error
> handling path. This may result in a partially working device if any
> step failed.
>
> Make the register function return proper error codes, and bail out if
> errors occur. Proper cleanup, i.e. unregister any clks that were
> successfully registered, is done in the new error path.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>

> ---
> drivers/clk/mediatek/clk-gate.c | 15 ++++++++++++++-
> 1 file changed, 14 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/clk/mediatek/clk-gate.c b/drivers/clk/mediatek/clk-gate.c
> index e8881ae1489a..631ff170b7b9 100644
> --- a/drivers/clk/mediatek/clk-gate.c
> +++ b/drivers/clk/mediatek/clk-gate.c
> @@ -237,13 +237,26 @@ int mtk_clk_register_gates_with_dev(struct device_node *node,
>
> if (IS_ERR(clk)) {
> pr_err("Failed to register clk %s: %pe\n", gate->name, clk);
> - continue;
> + goto err;
> }
>
> clk_data->clks[gate->id] = clk;
> }
>
> return 0;
> +
> +err:
> + while (--i >= 0) {
> + const struct mtk_gate *gate = &clks[i];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[gate->id]))
> + continue;
> +
> + mtk_clk_unregister_gate(clk_data->clks[gate->id]);
> + clk_data->clks[gate->id] = ERR_PTR(-ENOENT);
> + }
> +
> + return PTR_ERR(clk);
> }
>
> int mtk_clk_register_gates(struct device_node *node,
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 20:27:44

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 24/31] clk: mediatek: mux: Implement error handling in register API

> The mux clk type registration function does not stop or return errors
> if any clk failed to be registered, nor does it implement an error
> handling path. This may result in a partially working device if any
> step failed.
>
> Make the register function return proper error codes, and bail out if
> errors occur. Proper cleanup, i.e. unregister any clks that were
> successfully registered, is done in the new error path.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>
> ---
> drivers/clk/mediatek/clk-mux.c | 15 ++++++++++++++-
> 1 file changed, 14 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
> index 70aa42144632..f51e67650f03 100644
> --- a/drivers/clk/mediatek/clk-mux.c
> +++ b/drivers/clk/mediatek/clk-mux.c
> @@ -215,13 +215,26 @@ int mtk_clk_register_muxes(const struct mtk_mux *muxes,
>
> if (IS_ERR(clk)) {
> pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
> - continue;
> + goto err;
> }
>
> clk_data->clks[mux->id] = clk;
> }
>
> return 0;
> +
> +err:
> + while (--i >= 0) {
> + const struct mtk_mux *mux = &muxes[i];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[mux->id]))
> + continue;
> +
> + mtk_clk_unregister_mux(clk_data->clks[mux->id]);
> + clk_data->clks[mux->id] = ERR_PTR(-ENOENT);
> + }
> +
> + return PTR_ERR(clk);
> }
> EXPORT_SYMBOL_GPL(mtk_clk_register_muxes);
>
> --
> 2.35.0.rc0.227.g00780c9af4-goog
>
>

2022-01-26 20:28:19

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 23/31] clk: mediatek: mux: Reverse check for existing clk to reduce nesting level

> The clk registration code here currently does:
>
> if (IS_ERR_OR_NULL(clk_data->clks[mux->id])) {
> ... do clk registration ...
> }
>
> This extra level of nesting wastes screen real estate.
>
> Reduce the nesting level by reversing the conditional shown above.
> Other than that, functionality is not changed.
>
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>
> ---
> drivers/clk/mediatek/clk-mux.c | 15 ++++++++-------
> 1 file changed, 8 insertions(+), 7 deletions(-)
>
> Diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
> Index 01af6a52711a..70aa42144632 100644
> --- a/drivers/clk/mediatek/clk-mux.c
> +++ b/drivers/clk/mediatek/clk-mux.c
> @@ -208,16 +208,17 @@ int mtk_clk_register_muxes(const struct mtk_mux *muxes,
> for (i = 0; i < num; i++) {
> const struct mtk_mux *mux = &muxes[i];
>
> - if (IS_ERR_OR_NULL(clk_data->clks[mux->id])) {
> - clk = mtk_clk_register_mux(mux, regmap, lock);
> + if (!IS_ERR_OR_NULL(clk_data->clks[mux->id]))
> + continue;
>
> - if (IS_ERR(clk)) {
> - pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
> - continue;
> - }
> + clk = mtk_clk_register_mux(mux, regmap, lock);
>
> - clk_data->clks[mux->id] = clk;
> + if (IS_ERR(clk)) {
> + pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
> + continue;
> }
> +
> + clk_data->clks[mux->id] = clk;
> }
>
> return 0;
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 20:30:22

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 26/31] clk: mediatek: mtk: Implement error handling in register APIs

> The remaining clk registration functions do not stop or return errors
> if any clk failed to be registered, nor do they implement error
> handling paths. This may result in a partially working device if any
> step fails.
>
> Make the register functions return proper error codes, and bail out if
> errors occur. Proper cleanup, i.e. unregister any clks that were
> successfully registered, is done in the new error path.
>
> This also makes the |struct clk_data *| argument mandatory, as it is
> used to track the list of clks registered.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>

> ---
> drivers/clk/mediatek/clk-mtk.c | 118 ++++++++++++++++++++++++++-------
> drivers/clk/mediatek/clk-mtk.h | 20 +++---
> 2 files changed, 103 insertions(+), 35 deletions(-)
>
> diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
> index d2c50186cceb..e1977c8e130a 100644
> --- a/drivers/clk/mediatek/clk-mtk.c
> +++ b/drivers/clk/mediatek/clk-mtk.c
> @@ -53,16 +53,19 @@ void mtk_free_clk_data(struct clk_onecell_data *clk_data)
> kfree(clk_data);
> }
>
> -void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
> - int num, struct clk_onecell_data *clk_data)
> +int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
> + struct clk_onecell_data *clk_data)
> {
> int i;
> struct clk *clk;
>
> + if (!clk_data)
> + return -ENOMEM;
> +
> for (i = 0; i < num; i++) {
> const struct mtk_fixed_clk *rc = &clks[i];
>
> - if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[rc->id]))
> + if (!IS_ERR_OR_NULL(clk_data->clks[rc->id]))
> continue;
>
> clk = clk_register_fixed_rate(NULL, rc->name, rc->parent, 0,
> @@ -70,12 +73,26 @@ void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks,
>
> if (IS_ERR(clk)) {
> pr_err("Failed to register clk %s: %pe\n", rc->name, clk);
> - continue;
> + goto err;
> }
>
> - if (clk_data)
> - clk_data->clks[rc->id] = clk;
> + clk_data->clks[rc->id] = clk;
> }
> +
> + return 0;
> +
> +err:
> + while (--i >= 0) {
> + const struct mtk_fixed_clk *rc = &clks[i];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[rc->id]))
> + continue;
> +
> + clk_unregister_fixed_rate(clk_data->clks[rc->id]);
> + clk_data->clks[rc->id] = ERR_PTR(-ENOENT);
> + }
> +
> + return PTR_ERR(clk);
> }
> EXPORT_SYMBOL_GPL(mtk_clk_register_fixed_clks);
>
> @@ -99,16 +116,19 @@ void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
> }
> EXPORT_SYMBOL_GPL(mtk_clk_unregister_fixed_clks);
>
> -void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
> - int num, struct clk_onecell_data *clk_data)
> +int mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
> + struct clk_onecell_data *clk_data)
> {
> int i;
> struct clk *clk;
>
> + if (!clk_data)
> + return -ENOMEM;
> +
> for (i = 0; i < num; i++) {
> const struct mtk_fixed_factor *ff = &clks[i];
>
> - if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[ff->id]))
> + if (!IS_ERR_OR_NULL(clk_data->clks[ff->id]))
> continue;
>
> clk = clk_register_fixed_factor(NULL, ff->name, ff->parent_name,
> @@ -116,12 +136,26 @@ void mtk_clk_register_factors(const struct mtk_fixed_factor *clks,
>
> if (IS_ERR(clk)) {
> pr_err("Failed to register clk %s: %pe\n", ff->name, clk);
> - continue;
> + goto err;
> }
>
> - if (clk_data)
> - clk_data->clks[ff->id] = clk;
> + clk_data->clks[ff->id] = clk;
> + }
> +
> + return 0;
> +
> +err:
> + while (--i >= 0) {
> + const struct mtk_fixed_factor *ff = &clks[i];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[ff->id]))
> + continue;
> +
> + clk_unregister_fixed_factor(clk_data->clks[ff->id]);
> + clk_data->clks[ff->id] = ERR_PTR(-ENOENT);
> }
> +
> + return PTR_ERR(clk);
> }
> EXPORT_SYMBOL_GPL(mtk_clk_register_factors);
>
> @@ -253,13 +287,16 @@ static void mtk_clk_unregister_composite(struct clk *clk)
> kfree(mux);
> }
>
> -void mtk_clk_register_composites(const struct mtk_composite *mcs,
> - int num, void __iomem *base, spinlock_t *lock,
> - struct clk_onecell_data *clk_data)
> +int mtk_clk_register_composites(const struct mtk_composite *mcs, int num,
> + void __iomem *base, spinlock_t *lock,
> + struct clk_onecell_data *clk_data)
> {
> struct clk *clk;
> int i;
>
> + if (!clk_data)
> + return -ENOMEM;
> +
> for (i = 0; i < num; i++) {
> const struct mtk_composite *mc = &mcs[i];
>
> @@ -270,12 +307,26 @@ void mtk_clk_register_composites(const struct mtk_composite *mcs,
>
> if (IS_ERR(clk)) {
> pr_err("Failed to register clk %s: %pe\n", mc->name, clk);
> - continue;
> + goto err;
> }
>
> - if (clk_data)
> - clk_data->clks[mc->id] = clk;
> + clk_data->clks[mc->id] = clk;
> + }
> +
> + return 0;
> +
> +err:
> + while (--i >= 0) {
> + const struct mtk_composite *mc = &mcs[i];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[mcs->id]))
> + continue;
> +
> + mtk_clk_unregister_composite(clk_data->clks[mc->id]);
> + clk_data->clks[mc->id] = ERR_PTR(-ENOENT);
> }
> +
> + return PTR_ERR(clk);
> }
> EXPORT_SYMBOL_GPL(mtk_clk_register_composites);
>
> @@ -299,17 +350,20 @@ void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
> }
> EXPORT_SYMBOL_GPL(mtk_clk_unregister_composites);
>
> -void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
> - int num, void __iomem *base, spinlock_t *lock,
> - struct clk_onecell_data *clk_data)
> +int mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
> + void __iomem *base, spinlock_t *lock,
> + struct clk_onecell_data *clk_data)
> {
> struct clk *clk;
> int i;
>
> + if (!clk_data)
> + return -ENOMEM;
> +
> for (i = 0; i < num; i++) {
> const struct mtk_clk_divider *mcd = &mcds[i];
>
> - if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
> + if (!IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
> continue;
>
> clk = clk_register_divider(NULL, mcd->name, mcd->parent_name,
> @@ -318,12 +372,26 @@ void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds,
>
> if (IS_ERR(clk)) {
> pr_err("Failed to register clk %s: %pe\n", mcd->name, clk);
> - continue;
> + goto err;
> }
>
> - if (clk_data)
> - clk_data->clks[mcd->id] = clk;
> + clk_data->clks[mcd->id] = clk;
> + }
> +
> + return 0;
> +
> +err:
> + while (--i >= 0) {
> + const struct mtk_clk_divider *mcd = &mcds[i];
> +
> + if (IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
> + continue;
> +
> + mtk_clk_unregister_composite(clk_data->clks[mcd->id]);
> + clk_data->clks[mcd->id] = ERR_PTR(-ENOENT);
> }
> +
> + return PTR_ERR(clk);
> }
>
> void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
> diff --git a/drivers/clk/mediatek/clk-mtk.h b/drivers/clk/mediatek/clk-mtk.h
> index 7f902581a115..bf6565aa7319 100644
> --- a/drivers/clk/mediatek/clk-mtk.h
> +++ b/drivers/clk/mediatek/clk-mtk.h
> @@ -34,8 +34,8 @@ struct mtk_fixed_clk {
> .rate = _rate, \
> }
>
> -void mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
> - struct clk_onecell_data *clk_data);
> +int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
> + struct clk_onecell_data *clk_data);
> void mtk_clk_unregister_fixed_clks(const struct mtk_fixed_clk *clks, int num,
> struct clk_onecell_data *clk_data);
>
> @@ -55,8 +55,8 @@ struct mtk_fixed_factor {
> .div = _div, \
> }
>
> -void mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
> - struct clk_onecell_data *clk_data);
> +int mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
> + struct clk_onecell_data *clk_data);
> void mtk_clk_unregister_factors(const struct mtk_fixed_factor *clks, int num,
> struct clk_onecell_data *clk_data);
>
> @@ -150,9 +150,9 @@ struct mtk_composite {
> struct clk *mtk_clk_register_composite(const struct mtk_composite *mc,
> void __iomem *base, spinlock_t *lock);
>
> -void mtk_clk_register_composites(const struct mtk_composite *mcs,
> - int num, void __iomem *base, spinlock_t *lock,
> - struct clk_onecell_data *clk_data);
> +int mtk_clk_register_composites(const struct mtk_composite *mcs, int num,
> + void __iomem *base, spinlock_t *lock,
> + struct clk_onecell_data *clk_data);
> void mtk_clk_unregister_composites(const struct mtk_composite *mcs, int num,
> struct clk_onecell_data *clk_data);
>
> @@ -178,9 +178,9 @@ struct mtk_clk_divider {
> .div_width = _width, \
> }
>
> -void mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
> - void __iomem *base, spinlock_t *lock,
> - struct clk_onecell_data *clk_data);
> +int mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
> + void __iomem *base, spinlock_t *lock,
> + struct clk_onecell_data *clk_data);
> void mtk_clk_unregister_dividers(const struct mtk_clk_divider *mcds, int num,
> struct clk_onecell_data *clk_data);
>
> --
> 2.35.0.rc0.227.g00780c9af4-goog
>

2022-01-26 20:31:12

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 25/31] clk: mediatek: pll: Implement error handling in register API

> The pll clk type registration function does not stop or return errors
> if any clk failed to be registered, nor does it implement an error
> handling path. This may result in a partially working device if any
> step failed.
>
> Make the register function return proper error codes, and bail out if
> errors occur. Proper cleanup, i.e. unregister any clks that were
> successfully registered, and unmap the I/O space, is done in the new
> error path.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>
> ---
> drivers/clk/mediatek/clk-pll.c | 23 +++++++++++++++++++----
> drivers/clk/mediatek/clk-pll.h | 6 +++---
> 2 files changed, 22 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/clk/mediatek/clk-pll.c b/drivers/clk/mediatek/clk-pll.c
> index 9698d1c97cd6..1dd15f560659 100644
> --- a/drivers/clk/mediatek/clk-pll.c
> +++ b/drivers/clk/mediatek/clk-pll.c
> @@ -369,8 +369,9 @@ static void mtk_clk_unregister_pll(struct clk *clk)
> kfree(pll);
> }
>
> -void mtk_clk_register_plls(struct device_node *node,
> - const struct mtk_pll_data *plls, int num_plls, struct clk_onecell_data *clk_data)
> +int mtk_clk_register_plls(struct device_node *node,
> + const struct mtk_pll_data *plls, int num_plls,
> + struct clk_onecell_data *clk_data)
> {
> void __iomem *base;
> int i;
> @@ -379,7 +380,7 @@ void mtk_clk_register_plls(struct device_node *node,
> base = of_iomap(node, 0);
> if (!base) {
> pr_err("%s(): ioremap failed\n", __func__);
> - return;
> + return -EINVAL;
> }
>
> for (i = 0; i < num_plls; i++) {
> @@ -389,11 +390,25 @@ void mtk_clk_register_plls(struct device_node *node,
>
> if (IS_ERR(clk)) {
> pr_err("Failed to register clk %s: %pe\n", pll->name, clk);
> - continue;
> + goto err;
> }
>
> clk_data->clks[pll->id] = clk;
> }
> +
> + return 0;
> +
> +err:
> + while (--i >= 0) {
> + const struct mtk_pll_data *pll = &plls[i];
> +
> + mtk_clk_unregister_pll(clk_data->clks[pll->id]);
> + clk_data->clks[pll->id] = ERR_PTR(-ENOENT);
> + };
> +
> + iounmap(base);
> +
> + return PTR_ERR(clk);
> }
> EXPORT_SYMBOL_GPL(mtk_clk_register_plls);
>
> diff --git a/drivers/clk/mediatek/clk-pll.h b/drivers/clk/mediatek/clk-pll.h
> index a889b1e472e7..bf06e44caef9 100644
> --- a/drivers/clk/mediatek/clk-pll.h
> +++ b/drivers/clk/mediatek/clk-pll.h
> @@ -48,9 +48,9 @@ struct mtk_pll_data {
> u8 pll_en_bit; /* Assume 0, indicates BIT(0) by default */
> };
>
> -void mtk_clk_register_plls(struct device_node *node,
> - const struct mtk_pll_data *plls, int num_plls,
> - struct clk_onecell_data *clk_data);
> +int mtk_clk_register_plls(struct device_node *node,
> + const struct mtk_pll_data *plls, int num_plls,
> + struct clk_onecell_data *clk_data);
> void mtk_clk_unregister_plls(const struct mtk_pll_data *plls, int num_plls,
> struct clk_onecell_data *clk_data);
>
> --
> 2.35.0.rc0.227.g00780c9af4-goog


2022-01-26 20:31:19

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 27/31] clk: mediatek: Unregister clks in mtk_clk_simple_probe() error path

> Until now the mediatek clk driver library did not have any way to
> unregister clks, and so all drivers did not do proper cleanup in
> their error paths.
>
> Now that the library does have APIs to unregister clks, use them
> in the error path of mtk_clk_simple_probe() to do proper cleanup.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>

> ---
> drivers/clk/mediatek/clk-mtk.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
> index e1977c8e130a..6d0b8842971b 100644
> --- a/drivers/clk/mediatek/clk-mtk.c
> +++ b/drivers/clk/mediatek/clk-mtk.c
> @@ -434,12 +434,14 @@ int mtk_clk_simple_probe(struct platform_device *pdev)
>
> r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
> if (r)
> - goto free_data;
> + goto unregister_clks;
>
> platform_set_drvdata(pdev, clk_data);
>
> return r;
>
> +unregister_clks:
> + mtk_clk_unregister_gates(mcd->clks, mcd->num_clks, clk_data);
> free_data:
> mtk_free_clk_data(clk_data);
> return r;
> --
> 2.35.0.rc0.227.g00780c9af4-goog
>
>

2022-01-26 20:32:47

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 28/31] clk: mediatek: mt8195: Hook up mtk_clk_simple_remove()

> Various small clock controllers only have clock gates, and utilize
> mtk_clk_simple_probe() as their driver probe function.
>
> Now that we have a matching remove function, hook it up for the relevant
> drivers. This was done with the following command:
>
> sed -i -e '/mtk_clk_simple_probe/a \
> .remove = mtk_clk_simple_remove,' drivers/clk/mediatek/clk-mt8195-*.c
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Thanks for doing this exmaple, we can apply
mtk_clk_simple_probe/mtk_clk_simple_remove to other clk-mtxxxx in the future.

Reviewed-by: Miles Chen <[email protected]>

> ---
> drivers/clk/mediatek/clk-mt8195-cam.c | 1 +
> drivers/clk/mediatek/clk-mt8195-ccu.c | 1 +
> drivers/clk/mediatek/clk-mt8195-img.c | 1 +
> drivers/clk/mediatek/clk-mt8195-imp_iic_wrap.c | 1 +
> drivers/clk/mediatek/clk-mt8195-infra_ao.c | 1 +
> drivers/clk/mediatek/clk-mt8195-ipe.c | 1 +
> drivers/clk/mediatek/clk-mt8195-mfg.c | 1 +
> drivers/clk/mediatek/clk-mt8195-peri_ao.c | 1 +
> drivers/clk/mediatek/clk-mt8195-scp_adsp.c | 1 +
> drivers/clk/mediatek/clk-mt8195-vdec.c | 1 +
> drivers/clk/mediatek/clk-mt8195-venc.c | 1 +
> drivers/clk/mediatek/clk-mt8195-vpp0.c | 1 +
> drivers/clk/mediatek/clk-mt8195-vpp1.c | 1 +
> drivers/clk/mediatek/clk-mt8195-wpe.c | 1 +
> 14 files changed, 14 insertions(+)
>
> diff --git a/drivers/clk/mediatek/clk-mt8195-cam.c b/drivers/clk/mediatek/clk-mt8195-cam.c
> index 3d261fc3848e..e4d00fe6e757 100644
> --- a/drivers/clk/mediatek/clk-mt8195-cam.c
> +++ b/drivers/clk/mediatek/clk-mt8195-cam.c
> @@ -134,6 +134,7 @@ static const struct of_device_id of_match_clk_mt8195_cam[] = {
>
> static struct platform_driver clk_mt8195_cam_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-cam",
> .of_match_table = of_match_clk_mt8195_cam,
> diff --git a/drivers/clk/mediatek/clk-mt8195-ccu.c b/drivers/clk/mediatek/clk-mt8195-ccu.c
> index f846f1d73605..4e326b6301ba 100644
> --- a/drivers/clk/mediatek/clk-mt8195-ccu.c
> +++ b/drivers/clk/mediatek/clk-mt8195-ccu.c
> @@ -42,6 +42,7 @@ static const struct of_device_id of_match_clk_mt8195_ccu[] = {
>
> static struct platform_driver clk_mt8195_ccu_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-ccu",
> .of_match_table = of_match_clk_mt8195_ccu,
> diff --git a/drivers/clk/mediatek/clk-mt8195-img.c b/drivers/clk/mediatek/clk-mt8195-img.c
> index 22b52a8f15fe..12f5c436d075 100644
> --- a/drivers/clk/mediatek/clk-mt8195-img.c
> +++ b/drivers/clk/mediatek/clk-mt8195-img.c
> @@ -88,6 +88,7 @@ static const struct of_device_id of_match_clk_mt8195_img[] = {
>
> static struct platform_driver clk_mt8195_img_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-img",
> .of_match_table = of_match_clk_mt8195_img,
> diff --git a/drivers/clk/mediatek/clk-mt8195-imp_iic_wrap.c b/drivers/clk/mediatek/clk-mt8195-imp_iic_wrap.c
> index 4ab312eb26a5..fbc809d05072 100644
> --- a/drivers/clk/mediatek/clk-mt8195-imp_iic_wrap.c
> +++ b/drivers/clk/mediatek/clk-mt8195-imp_iic_wrap.c
> @@ -58,6 +58,7 @@ static const struct of_device_id of_match_clk_mt8195_imp_iic_wrap[] = {
>
> static struct platform_driver clk_mt8195_imp_iic_wrap_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-imp_iic_wrap",
> .of_match_table = of_match_clk_mt8195_imp_iic_wrap,
> diff --git a/drivers/clk/mediatek/clk-mt8195-infra_ao.c b/drivers/clk/mediatek/clk-mt8195-infra_ao.c
> index 5f9b69967459..8ebe3b9415c4 100644
> --- a/drivers/clk/mediatek/clk-mt8195-infra_ao.c
> +++ b/drivers/clk/mediatek/clk-mt8195-infra_ao.c
> @@ -198,6 +198,7 @@ static const struct of_device_id of_match_clk_mt8195_infra_ao[] = {
>
> static struct platform_driver clk_mt8195_infra_ao_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-infra_ao",
> .of_match_table = of_match_clk_mt8195_infra_ao,
> diff --git a/drivers/clk/mediatek/clk-mt8195-ipe.c b/drivers/clk/mediatek/clk-mt8195-ipe.c
> index fc1d42b6ac84..b0d745cf7752 100644
> --- a/drivers/clk/mediatek/clk-mt8195-ipe.c
> +++ b/drivers/clk/mediatek/clk-mt8195-ipe.c
> @@ -43,6 +43,7 @@ static const struct of_device_id of_match_clk_mt8195_ipe[] = {
>
> static struct platform_driver clk_mt8195_ipe_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-ipe",
> .of_match_table = of_match_clk_mt8195_ipe,
> diff --git a/drivers/clk/mediatek/clk-mt8195-mfg.c b/drivers/clk/mediatek/clk-mt8195-mfg.c
> index aca6d9c0837c..9411c556a5a9 100644
> --- a/drivers/clk/mediatek/clk-mt8195-mfg.c
> +++ b/drivers/clk/mediatek/clk-mt8195-mfg.c
> @@ -39,6 +39,7 @@ static const struct of_device_id of_match_clk_mt8195_mfg[] = {
>
> static struct platform_driver clk_mt8195_mfg_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-mfg",
> .of_match_table = of_match_clk_mt8195_mfg,
> diff --git a/drivers/clk/mediatek/clk-mt8195-peri_ao.c b/drivers/clk/mediatek/clk-mt8195-peri_ao.c
> index 907a92b22de8..2f6b3bb657db 100644
> --- a/drivers/clk/mediatek/clk-mt8195-peri_ao.c
> +++ b/drivers/clk/mediatek/clk-mt8195-peri_ao.c
> @@ -54,6 +54,7 @@ static const struct of_device_id of_match_clk_mt8195_peri_ao[] = {
>
> static struct platform_driver clk_mt8195_peri_ao_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-peri_ao",
> .of_match_table = of_match_clk_mt8195_peri_ao,
> diff --git a/drivers/clk/mediatek/clk-mt8195-scp_adsp.c b/drivers/clk/mediatek/clk-mt8195-scp_adsp.c
> index 26b4846c5894..e16c383f631b 100644
> --- a/drivers/clk/mediatek/clk-mt8195-scp_adsp.c
> +++ b/drivers/clk/mediatek/clk-mt8195-scp_adsp.c
> @@ -39,6 +39,7 @@ static const struct of_device_id of_match_clk_mt8195_scp_adsp[] = {
>
> static struct platform_driver clk_mt8195_scp_adsp_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-scp_adsp",
> .of_match_table = of_match_clk_mt8195_scp_adsp,
> diff --git a/drivers/clk/mediatek/clk-mt8195-vdec.c b/drivers/clk/mediatek/clk-mt8195-vdec.c
> index a1df04f42a90..a1446b666385 100644
> --- a/drivers/clk/mediatek/clk-mt8195-vdec.c
> +++ b/drivers/clk/mediatek/clk-mt8195-vdec.c
> @@ -96,6 +96,7 @@ static const struct of_device_id of_match_clk_mt8195_vdec[] = {
>
> static struct platform_driver clk_mt8195_vdec_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-vdec",
> .of_match_table = of_match_clk_mt8195_vdec,
> diff --git a/drivers/clk/mediatek/clk-mt8195-venc.c b/drivers/clk/mediatek/clk-mt8195-venc.c
> index 7339851a0856..622f57804f96 100644
> --- a/drivers/clk/mediatek/clk-mt8195-venc.c
> +++ b/drivers/clk/mediatek/clk-mt8195-venc.c
> @@ -61,6 +61,7 @@ static const struct of_device_id of_match_clk_mt8195_venc[] = {
>
> static struct platform_driver clk_mt8195_venc_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-venc",
> .of_match_table = of_match_clk_mt8195_venc,
> diff --git a/drivers/clk/mediatek/clk-mt8195-vpp0.c b/drivers/clk/mediatek/clk-mt8195-vpp0.c
> index c3241466a8d0..bf2939c3a023 100644
> --- a/drivers/clk/mediatek/clk-mt8195-vpp0.c
> +++ b/drivers/clk/mediatek/clk-mt8195-vpp0.c
> @@ -102,6 +102,7 @@ static const struct of_device_id of_match_clk_mt8195_vpp0[] = {
>
> static struct platform_driver clk_mt8195_vpp0_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-vpp0",
> .of_match_table = of_match_clk_mt8195_vpp0,
> diff --git a/drivers/clk/mediatek/clk-mt8195-vpp1.c b/drivers/clk/mediatek/clk-mt8195-vpp1.c
> index ce0b9a40a179..ffd52c762890 100644
> --- a/drivers/clk/mediatek/clk-mt8195-vpp1.c
> +++ b/drivers/clk/mediatek/clk-mt8195-vpp1.c
> @@ -100,6 +100,7 @@ static const struct of_device_id of_match_clk_mt8195_vpp1[] = {
>
> static struct platform_driver clk_mt8195_vpp1_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-vpp1",
> .of_match_table = of_match_clk_mt8195_vpp1,
> diff --git a/drivers/clk/mediatek/clk-mt8195-wpe.c b/drivers/clk/mediatek/clk-mt8195-wpe.c
> index 274d60838d8e..b483fab10e18 100644
> --- a/drivers/clk/mediatek/clk-mt8195-wpe.c
> +++ b/drivers/clk/mediatek/clk-mt8195-wpe.c
> @@ -135,6 +135,7 @@ static const struct of_device_id of_match_clk_mt8195_wpe[] = {
>
> static struct platform_driver clk_mt8195_wpe_drv = {
> .probe = mtk_clk_simple_probe,
> + .remove = mtk_clk_simple_remove,
> .driver = {
> .name = "clk-mt8195-wpe",
> .of_match_table = of_match_clk_mt8195_wpe,
> --
> 2.35.0.rc0.227.g00780c9af4-goog
>
>

2022-01-26 20:35:19

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 29/31] clk: mediatek: mt8195: Implement error handling in probe functions

> Until now the mediatek clk driver library did not have any way to
> unregister clks, and so all drivers did not do proper cleanup in
> their error paths.
>
> Now that the library does have APIs to unregister clks, use them
> in the error path of the probe functions for the mt8195 clk drivers
> to do proper cleanup.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>

> ---
> drivers/clk/mediatek/clk-mt8195-apmixedsys.c | 13 ++++--
> drivers/clk/mediatek/clk-mt8195-apusys_pll.c | 11 ++++-
> drivers/clk/mediatek/clk-mt8195-topckgen.c | 49 +++++++++++++++-----
> drivers/clk/mediatek/clk-mt8195-vdo0.c | 4 +-
> drivers/clk/mediatek/clk-mt8195-vdo1.c | 4 +-
> 5 files changed, 63 insertions(+), 18 deletions(-)
>
> diff --git a/drivers/clk/mediatek/clk-mt8195-apmixedsys.c b/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
> index 5b1b7dc447eb..af8d80f25f30 100644
> --- a/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
> +++ b/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
> @@ -120,17 +120,24 @@ static int clk_mt8195_apmixed_probe(struct platform_device *pdev)
> if (!clk_data)
> return -ENOMEM;
>
> - mtk_clk_register_plls(node, plls, ARRAY_SIZE(plls), clk_data);
> - r = mtk_clk_register_gates(node, apmixed_clks, ARRAY_SIZE(apmixed_clks), clk_data);
> + r = mtk_clk_register_plls(node, plls, ARRAY_SIZE(plls), clk_data);
> if (r)
> goto free_apmixed_data;
>
> + r = mtk_clk_register_gates(node, apmixed_clks, ARRAY_SIZE(apmixed_clks), clk_data);
> + if (r)
> + goto unregister_plls;
> +
> r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
> if (r)
> - goto free_apmixed_data;
> + goto unregister_gates;
>
> return r;
>
> +unregister_gates:
> + mtk_clk_register_gates(node, apmixed_clks, ARRAY_SIZE(apmixed_clks), clk_data);
> +unregister_plls:
> + mtk_clk_register_plls(node, plls, ARRAY_SIZE(plls), clk_data);
> free_apmixed_data:
> mtk_free_clk_data(clk_data);
> return r;
> diff --git a/drivers/clk/mediatek/clk-mt8195-apusys_pll.c b/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
> index db449ff877d7..1fff6f3d2dc7 100644
> --- a/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
> +++ b/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
> @@ -66,13 +66,20 @@ static int clk_mt8195_apusys_pll_probe(struct platform_device *pdev)
> if (!clk_data)
> return -ENOMEM;
>
> - mtk_clk_register_plls(node, apusys_plls, ARRAY_SIZE(apusys_plls), clk_data);
> - r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
> + r = mtk_clk_register_plls(node, apusys_plls, ARRAY_SIZE(apusys_plls), clk_data);
> if (r)
> goto free_apusys_pll_data;
>
> + r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
> + if (r)
> + goto unregister_plls;
> +
> + platform_set_drvdata(pdev, clk_data);
> +
> return r;
>
> +unregister_plls:
> + mtk_clk_unregister_plls(apusys_plls, ARRAY_SIZE(apusys_plls), clk_data);
> free_apusys_pll_data:
> mtk_free_clk_data(clk_data);
> return r;
> diff --git a/drivers/clk/mediatek/clk-mt8195-topckgen.c b/drivers/clk/mediatek/clk-mt8195-topckgen.c
> index 3e2aba9c40bb..3631f49a5e5a 100644
> --- a/drivers/clk/mediatek/clk-mt8195-topckgen.c
> +++ b/drivers/clk/mediatek/clk-mt8195-topckgen.c
> @@ -1239,25 +1239,52 @@ static int clk_mt8195_topck_probe(struct platform_device *pdev)
> goto free_top_data;
> }
>
> - mtk_clk_register_fixed_clks(top_fixed_clks, ARRAY_SIZE(top_fixed_clks),
> - top_clk_data);
> - mtk_clk_register_factors(top_divs, ARRAY_SIZE(top_divs), top_clk_data);
> - mtk_clk_register_muxes(top_mtk_muxes, ARRAY_SIZE(top_mtk_muxes), node,
> - &mt8195_clk_lock, top_clk_data);
> - mtk_clk_register_composites(top_muxes, ARRAY_SIZE(top_muxes), base,
> - &mt8195_clk_lock, top_clk_data);
> - mtk_clk_register_composites(top_adj_divs, ARRAY_SIZE(top_adj_divs), base,
> - &mt8195_clk_lock, top_clk_data);
> - r = mtk_clk_register_gates(node, top_clks, ARRAY_SIZE(top_clks), top_clk_data);
> + r = mtk_clk_register_fixed_clks(top_fixed_clks, ARRAY_SIZE(top_fixed_clks),
> + top_clk_data);
> if (r)
> goto free_top_data;
>
> + r = mtk_clk_register_factors(top_divs, ARRAY_SIZE(top_divs), top_clk_data);
> + if (r)
> + goto unregister_fixed_clks;
> +
> + r = mtk_clk_register_muxes(top_mtk_muxes, ARRAY_SIZE(top_mtk_muxes), node,
> + &mt8195_clk_lock, top_clk_data);
> + if (r)
> + goto unregister_factors;
> +
> + r = mtk_clk_register_composites(top_muxes, ARRAY_SIZE(top_muxes), base,
> + &mt8195_clk_lock, top_clk_data);
> + if (r)
> + goto unregister_muxes;
> +
> + r = mtk_clk_register_composites(top_adj_divs, ARRAY_SIZE(top_adj_divs), base,
> + &mt8195_clk_lock, top_clk_data);
> + if (r)
> + goto unregister_composite_muxes;
> +
> + r = mtk_clk_register_gates(node, top_clks, ARRAY_SIZE(top_clks), top_clk_data);
> + if (r)
> + goto unregister_composite_divs;
> +
> r = of_clk_add_provider(node, of_clk_src_onecell_get, top_clk_data);
> if (r)
> - goto free_top_data;
> + goto unregister_gates;
>
> return r;
>
> +unregister_gates:
> + mtk_clk_unregister_gates(top_clks, ARRAY_SIZE(top_clks), top_clk_data);
> +unregister_composite_divs:
> + mtk_clk_unregister_composites(top_adj_divs, ARRAY_SIZE(top_adj_divs), top_clk_data);
> +unregister_composite_muxes:
> + mtk_clk_unregister_composites(top_muxes, ARRAY_SIZE(top_muxes), top_clk_data);
> +unregister_muxes:
> + mtk_clk_unregister_muxes(top_mtk_muxes, ARRAY_SIZE(top_mtk_muxes), top_clk_data);
> +unregister_factors:
> + mtk_clk_unregister_factors(top_divs, ARRAY_SIZE(top_divs), top_clk_data);
> +unregister_fixed_clks:
> + mtk_clk_unregister_fixed_clks(top_fixed_clks, ARRAY_SIZE(top_fixed_clks), top_clk_data);
> free_top_data:
> mtk_free_clk_data(top_clk_data);
> return r;
> diff --git a/drivers/clk/mediatek/clk-mt8195-vdo0.c b/drivers/clk/mediatek/clk-mt8195-vdo0.c
> index f7ff7618c714..af34eb564b1d 100644
> --- a/drivers/clk/mediatek/clk-mt8195-vdo0.c
> +++ b/drivers/clk/mediatek/clk-mt8195-vdo0.c
> @@ -105,10 +105,12 @@ static int clk_mt8195_vdo0_probe(struct platform_device *pdev)
>
> r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
> if (r)
> - goto free_vdo0_data;
> + goto unregister_gates;
>
> return r;
>
> +unregister_gates:
> + mtk_clk_unregister_gates(vdo0_clks, ARRAY_SIZE(vdo0_clks), clk_data);
> free_vdo0_data:
> mtk_free_clk_data(clk_data);
> return r;
> diff --git a/drivers/clk/mediatek/clk-mt8195-vdo1.c b/drivers/clk/mediatek/clk-mt8195-vdo1.c
> index 03df8eae8838..6b502bbc730c 100644
> --- a/drivers/clk/mediatek/clk-mt8195-vdo1.c
> +++ b/drivers/clk/mediatek/clk-mt8195-vdo1.c
> @@ -122,10 +122,12 @@ static int clk_mt8195_vdo1_probe(struct platform_device *pdev)
>
> r = of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
> if (r)
> - goto free_vdo1_data;
> + goto unregister_gates;
>
> return r;
>
> +unregister_gates:
> + mtk_clk_unregister_gates(vdo1_clks, ARRAY_SIZE(vdo1_clks), clk_data);
> free_vdo1_data:
> mtk_free_clk_data(clk_data);
> return r;
> --
> 2.35.0.rc0.227.g00780c9af4-goog
>
>

2022-01-26 20:36:34

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 30/31] clk: mediatek: mt8195: Implement remove functions

> Until now the mediatek clk driver library did not have any way to
> unregister clks, and so none of the drivers implemented remove
> functions.
>
> Now that the library does have APIs to unregister clks, use them
> to implement remove functions for the mt8195 clk drivers.
>
> Signed-off-by: Chen-Yu Tsai <[email protected]>

Reviewed-by: Miles Chen <[email protected]>

> ---
> drivers/clk/mediatek/clk-mt8195-apmixedsys.c | 16 ++++++++++++++++
> drivers/clk/mediatek/clk-mt8195-apusys_pll.c | 13 +++++++++++++
> drivers/clk/mediatek/clk-mt8195-topckgen.c | 20 ++++++++++++++++++++
> drivers/clk/mediatek/clk-mt8195-vdo0.c | 16 ++++++++++++++++
> drivers/clk/mediatek/clk-mt8195-vdo1.c | 16 ++++++++++++++++
> 5 files changed, 81 insertions(+)
>
> diff --git a/drivers/clk/mediatek/clk-mt8195-apmixedsys.c b/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
> index af8d80f25f30..29cac3cf5f53 100644
> --- a/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
> +++ b/drivers/clk/mediatek/clk-mt8195-apmixedsys.c
> @@ -132,6 +132,8 @@ static int clk_mt8195_apmixed_probe(struct platform_device *pdev)
> if (r)
> goto unregister_gates;
>
> + platform_set_drvdata(pdev, clk_data);
> +
> return r;
>
> unregister_gates:
> @@ -143,8 +145,22 @@ static int clk_mt8195_apmixed_probe(struct platform_device *pdev)
> return r;
> }
>
> +static int clk_mt8195_apmixed_remove(struct platform_device *pdev)
> +{
> + struct device_node *node = pdev->dev.of_node;
> + struct clk_onecell_data *clk_data = platform_get_drvdata(pdev);
> +
> + of_clk_del_provider(node);
> + mtk_clk_unregister_gates(apmixed_clks, ARRAY_SIZE(apmixed_clks), clk_data);
> + mtk_clk_unregister_plls(plls, ARRAY_SIZE(plls), clk_data);
> + mtk_free_clk_data(clk_data);
> +
> + return 0;
> +}
> +
> static struct platform_driver clk_mt8195_apmixed_drv = {
> .probe = clk_mt8195_apmixed_probe,
> + .remove = clk_mt8195_apmixed_remove,
> .driver = {
> .name = "clk-mt8195-apmixed",
> .of_match_table = of_match_clk_mt8195_apmixed,
> diff --git a/drivers/clk/mediatek/clk-mt8195-apusys_pll.c b/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
> index 1fff6f3d2dc7..8cd88dfc3283 100644
> --- a/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
> +++ b/drivers/clk/mediatek/clk-mt8195-apusys_pll.c
> @@ -85,6 +85,18 @@ static int clk_mt8195_apusys_pll_probe(struct platform_device *pdev)
> return r;
> }
>
> +static int clk_mt8195_apusys_pll_remove(struct platform_device *pdev)
> +{
> + struct clk_onecell_data *clk_data = platform_get_drvdata(pdev);
> + struct device_node *node = pdev->dev.of_node;
> +
> + of_clk_del_provider(node);
> + mtk_clk_unregister_plls(apusys_plls, ARRAY_SIZE(apusys_plls), clk_data);
> + mtk_free_clk_data(clk_data);
> +
> + return 0;
> +}
> +
> static const struct of_device_id of_match_clk_mt8195_apusys_pll[] = {
> { .compatible = "mediatek,mt8195-apusys_pll", },
> {}
> @@ -92,6 +104,7 @@ static const struct of_device_id of_match_clk_mt8195_apusys_pll[] = {
>
> static struct platform_driver clk_mt8195_apusys_pll_drv = {
> .probe = clk_mt8195_apusys_pll_probe,
> + .remove = clk_mt8195_apusys_pll_remove,
> .driver = {
> .name = "clk-mt8195-apusys_pll",
> .of_match_table = of_match_clk_mt8195_apusys_pll,
> diff --git a/drivers/clk/mediatek/clk-mt8195-topckgen.c b/drivers/clk/mediatek/clk-mt8195-topckgen.c
> index 3631f49a5e5a..b602fcd7f1d1 100644
> --- a/drivers/clk/mediatek/clk-mt8195-topckgen.c
> +++ b/drivers/clk/mediatek/clk-mt8195-topckgen.c
> @@ -1271,6 +1271,8 @@ static int clk_mt8195_topck_probe(struct platform_device *pdev)
> if (r)
> goto unregister_gates;
>
> + platform_set_drvdata(pdev, top_clk_data);
> +
> return r;
>
> unregister_gates:
> @@ -1290,8 +1292,26 @@ static int clk_mt8195_topck_probe(struct platform_device *pdev)
> return r;
> }
>
> +static int clk_mt8195_topck_remove(struct platform_device *pdev)
> +{
> + struct clk_onecell_data *top_clk_data = platform_get_drvdata(pdev);
> + struct device_node *node = pdev->dev.of_node;
> +
> + of_clk_del_provider(node);
> + mtk_clk_unregister_gates(top_clks, ARRAY_SIZE(top_clks), top_clk_data);
> + mtk_clk_unregister_composites(top_adj_divs, ARRAY_SIZE(top_adj_divs), top_clk_data);
> + mtk_clk_unregister_composites(top_muxes, ARRAY_SIZE(top_muxes), top_clk_data);
> + mtk_clk_unregister_muxes(top_mtk_muxes, ARRAY_SIZE(top_mtk_muxes), top_clk_data);
> + mtk_clk_unregister_factors(top_divs, ARRAY_SIZE(top_divs), top_clk_data);
> + mtk_clk_unregister_fixed_clks(top_fixed_clks, ARRAY_SIZE(top_fixed_clks), top_clk_data);
> + mtk_free_clk_data(top_clk_data);
> +
> + return 0;
> +}
> +
> static struct platform_driver clk_mt8195_topck_drv = {
> .probe = clk_mt8195_topck_probe,
> + .remove = clk_mt8195_topck_remove,
> .driver = {
> .name = "clk-mt8195-topck",
> .of_match_table = of_match_clk_mt8195_topck,
> diff --git a/drivers/clk/mediatek/clk-mt8195-vdo0.c b/drivers/clk/mediatek/clk-mt8195-vdo0.c
> index af34eb564b1d..3bc7ed19d550 100644
> --- a/drivers/clk/mediatek/clk-mt8195-vdo0.c
> +++ b/drivers/clk/mediatek/clk-mt8195-vdo0.c
> @@ -107,6 +107,8 @@ static int clk_mt8195_vdo0_probe(struct platform_device *pdev)
> if (r)
> goto unregister_gates;
>
> + platform_set_drvdata(pdev, clk_data);
> +
> return r;
>
> unregister_gates:
> @@ -116,8 +118,22 @@ static int clk_mt8195_vdo0_probe(struct platform_device *pdev)
> return r;
> }
>
> +static int clk_mt8195_vdo0_remove(struct platform_device *pdev)
> +{
> + struct device *dev = &pdev->dev;
> + struct device_node *node = dev->parent->of_node;
> + struct clk_onecell_data *clk_data = platform_get_drvdata(pdev);
> +
> + of_clk_del_provider(node);
> + mtk_clk_unregister_gates(vdo0_clks, ARRAY_SIZE(vdo0_clks), clk_data);
> + mtk_free_clk_data(clk_data);
> +
> + return 0;
> +}
> +
> static struct platform_driver clk_mt8195_vdo0_drv = {
> .probe = clk_mt8195_vdo0_probe,
> + .remove = clk_mt8195_vdo0_remove,
> .driver = {
> .name = "clk-mt8195-vdo0",
> },
> diff --git a/drivers/clk/mediatek/clk-mt8195-vdo1.c b/drivers/clk/mediatek/clk-mt8195-vdo1.c
> index 6b502bbc730c..90c738a85ff1 100644
> --- a/drivers/clk/mediatek/clk-mt8195-vdo1.c
> +++ b/drivers/clk/mediatek/clk-mt8195-vdo1.c
> @@ -124,6 +124,8 @@ static int clk_mt8195_vdo1_probe(struct platform_device *pdev)
> if (r)
> goto unregister_gates;
>
> + platform_set_drvdata(pdev, clk_data);
> +
> return r;
>
> unregister_gates:
> @@ -133,8 +135,22 @@ static int clk_mt8195_vdo1_probe(struct platform_device *pdev)
> return r;
> }
>
> +static int clk_mt8195_vdo1_remove(struct platform_device *pdev)
> +{
> + struct device *dev = &pdev->dev;
> + struct device_node *node = dev->parent->of_node;
> + struct clk_onecell_data *clk_data = platform_get_drvdata(pdev);
> +
> + of_clk_del_provider(node);
> + mtk_clk_unregister_gates(vdo1_clks, ARRAY_SIZE(vdo1_clks), clk_data);
> + mtk_free_clk_data(clk_data);
> +
> + return 0;
> +}
> +
> static struct platform_driver clk_mt8195_vdo1_drv = {
> .probe = clk_mt8195_vdo1_probe,
> + .remove = clk_mt8195_vdo1_remove,
> .driver = {
> .name = "clk-mt8195-vdo1",
> },
> --
> 2.35.0.rc0.227.g00780c9af4-goog
>
>

2022-01-26 20:37:17

by Miles Chen

[permalink] [raw]
Subject: Re: [PATCH 31/31] clk: mediatek: Warn if clk IDs are duplicated

>The Mediatek clk driver library handles duplicate clock IDs in two
>different ways: either ignoring the duplicate entry, or overwriting
>the old clk. Either way may cause unexpected behavior, and the latter
>also causes an orphan clk that cannot be cleaned up.
>
>Align the behavior so that later duplicate entries are ignored, and
>a warning printed. The warning will also aid in making the issue
>noticeable.
>
>Signed-off-by: Chen-Yu Tsai <[email protected]>
>---
> drivers/clk/mediatek/clk-cpumux.c | 6 ++++++
> drivers/clk/mediatek/clk-gate.c | 5 ++++-
> drivers/clk/mediatek/clk-mtk.c | 18 ++++++++++++++----
> drivers/clk/mediatek/clk-mux.c | 5 ++++-
> drivers/clk/mediatek/clk-pll.c | 6 ++++++
> 5 files changed, 34 insertions(+), 6 deletions(-)
>
>diff --git a/drivers/clk/mediatek/clk-cpumux.c b/drivers/clk/mediatek/clk-cpumux.c
>index 499c60432280..c11b3fae622e 100644
>--- a/drivers/clk/mediatek/clk-cpumux.c
>+++ b/drivers/clk/mediatek/clk-cpumux.c
>@@ -120,6 +120,12 @@ int mtk_clk_register_cpumuxes(struct device_node *node,
> for (i = 0; i < num; i++) {
> const struct mtk_composite *mux = &clks[i];
>
>+ if (!IS_ERR_OR_NULL(clk_data->clks[mux->id])) {
>+ pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
>+ node, mux->id);
>+ continue;

%pOF is an useful information when this happens.

Reviewed-by: Miles Chen <[email protected]>

>+ }
>+
> clk = mtk_clk_register_cpumux(mux, regmap);
> if (IS_ERR(clk)) {
> pr_err("Failed to register clk %s: %pe\n", mux->name, clk);
>diff --git a/drivers/clk/mediatek/clk-gate.c b/drivers/clk/mediatek/clk-gate.c
>index 631ff170b7b9..da52023f8455 100644
>--- a/drivers/clk/mediatek/clk-gate.c
>+++ b/drivers/clk/mediatek/clk-gate.c
>@@ -224,8 +224,11 @@ int mtk_clk_register_gates_with_dev(struct device_node *node,
> for (i = 0; i < num; i++) {
> const struct mtk_gate *gate = &clks[i];
>
>- if (!IS_ERR_OR_NULL(clk_data->clks[gate->id]))
>+ if (!IS_ERR_OR_NULL(clk_data->clks[gate->id])) {
>+ pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
>+ node, gate->id);
> continue;
>+ }
>
> clk = mtk_clk_register_gate(gate->name, gate->parent_name,
> regmap,
>diff --git a/drivers/clk/mediatek/clk-mtk.c b/drivers/clk/mediatek/clk-mtk.c
>index 6d0b8842971b..b2a3568922b2 100644
>--- a/drivers/clk/mediatek/clk-mtk.c
>+++ b/drivers/clk/mediatek/clk-mtk.c
>@@ -65,8 +65,10 @@ int mtk_clk_register_fixed_clks(const struct mtk_fixed_clk *clks, int num,
> for (i = 0; i < num; i++) {
> const struct mtk_fixed_clk *rc = &clks[i];
>
>- if (!IS_ERR_OR_NULL(clk_data->clks[rc->id]))
>+ if (!IS_ERR_OR_NULL(clk_data->clks[rc->id])) {
>+ pr_warn("Trying to register duplicate clock ID: %d\n", rc->id);
> continue;
>+ }
>
> clk = clk_register_fixed_rate(NULL, rc->name, rc->parent, 0,
> rc->rate);
>@@ -128,8 +130,10 @@ int mtk_clk_register_factors(const struct mtk_fixed_factor *clks, int num,
> for (i = 0; i < num; i++) {
> const struct mtk_fixed_factor *ff = &clks[i];
>
>- if (!IS_ERR_OR_NULL(clk_data->clks[ff->id]))
>+ if (!IS_ERR_OR_NULL(clk_data->clks[ff->id])) {
>+ pr_warn("Trying to register duplicate clock ID: %d\n", ff->id);
> continue;
>+ }
>
> clk = clk_register_fixed_factor(NULL, ff->name, ff->parent_name,
> CLK_SET_RATE_PARENT, ff->mult, ff->div);
>@@ -300,8 +304,11 @@ int mtk_clk_register_composites(const struct mtk_composite *mcs, int num,
> for (i = 0; i < num; i++) {
> const struct mtk_composite *mc = &mcs[i];
>
>- if (clk_data && !IS_ERR_OR_NULL(clk_data->clks[mc->id]))
>+ if (!IS_ERR_OR_NULL(clk_data->clks[mc->id])) {
>+ pr_warn("Trying to register duplicate clock ID: %d\n",
>+ mc->id);
> continue;
>+ }
>
> clk = mtk_clk_register_composite(mc, base, lock);
>
>@@ -363,8 +370,11 @@ int mtk_clk_register_dividers(const struct mtk_clk_divider *mcds, int num,
> for (i = 0; i < num; i++) {
> const struct mtk_clk_divider *mcd = &mcds[i];
>
>- if (!IS_ERR_OR_NULL(clk_data->clks[mcd->id]))
>+ if (!IS_ERR_OR_NULL(clk_data->clks[mcd->id])) {
>+ pr_warn("Trying to register duplicate clock ID: %d\n",
>+ mcd->id);
> continue;
>+ }
>
> clk = clk_register_divider(NULL, mcd->name, mcd->parent_name,
> mcd->flags, base + mcd->div_reg, mcd->div_shift,
>diff --git a/drivers/clk/mediatek/clk-mux.c b/drivers/clk/mediatek/clk-mux.c
>index f51e67650f03..21ad5a4afd65 100644
>--- a/drivers/clk/mediatek/clk-mux.c
>+++ b/drivers/clk/mediatek/clk-mux.c
>@@ -208,8 +208,11 @@ int mtk_clk_register_muxes(const struct mtk_mux *muxes,
> for (i = 0; i < num; i++) {
> const struct mtk_mux *mux = &muxes[i];
>
>- if (!IS_ERR_OR_NULL(clk_data->clks[mux->id]))
>+ if (!IS_ERR_OR_NULL(clk_data->clks[mux->id])) {
>+ pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
>+ node, mux->id);
> continue;
>+ }
>
> clk = mtk_clk_register_mux(mux, regmap, lock);
>
>diff --git a/drivers/clk/mediatek/clk-pll.c b/drivers/clk/mediatek/clk-pll.c
>index 1dd15f560659..e5e9c188be99 100644
>--- a/drivers/clk/mediatek/clk-pll.c
>+++ b/drivers/clk/mediatek/clk-pll.c
>@@ -386,6 +386,12 @@ int mtk_clk_register_plls(struct device_node *node,
> for (i = 0; i < num_plls; i++) {
> const struct mtk_pll_data *pll = &plls[i];
>
>+ if (!IS_ERR_OR_NULL(clk_data->clks[pll->id])) {
>+ pr_warn("%pOF: Trying to register duplicate clock ID: %d\n",
>+ node, pll->id);
>+ continue;
>+ }
>+
> clk = mtk_clk_register_pll(pll, base);
>
> if (IS_ERR(clk)) {
>--
>2.35.0.rc0.227.g00780c9af4-goog
>
>

2022-02-02 21:27:35

by Chen-Yu Tsai

[permalink] [raw]
Subject: Re: [PATCH 14/31] clk: mediatek: pll: Clean up included headers

On Wed, Jan 26, 2022 at 2:36 PM Miles Chen <[email protected]> wrote:
>
> > Some included headers aren't actually used anywhere, while other headers
> > with the declaration of functions and structures aren't directly
> > included.
> >
> > Get rid of the unused ones, and add the ones that should be included
> > directly.
> >
> > Also, expand the MHZ macro with spelled-out "1000 * 1000" to be able
> > to not include clk-mtk.h. The existing ternary operator is rewritten
> > in a shortened form to accommodate the expanded macro.
>
> I think MHZ is more human readable than 1000*1000,
> like SZ_4K in linux/sizes.h.
>
> MHZ is also use by other clk-mtxxxx.c (by including clk-mtk.h)
> , so maybe we should keep clk-mtk.h?

This seems like overkill. I'll define MHZ in the file instead.

ChenYu

> Miles
> >
> > Signed-off-by: Chen-Yu Tsai <[email protected]>
> > ---
> > drivers/clk/mediatek/clk-pll.c | 12 ++++++------
> > 1 file changed, 6 insertions(+), 6 deletions(-)
> >
> > diff --git a/drivers/clk/mediatek/clk-pll.c b/drivers/clk/mediatek/clk-pll.c
> > index 081e0df8203e..9698d1c97cd6 100644
> > --- a/drivers/clk/mediatek/clk-pll.c
> > +++ b/drivers/clk/mediatek/clk-pll.c
> > @@ -4,15 +4,15 @@
> > * Author: James Liao <[email protected]>
> > */
> >
> > -#include <linux/of.h>
> > -#include <linux/of_address.h>
> > +#include <linux/clk-provider.h>
> > +#include <linux/container_of.h>
> > +#include <linux/delay.h>
> > +#include <linux/err.h>
> > #include <linux/io.h>
> > #include <linux/module.h>
> > +#include <linux/of_address.h>
> > #include <linux/slab.h>
> > -#include <linux/clkdev.h>
> > -#include <linux/delay.h>
> >
> > -#include "clk-mtk.h"
> > #include "clk-pll.h"
> >
> > #define REG_CON0 0
> > @@ -162,7 +162,7 @@ static void mtk_pll_set_rate_regs(struct mtk_clk_pll *pll, u32 pcw,
> > static void mtk_pll_calc_values(struct mtk_clk_pll *pll, u32 *pcw, u32 *postdiv,
> > u32 freq, u32 fin)
> > {
> > - unsigned long fmin = pll->data->fmin ? pll->data->fmin : (1000 * MHZ);
> > + unsigned long fmin = pll->data->fmin ?: (1000 * 1000 * 1000);
> > const struct mtk_pll_div_table *div_table = pll->data->div_table;
> > u64 _pcw;
> > int ibits;
> > --
> > 2.35.0.rc0.227.g00780c9af4-goog
>
>