189a3dfb7SMaxime Ripard /* 289a3dfb7SMaxime Ripard * Copyright (C) 2016 Maxime Ripard 389a3dfb7SMaxime Ripard * Maxime Ripard <maxime.ripard@free-electrons.com> 489a3dfb7SMaxime Ripard * 589a3dfb7SMaxime Ripard * This program is free software; you can redistribute it and/or 689a3dfb7SMaxime Ripard * modify it under the terms of the GNU General Public License as 789a3dfb7SMaxime Ripard * published by the Free Software Foundation; either version 2 of 889a3dfb7SMaxime Ripard * the License, or (at your option) any later version. 989a3dfb7SMaxime Ripard */ 1089a3dfb7SMaxime Ripard 1189a3dfb7SMaxime Ripard #include <linux/clk-provider.h> 12*62e59c4eSStephen Boyd #include <linux/io.h> 1389a3dfb7SMaxime Ripard #include <linux/spinlock.h> 1489a3dfb7SMaxime Ripard 1589a3dfb7SMaxime Ripard #include "ccu_frac.h" 1689a3dfb7SMaxime Ripard 1789a3dfb7SMaxime Ripard bool ccu_frac_helper_is_enabled(struct ccu_common *common, 18a501a14eSMaxime Ripard struct ccu_frac_internal *cf) 1989a3dfb7SMaxime Ripard { 2089a3dfb7SMaxime Ripard if (!(common->features & CCU_FEATURE_FRACTIONAL)) 2189a3dfb7SMaxime Ripard return false; 2289a3dfb7SMaxime Ripard 2389a3dfb7SMaxime Ripard return !(readl(common->base + common->reg) & cf->enable); 2489a3dfb7SMaxime Ripard } 2589a3dfb7SMaxime Ripard 2689a3dfb7SMaxime Ripard void ccu_frac_helper_enable(struct ccu_common *common, 27a501a14eSMaxime Ripard struct ccu_frac_internal *cf) 2889a3dfb7SMaxime Ripard { 2989a3dfb7SMaxime Ripard unsigned long flags; 3089a3dfb7SMaxime Ripard u32 reg; 3189a3dfb7SMaxime Ripard 3289a3dfb7SMaxime Ripard if (!(common->features & CCU_FEATURE_FRACTIONAL)) 3389a3dfb7SMaxime Ripard return; 3489a3dfb7SMaxime Ripard 3589a3dfb7SMaxime Ripard spin_lock_irqsave(common->lock, flags); 3689a3dfb7SMaxime Ripard reg = readl(common->base + common->reg); 3789a3dfb7SMaxime Ripard writel(reg & ~cf->enable, common->base + common->reg); 3889a3dfb7SMaxime Ripard spin_unlock_irqrestore(common->lock, flags); 3989a3dfb7SMaxime Ripard } 4089a3dfb7SMaxime Ripard 4189a3dfb7SMaxime Ripard void ccu_frac_helper_disable(struct ccu_common *common, 42a501a14eSMaxime Ripard struct ccu_frac_internal *cf) 4389a3dfb7SMaxime Ripard { 4489a3dfb7SMaxime Ripard unsigned long flags; 4589a3dfb7SMaxime Ripard u32 reg; 4689a3dfb7SMaxime Ripard 4789a3dfb7SMaxime Ripard if (!(common->features & CCU_FEATURE_FRACTIONAL)) 4889a3dfb7SMaxime Ripard return; 4989a3dfb7SMaxime Ripard 5089a3dfb7SMaxime Ripard spin_lock_irqsave(common->lock, flags); 5189a3dfb7SMaxime Ripard reg = readl(common->base + common->reg); 5289a3dfb7SMaxime Ripard writel(reg | cf->enable, common->base + common->reg); 5389a3dfb7SMaxime Ripard spin_unlock_irqrestore(common->lock, flags); 5489a3dfb7SMaxime Ripard } 5589a3dfb7SMaxime Ripard 5689a3dfb7SMaxime Ripard bool ccu_frac_helper_has_rate(struct ccu_common *common, 57a501a14eSMaxime Ripard struct ccu_frac_internal *cf, 5889a3dfb7SMaxime Ripard unsigned long rate) 5989a3dfb7SMaxime Ripard { 6089a3dfb7SMaxime Ripard if (!(common->features & CCU_FEATURE_FRACTIONAL)) 6189a3dfb7SMaxime Ripard return false; 6289a3dfb7SMaxime Ripard 6389a3dfb7SMaxime Ripard return (cf->rates[0] == rate) || (cf->rates[1] == rate); 6489a3dfb7SMaxime Ripard } 6589a3dfb7SMaxime Ripard 6689a3dfb7SMaxime Ripard unsigned long ccu_frac_helper_read_rate(struct ccu_common *common, 67a501a14eSMaxime Ripard struct ccu_frac_internal *cf) 6889a3dfb7SMaxime Ripard { 6989a3dfb7SMaxime Ripard u32 reg; 7089a3dfb7SMaxime Ripard 71b655f36eSJernej Škrabec pr_debug("%s: Read fractional\n", clk_hw_get_name(&common->hw)); 7289a3dfb7SMaxime Ripard 7389a3dfb7SMaxime Ripard if (!(common->features & CCU_FEATURE_FRACTIONAL)) 7489a3dfb7SMaxime Ripard return 0; 7589a3dfb7SMaxime Ripard 76b655f36eSJernej Škrabec pr_debug("%s: clock is fractional (rates %lu and %lu)\n", 7789a3dfb7SMaxime Ripard clk_hw_get_name(&common->hw), cf->rates[0], cf->rates[1]); 7889a3dfb7SMaxime Ripard 7989a3dfb7SMaxime Ripard reg = readl(common->base + common->reg); 8089a3dfb7SMaxime Ripard 81b655f36eSJernej Škrabec pr_debug("%s: clock reg is 0x%x (select is 0x%x)\n", 8289a3dfb7SMaxime Ripard clk_hw_get_name(&common->hw), reg, cf->select); 8389a3dfb7SMaxime Ripard 8489a3dfb7SMaxime Ripard return (reg & cf->select) ? cf->rates[1] : cf->rates[0]; 8589a3dfb7SMaxime Ripard } 8689a3dfb7SMaxime Ripard 8789a3dfb7SMaxime Ripard int ccu_frac_helper_set_rate(struct ccu_common *common, 88a501a14eSMaxime Ripard struct ccu_frac_internal *cf, 891d42460aSJernej Škrabec unsigned long rate, u32 lock) 9089a3dfb7SMaxime Ripard { 9189a3dfb7SMaxime Ripard unsigned long flags; 9289a3dfb7SMaxime Ripard u32 reg, sel; 9389a3dfb7SMaxime Ripard 9489a3dfb7SMaxime Ripard if (!(common->features & CCU_FEATURE_FRACTIONAL)) 9589a3dfb7SMaxime Ripard return -EINVAL; 9689a3dfb7SMaxime Ripard 9789a3dfb7SMaxime Ripard if (cf->rates[0] == rate) 9889a3dfb7SMaxime Ripard sel = 0; 9989a3dfb7SMaxime Ripard else if (cf->rates[1] == rate) 10089a3dfb7SMaxime Ripard sel = cf->select; 10189a3dfb7SMaxime Ripard else 10289a3dfb7SMaxime Ripard return -EINVAL; 10389a3dfb7SMaxime Ripard 10489a3dfb7SMaxime Ripard spin_lock_irqsave(common->lock, flags); 10589a3dfb7SMaxime Ripard reg = readl(common->base + common->reg); 10689a3dfb7SMaxime Ripard reg &= ~cf->select; 10789a3dfb7SMaxime Ripard writel(reg | sel, common->base + common->reg); 10889a3dfb7SMaxime Ripard spin_unlock_irqrestore(common->lock, flags); 10989a3dfb7SMaxime Ripard 1101d42460aSJernej Škrabec ccu_helper_wait_for_lock(common, lock); 1111d42460aSJernej Škrabec 11289a3dfb7SMaxime Ripard return 0; 11389a3dfb7SMaxime Ripard } 114