xref: /linux/drivers/clk/ti/mux.c (revision e665f029a283aff4f36f0c5388f7c708be67470e)
16a369c58STero Kristo /*
26a369c58STero Kristo  * TI Multiplexer Clock
36a369c58STero Kristo  *
46a369c58STero Kristo  * Copyright (C) 2013 Texas Instruments, Inc.
56a369c58STero Kristo  *
66a369c58STero Kristo  * Tero Kristo <t-kristo@ti.com>
76a369c58STero Kristo  *
86a369c58STero Kristo  * This program is free software; you can redistribute it and/or modify
96a369c58STero Kristo  * it under the terms of the GNU General Public License version 2 as
106a369c58STero Kristo  * published by the Free Software Foundation.
116a369c58STero Kristo  *
126a369c58STero Kristo  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
136a369c58STero Kristo  * kind, whether express or implied; without even the implied warranty
146a369c58STero Kristo  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
156a369c58STero Kristo  * GNU General Public License for more details.
166a369c58STero Kristo  */
176a369c58STero Kristo 
186a369c58STero Kristo #include <linux/clk-provider.h>
196a369c58STero Kristo #include <linux/slab.h>
206a369c58STero Kristo #include <linux/err.h>
216a369c58STero Kristo #include <linux/of.h>
226a369c58STero Kristo #include <linux/of_address.h>
236a369c58STero Kristo #include <linux/clk/ti.h>
247c18a65cSTero Kristo #include "clock.h"
256a369c58STero Kristo 
266a369c58STero Kristo #undef pr_fmt
276a369c58STero Kristo #define pr_fmt(fmt) "%s: " fmt, __func__
286a369c58STero Kristo 
296a369c58STero Kristo static u8 ti_clk_mux_get_parent(struct clk_hw *hw)
306a369c58STero Kristo {
31d83bc5b6STero Kristo 	struct clk_omap_mux *mux = to_clk_omap_mux(hw);
32497295afSStephen Boyd 	int num_parents = clk_hw_get_num_parents(hw);
336a369c58STero Kristo 	u32 val;
346a369c58STero Kristo 
356a369c58STero Kristo 	/*
366a369c58STero Kristo 	 * FIXME need a mux-specific flag to determine if val is bitwise or
376a369c58STero Kristo 	 * numeric. e.g. sys_clkin_ck's clksel field is 3 bits wide, but ranges
386a369c58STero Kristo 	 * from 0x1 to 0x7 (index starts at one)
396a369c58STero Kristo 	 * OTOH, pmd_trace_clk_mux_ck uses a separate bit for each clock, so
406a369c58STero Kristo 	 * val = 0x4 really means "bit 2, index starts at bit 0"
416a369c58STero Kristo 	 */
426c0afb50STero Kristo 	val = ti_clk_ll_ops->clk_readl(&mux->reg) >> mux->shift;
436a369c58STero Kristo 	val &= mux->mask;
446a369c58STero Kristo 
456a369c58STero Kristo 	if (mux->table) {
466a369c58STero Kristo 		int i;
476a369c58STero Kristo 
486a369c58STero Kristo 		for (i = 0; i < num_parents; i++)
496a369c58STero Kristo 			if (mux->table[i] == val)
506a369c58STero Kristo 				return i;
516a369c58STero Kristo 		return -EINVAL;
526a369c58STero Kristo 	}
536a369c58STero Kristo 
546a369c58STero Kristo 	if (val && (mux->flags & CLK_MUX_INDEX_BIT))
556a369c58STero Kristo 		val = ffs(val) - 1;
566a369c58STero Kristo 
576a369c58STero Kristo 	if (val && (mux->flags & CLK_MUX_INDEX_ONE))
586a369c58STero Kristo 		val--;
596a369c58STero Kristo 
606a369c58STero Kristo 	if (val >= num_parents)
616a369c58STero Kristo 		return -EINVAL;
626a369c58STero Kristo 
636a369c58STero Kristo 	return val;
646a369c58STero Kristo }
656a369c58STero Kristo 
666a369c58STero Kristo static int ti_clk_mux_set_parent(struct clk_hw *hw, u8 index)
676a369c58STero Kristo {
68d83bc5b6STero Kristo 	struct clk_omap_mux *mux = to_clk_omap_mux(hw);
696a369c58STero Kristo 	u32 val;
706a369c58STero Kristo 
716a369c58STero Kristo 	if (mux->table) {
726a369c58STero Kristo 		index = mux->table[index];
736a369c58STero Kristo 	} else {
746a369c58STero Kristo 		if (mux->flags & CLK_MUX_INDEX_BIT)
756a369c58STero Kristo 			index = (1 << ffs(index));
766a369c58STero Kristo 
776a369c58STero Kristo 		if (mux->flags & CLK_MUX_INDEX_ONE)
786a369c58STero Kristo 			index++;
796a369c58STero Kristo 	}
806a369c58STero Kristo 
816a369c58STero Kristo 	if (mux->flags & CLK_MUX_HIWORD_MASK) {
826a369c58STero Kristo 		val = mux->mask << (mux->shift + 16);
836a369c58STero Kristo 	} else {
846c0afb50STero Kristo 		val = ti_clk_ll_ops->clk_readl(&mux->reg);
856a369c58STero Kristo 		val &= ~(mux->mask << mux->shift);
866a369c58STero Kristo 	}
876a369c58STero Kristo 	val |= index << mux->shift;
886c0afb50STero Kristo 	ti_clk_ll_ops->clk_writel(val, &mux->reg);
89ee2fc3c5STero Kristo 	ti_clk_latch(&mux->reg, mux->latch);
906a369c58STero Kristo 
916a369c58STero Kristo 	return 0;
926a369c58STero Kristo }
936a369c58STero Kristo 
946a369c58STero Kristo const struct clk_ops ti_clk_mux_ops = {
956a369c58STero Kristo 	.get_parent = ti_clk_mux_get_parent,
966a369c58STero Kristo 	.set_parent = ti_clk_mux_set_parent,
976a369c58STero Kristo 	.determine_rate = __clk_mux_determine_rate,
986a369c58STero Kristo };
996a369c58STero Kristo 
1006a369c58STero Kristo static struct clk *_register_mux(struct device *dev, const char *name,
101ce382d47STero Kristo 				 const char * const *parent_names,
102ce382d47STero Kristo 				 u8 num_parents, unsigned long flags,
1036c0afb50STero Kristo 				 struct clk_omap_reg *reg, u8 shift, u32 mask,
104ee2fc3c5STero Kristo 				 s8 latch, u8 clk_mux_flags, u32 *table)
1056a369c58STero Kristo {
106d83bc5b6STero Kristo 	struct clk_omap_mux *mux;
1076a369c58STero Kristo 	struct clk *clk;
1086a369c58STero Kristo 	struct clk_init_data init;
1096a369c58STero Kristo 
1106a369c58STero Kristo 	/* allocate the mux */
1116a369c58STero Kristo 	mux = kzalloc(sizeof(*mux), GFP_KERNEL);
112f80c8a29SMarkus Elfring 	if (!mux)
1136a369c58STero Kristo 		return ERR_PTR(-ENOMEM);
1146a369c58STero Kristo 
1156a369c58STero Kristo 	init.name = name;
1166a369c58STero Kristo 	init.ops = &ti_clk_mux_ops;
1176a369c58STero Kristo 	init.flags = flags | CLK_IS_BASIC;
1186a369c58STero Kristo 	init.parent_names = parent_names;
1196a369c58STero Kristo 	init.num_parents = num_parents;
1206a369c58STero Kristo 
1216a369c58STero Kristo 	/* struct clk_mux assignments */
1226c0afb50STero Kristo 	memcpy(&mux->reg, reg, sizeof(*reg));
1236a369c58STero Kristo 	mux->shift = shift;
1246a369c58STero Kristo 	mux->mask = mask;
125ee2fc3c5STero Kristo 	mux->latch = latch;
1266a369c58STero Kristo 	mux->flags = clk_mux_flags;
1276a369c58STero Kristo 	mux->table = table;
1286a369c58STero Kristo 	mux->hw.init = &init;
1296a369c58STero Kristo 
1301ae79c46STero Kristo 	clk = ti_clk_register(dev, &mux->hw, name);
1316a369c58STero Kristo 
1326a369c58STero Kristo 	if (IS_ERR(clk))
1336a369c58STero Kristo 		kfree(mux);
1346a369c58STero Kristo 
1356a369c58STero Kristo 	return clk;
1366a369c58STero Kristo }
1376a369c58STero Kristo 
1387c18a65cSTero Kristo struct clk *ti_clk_register_mux(struct ti_clk *setup)
1397c18a65cSTero Kristo {
1407c18a65cSTero Kristo 	struct ti_clk_mux *mux;
1417c18a65cSTero Kristo 	u32 flags;
1427c18a65cSTero Kristo 	u8 mux_flags = 0;
1436c0afb50STero Kristo 	struct clk_omap_reg reg;
1447c18a65cSTero Kristo 	u32 mask;
1457c18a65cSTero Kristo 
1467c18a65cSTero Kristo 	mux = setup->data;
1477c18a65cSTero Kristo 	flags = CLK_SET_RATE_NO_REPARENT;
1487c18a65cSTero Kristo 
1497c18a65cSTero Kristo 	mask = mux->num_parents;
1507c18a65cSTero Kristo 	if (!(mux->flags & CLKF_INDEX_STARTS_AT_ONE))
1517c18a65cSTero Kristo 		mask--;
1527c18a65cSTero Kristo 
1537c18a65cSTero Kristo 	mask = (1 << fls(mask)) - 1;
1546c0afb50STero Kristo 	reg.index = mux->module;
1556c0afb50STero Kristo 	reg.offset = mux->reg;
1566c0afb50STero Kristo 	reg.ptr = NULL;
1577c18a65cSTero Kristo 
1587c18a65cSTero Kristo 	if (mux->flags & CLKF_INDEX_STARTS_AT_ONE)
1597c18a65cSTero Kristo 		mux_flags |= CLK_MUX_INDEX_ONE;
1607c18a65cSTero Kristo 
1617c18a65cSTero Kristo 	if (mux->flags & CLKF_SET_RATE_PARENT)
1627c18a65cSTero Kristo 		flags |= CLK_SET_RATE_PARENT;
1637c18a65cSTero Kristo 
1647c18a65cSTero Kristo 	return _register_mux(NULL, setup->name, mux->parents, mux->num_parents,
165ee2fc3c5STero Kristo 			     flags, &reg, mux->bit_shift, mask, -EINVAL,
166167af5efSGrygorii Strashko 			     mux_flags, NULL);
1677c18a65cSTero Kristo }
1687c18a65cSTero Kristo 
1696a369c58STero Kristo /**
1706a369c58STero Kristo  * of_mux_clk_setup - Setup function for simple mux rate clock
1716a369c58STero Kristo  * @node: DT node for the clock
1726a369c58STero Kristo  *
1736a369c58STero Kristo  * Sets up a basic clock multiplexer.
1746a369c58STero Kristo  */
1756a369c58STero Kristo static void of_mux_clk_setup(struct device_node *node)
1766a369c58STero Kristo {
1776a369c58STero Kristo 	struct clk *clk;
1786c0afb50STero Kristo 	struct clk_omap_reg reg;
179921bacfaSStephen Boyd 	unsigned int num_parents;
1806a369c58STero Kristo 	const char **parent_names;
1816a369c58STero Kristo 	u8 clk_mux_flags = 0;
1826a369c58STero Kristo 	u32 mask = 0;
1836a369c58STero Kristo 	u32 shift = 0;
184ee2fc3c5STero Kristo 	s32 latch = -EINVAL;
1857d5fc85dSTomi Valkeinen 	u32 flags = CLK_SET_RATE_NO_REPARENT;
1866a369c58STero Kristo 
1876a369c58STero Kristo 	num_parents = of_clk_get_parent_count(node);
1886a369c58STero Kristo 	if (num_parents < 2) {
189*e665f029SRob Herring 		pr_err("mux-clock %pOFn must have parents\n", node);
1906a369c58STero Kristo 		return;
1916a369c58STero Kristo 	}
1926a369c58STero Kristo 	parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL);
1936a369c58STero Kristo 	if (!parent_names)
1946a369c58STero Kristo 		goto cleanup;
1956a369c58STero Kristo 
1969da9e761SDinh Nguyen 	of_clk_parent_fill(node, parent_names, num_parents);
1976a369c58STero Kristo 
1986c0afb50STero Kristo 	if (ti_clk_get_reg_addr(node, 0, &reg))
1996a369c58STero Kristo 		goto cleanup;
2006a369c58STero Kristo 
2016a369c58STero Kristo 	of_property_read_u32(node, "ti,bit-shift", &shift);
2026a369c58STero Kristo 
203ee2fc3c5STero Kristo 	of_property_read_u32(node, "ti,latch-bit", &latch);
204ee2fc3c5STero Kristo 
2056a369c58STero Kristo 	if (of_property_read_bool(node, "ti,index-starts-at-one"))
2066a369c58STero Kristo 		clk_mux_flags |= CLK_MUX_INDEX_ONE;
2076a369c58STero Kristo 
2086a369c58STero Kristo 	if (of_property_read_bool(node, "ti,set-rate-parent"))
2096a369c58STero Kristo 		flags |= CLK_SET_RATE_PARENT;
2106a369c58STero Kristo 
2116a369c58STero Kristo 	/* Generate bit-mask based on parent info */
2126a369c58STero Kristo 	mask = num_parents;
2136a369c58STero Kristo 	if (!(clk_mux_flags & CLK_MUX_INDEX_ONE))
2146a369c58STero Kristo 		mask--;
2156a369c58STero Kristo 
2166a369c58STero Kristo 	mask = (1 << fls(mask)) - 1;
2176a369c58STero Kristo 
2187c18a65cSTero Kristo 	clk = _register_mux(NULL, node->name, parent_names, num_parents,
219ee2fc3c5STero Kristo 			    flags, &reg, shift, mask, latch, clk_mux_flags,
220ee2fc3c5STero Kristo 			    NULL);
2216a369c58STero Kristo 
2226a369c58STero Kristo 	if (!IS_ERR(clk))
2236a369c58STero Kristo 		of_clk_add_provider(node, of_clk_src_simple_get, clk);
2246a369c58STero Kristo 
2256a369c58STero Kristo cleanup:
2266a369c58STero Kristo 	kfree(parent_names);
2276a369c58STero Kristo }
2286a369c58STero Kristo CLK_OF_DECLARE(mux_clk, "ti,mux-clock", of_mux_clk_setup);
2296a369c58STero Kristo 
2307c18a65cSTero Kristo struct clk_hw *ti_clk_build_component_mux(struct ti_clk_mux *setup)
2317c18a65cSTero Kristo {
232d83bc5b6STero Kristo 	struct clk_omap_mux *mux;
2337c18a65cSTero Kristo 	int num_parents;
2347c18a65cSTero Kristo 
2357c18a65cSTero Kristo 	if (!setup)
2367c18a65cSTero Kristo 		return NULL;
2377c18a65cSTero Kristo 
2387c18a65cSTero Kristo 	mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2397c18a65cSTero Kristo 	if (!mux)
2407c18a65cSTero Kristo 		return ERR_PTR(-ENOMEM);
2417c18a65cSTero Kristo 
2427c18a65cSTero Kristo 	mux->shift = setup->bit_shift;
243ee2fc3c5STero Kristo 	mux->latch = -EINVAL;
2447c18a65cSTero Kristo 
2456c0afb50STero Kristo 	mux->reg.index = setup->module;
2466c0afb50STero Kristo 	mux->reg.offset = setup->reg;
2477c18a65cSTero Kristo 
2487c18a65cSTero Kristo 	if (setup->flags & CLKF_INDEX_STARTS_AT_ONE)
2497c18a65cSTero Kristo 		mux->flags |= CLK_MUX_INDEX_ONE;
2507c18a65cSTero Kristo 
2517c18a65cSTero Kristo 	num_parents = setup->num_parents;
2527c18a65cSTero Kristo 
2537c18a65cSTero Kristo 	mux->mask = num_parents - 1;
2547c18a65cSTero Kristo 	mux->mask = (1 << fls(mux->mask)) - 1;
2557c18a65cSTero Kristo 
2567c18a65cSTero Kristo 	return &mux->hw;
2577c18a65cSTero Kristo }
2587c18a65cSTero Kristo 
2596a369c58STero Kristo static void __init of_ti_composite_mux_clk_setup(struct device_node *node)
2606a369c58STero Kristo {
261d83bc5b6STero Kristo 	struct clk_omap_mux *mux;
262921bacfaSStephen Boyd 	unsigned int num_parents;
2636a369c58STero Kristo 	u32 val;
2646a369c58STero Kristo 
2656a369c58STero Kristo 	mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2666a369c58STero Kristo 	if (!mux)
2676a369c58STero Kristo 		return;
2686a369c58STero Kristo 
2696c0afb50STero Kristo 	if (ti_clk_get_reg_addr(node, 0, &mux->reg))
2706a369c58STero Kristo 		goto cleanup;
2716a369c58STero Kristo 
2726a369c58STero Kristo 	if (!of_property_read_u32(node, "ti,bit-shift", &val))
2736a369c58STero Kristo 		mux->shift = val;
2746a369c58STero Kristo 
2756a369c58STero Kristo 	if (of_property_read_bool(node, "ti,index-starts-at-one"))
2766a369c58STero Kristo 		mux->flags |= CLK_MUX_INDEX_ONE;
2776a369c58STero Kristo 
2786a369c58STero Kristo 	num_parents = of_clk_get_parent_count(node);
2796a369c58STero Kristo 
2806a369c58STero Kristo 	if (num_parents < 2) {
281*e665f029SRob Herring 		pr_err("%pOFn must have parents\n", node);
2826a369c58STero Kristo 		goto cleanup;
2836a369c58STero Kristo 	}
2846a369c58STero Kristo 
2856a369c58STero Kristo 	mux->mask = num_parents - 1;
2866a369c58STero Kristo 	mux->mask = (1 << fls(mux->mask)) - 1;
2876a369c58STero Kristo 
2886a369c58STero Kristo 	if (!ti_clk_add_component(node, &mux->hw, CLK_COMPONENT_TYPE_MUX))
2896a369c58STero Kristo 		return;
2906a369c58STero Kristo 
2916a369c58STero Kristo cleanup:
2926a369c58STero Kristo 	kfree(mux);
2936a369c58STero Kristo }
2946a369c58STero Kristo CLK_OF_DECLARE(ti_composite_mux_clk_setup, "ti,composite-mux-clock",
2956a369c58STero Kristo 	       of_ti_composite_mux_clk_setup);
296