cregit-Linux how code gets into the kernel

Release 4.14 drivers/clk/clk-fractional-divider.c

Directory: drivers/clk
/*
 * Copyright (C) 2014 Intel Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * Adjustable fractional divider clock implementation.
 * Output rate = (m / n) * parent_rate.
 * Uses rational best approximation algorithm.
 */

#include <linux/clk-provider.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/slab.h>
#include <linux/rational.h>


static unsigned long clk_fd_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) { struct clk_fractional_divider *fd = to_clk_fd(hw); unsigned long flags = 0; unsigned long m, n; u32 val; u64 ret; if (fd->lock) spin_lock_irqsave(fd->lock, flags); else __acquire(fd->lock); val = clk_readl(fd->reg); if (fd->lock) spin_unlock_irqrestore(fd->lock, flags); else __release(fd->lock); m = (val & fd->mmask) >> fd->mshift; n = (val & fd->nmask) >> fd->nshift; if (!n || !m) return parent_rate; ret = (u64)parent_rate * m; do_div(ret, n); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Heikki Krogerus13384.71%240.00%
Stephen Boyd1610.19%120.00%
Andy Shevchenko53.18%120.00%
Heiko Stübner31.91%120.00%
Total157100.00%5100.00%


static void clk_fd_general_approximation(struct clk_hw *hw, unsigned long rate, unsigned long *parent_rate, unsigned long *m, unsigned long *n) { struct clk_fractional_divider *fd = to_clk_fd(hw); unsigned long scale; /* * Get rate closer to *parent_rate to guarantee there is no overflow * for m and n. In the result it will be the nearest rate left shifted * by (scale - fd->nwidth) bits. */ scale = fls_long(*parent_rate / rate - 1); if (scale > fd->nwidth) rate <<= scale - fd->nwidth; rational_best_approximation(rate, *parent_rate, GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), m, n); }

Contributors

PersonTokensPropCommitsCommitProp
Andy Shevchenko4844.86%250.00%
Heikki Krogerus4138.32%125.00%
Elaine Zhang1816.82%125.00%
Total107100.00%4100.00%


static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate, unsigned long *parent_rate) { struct clk_fractional_divider *fd = to_clk_fd(hw); unsigned long m, n; u64 ret; if (!rate || rate >= *parent_rate) return *parent_rate; if (fd->approximation) fd->approximation(hw, rate, parent_rate, &m, &n); else clk_fd_general_approximation(hw, rate, parent_rate, &m, &n); ret = (u64)*parent_rate * m; do_div(ret, n); return ret; }

Contributors

PersonTokensPropCommitsCommitProp
Elaine Zhang8475.00%133.33%
Andy Shevchenko2421.43%133.33%
Heikki Krogerus43.57%133.33%
Total112100.00%3100.00%


static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long parent_rate) { struct clk_fractional_divider *fd = to_clk_fd(hw); unsigned long flags = 0; unsigned long m, n; u32 val; rational_best_approximation(rate, parent_rate, GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), &m, &n); if (fd->lock) spin_lock_irqsave(fd->lock, flags); else __acquire(fd->lock); val = clk_readl(fd->reg); val &= ~(fd->mmask | fd->nmask); val |= (m << fd->mshift) | (n << fd->nshift); clk_writel(val, fd->reg); if (fd->lock) spin_unlock_irqrestore(fd->lock, flags); else __release(fd->lock); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Heikki Krogerus13174.01%133.33%
Andy Shevchenko3016.95%133.33%
Stephen Boyd169.04%133.33%
Total177100.00%3100.00%

const struct clk_ops clk_fractional_divider_ops = { .recalc_rate = clk_fd_recalc_rate, .round_rate = clk_fd_round_rate, .set_rate = clk_fd_set_rate, }; EXPORT_SYMBOL_GPL(clk_fractional_divider_ops);
struct clk_hw *clk_hw_register_fractional_divider(struct device *dev, const char *name, const char *parent_name, unsigned long flags, void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth, u8 clk_divider_flags, spinlock_t *lock) { struct clk_fractional_divider *fd; struct clk_init_data init; struct clk_hw *hw; int ret; fd = kzalloc(sizeof(*fd), GFP_KERNEL); if (!fd) return ERR_PTR(-ENOMEM); init.name = name; init.ops = &clk_fractional_divider_ops; init.flags = flags | CLK_IS_BASIC; init.parent_names = parent_name ? &parent_name : NULL; init.num_parents = parent_name ? 1 : 0; fd->reg = reg; fd->mshift = mshift; fd->mwidth = mwidth; fd->mmask = GENMASK(mwidth - 1, 0) << mshift; fd->nshift = nshift; fd->nwidth = nwidth; fd->nmask = GENMASK(nwidth - 1, 0) << nshift; fd->flags = clk_divider_flags; fd->lock = lock; fd->hw.init = &init; hw = &fd->hw; ret = clk_hw_register(dev, hw); if (ret) { kfree(fd); hw = ERR_PTR(ret); } return hw; }

Contributors

PersonTokensPropCommitsCommitProp
Heikki Krogerus19376.59%133.33%
Stephen Boyd3915.48%133.33%
Andy Shevchenko207.94%133.33%
Total252100.00%3100.00%

EXPORT_SYMBOL_GPL(clk_hw_register_fractional_divider);
struct clk *clk_register_fractional_divider(struct device *dev, const char *name, const char *parent_name, unsigned long flags, void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth, u8 clk_divider_flags, spinlock_t *lock) { struct clk_hw *hw; hw = clk_hw_register_fractional_divider(dev, name, parent_name, flags, reg, mshift, mwidth, nshift, nwidth, clk_divider_flags, lock); if (IS_ERR(hw)) return ERR_CAST(hw); return hw->clk; }

Contributors

PersonTokensPropCommitsCommitProp
Stephen Boyd8686.00%150.00%
Heikki Krogerus1414.00%150.00%
Total100100.00%2100.00%

EXPORT_SYMBOL_GPL(clk_register_fractional_divider);
void clk_hw_unregister_fractional_divider(struct clk_hw *hw) { struct clk_fractional_divider *fd; fd = to_clk_fd(hw); clk_hw_unregister(hw); kfree(fd); }

Contributors

PersonTokensPropCommitsCommitProp
Stephen Boyd32100.00%1100.00%
Total32100.00%1100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
Heikki Krogerus56256.77%222.22%
Stephen Boyd19419.60%222.22%
Andy Shevchenko12913.03%333.33%
Elaine Zhang10210.30%111.11%
Heiko Stübner30.30%111.11%
Total990100.00%9100.00%
Directory: drivers/clk
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.