4 * Copyright (C) 2013 Texas Instruments, Inc.
6 * Tero Kristo <t-kristo@ti.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13 * kind, whether express or implied; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/clk-provider.h>
19 #include <linux/slab.h>
20 #include <linux/err.h>
22 #include <linux/of_address.h>
23 #include <linux/clk/ti.h>
26 #define pr_fmt(fmt) "%s: " fmt, __func__
28 #define to_clk_mux(_hw) container_of(_hw, struct clk_mux, hw)
30 static u8
ti_clk_mux_get_parent(struct clk_hw
*hw
)
32 struct clk_mux
*mux
= to_clk_mux(hw
);
33 int num_parents
= __clk_get_num_parents(hw
->clk
);
37 * FIXME need a mux-specific flag to determine if val is bitwise or
38 * numeric. e.g. sys_clkin_ck's clksel field is 3 bits wide, but ranges
39 * from 0x1 to 0x7 (index starts at one)
40 * OTOH, pmd_trace_clk_mux_ck uses a separate bit for each clock, so
41 * val = 0x4 really means "bit 2, index starts at bit 0"
43 val
= ti_clk_ll_ops
->clk_readl(mux
->reg
) >> mux
->shift
;
49 for (i
= 0; i
< num_parents
; i
++)
50 if (mux
->table
[i
] == val
)
55 if (val
&& (mux
->flags
& CLK_MUX_INDEX_BIT
))
58 if (val
&& (mux
->flags
& CLK_MUX_INDEX_ONE
))
61 if (val
>= num_parents
)
67 static int ti_clk_mux_set_parent(struct clk_hw
*hw
, u8 index
)
69 struct clk_mux
*mux
= to_clk_mux(hw
);
71 unsigned long flags
= 0;
74 index
= mux
->table
[index
];
76 if (mux
->flags
& CLK_MUX_INDEX_BIT
)
77 index
= (1 << ffs(index
));
79 if (mux
->flags
& CLK_MUX_INDEX_ONE
)
84 spin_lock_irqsave(mux
->lock
, flags
);
86 if (mux
->flags
& CLK_MUX_HIWORD_MASK
) {
87 val
= mux
->mask
<< (mux
->shift
+ 16);
89 val
= ti_clk_ll_ops
->clk_readl(mux
->reg
);
90 val
&= ~(mux
->mask
<< mux
->shift
);
92 val
|= index
<< mux
->shift
;
93 ti_clk_ll_ops
->clk_writel(val
, mux
->reg
);
96 spin_unlock_irqrestore(mux
->lock
, flags
);
101 const struct clk_ops ti_clk_mux_ops
= {
102 .get_parent
= ti_clk_mux_get_parent
,
103 .set_parent
= ti_clk_mux_set_parent
,
104 .determine_rate
= __clk_mux_determine_rate
,
107 static struct clk
*_register_mux(struct device
*dev
, const char *name
,
108 const char **parent_names
, u8 num_parents
,
109 unsigned long flags
, void __iomem
*reg
,
110 u8 shift
, u32 mask
, u8 clk_mux_flags
,
111 u32
*table
, spinlock_t
*lock
)
115 struct clk_init_data init
;
117 /* allocate the mux */
118 mux
= kzalloc(sizeof(*mux
), GFP_KERNEL
);
120 pr_err("%s: could not allocate mux clk\n", __func__
);
121 return ERR_PTR(-ENOMEM
);
125 init
.ops
= &ti_clk_mux_ops
;
126 init
.flags
= flags
| CLK_IS_BASIC
;
127 init
.parent_names
= parent_names
;
128 init
.num_parents
= num_parents
;
130 /* struct clk_mux assignments */
134 mux
->flags
= clk_mux_flags
;
137 mux
->hw
.init
= &init
;
139 clk
= clk_register(dev
, &mux
->hw
);
148 * of_mux_clk_setup - Setup function for simple mux rate clock
149 * @node: DT node for the clock
151 * Sets up a basic clock multiplexer.
153 static void of_mux_clk_setup(struct device_node
*node
)
158 const char **parent_names
;
160 u8 clk_mux_flags
= 0;
165 num_parents
= of_clk_get_parent_count(node
);
166 if (num_parents
< 2) {
167 pr_err("mux-clock %s must have parents\n", node
->name
);
170 parent_names
= kzalloc((sizeof(char *) * num_parents
), GFP_KERNEL
);
174 for (i
= 0; i
< num_parents
; i
++)
175 parent_names
[i
] = of_clk_get_parent_name(node
, i
);
177 reg
= ti_clk_get_reg_addr(node
, 0);
182 of_property_read_u32(node
, "ti,bit-shift", &shift
);
184 if (of_property_read_bool(node
, "ti,index-starts-at-one"))
185 clk_mux_flags
|= CLK_MUX_INDEX_ONE
;
187 if (of_property_read_bool(node
, "ti,set-rate-parent"))
188 flags
|= CLK_SET_RATE_PARENT
;
190 /* Generate bit-mask based on parent info */
192 if (!(clk_mux_flags
& CLK_MUX_INDEX_ONE
))
195 mask
= (1 << fls(mask
)) - 1;
197 clk
= _register_mux(NULL
, node
->name
, parent_names
, num_parents
, flags
,
198 reg
, shift
, mask
, clk_mux_flags
, NULL
, NULL
);
201 of_clk_add_provider(node
, of_clk_src_simple_get
, clk
);
206 CLK_OF_DECLARE(mux_clk
, "ti,mux-clock", of_mux_clk_setup
);
208 static void __init
of_ti_composite_mux_clk_setup(struct device_node
*node
)
214 mux
= kzalloc(sizeof(*mux
), GFP_KERNEL
);
218 mux
->reg
= ti_clk_get_reg_addr(node
, 0);
223 if (!of_property_read_u32(node
, "ti,bit-shift", &val
))
226 if (of_property_read_bool(node
, "ti,index-starts-at-one"))
227 mux
->flags
|= CLK_MUX_INDEX_ONE
;
229 num_parents
= of_clk_get_parent_count(node
);
231 if (num_parents
< 2) {
232 pr_err("%s must have parents\n", node
->name
);
236 mux
->mask
= num_parents
- 1;
237 mux
->mask
= (1 << fls(mux
->mask
)) - 1;
239 if (!ti_clk_add_component(node
, &mux
->hw
, CLK_COMPONENT_TYPE_MUX
))
245 CLK_OF_DECLARE(ti_composite_mux_clk_setup
, "ti,composite-mux-clock",
246 of_ti_composite_mux_clk_setup
);