clk: exynos: Fix incorrect clock lookup for non-top CMUs

Samsung clock drivers usually define the clock indices that are unique
per one CMU, but are not unique across all CMUs. That is, clock indices
start from 1 for each CMU, as provided in CMU bindings header. The way
the clock lookup via clk_get_by_index() works at the moment is by using
clk_of_xlate_default(), which returns globally non-unique clock ids for
for clocks registered with Samsung CCF API, which leads to incorrect
clocks being obtained. One way to fix that would be to make all clock
ids defined in the bindings header unique, but it'd make it incompatible
with Linux kernel bindings header. A better way to solve this issue is
to calculate the global clock id and use it when registering a clock
with clk_dm() and when obtaining it, in a custom .of_xlate function.

This patch adds an API for such mapping calculation, introducing the
necessary modifications to CMU registering functions in Samsung CCF.
Exynos850 clock driver (the only driver that uses Samsung CCF at the
moment) is modified accordingly, as it uses the changed API. So the
clock lookup with clk-exynos850.c driver is also fixed here.

The global clock id is calculated from CMU id and local clock id in
SAMSUNG_TO_CLK_ID() macro like this:

    clk_id_global = cmu_id * 256 + clk_id_local

leaving a range of up to 256 clocks for each CMU. Then this mapping
macro is used in clk_dm() to register clocks using their global ids, and
in .of_xlate() to lookup the clock by its local id correctly. Because
.of_xlate() operation has a separate function for each CMU, it "knows"
the correct way of finding the correct clk_id_global by provided
clk_id_local.

Fixes: ff3e8b8c6c22 ("clk: exynos: Add Samsung clock framework")
Fixes: a36cc5e3ef4d ("clk: exynos: Add Exynos850 clock driver")
Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
Signed-off-by: Minkyu Kang <mk7.kang@samsung.com>
diff --git a/drivers/clk/exynos/clk.h b/drivers/clk/exynos/clk.h
index 14e06b2..ed0a395 100644
--- a/drivers/clk/exynos/clk.h
+++ b/drivers/clk/exynos/clk.h
@@ -13,6 +13,51 @@
 #include <linux/clk-provider.h>
 #include "clk-pll.h"
 
+#define _SAMSUNG_CLK_OPS(_name, _cmu)					\
+static int _name##_of_xlate(struct clk *clk,				\
+			    struct ofnode_phandle_args *args)		\
+{									\
+	if (args->args_count > 1) {					\
+		debug("Invalid args_count: %d\n", args->args_count);	\
+		return -EINVAL;						\
+	}								\
+									\
+	if (args->args_count)						\
+		clk->id = SAMSUNG_TO_CLK_ID(_cmu, args->args[0]);	\
+	else								\
+		clk->id = 0;						\
+									\
+	return 0;							\
+}									\
+									\
+static const struct clk_ops _name##_clk_ops = {				\
+	.set_rate = ccf_clk_set_rate,					\
+	.get_rate = ccf_clk_get_rate,					\
+	.set_parent = ccf_clk_set_parent,				\
+	.enable = ccf_clk_enable,					\
+	.disable = ccf_clk_disable,					\
+	.of_xlate = _name##_of_xlate,					\
+}
+
+/**
+ * SAMSUNG_CLK_OPS - Define clock operations structure for specified CMU.
+ * @name: name of generated structure
+ * @cmu: CMU index
+ *
+ * Like ccf_clk_ops, but with custom .of_xlate callback.
+ */
+#define SAMSUNG_CLK_OPS(name, cmu) _SAMSUNG_CLK_OPS(name, cmu)
+
+/**
+ * SAMSUNG_TO_CLK_ID - Calculate a global clock index.
+ * @_cmu: CMU index
+ * @_id: local clock index (unique across @_cmu)
+ *
+ * Return: A global clock index unique across all CMUs.
+ * Keeps a range of 256 available clocks for every CMU.
+ */
+#define SAMSUNG_TO_CLK_ID(_cmu, _id)	(((_cmu) << 8) | ((_id) & 0xff))
+
 /**
  * struct samsung_mux_clock - information about mux clock
  * @id: platform specific id of the clock
@@ -179,13 +224,14 @@
 	unsigned int nr_clk;
 };
 
-int samsung_cmu_register_one(struct udevice *dev,
+int samsung_cmu_register_one(struct udevice *dev, unsigned int cmu_id,
 			     const struct samsung_clk_group *clk_groups,
 			     unsigned int nr_groups);
 
 /**
  * samsung_register_cmu - Register CMU clocks ensuring parent CMU is present
  * @dev: CMU device
+ * @cmu_id: CMU index number
  * @clk_groups: list of CMU clock groups
  * @parent_drv: name of parent CMU driver
  *
@@ -194,7 +240,7 @@
  *
  * Return: 0 on success or negative value on error.
  */
-#define samsung_register_cmu(dev, clk_groups, parent_drv)		\
+#define samsung_register_cmu(dev, cmu_id, clk_groups, parent_drv)	\
 ({									\
 	struct udevice *__parent;					\
 	int __ret;							\
@@ -204,8 +250,8 @@
 	if (__ret || !__parent)						\
 		__ret = -ENOENT;					\
 	else								\
-		__ret = samsung_cmu_register_one(dev, clk_groups,	\
-			ARRAY_SIZE(clk_groups));			\
+		__ret = samsung_cmu_register_one(dev, cmu_id,		\
+			clk_groups, ARRAY_SIZE(clk_groups));		\
 	__ret;								\
 })