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 94d6e7bbc1SRuss Dill /** 95d6e7bbc1SRuss Dill * clk_mux_save_context - Save the parent selcted in the mux 96d6e7bbc1SRuss Dill * @hw: pointer struct clk_hw 97d6e7bbc1SRuss Dill * 98d6e7bbc1SRuss Dill * Save the parent mux value. 99d6e7bbc1SRuss Dill */ 100d6e7bbc1SRuss Dill static int clk_mux_save_context(struct clk_hw *hw) 101d6e7bbc1SRuss Dill { 102d6e7bbc1SRuss Dill struct clk_omap_mux *mux = to_clk_omap_mux(hw); 103d6e7bbc1SRuss Dill 104d6e7bbc1SRuss Dill mux->saved_parent = ti_clk_mux_get_parent(hw); 105d6e7bbc1SRuss Dill return 0; 106d6e7bbc1SRuss Dill } 107d6e7bbc1SRuss Dill 108d6e7bbc1SRuss Dill /** 109d6e7bbc1SRuss Dill * clk_mux_restore_context - Restore the parent in the mux 110d6e7bbc1SRuss Dill * @hw: pointer struct clk_hw 111d6e7bbc1SRuss Dill * 112d6e7bbc1SRuss Dill * Restore the saved parent mux value. 113d6e7bbc1SRuss Dill */ 114d6e7bbc1SRuss Dill static void clk_mux_restore_context(struct clk_hw *hw) 115d6e7bbc1SRuss Dill { 116d6e7bbc1SRuss Dill struct clk_omap_mux *mux = to_clk_omap_mux(hw); 117d6e7bbc1SRuss Dill 118d6e7bbc1SRuss Dill ti_clk_mux_set_parent(hw, mux->saved_parent); 119d6e7bbc1SRuss Dill } 120d6e7bbc1SRuss Dill 1216a369c58STero Kristo const struct clk_ops ti_clk_mux_ops = { 1226a369c58STero Kristo .get_parent = ti_clk_mux_get_parent, 1236a369c58STero Kristo .set_parent = ti_clk_mux_set_parent, 1246a369c58STero Kristo .determine_rate = __clk_mux_determine_rate, 125d6e7bbc1SRuss Dill .save_context = clk_mux_save_context, 126d6e7bbc1SRuss Dill .restore_context = clk_mux_restore_context, 1276a369c58STero Kristo }; 1286a369c58STero Kristo 1296a369c58STero Kristo static struct clk *_register_mux(struct device *dev, const char *name, 130ce382d47STero Kristo const char * const *parent_names, 131ce382d47STero Kristo u8 num_parents, unsigned long flags, 1326c0afb50STero Kristo struct clk_omap_reg *reg, u8 shift, u32 mask, 133ee2fc3c5STero Kristo s8 latch, u8 clk_mux_flags, u32 *table) 1346a369c58STero Kristo { 135d83bc5b6STero Kristo struct clk_omap_mux *mux; 1366a369c58STero Kristo struct clk *clk; 1376a369c58STero Kristo struct clk_init_data init; 1386a369c58STero Kristo 1396a369c58STero Kristo /* allocate the mux */ 1406a369c58STero Kristo mux = kzalloc(sizeof(*mux), GFP_KERNEL); 141f80c8a29SMarkus Elfring if (!mux) 1426a369c58STero Kristo return ERR_PTR(-ENOMEM); 1436a369c58STero Kristo 1446a369c58STero Kristo init.name = name; 1456a369c58STero Kristo init.ops = &ti_clk_mux_ops; 146*8aa09cf3STero Kristo init.flags = flags; 1476a369c58STero Kristo init.parent_names = parent_names; 1486a369c58STero Kristo init.num_parents = num_parents; 1496a369c58STero Kristo 1506a369c58STero Kristo /* struct clk_mux assignments */ 1516c0afb50STero Kristo memcpy(&mux->reg, reg, sizeof(*reg)); 1526a369c58STero Kristo mux->shift = shift; 1536a369c58STero Kristo mux->mask = mask; 154ee2fc3c5STero Kristo mux->latch = latch; 1556a369c58STero Kristo mux->flags = clk_mux_flags; 1566a369c58STero Kristo mux->table = table; 1576a369c58STero Kristo mux->hw.init = &init; 1586a369c58STero Kristo 1591ae79c46STero Kristo clk = ti_clk_register(dev, &mux->hw, name); 1606a369c58STero Kristo 1616a369c58STero Kristo if (IS_ERR(clk)) 1626a369c58STero Kristo kfree(mux); 1636a369c58STero Kristo 1646a369c58STero Kristo return clk; 1656a369c58STero Kristo } 1666a369c58STero Kristo 1677c18a65cSTero Kristo struct clk *ti_clk_register_mux(struct ti_clk *setup) 1687c18a65cSTero Kristo { 1697c18a65cSTero Kristo struct ti_clk_mux *mux; 1707c18a65cSTero Kristo u32 flags; 1717c18a65cSTero Kristo u8 mux_flags = 0; 1726c0afb50STero Kristo struct clk_omap_reg reg; 1737c18a65cSTero Kristo u32 mask; 1747c18a65cSTero Kristo 1757c18a65cSTero Kristo mux = setup->data; 1767c18a65cSTero Kristo flags = CLK_SET_RATE_NO_REPARENT; 1777c18a65cSTero Kristo 1787c18a65cSTero Kristo mask = mux->num_parents; 1797c18a65cSTero Kristo if (!(mux->flags & CLKF_INDEX_STARTS_AT_ONE)) 1807c18a65cSTero Kristo mask--; 1817c18a65cSTero Kristo 1827c18a65cSTero Kristo mask = (1 << fls(mask)) - 1; 1836c0afb50STero Kristo reg.index = mux->module; 1846c0afb50STero Kristo reg.offset = mux->reg; 1856c0afb50STero Kristo reg.ptr = NULL; 1867c18a65cSTero Kristo 1877c18a65cSTero Kristo if (mux->flags & CLKF_INDEX_STARTS_AT_ONE) 1887c18a65cSTero Kristo mux_flags |= CLK_MUX_INDEX_ONE; 1897c18a65cSTero Kristo 1907c18a65cSTero Kristo if (mux->flags & CLKF_SET_RATE_PARENT) 1917c18a65cSTero Kristo flags |= CLK_SET_RATE_PARENT; 1927c18a65cSTero Kristo 1937c18a65cSTero Kristo return _register_mux(NULL, setup->name, mux->parents, mux->num_parents, 194ee2fc3c5STero Kristo flags, ®, mux->bit_shift, mask, -EINVAL, 195167af5efSGrygorii Strashko mux_flags, NULL); 1967c18a65cSTero Kristo } 1977c18a65cSTero Kristo 1986a369c58STero Kristo /** 1996a369c58STero Kristo * of_mux_clk_setup - Setup function for simple mux rate clock 2006a369c58STero Kristo * @node: DT node for the clock 2016a369c58STero Kristo * 2026a369c58STero Kristo * Sets up a basic clock multiplexer. 2036a369c58STero Kristo */ 2046a369c58STero Kristo static void of_mux_clk_setup(struct device_node *node) 2056a369c58STero Kristo { 2066a369c58STero Kristo struct clk *clk; 2076c0afb50STero Kristo struct clk_omap_reg reg; 208921bacfaSStephen Boyd unsigned int num_parents; 2096a369c58STero Kristo const char **parent_names; 2106a369c58STero Kristo u8 clk_mux_flags = 0; 2116a369c58STero Kristo u32 mask = 0; 2126a369c58STero Kristo u32 shift = 0; 213ee2fc3c5STero Kristo s32 latch = -EINVAL; 2147d5fc85dSTomi Valkeinen u32 flags = CLK_SET_RATE_NO_REPARENT; 2156a369c58STero Kristo 2166a369c58STero Kristo num_parents = of_clk_get_parent_count(node); 2176a369c58STero Kristo if (num_parents < 2) { 218e665f029SRob Herring pr_err("mux-clock %pOFn must have parents\n", node); 2196a369c58STero Kristo return; 2206a369c58STero Kristo } 2216a369c58STero Kristo parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL); 2226a369c58STero Kristo if (!parent_names) 2236a369c58STero Kristo goto cleanup; 2246a369c58STero Kristo 2259da9e761SDinh Nguyen of_clk_parent_fill(node, parent_names, num_parents); 2266a369c58STero Kristo 2276c0afb50STero Kristo if (ti_clk_get_reg_addr(node, 0, ®)) 2286a369c58STero Kristo goto cleanup; 2296a369c58STero Kristo 2306a369c58STero Kristo of_property_read_u32(node, "ti,bit-shift", &shift); 2316a369c58STero Kristo 232ee2fc3c5STero Kristo of_property_read_u32(node, "ti,latch-bit", &latch); 233ee2fc3c5STero Kristo 2346a369c58STero Kristo if (of_property_read_bool(node, "ti,index-starts-at-one")) 2356a369c58STero Kristo clk_mux_flags |= CLK_MUX_INDEX_ONE; 2366a369c58STero Kristo 2376a369c58STero Kristo if (of_property_read_bool(node, "ti,set-rate-parent")) 2386a369c58STero Kristo flags |= CLK_SET_RATE_PARENT; 2396a369c58STero Kristo 2406a369c58STero Kristo /* Generate bit-mask based on parent info */ 2416a369c58STero Kristo mask = num_parents; 2426a369c58STero Kristo if (!(clk_mux_flags & CLK_MUX_INDEX_ONE)) 2436a369c58STero Kristo mask--; 2446a369c58STero Kristo 2456a369c58STero Kristo mask = (1 << fls(mask)) - 1; 2466a369c58STero Kristo 2477c18a65cSTero Kristo clk = _register_mux(NULL, node->name, parent_names, num_parents, 248ee2fc3c5STero Kristo flags, ®, shift, mask, latch, clk_mux_flags, 249ee2fc3c5STero Kristo NULL); 2506a369c58STero Kristo 2516a369c58STero Kristo if (!IS_ERR(clk)) 2526a369c58STero Kristo of_clk_add_provider(node, of_clk_src_simple_get, clk); 2536a369c58STero Kristo 2546a369c58STero Kristo cleanup: 2556a369c58STero Kristo kfree(parent_names); 2566a369c58STero Kristo } 2576a369c58STero Kristo CLK_OF_DECLARE(mux_clk, "ti,mux-clock", of_mux_clk_setup); 2586a369c58STero Kristo 2597c18a65cSTero Kristo struct clk_hw *ti_clk_build_component_mux(struct ti_clk_mux *setup) 2607c18a65cSTero Kristo { 261d83bc5b6STero Kristo struct clk_omap_mux *mux; 2627c18a65cSTero Kristo int num_parents; 2637c18a65cSTero Kristo 2647c18a65cSTero Kristo if (!setup) 2657c18a65cSTero Kristo return NULL; 2667c18a65cSTero Kristo 2677c18a65cSTero Kristo mux = kzalloc(sizeof(*mux), GFP_KERNEL); 2687c18a65cSTero Kristo if (!mux) 2697c18a65cSTero Kristo return ERR_PTR(-ENOMEM); 2707c18a65cSTero Kristo 2717c18a65cSTero Kristo mux->shift = setup->bit_shift; 272ee2fc3c5STero Kristo mux->latch = -EINVAL; 2737c18a65cSTero Kristo 2746c0afb50STero Kristo mux->reg.index = setup->module; 2756c0afb50STero Kristo mux->reg.offset = setup->reg; 2767c18a65cSTero Kristo 2777c18a65cSTero Kristo if (setup->flags & CLKF_INDEX_STARTS_AT_ONE) 2787c18a65cSTero Kristo mux->flags |= CLK_MUX_INDEX_ONE; 2797c18a65cSTero Kristo 2807c18a65cSTero Kristo num_parents = setup->num_parents; 2817c18a65cSTero Kristo 2827c18a65cSTero Kristo mux->mask = num_parents - 1; 2837c18a65cSTero Kristo mux->mask = (1 << fls(mux->mask)) - 1; 2847c18a65cSTero Kristo 2857c18a65cSTero Kristo return &mux->hw; 2867c18a65cSTero Kristo } 2877c18a65cSTero Kristo 2886a369c58STero Kristo static void __init of_ti_composite_mux_clk_setup(struct device_node *node) 2896a369c58STero Kristo { 290d83bc5b6STero Kristo struct clk_omap_mux *mux; 291921bacfaSStephen Boyd unsigned int num_parents; 2926a369c58STero Kristo u32 val; 2936a369c58STero Kristo 2946a369c58STero Kristo mux = kzalloc(sizeof(*mux), GFP_KERNEL); 2956a369c58STero Kristo if (!mux) 2966a369c58STero Kristo return; 2976a369c58STero Kristo 2986c0afb50STero Kristo if (ti_clk_get_reg_addr(node, 0, &mux->reg)) 2996a369c58STero Kristo goto cleanup; 3006a369c58STero Kristo 3016a369c58STero Kristo if (!of_property_read_u32(node, "ti,bit-shift", &val)) 3026a369c58STero Kristo mux->shift = val; 3036a369c58STero Kristo 3046a369c58STero Kristo if (of_property_read_bool(node, "ti,index-starts-at-one")) 3056a369c58STero Kristo mux->flags |= CLK_MUX_INDEX_ONE; 3066a369c58STero Kristo 3076a369c58STero Kristo num_parents = of_clk_get_parent_count(node); 3086a369c58STero Kristo 3096a369c58STero Kristo if (num_parents < 2) { 310e665f029SRob Herring pr_err("%pOFn must have parents\n", node); 3116a369c58STero Kristo goto cleanup; 3126a369c58STero Kristo } 3136a369c58STero Kristo 3146a369c58STero Kristo mux->mask = num_parents - 1; 3156a369c58STero Kristo mux->mask = (1 << fls(mux->mask)) - 1; 3166a369c58STero Kristo 3176a369c58STero Kristo if (!ti_clk_add_component(node, &mux->hw, CLK_COMPONENT_TYPE_MUX)) 3186a369c58STero Kristo return; 3196a369c58STero Kristo 3206a369c58STero Kristo cleanup: 3216a369c58STero Kristo kfree(mux); 3226a369c58STero Kristo } 3236a369c58STero Kristo CLK_OF_DECLARE(ti_composite_mux_clk_setup, "ti,composite-mux-clock", 3246a369c58STero Kristo of_ti_composite_mux_clk_setup); 325