xref: /linux/drivers/clk/ti/mux.c (revision f80c8a29f94faf6d4a2a3b25d4473d62edbd7aab)
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);
896a369c58STero Kristo 
906a369c58STero Kristo 	return 0;
916a369c58STero Kristo }
926a369c58STero Kristo 
936a369c58STero Kristo const struct clk_ops ti_clk_mux_ops = {
946a369c58STero Kristo 	.get_parent = ti_clk_mux_get_parent,
956a369c58STero Kristo 	.set_parent = ti_clk_mux_set_parent,
966a369c58STero Kristo 	.determine_rate = __clk_mux_determine_rate,
976a369c58STero Kristo };
986a369c58STero Kristo 
996a369c58STero Kristo static struct clk *_register_mux(struct device *dev, const char *name,
100ce382d47STero Kristo 				 const char * const *parent_names,
101ce382d47STero Kristo 				 u8 num_parents, unsigned long flags,
1026c0afb50STero Kristo 				 struct clk_omap_reg *reg, u8 shift, u32 mask,
103ce382d47STero Kristo 				 u8 clk_mux_flags, u32 *table)
1046a369c58STero Kristo {
105d83bc5b6STero Kristo 	struct clk_omap_mux *mux;
1066a369c58STero Kristo 	struct clk *clk;
1076a369c58STero Kristo 	struct clk_init_data init;
1086a369c58STero Kristo 
1096a369c58STero Kristo 	/* allocate the mux */
1106a369c58STero Kristo 	mux = kzalloc(sizeof(*mux), GFP_KERNEL);
111*f80c8a29SMarkus Elfring 	if (!mux)
1126a369c58STero Kristo 		return ERR_PTR(-ENOMEM);
1136a369c58STero Kristo 
1146a369c58STero Kristo 	init.name = name;
1156a369c58STero Kristo 	init.ops = &ti_clk_mux_ops;
1166a369c58STero Kristo 	init.flags = flags | CLK_IS_BASIC;
1176a369c58STero Kristo 	init.parent_names = parent_names;
1186a369c58STero Kristo 	init.num_parents = num_parents;
1196a369c58STero Kristo 
1206a369c58STero Kristo 	/* struct clk_mux assignments */
1216c0afb50STero Kristo 	memcpy(&mux->reg, reg, sizeof(*reg));
1226a369c58STero Kristo 	mux->shift = shift;
1236a369c58STero Kristo 	mux->mask = mask;
1246a369c58STero Kristo 	mux->flags = clk_mux_flags;
1256a369c58STero Kristo 	mux->table = table;
1266a369c58STero Kristo 	mux->hw.init = &init;
1276a369c58STero Kristo 
1281ae79c46STero Kristo 	clk = ti_clk_register(dev, &mux->hw, name);
1296a369c58STero Kristo 
1306a369c58STero Kristo 	if (IS_ERR(clk))
1316a369c58STero Kristo 		kfree(mux);
1326a369c58STero Kristo 
1336a369c58STero Kristo 	return clk;
1346a369c58STero Kristo }
1356a369c58STero Kristo 
1367c18a65cSTero Kristo struct clk *ti_clk_register_mux(struct ti_clk *setup)
1377c18a65cSTero Kristo {
1387c18a65cSTero Kristo 	struct ti_clk_mux *mux;
1397c18a65cSTero Kristo 	u32 flags;
1407c18a65cSTero Kristo 	u8 mux_flags = 0;
1416c0afb50STero Kristo 	struct clk_omap_reg reg;
1427c18a65cSTero Kristo 	u32 mask;
1437c18a65cSTero Kristo 
1447c18a65cSTero Kristo 	mux = setup->data;
1457c18a65cSTero Kristo 	flags = CLK_SET_RATE_NO_REPARENT;
1467c18a65cSTero Kristo 
1477c18a65cSTero Kristo 	mask = mux->num_parents;
1487c18a65cSTero Kristo 	if (!(mux->flags & CLKF_INDEX_STARTS_AT_ONE))
1497c18a65cSTero Kristo 		mask--;
1507c18a65cSTero Kristo 
1517c18a65cSTero Kristo 	mask = (1 << fls(mask)) - 1;
1526c0afb50STero Kristo 	reg.index = mux->module;
1536c0afb50STero Kristo 	reg.offset = mux->reg;
1546c0afb50STero Kristo 	reg.ptr = NULL;
1557c18a65cSTero Kristo 
1567c18a65cSTero Kristo 	if (mux->flags & CLKF_INDEX_STARTS_AT_ONE)
1577c18a65cSTero Kristo 		mux_flags |= CLK_MUX_INDEX_ONE;
1587c18a65cSTero Kristo 
1597c18a65cSTero Kristo 	if (mux->flags & CLKF_SET_RATE_PARENT)
1607c18a65cSTero Kristo 		flags |= CLK_SET_RATE_PARENT;
1617c18a65cSTero Kristo 
1627c18a65cSTero Kristo 	return _register_mux(NULL, setup->name, mux->parents, mux->num_parents,
1636c0afb50STero Kristo 			     flags, &reg, mux->bit_shift, mask,
164167af5efSGrygorii Strashko 			     mux_flags, NULL);
1657c18a65cSTero Kristo }
1667c18a65cSTero Kristo 
1676a369c58STero Kristo /**
1686a369c58STero Kristo  * of_mux_clk_setup - Setup function for simple mux rate clock
1696a369c58STero Kristo  * @node: DT node for the clock
1706a369c58STero Kristo  *
1716a369c58STero Kristo  * Sets up a basic clock multiplexer.
1726a369c58STero Kristo  */
1736a369c58STero Kristo static void of_mux_clk_setup(struct device_node *node)
1746a369c58STero Kristo {
1756a369c58STero Kristo 	struct clk *clk;
1766c0afb50STero Kristo 	struct clk_omap_reg reg;
177921bacfaSStephen Boyd 	unsigned int num_parents;
1786a369c58STero Kristo 	const char **parent_names;
1796a369c58STero Kristo 	u8 clk_mux_flags = 0;
1806a369c58STero Kristo 	u32 mask = 0;
1816a369c58STero Kristo 	u32 shift = 0;
1827d5fc85dSTomi Valkeinen 	u32 flags = CLK_SET_RATE_NO_REPARENT;
1836a369c58STero Kristo 
1846a369c58STero Kristo 	num_parents = of_clk_get_parent_count(node);
1856a369c58STero Kristo 	if (num_parents < 2) {
1866a369c58STero Kristo 		pr_err("mux-clock %s must have parents\n", node->name);
1876a369c58STero Kristo 		return;
1886a369c58STero Kristo 	}
1896a369c58STero Kristo 	parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL);
1906a369c58STero Kristo 	if (!parent_names)
1916a369c58STero Kristo 		goto cleanup;
1926a369c58STero Kristo 
1939da9e761SDinh Nguyen 	of_clk_parent_fill(node, parent_names, num_parents);
1946a369c58STero Kristo 
1956c0afb50STero Kristo 	if (ti_clk_get_reg_addr(node, 0, &reg))
1966a369c58STero Kristo 		goto cleanup;
1976a369c58STero Kristo 
1986a369c58STero Kristo 	of_property_read_u32(node, "ti,bit-shift", &shift);
1996a369c58STero Kristo 
2006a369c58STero Kristo 	if (of_property_read_bool(node, "ti,index-starts-at-one"))
2016a369c58STero Kristo 		clk_mux_flags |= CLK_MUX_INDEX_ONE;
2026a369c58STero Kristo 
2036a369c58STero Kristo 	if (of_property_read_bool(node, "ti,set-rate-parent"))
2046a369c58STero Kristo 		flags |= CLK_SET_RATE_PARENT;
2056a369c58STero Kristo 
2066a369c58STero Kristo 	/* Generate bit-mask based on parent info */
2076a369c58STero Kristo 	mask = num_parents;
2086a369c58STero Kristo 	if (!(clk_mux_flags & CLK_MUX_INDEX_ONE))
2096a369c58STero Kristo 		mask--;
2106a369c58STero Kristo 
2116a369c58STero Kristo 	mask = (1 << fls(mask)) - 1;
2126a369c58STero Kristo 
2137c18a65cSTero Kristo 	clk = _register_mux(NULL, node->name, parent_names, num_parents,
2146c0afb50STero Kristo 			    flags, &reg, shift, mask, clk_mux_flags, NULL);
2156a369c58STero Kristo 
2166a369c58STero Kristo 	if (!IS_ERR(clk))
2176a369c58STero Kristo 		of_clk_add_provider(node, of_clk_src_simple_get, clk);
2186a369c58STero Kristo 
2196a369c58STero Kristo cleanup:
2206a369c58STero Kristo 	kfree(parent_names);
2216a369c58STero Kristo }
2226a369c58STero Kristo CLK_OF_DECLARE(mux_clk, "ti,mux-clock", of_mux_clk_setup);
2236a369c58STero Kristo 
2247c18a65cSTero Kristo struct clk_hw *ti_clk_build_component_mux(struct ti_clk_mux *setup)
2257c18a65cSTero Kristo {
226d83bc5b6STero Kristo 	struct clk_omap_mux *mux;
2277c18a65cSTero Kristo 	int num_parents;
2287c18a65cSTero Kristo 
2297c18a65cSTero Kristo 	if (!setup)
2307c18a65cSTero Kristo 		return NULL;
2317c18a65cSTero Kristo 
2327c18a65cSTero Kristo 	mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2337c18a65cSTero Kristo 	if (!mux)
2347c18a65cSTero Kristo 		return ERR_PTR(-ENOMEM);
2357c18a65cSTero Kristo 
2367c18a65cSTero Kristo 	mux->shift = setup->bit_shift;
2377c18a65cSTero Kristo 
2386c0afb50STero Kristo 	mux->reg.index = setup->module;
2396c0afb50STero Kristo 	mux->reg.offset = setup->reg;
2407c18a65cSTero Kristo 
2417c18a65cSTero Kristo 	if (setup->flags & CLKF_INDEX_STARTS_AT_ONE)
2427c18a65cSTero Kristo 		mux->flags |= CLK_MUX_INDEX_ONE;
2437c18a65cSTero Kristo 
2447c18a65cSTero Kristo 	num_parents = setup->num_parents;
2457c18a65cSTero Kristo 
2467c18a65cSTero Kristo 	mux->mask = num_parents - 1;
2477c18a65cSTero Kristo 	mux->mask = (1 << fls(mux->mask)) - 1;
2487c18a65cSTero Kristo 
2497c18a65cSTero Kristo 	return &mux->hw;
2507c18a65cSTero Kristo }
2517c18a65cSTero Kristo 
2526a369c58STero Kristo static void __init of_ti_composite_mux_clk_setup(struct device_node *node)
2536a369c58STero Kristo {
254d83bc5b6STero Kristo 	struct clk_omap_mux *mux;
255921bacfaSStephen Boyd 	unsigned int num_parents;
2566a369c58STero Kristo 	u32 val;
2576a369c58STero Kristo 
2586a369c58STero Kristo 	mux = kzalloc(sizeof(*mux), GFP_KERNEL);
2596a369c58STero Kristo 	if (!mux)
2606a369c58STero Kristo 		return;
2616a369c58STero Kristo 
2626c0afb50STero Kristo 	if (ti_clk_get_reg_addr(node, 0, &mux->reg))
2636a369c58STero Kristo 		goto cleanup;
2646a369c58STero Kristo 
2656a369c58STero Kristo 	if (!of_property_read_u32(node, "ti,bit-shift", &val))
2666a369c58STero Kristo 		mux->shift = val;
2676a369c58STero Kristo 
2686a369c58STero Kristo 	if (of_property_read_bool(node, "ti,index-starts-at-one"))
2696a369c58STero Kristo 		mux->flags |= CLK_MUX_INDEX_ONE;
2706a369c58STero Kristo 
2716a369c58STero Kristo 	num_parents = of_clk_get_parent_count(node);
2726a369c58STero Kristo 
2736a369c58STero Kristo 	if (num_parents < 2) {
2746a369c58STero Kristo 		pr_err("%s must have parents\n", node->name);
2756a369c58STero Kristo 		goto cleanup;
2766a369c58STero Kristo 	}
2776a369c58STero Kristo 
2786a369c58STero Kristo 	mux->mask = num_parents - 1;
2796a369c58STero Kristo 	mux->mask = (1 << fls(mux->mask)) - 1;
2806a369c58STero Kristo 
2816a369c58STero Kristo 	if (!ti_clk_add_component(node, &mux->hw, CLK_COMPONENT_TYPE_MUX))
2826a369c58STero Kristo 		return;
2836a369c58STero Kristo 
2846a369c58STero Kristo cleanup:
2856a369c58STero Kristo 	kfree(mux);
2866a369c58STero Kristo }
2876a369c58STero Kristo CLK_OF_DECLARE(ti_composite_mux_clk_setup, "ti,composite-mux-clock",
2886a369c58STero Kristo 	       of_ti_composite_mux_clk_setup);
289