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; 1468aa09cf3STero 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 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; 179*ed06099cSTony Lindgren const char *name; 1806a369c58STero Kristo u8 clk_mux_flags = 0; 1816a369c58STero Kristo u32 mask = 0; 1826a369c58STero Kristo u32 shift = 0; 183ee2fc3c5STero Kristo s32 latch = -EINVAL; 1847d5fc85dSTomi Valkeinen u32 flags = CLK_SET_RATE_NO_REPARENT; 1856a369c58STero Kristo 1866a369c58STero Kristo num_parents = of_clk_get_parent_count(node); 1876a369c58STero Kristo if (num_parents < 2) { 188e665f029SRob Herring pr_err("mux-clock %pOFn must have parents\n", node); 1896a369c58STero Kristo return; 1906a369c58STero Kristo } 1916a369c58STero Kristo parent_names = kzalloc((sizeof(char *) * num_parents), GFP_KERNEL); 1926a369c58STero Kristo if (!parent_names) 1936a369c58STero Kristo goto cleanup; 1946a369c58STero Kristo 1959da9e761SDinh Nguyen of_clk_parent_fill(node, parent_names, num_parents); 1966a369c58STero Kristo 1976c0afb50STero Kristo if (ti_clk_get_reg_addr(node, 0, ®)) 1986a369c58STero Kristo goto cleanup; 1996a369c58STero Kristo 2006a369c58STero Kristo of_property_read_u32(node, "ti,bit-shift", &shift); 2016a369c58STero Kristo 202ee2fc3c5STero Kristo of_property_read_u32(node, "ti,latch-bit", &latch); 203ee2fc3c5STero Kristo 2046a369c58STero Kristo if (of_property_read_bool(node, "ti,index-starts-at-one")) 2056a369c58STero Kristo clk_mux_flags |= CLK_MUX_INDEX_ONE; 2066a369c58STero Kristo 2076a369c58STero Kristo if (of_property_read_bool(node, "ti,set-rate-parent")) 2086a369c58STero Kristo flags |= CLK_SET_RATE_PARENT; 2096a369c58STero Kristo 2106a369c58STero Kristo /* Generate bit-mask based on parent info */ 2116a369c58STero Kristo mask = num_parents; 2126a369c58STero Kristo if (!(clk_mux_flags & CLK_MUX_INDEX_ONE)) 2136a369c58STero Kristo mask--; 2146a369c58STero Kristo 2156a369c58STero Kristo mask = (1 << fls(mask)) - 1; 2166a369c58STero Kristo 217*ed06099cSTony Lindgren name = ti_dt_clk_name(node); 218*ed06099cSTony Lindgren clk = _register_mux(NULL, name, parent_names, num_parents, 219ee2fc3c5STero Kristo flags, ®, 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) { 281e665f029SRob 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