Bluetooth: Remove unneeded initializer for management command table
[linux/fpc-iii.git] / drivers / clk / clk-fractional-divider.c
blob82a59d0086cc79d654c4ceaf4aa42a8491467dbc
1 /*
2 * Copyright (C) 2014 Intel Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
8 * Adjustable fractional divider clock implementation.
9 * Output rate = (m / n) * parent_rate.
12 #include <linux/clk-provider.h>
13 #include <linux/module.h>
14 #include <linux/device.h>
15 #include <linux/slab.h>
16 #include <linux/gcd.h>
18 #define to_clk_fd(_hw) container_of(_hw, struct clk_fractional_divider, hw)
20 static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
21 unsigned long parent_rate)
23 struct clk_fractional_divider *fd = to_clk_fd(hw);
24 unsigned long flags = 0;
25 u32 val, m, n;
26 u64 ret;
28 if (fd->lock)
29 spin_lock_irqsave(fd->lock, flags);
31 val = clk_readl(fd->reg);
33 if (fd->lock)
34 spin_unlock_irqrestore(fd->lock, flags);
36 m = (val & fd->mmask) >> fd->mshift;
37 n = (val & fd->nmask) >> fd->nshift;
39 ret = (u64)parent_rate * m;
40 do_div(ret, n);
42 return ret;
45 static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
46 unsigned long *prate)
48 struct clk_fractional_divider *fd = to_clk_fd(hw);
49 unsigned maxn = (fd->nmask >> fd->nshift) + 1;
50 unsigned div;
52 if (!rate || rate >= *prate)
53 return *prate;
55 div = gcd(*prate, rate);
57 while ((*prate / div) > maxn) {
58 div <<= 1;
59 rate <<= 1;
62 return rate;
65 static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
66 unsigned long parent_rate)
68 struct clk_fractional_divider *fd = to_clk_fd(hw);
69 unsigned long flags = 0;
70 unsigned long div;
71 unsigned n, m;
72 u32 val;
74 div = gcd(parent_rate, rate);
75 m = rate / div;
76 n = parent_rate / div;
78 if (fd->lock)
79 spin_lock_irqsave(fd->lock, flags);
81 val = clk_readl(fd->reg);
82 val &= ~(fd->mmask | fd->nmask);
83 val |= (m << fd->mshift) | (n << fd->nshift);
84 clk_writel(val, fd->reg);
86 if (fd->lock)
87 spin_unlock_irqrestore(fd->lock, flags);
89 return 0;
92 const struct clk_ops clk_fractional_divider_ops = {
93 .recalc_rate = clk_fd_recalc_rate,
94 .round_rate = clk_fd_round_rate,
95 .set_rate = clk_fd_set_rate,
97 EXPORT_SYMBOL_GPL(clk_fractional_divider_ops);
99 struct clk *clk_register_fractional_divider(struct device *dev,
100 const char *name, const char *parent_name, unsigned long flags,
101 void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
102 u8 clk_divider_flags, spinlock_t *lock)
104 struct clk_fractional_divider *fd;
105 struct clk_init_data init;
106 struct clk *clk;
108 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
109 if (!fd) {
110 dev_err(dev, "could not allocate fractional divider clk\n");
111 return ERR_PTR(-ENOMEM);
114 init.name = name;
115 init.ops = &clk_fractional_divider_ops;
116 init.flags = flags | CLK_IS_BASIC;
117 init.parent_names = parent_name ? &parent_name : NULL;
118 init.num_parents = parent_name ? 1 : 0;
120 fd->reg = reg;
121 fd->mshift = mshift;
122 fd->mmask = (BIT(mwidth) - 1) << mshift;
123 fd->nshift = nshift;
124 fd->nmask = (BIT(nwidth) - 1) << nshift;
125 fd->flags = clk_divider_flags;
126 fd->lock = lock;
127 fd->hw.init = &init;
129 clk = clk_register(dev, &fd->hw);
130 if (IS_ERR(clk))
131 kfree(fd);
133 return clk;
135 EXPORT_SYMBOL_GPL(clk_register_fractional_divider);