blob: 18c267b38461dc96ff1d5dd26f518136a342ffb3 [file] [log] [blame]
Tero Kristo6a369c52013-09-13 20:22:27 +03001/*
2 * TI Multiplexer Clock
3 *
4 * Copyright (C) 2013 Texas Instruments, Inc.
5 *
6 * Tero Kristo <t-kristo@ti.com>
7 *
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.
11 *
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.
16 */
17
18#include <linux/clk-provider.h>
19#include <linux/slab.h>
20#include <linux/err.h>
21#include <linux/of.h>
22#include <linux/of_address.h>
23#include <linux/clk/ti.h>
Tero Kristo7c18a652014-12-16 18:20:47 +020024#include "clock.h"
Tero Kristo6a369c52013-09-13 20:22:27 +030025
26#undef pr_fmt
27#define pr_fmt(fmt) "%s: " fmt, __func__
28
Tero Kristo6a369c52013-09-13 20:22:27 +030029static u8 ti_clk_mux_get_parent(struct clk_hw *hw)
30{
Tero Kristod83bc5b2017-02-09 14:40:40 +020031 struct clk_omap_mux *mux = to_clk_omap_mux(hw);
Stephen Boyd497295a2015-06-25 16:53:23 -070032 int num_parents = clk_hw_get_num_parents(hw);
Tero Kristo6a369c52013-09-13 20:22:27 +030033 u32 val;
34
35 /*
36 * FIXME need a mux-specific flag to determine if val is bitwise or
37 * numeric. e.g. sys_clkin_ck's clksel field is 3 bits wide, but ranges
38 * from 0x1 to 0x7 (index starts at one)
39 * OTOH, pmd_trace_clk_mux_ck uses a separate bit for each clock, so
40 * val = 0x4 really means "bit 2, index starts at bit 0"
41 */
Tero Kristo6c0afb52017-02-09 11:24:37 +020042 val = ti_clk_ll_ops->clk_readl(&mux->reg) >> mux->shift;
Tero Kristo6a369c52013-09-13 20:22:27 +030043 val &= mux->mask;
44
45 if (mux->table) {
46 int i;
47
48 for (i = 0; i < num_parents; i++)
49 if (mux->table[i] == val)
50 return i;
51 return -EINVAL;
52 }
53
54 if (val && (mux->flags & CLK_MUX_INDEX_BIT))
55 val = ffs(val) - 1;
56
57 if (val && (mux->flags & CLK_MUX_INDEX_ONE))
58 val--;
59
60 if (val >= num_parents)
61 return -EINVAL;
62
63 return val;
64}
65
66static int ti_clk_mux_set_parent(struct clk_hw *hw, u8 index)
67{
Tero Kristod83bc5b2017-02-09 14:40:40 +020068 struct clk_omap_mux *mux = to_clk_omap_mux(hw);
Tero Kristo6a369c52013-09-13 20:22:27 +030069 u32 val;
Tero Kristo6a369c52013-09-13 20:22:27 +030070
71 if (mux->table) {
72 index = mux->table[index];
73 } else {
74 if (mux->flags & CLK_MUX_INDEX_BIT)
75 index = (1 << ffs(index));
76
77 if (mux->flags & CLK_MUX_INDEX_ONE)
78 index++;
79 }
80
Tero Kristo6a369c52013-09-13 20:22:27 +030081 if (mux->flags & CLK_MUX_HIWORD_MASK) {
82 val = mux->mask << (mux->shift + 16);
83 } else {
Tero Kristo6c0afb52017-02-09 11:24:37 +020084 val = ti_clk_ll_ops->clk_readl(&mux->reg);
Tero Kristo6a369c52013-09-13 20:22:27 +030085 val &= ~(mux->mask << mux->shift);
86 }
87 val |= index << mux->shift;
Tero Kristo6c0afb52017-02-09 11:24:37 +020088 ti_clk_ll_ops->clk_writel(val, &mux->reg);
Tero Kristo6a369c52013-09-13 20:22:27 +030089
Tero Kristo6a369c52013-09-13 20:22:27 +030090 return 0;
91}
92
93const struct clk_ops ti_clk_mux_ops = {
94 .get_parent = ti_clk_mux_get_parent,
95 .set_parent = ti_clk_mux_set_parent,
96 .determine_rate = __clk_mux_determine_rate,
97};
98
99static struct clk *_register_mux(struct device *dev, const char *name,
Tero Kristoce382d42016-10-05 15:37:02 +0300100 const char * const *parent_names,
101 u8 num_parents, unsigned long flags,
Tero Kristo6c0afb52017-02-09 11:24:37 +0200102 struct clk_omap_reg *reg, u8 shift, u32 mask,
Tero Kristoce382d42016-10-05 15:37:02 +0300103 u8 clk_mux_flags, u32 *table)
Tero Kristo6a369c52013-09-13 20:22:27 +0300104{
Tero Kristod83bc5b2017-02-09 14:40:40 +0200105 struct clk_omap_mux *mux;
Tero Kristo6a369c52013-09-13 20:22:27 +0300106 struct clk *clk;
107 struct clk_init_data init;
108
109 /* allocate the mux */
110 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
111 if (!mux) {
112 pr_err("%s: could not allocate mux clk\n", __func__);
113 return ERR_PTR(-ENOMEM);
114 }
115
116 init.name = name;
117 init.ops = &ti_clk_mux_ops;
118 init.flags = flags | CLK_IS_BASIC;
119 init.parent_names = parent_names;
120 init.num_parents = num_parents;
121
122 /* struct clk_mux assignments */
Tero Kristo6c0afb52017-02-09 11:24:37 +0200123 memcpy(&mux->reg, reg, sizeof(*reg));
Tero Kristo6a369c52013-09-13 20:22:27 +0300124 mux->shift = shift;
125 mux->mask = mask;
126 mux->flags = clk_mux_flags;
Tero Kristo6a369c52013-09-13 20:22:27 +0300127 mux->table = table;
128 mux->hw.init = &init;
129
Tero Kristo1ae79c42016-09-29 12:06:40 +0300130 clk = ti_clk_register(dev, &mux->hw, name);
Tero Kristo6a369c52013-09-13 20:22:27 +0300131
132 if (IS_ERR(clk))
133 kfree(mux);
134
135 return clk;
136}
137
Tero Kristo7c18a652014-12-16 18:20:47 +0200138struct clk *ti_clk_register_mux(struct ti_clk *setup)
139{
140 struct ti_clk_mux *mux;
141 u32 flags;
142 u8 mux_flags = 0;
Tero Kristo6c0afb52017-02-09 11:24:37 +0200143 struct clk_omap_reg reg;
Tero Kristo7c18a652014-12-16 18:20:47 +0200144 u32 mask;
145
Tero Kristo7c18a652014-12-16 18:20:47 +0200146 mux = setup->data;
147 flags = CLK_SET_RATE_NO_REPARENT;
148
149 mask = mux->num_parents;
150 if (!(mux->flags & CLKF_INDEX_STARTS_AT_ONE))
151 mask--;
152
153 mask = (1 << fls(mask)) - 1;
Tero Kristo6c0afb52017-02-09 11:24:37 +0200154 reg.index = mux->module;
155 reg.offset = mux->reg;
156 reg.ptr = NULL;
Tero Kristo7c18a652014-12-16 18:20:47 +0200157
158 if (mux->flags & CLKF_INDEX_STARTS_AT_ONE)
159 mux_flags |= CLK_MUX_INDEX_ONE;
160
161 if (mux->flags & CLKF_SET_RATE_PARENT)
162 flags |= CLK_SET_RATE_PARENT;
163
164 return _register_mux(NULL, setup->name, mux->parents, mux->num_parents,
Tero Kristo6c0afb52017-02-09 11:24:37 +0200165 flags, &reg, mux->bit_shift, mask,
Grygorii Strashko167af5e2015-10-01 14:20:37 -0500166 mux_flags, NULL);
Tero Kristo7c18a652014-12-16 18:20:47 +0200167}
168
Tero Kristo6a369c52013-09-13 20:22:27 +0300169/**
170 * of_mux_clk_setup - Setup function for simple mux rate clock
171 * @node: DT node for the clock
172 *
173 * Sets up a basic clock multiplexer.
174 */
175static void of_mux_clk_setup(struct device_node *node)
176{
177 struct clk *clk;
Tero Kristo6c0afb52017-02-09 11:24:37 +0200178 struct clk_omap_reg reg;
Stephen Boyd921bacf2016-02-19 17:49:23 -0800179 unsigned int num_parents;
Tero Kristo6a369c52013-09-13 20:22:27 +0300180 const char **parent_names;
Tero Kristo6a369c52013-09-13 20:22:27 +0300181 u8 clk_mux_flags = 0;
182 u32 mask = 0;
183 u32 shift = 0;
Tomi Valkeinen7d5fc852014-06-17 11:04:32 +0300184 u32 flags = CLK_SET_RATE_NO_REPARENT;
Tero Kristo6a369c52013-09-13 20:22:27 +0300185
186 num_parents = of_clk_get_parent_count(node);
187 if (num_parents < 2) {
188 pr_err("mux-clock %s must have parents\n", node->name);
189 return;
190 }
191 parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL);
192 if (!parent_names)
193 goto cleanup;
194
Dinh Nguyen9da9e762015-07-06 22:59:06 -0500195 of_clk_parent_fill(node, parent_names, num_parents);
Tero Kristo6a369c52013-09-13 20:22:27 +0300196
Tero Kristo6c0afb52017-02-09 11:24:37 +0200197 if (ti_clk_get_reg_addr(node, 0, &reg))
Tero Kristo6a369c52013-09-13 20:22:27 +0300198 goto cleanup;
199
200 of_property_read_u32(node, "ti,bit-shift", &shift);
201
202 if (of_property_read_bool(node, "ti,index-starts-at-one"))
203 clk_mux_flags |= CLK_MUX_INDEX_ONE;
204
205 if (of_property_read_bool(node, "ti,set-rate-parent"))
206 flags |= CLK_SET_RATE_PARENT;
207
208 /* Generate bit-mask based on parent info */
209 mask = num_parents;
210 if (!(clk_mux_flags & CLK_MUX_INDEX_ONE))
211 mask--;
212
213 mask = (1 << fls(mask)) - 1;
214
Tero Kristo7c18a652014-12-16 18:20:47 +0200215 clk = _register_mux(NULL, node->name, parent_names, num_parents,
Tero Kristo6c0afb52017-02-09 11:24:37 +0200216 flags, &reg, shift, mask, clk_mux_flags, NULL);
Tero Kristo6a369c52013-09-13 20:22:27 +0300217
218 if (!IS_ERR(clk))
219 of_clk_add_provider(node, of_clk_src_simple_get, clk);
220
221cleanup:
222 kfree(parent_names);
223}
224CLK_OF_DECLARE(mux_clk, "ti,mux-clock", of_mux_clk_setup);
225
Tero Kristo7c18a652014-12-16 18:20:47 +0200226struct clk_hw *ti_clk_build_component_mux(struct ti_clk_mux *setup)
227{
Tero Kristod83bc5b2017-02-09 14:40:40 +0200228 struct clk_omap_mux *mux;
Tero Kristo7c18a652014-12-16 18:20:47 +0200229 int num_parents;
230
231 if (!setup)
232 return NULL;
233
234 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
235 if (!mux)
236 return ERR_PTR(-ENOMEM);
237
Tero Kristo7c18a652014-12-16 18:20:47 +0200238 mux->shift = setup->bit_shift;
239
Tero Kristo6c0afb52017-02-09 11:24:37 +0200240 mux->reg.index = setup->module;
241 mux->reg.offset = setup->reg;
Tero Kristo7c18a652014-12-16 18:20:47 +0200242
243 if (setup->flags & CLKF_INDEX_STARTS_AT_ONE)
244 mux->flags |= CLK_MUX_INDEX_ONE;
245
246 num_parents = setup->num_parents;
247
248 mux->mask = num_parents - 1;
249 mux->mask = (1 << fls(mux->mask)) - 1;
250
251 return &mux->hw;
252}
253
Tero Kristo6a369c52013-09-13 20:22:27 +0300254static void __init of_ti_composite_mux_clk_setup(struct device_node *node)
255{
Tero Kristod83bc5b2017-02-09 14:40:40 +0200256 struct clk_omap_mux *mux;
Stephen Boyd921bacf2016-02-19 17:49:23 -0800257 unsigned int num_parents;
Tero Kristo6a369c52013-09-13 20:22:27 +0300258 u32 val;
259
260 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
261 if (!mux)
262 return;
263
Tero Kristo6c0afb52017-02-09 11:24:37 +0200264 if (ti_clk_get_reg_addr(node, 0, &mux->reg))
Tero Kristo6a369c52013-09-13 20:22:27 +0300265 goto cleanup;
266
267 if (!of_property_read_u32(node, "ti,bit-shift", &val))
268 mux->shift = val;
269
270 if (of_property_read_bool(node, "ti,index-starts-at-one"))
271 mux->flags |= CLK_MUX_INDEX_ONE;
272
273 num_parents = of_clk_get_parent_count(node);
274
275 if (num_parents < 2) {
276 pr_err("%s must have parents\n", node->name);
277 goto cleanup;
278 }
279
280 mux->mask = num_parents - 1;
281 mux->mask = (1 << fls(mux->mask)) - 1;
282
283 if (!ti_clk_add_component(node, &mux->hw, CLK_COMPONENT_TYPE_MUX))
284 return;
285
286cleanup:
287 kfree(mux);
288}
289CLK_OF_DECLARE(ti_composite_mux_clk_setup, "ti,composite-mux-clock",
290 of_ti_composite_mux_clk_setup);