1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * TI Multiplexer Clock
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2013 Texas Instruments, Inc.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Tero Kristo <t-kristo@ti.com>
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * This program is free software; you can redistribute it and/or modify
9*4882a593Smuzhiyun * it under the terms of the GNU General Public License version 2 as
10*4882a593Smuzhiyun * published by the Free Software Foundation.
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13*4882a593Smuzhiyun * kind, whether express or implied; without even the implied warranty
14*4882a593Smuzhiyun * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*4882a593Smuzhiyun * GNU General Public License for more details.
16*4882a593Smuzhiyun */
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <linux/clk-provider.h>
19*4882a593Smuzhiyun #include <linux/slab.h>
20*4882a593Smuzhiyun #include <linux/err.h>
21*4882a593Smuzhiyun #include <linux/of.h>
22*4882a593Smuzhiyun #include <linux/of_address.h>
23*4882a593Smuzhiyun #include <linux/clk/ti.h>
24*4882a593Smuzhiyun #include "clock.h"
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #undef pr_fmt
27*4882a593Smuzhiyun #define pr_fmt(fmt) "%s: " fmt, __func__
28*4882a593Smuzhiyun
ti_clk_mux_get_parent(struct clk_hw * hw)29*4882a593Smuzhiyun static u8 ti_clk_mux_get_parent(struct clk_hw *hw)
30*4882a593Smuzhiyun {
31*4882a593Smuzhiyun struct clk_omap_mux *mux = to_clk_omap_mux(hw);
32*4882a593Smuzhiyun int num_parents = clk_hw_get_num_parents(hw);
33*4882a593Smuzhiyun u32 val;
34*4882a593Smuzhiyun
35*4882a593Smuzhiyun /*
36*4882a593Smuzhiyun * FIXME need a mux-specific flag to determine if val is bitwise or
37*4882a593Smuzhiyun * numeric. e.g. sys_clkin_ck's clksel field is 3 bits wide, but ranges
38*4882a593Smuzhiyun * from 0x1 to 0x7 (index starts at one)
39*4882a593Smuzhiyun * OTOH, pmd_trace_clk_mux_ck uses a separate bit for each clock, so
40*4882a593Smuzhiyun * val = 0x4 really means "bit 2, index starts at bit 0"
41*4882a593Smuzhiyun */
42*4882a593Smuzhiyun val = ti_clk_ll_ops->clk_readl(&mux->reg) >> mux->shift;
43*4882a593Smuzhiyun val &= mux->mask;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun if (mux->table) {
46*4882a593Smuzhiyun int i;
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun for (i = 0; i < num_parents; i++)
49*4882a593Smuzhiyun if (mux->table[i] == val)
50*4882a593Smuzhiyun return i;
51*4882a593Smuzhiyun return -EINVAL;
52*4882a593Smuzhiyun }
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun if (val && (mux->flags & CLK_MUX_INDEX_BIT))
55*4882a593Smuzhiyun val = ffs(val) - 1;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun if (val && (mux->flags & CLK_MUX_INDEX_ONE))
58*4882a593Smuzhiyun val--;
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun if (val >= num_parents)
61*4882a593Smuzhiyun return -EINVAL;
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun return val;
64*4882a593Smuzhiyun }
65*4882a593Smuzhiyun
ti_clk_mux_set_parent(struct clk_hw * hw,u8 index)66*4882a593Smuzhiyun static int ti_clk_mux_set_parent(struct clk_hw *hw, u8 index)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun struct clk_omap_mux *mux = to_clk_omap_mux(hw);
69*4882a593Smuzhiyun u32 val;
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun if (mux->table) {
72*4882a593Smuzhiyun index = mux->table[index];
73*4882a593Smuzhiyun } else {
74*4882a593Smuzhiyun if (mux->flags & CLK_MUX_INDEX_BIT)
75*4882a593Smuzhiyun index = (1 << ffs(index));
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun if (mux->flags & CLK_MUX_INDEX_ONE)
78*4882a593Smuzhiyun index++;
79*4882a593Smuzhiyun }
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun if (mux->flags & CLK_MUX_HIWORD_MASK) {
82*4882a593Smuzhiyun val = mux->mask << (mux->shift + 16);
83*4882a593Smuzhiyun } else {
84*4882a593Smuzhiyun val = ti_clk_ll_ops->clk_readl(&mux->reg);
85*4882a593Smuzhiyun val &= ~(mux->mask << mux->shift);
86*4882a593Smuzhiyun }
87*4882a593Smuzhiyun val |= index << mux->shift;
88*4882a593Smuzhiyun ti_clk_ll_ops->clk_writel(val, &mux->reg);
89*4882a593Smuzhiyun ti_clk_latch(&mux->reg, mux->latch);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun return 0;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /**
95*4882a593Smuzhiyun * clk_mux_save_context - Save the parent selcted in the mux
96*4882a593Smuzhiyun * @hw: pointer struct clk_hw
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun * Save the parent mux value.
99*4882a593Smuzhiyun */
clk_mux_save_context(struct clk_hw * hw)100*4882a593Smuzhiyun static int clk_mux_save_context(struct clk_hw *hw)
101*4882a593Smuzhiyun {
102*4882a593Smuzhiyun struct clk_omap_mux *mux = to_clk_omap_mux(hw);
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun mux->saved_parent = ti_clk_mux_get_parent(hw);
105*4882a593Smuzhiyun return 0;
106*4882a593Smuzhiyun }
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /**
109*4882a593Smuzhiyun * clk_mux_restore_context - Restore the parent in the mux
110*4882a593Smuzhiyun * @hw: pointer struct clk_hw
111*4882a593Smuzhiyun *
112*4882a593Smuzhiyun * Restore the saved parent mux value.
113*4882a593Smuzhiyun */
clk_mux_restore_context(struct clk_hw * hw)114*4882a593Smuzhiyun static void clk_mux_restore_context(struct clk_hw *hw)
115*4882a593Smuzhiyun {
116*4882a593Smuzhiyun struct clk_omap_mux *mux = to_clk_omap_mux(hw);
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun ti_clk_mux_set_parent(hw, mux->saved_parent);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun const struct clk_ops ti_clk_mux_ops = {
122*4882a593Smuzhiyun .get_parent = ti_clk_mux_get_parent,
123*4882a593Smuzhiyun .set_parent = ti_clk_mux_set_parent,
124*4882a593Smuzhiyun .determine_rate = __clk_mux_determine_rate,
125*4882a593Smuzhiyun .save_context = clk_mux_save_context,
126*4882a593Smuzhiyun .restore_context = clk_mux_restore_context,
127*4882a593Smuzhiyun };
128*4882a593Smuzhiyun
_register_mux(struct device * dev,const char * name,const char * const * parent_names,u8 num_parents,unsigned long flags,struct clk_omap_reg * reg,u8 shift,u32 mask,s8 latch,u8 clk_mux_flags,u32 * table)129*4882a593Smuzhiyun static struct clk *_register_mux(struct device *dev, const char *name,
130*4882a593Smuzhiyun const char * const *parent_names,
131*4882a593Smuzhiyun u8 num_parents, unsigned long flags,
132*4882a593Smuzhiyun struct clk_omap_reg *reg, u8 shift, u32 mask,
133*4882a593Smuzhiyun s8 latch, u8 clk_mux_flags, u32 *table)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun struct clk_omap_mux *mux;
136*4882a593Smuzhiyun struct clk *clk;
137*4882a593Smuzhiyun struct clk_init_data init;
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun /* allocate the mux */
140*4882a593Smuzhiyun mux = kzalloc(sizeof(*mux), GFP_KERNEL);
141*4882a593Smuzhiyun if (!mux)
142*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun init.name = name;
145*4882a593Smuzhiyun init.ops = &ti_clk_mux_ops;
146*4882a593Smuzhiyun init.flags = flags;
147*4882a593Smuzhiyun init.parent_names = parent_names;
148*4882a593Smuzhiyun init.num_parents = num_parents;
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun /* struct clk_mux assignments */
151*4882a593Smuzhiyun memcpy(&mux->reg, reg, sizeof(*reg));
152*4882a593Smuzhiyun mux->shift = shift;
153*4882a593Smuzhiyun mux->mask = mask;
154*4882a593Smuzhiyun mux->latch = latch;
155*4882a593Smuzhiyun mux->flags = clk_mux_flags;
156*4882a593Smuzhiyun mux->table = table;
157*4882a593Smuzhiyun mux->hw.init = &init;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun clk = ti_clk_register(dev, &mux->hw, name);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun if (IS_ERR(clk))
162*4882a593Smuzhiyun kfree(mux);
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun return clk;
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun * of_mux_clk_setup - Setup function for simple mux rate clock
169*4882a593Smuzhiyun * @node: DT node for the clock
170*4882a593Smuzhiyun *
171*4882a593Smuzhiyun * Sets up a basic clock multiplexer.
172*4882a593Smuzhiyun */
of_mux_clk_setup(struct device_node * node)173*4882a593Smuzhiyun static void of_mux_clk_setup(struct device_node *node)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun struct clk *clk;
176*4882a593Smuzhiyun struct clk_omap_reg reg;
177*4882a593Smuzhiyun unsigned int num_parents;
178*4882a593Smuzhiyun const char **parent_names;
179*4882a593Smuzhiyun u8 clk_mux_flags = 0;
180*4882a593Smuzhiyun u32 mask = 0;
181*4882a593Smuzhiyun u32 shift = 0;
182*4882a593Smuzhiyun s32 latch = -EINVAL;
183*4882a593Smuzhiyun u32 flags = CLK_SET_RATE_NO_REPARENT;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun num_parents = of_clk_get_parent_count(node);
186*4882a593Smuzhiyun if (num_parents < 2) {
187*4882a593Smuzhiyun pr_err("mux-clock %pOFn must have parents\n", node);
188*4882a593Smuzhiyun return;
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL);
191*4882a593Smuzhiyun if (!parent_names)
192*4882a593Smuzhiyun goto cleanup;
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun of_clk_parent_fill(node, parent_names, num_parents);
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun if (ti_clk_get_reg_addr(node, 0, ®))
197*4882a593Smuzhiyun goto cleanup;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun of_property_read_u32(node, "ti,bit-shift", &shift);
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun of_property_read_u32(node, "ti,latch-bit", &latch);
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun if (of_property_read_bool(node, "ti,index-starts-at-one"))
204*4882a593Smuzhiyun clk_mux_flags |= CLK_MUX_INDEX_ONE;
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun if (of_property_read_bool(node, "ti,set-rate-parent"))
207*4882a593Smuzhiyun flags |= CLK_SET_RATE_PARENT;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /* Generate bit-mask based on parent info */
210*4882a593Smuzhiyun mask = num_parents;
211*4882a593Smuzhiyun if (!(clk_mux_flags & CLK_MUX_INDEX_ONE))
212*4882a593Smuzhiyun mask--;
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun mask = (1 << fls(mask)) - 1;
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun clk = _register_mux(NULL, node->name, parent_names, num_parents,
217*4882a593Smuzhiyun flags, ®, shift, mask, latch, clk_mux_flags,
218*4882a593Smuzhiyun NULL);
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun if (!IS_ERR(clk))
221*4882a593Smuzhiyun of_clk_add_provider(node, of_clk_src_simple_get, clk);
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun cleanup:
224*4882a593Smuzhiyun kfree(parent_names);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun CLK_OF_DECLARE(mux_clk, "ti,mux-clock", of_mux_clk_setup);
227*4882a593Smuzhiyun
ti_clk_build_component_mux(struct ti_clk_mux * setup)228*4882a593Smuzhiyun struct clk_hw *ti_clk_build_component_mux(struct ti_clk_mux *setup)
229*4882a593Smuzhiyun {
230*4882a593Smuzhiyun struct clk_omap_mux *mux;
231*4882a593Smuzhiyun int num_parents;
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun if (!setup)
234*4882a593Smuzhiyun return NULL;
235*4882a593Smuzhiyun
236*4882a593Smuzhiyun mux = kzalloc(sizeof(*mux), GFP_KERNEL);
237*4882a593Smuzhiyun if (!mux)
238*4882a593Smuzhiyun return ERR_PTR(-ENOMEM);
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun mux->shift = setup->bit_shift;
241*4882a593Smuzhiyun mux->latch = -EINVAL;
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun mux->reg.index = setup->module;
244*4882a593Smuzhiyun mux->reg.offset = setup->reg;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun if (setup->flags & CLKF_INDEX_STARTS_AT_ONE)
247*4882a593Smuzhiyun mux->flags |= CLK_MUX_INDEX_ONE;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun num_parents = setup->num_parents;
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun mux->mask = num_parents - 1;
252*4882a593Smuzhiyun mux->mask = (1 << fls(mux->mask)) - 1;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun return &mux->hw;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun
of_ti_composite_mux_clk_setup(struct device_node * node)257*4882a593Smuzhiyun static void __init of_ti_composite_mux_clk_setup(struct device_node *node)
258*4882a593Smuzhiyun {
259*4882a593Smuzhiyun struct clk_omap_mux *mux;
260*4882a593Smuzhiyun unsigned int num_parents;
261*4882a593Smuzhiyun u32 val;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun mux = kzalloc(sizeof(*mux), GFP_KERNEL);
264*4882a593Smuzhiyun if (!mux)
265*4882a593Smuzhiyun return;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun if (ti_clk_get_reg_addr(node, 0, &mux->reg))
268*4882a593Smuzhiyun goto cleanup;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun if (!of_property_read_u32(node, "ti,bit-shift", &val))
271*4882a593Smuzhiyun mux->shift = val;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun if (of_property_read_bool(node, "ti,index-starts-at-one"))
274*4882a593Smuzhiyun mux->flags |= CLK_MUX_INDEX_ONE;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun num_parents = of_clk_get_parent_count(node);
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun if (num_parents < 2) {
279*4882a593Smuzhiyun pr_err("%pOFn must have parents\n", node);
280*4882a593Smuzhiyun goto cleanup;
281*4882a593Smuzhiyun }
282*4882a593Smuzhiyun
283*4882a593Smuzhiyun mux->mask = num_parents - 1;
284*4882a593Smuzhiyun mux->mask = (1 << fls(mux->mask)) - 1;
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun if (!ti_clk_add_component(node, &mux->hw, CLK_COMPONENT_TYPE_MUX))
287*4882a593Smuzhiyun return;
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun cleanup:
290*4882a593Smuzhiyun kfree(mux);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun CLK_OF_DECLARE(ti_composite_mux_clk_setup, "ti,composite-mux-clock",
293*4882a593Smuzhiyun of_ti_composite_mux_clk_setup);
294