14b9f2b1d9eeaecb67c8e17847c01dc76bfbb9a4
[platform/kernel/u-boot.git] / drivers / clk / clk-mux.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019 DENX Software Engineering
4  * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5  *
6  * Copyright (C) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
7  * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
8  * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
9  *
10  * Simple multiplexer clock implementation
11  */
12
13 /*
14  * U-Boot CCF porting node:
15  *
16  * The Linux kernel - as of tag: 5.0-rc3 is using also the imx_clk_fixup_mux()
17  * version of CCF mux. It is used on e.g. imx6q to provide fixes (like
18  * imx_cscmr1_fixup) for broken HW.
19  *
20  * At least for IMX6Q (but NOT IMX6QP) it is important when we set the parent
21  * clock.
22  */
23
24 #include <common.h>
25 #include <asm/io.h>
26 #include <malloc.h>
27 #include <clk-uclass.h>
28 #include <dm/device.h>
29 #include <linux/clk-provider.h>
30 #include <clk.h>
31 #include "clk.h"
32
33 #define UBOOT_DM_CLK_CCF_MUX "ccf_clk_mux"
34
35 int clk_mux_val_to_index(struct clk *clk, u32 *table, unsigned int flags,
36                          unsigned int val)
37 {
38         struct clk_mux *mux = to_clk_mux(clk);
39         int num_parents = mux->num_parents;
40
41         if (table) {
42                 int i;
43
44                 for (i = 0; i < num_parents; i++)
45                         if (table[i] == val)
46                                 return i;
47                 return -EINVAL;
48         }
49
50         if (val && (flags & CLK_MUX_INDEX_BIT))
51                 val = ffs(val) - 1;
52
53         if (val && (flags & CLK_MUX_INDEX_ONE))
54                 val--;
55
56         if (val >= num_parents)
57                 return -EINVAL;
58
59         return val;
60 }
61
62 static u8 clk_mux_get_parent(struct clk *clk)
63 {
64         struct clk_mux *mux = to_clk_mux(clk);
65         u32 val;
66
67         val = readl(mux->reg) >> mux->shift;
68         val &= mux->mask;
69
70         return clk_mux_val_to_index(clk, mux->table, mux->flags, val);
71 }
72
73 const struct clk_ops clk_mux_ops = {
74                 .get_rate = clk_generic_get_rate,
75 };
76
77 struct clk *clk_hw_register_mux_table(struct device *dev, const char *name,
78                 const char * const *parent_names, u8 num_parents,
79                 unsigned long flags,
80                 void __iomem *reg, u8 shift, u32 mask,
81                 u8 clk_mux_flags, u32 *table)
82 {
83         struct clk_mux *mux;
84         struct clk *clk;
85         u8 width = 0;
86         int ret;
87
88         if (clk_mux_flags & CLK_MUX_HIWORD_MASK) {
89                 width = fls(mask) - ffs(mask) + 1;
90                 if (width + shift > 16) {
91                         pr_err("mux value exceeds LOWORD field\n");
92                         return ERR_PTR(-EINVAL);
93                 }
94         }
95
96         /* allocate the mux */
97         mux = kzalloc(sizeof(*mux), GFP_KERNEL);
98         if (!mux)
99                 return ERR_PTR(-ENOMEM);
100
101         /* U-boot specific assignments */
102         mux->parent_names = parent_names;
103         mux->num_parents = num_parents;
104
105         /* struct clk_mux assignments */
106         mux->reg = reg;
107         mux->shift = shift;
108         mux->mask = mask;
109         mux->flags = clk_mux_flags;
110         mux->table = table;
111
112         clk = &mux->clk;
113
114         /*
115          * Read the current mux setup - so we assign correct parent.
116          *
117          * Changing parent would require changing internals of udevice struct
118          * for the corresponding clock (to do that define .set_parent() method.
119          */
120         ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name,
121                            parent_names[clk_mux_get_parent(clk)]);
122         if (ret) {
123                 kfree(mux);
124                 return ERR_PTR(ret);
125         }
126
127         return clk;
128 }
129
130 struct clk *clk_register_mux_table(struct device *dev, const char *name,
131                 const char * const *parent_names, u8 num_parents,
132                 unsigned long flags,
133                 void __iomem *reg, u8 shift, u32 mask,
134                 u8 clk_mux_flags, u32 *table)
135 {
136         struct clk *clk;
137
138         clk = clk_hw_register_mux_table(dev, name, parent_names, num_parents,
139                                        flags, reg, shift, mask, clk_mux_flags,
140                                        table);
141         if (IS_ERR(clk))
142                 return ERR_CAST(clk);
143         return clk;
144 }
145
146 struct clk *clk_register_mux(struct device *dev, const char *name,
147                 const char * const *parent_names, u8 num_parents,
148                 unsigned long flags,
149                 void __iomem *reg, u8 shift, u8 width,
150                 u8 clk_mux_flags)
151 {
152         u32 mask = BIT(width) - 1;
153
154         return clk_register_mux_table(dev, name, parent_names, num_parents,
155                                       flags, reg, shift, mask, clk_mux_flags,
156                                       NULL);
157 }
158
159 U_BOOT_DRIVER(ccf_clk_mux) = {
160         .name   = UBOOT_DM_CLK_CCF_MUX,
161         .id     = UCLASS_CLK,
162         .ops    = &clk_mux_ops,
163         .flags = DM_FLAG_PRE_RELOC,
164 };