Linux Audio

Check our new training course

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