blob: 1abcd76b4993805f2b135b92e5bc285b10de5c95 [file] [log] [blame]
Heikki Krogeruse2d0e902014-05-15 16:40:25 +03001/*
2 * Copyright (C) 2014 Intel Corporation
3 *
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.
7 *
8 * Adjustable fractional divider clock implementation.
9 * Output rate = (m / n) * parent_rate.
Andy Shevchenko07775912015-09-22 18:54:11 +030010 * Uses rational best approximation algorithm.
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030011 */
12
13#include <linux/clk-provider.h>
14#include <linux/module.h>
15#include <linux/device.h>
16#include <linux/slab.h>
Andy Shevchenko07775912015-09-22 18:54:11 +030017#include <linux/rational.h>
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030018
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030019static unsigned long clk_fd_recalc_rate(struct clk_hw *hw,
20 unsigned long parent_rate)
21{
22 struct clk_fractional_divider *fd = to_clk_fd(hw);
23 unsigned long flags = 0;
Andy Shevchenko07775912015-09-22 18:54:11 +030024 unsigned long m, n;
25 u32 val;
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030026 u64 ret;
27
28 if (fd->lock)
29 spin_lock_irqsave(fd->lock, flags);
Stephen Boyd661e2182015-07-24 12:21:12 -070030 else
31 __acquire(fd->lock);
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030032
33 val = clk_readl(fd->reg);
34
35 if (fd->lock)
36 spin_unlock_irqrestore(fd->lock, flags);
Stephen Boyd661e2182015-07-24 12:21:12 -070037 else
38 __release(fd->lock);
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030039
40 m = (val & fd->mmask) >> fd->mshift;
41 n = (val & fd->nmask) >> fd->nshift;
42
Heikki Krogerus6b547832015-02-02 15:37:04 +020043 if (!n || !m)
44 return parent_rate;
45
Heiko Stübnerfeaefa02014-08-28 12:46:10 +020046 ret = (u64)parent_rate * m;
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030047 do_div(ret, n);
48
49 return ret;
50}
51
52static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate,
Andy Shevchenkof7f087c2015-09-22 18:54:08 +030053 unsigned long *parent_rate)
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030054{
55 struct clk_fractional_divider *fd = to_clk_fd(hw);
Andy Shevchenko07775912015-09-22 18:54:11 +030056 unsigned long scale;
57 unsigned long m, n;
58 u64 ret;
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030059
Andy Shevchenkof7f087c2015-09-22 18:54:08 +030060 if (!rate || rate >= *parent_rate)
61 return *parent_rate;
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030062
Andy Shevchenko07775912015-09-22 18:54:11 +030063 /*
64 * Get rate closer to *parent_rate to guarantee there is no overflow
65 * for m and n. In the result it will be the nearest rate left shifted
66 * by (scale - fd->nwidth) bits.
67 */
68 scale = fls_long(*parent_rate / rate - 1);
69 if (scale > fd->nwidth)
70 rate <<= scale - fd->nwidth;
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030071
Andy Shevchenko07775912015-09-22 18:54:11 +030072 rational_best_approximation(rate, *parent_rate,
73 GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
74 &m, &n);
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030075
Andy Shevchenko07775912015-09-22 18:54:11 +030076 ret = (u64)*parent_rate * m;
77 do_div(ret, n);
78
79 return ret;
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030080}
81
82static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate,
83 unsigned long parent_rate)
84{
85 struct clk_fractional_divider *fd = to_clk_fd(hw);
86 unsigned long flags = 0;
Andy Shevchenko07775912015-09-22 18:54:11 +030087 unsigned long m, n;
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030088 u32 val;
89
Andy Shevchenko07775912015-09-22 18:54:11 +030090 rational_best_approximation(rate, parent_rate,
91 GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0),
92 &m, &n);
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030093
94 if (fd->lock)
95 spin_lock_irqsave(fd->lock, flags);
Stephen Boyd661e2182015-07-24 12:21:12 -070096 else
97 __acquire(fd->lock);
Heikki Krogeruse2d0e902014-05-15 16:40:25 +030098
99 val = clk_readl(fd->reg);
100 val &= ~(fd->mmask | fd->nmask);
101 val |= (m << fd->mshift) | (n << fd->nshift);
102 clk_writel(val, fd->reg);
103
104 if (fd->lock)
105 spin_unlock_irqrestore(fd->lock, flags);
Stephen Boyd661e2182015-07-24 12:21:12 -0700106 else
107 __release(fd->lock);
Heikki Krogeruse2d0e902014-05-15 16:40:25 +0300108
109 return 0;
110}
111
112const struct clk_ops clk_fractional_divider_ops = {
113 .recalc_rate = clk_fd_recalc_rate,
114 .round_rate = clk_fd_round_rate,
115 .set_rate = clk_fd_set_rate,
116};
117EXPORT_SYMBOL_GPL(clk_fractional_divider_ops);
118
119struct clk *clk_register_fractional_divider(struct device *dev,
120 const char *name, const char *parent_name, unsigned long flags,
121 void __iomem *reg, u8 mshift, u8 mwidth, u8 nshift, u8 nwidth,
122 u8 clk_divider_flags, spinlock_t *lock)
123{
124 struct clk_fractional_divider *fd;
125 struct clk_init_data init;
126 struct clk *clk;
127
128 fd = kzalloc(sizeof(*fd), GFP_KERNEL);
Stephen Boydd122db72015-05-14 16:47:10 -0700129 if (!fd)
Heikki Krogeruse2d0e902014-05-15 16:40:25 +0300130 return ERR_PTR(-ENOMEM);
Heikki Krogeruse2d0e902014-05-15 16:40:25 +0300131
132 init.name = name;
133 init.ops = &clk_fractional_divider_ops;
134 init.flags = flags | CLK_IS_BASIC;
135 init.parent_names = parent_name ? &parent_name : NULL;
136 init.num_parents = parent_name ? 1 : 0;
137
138 fd->reg = reg;
139 fd->mshift = mshift;
Andy Shevchenko934e2532015-09-22 18:54:09 +0300140 fd->mwidth = mwidth;
141 fd->mmask = GENMASK(mwidth - 1, 0) << mshift;
Heikki Krogeruse2d0e902014-05-15 16:40:25 +0300142 fd->nshift = nshift;
Andy Shevchenko934e2532015-09-22 18:54:09 +0300143 fd->nwidth = nwidth;
144 fd->nmask = GENMASK(nwidth - 1, 0) << nshift;
Heikki Krogeruse2d0e902014-05-15 16:40:25 +0300145 fd->flags = clk_divider_flags;
146 fd->lock = lock;
147 fd->hw.init = &init;
148
149 clk = clk_register(dev, &fd->hw);
150 if (IS_ERR(clk))
151 kfree(fd);
152
153 return clk;
154}
155EXPORT_SYMBOL_GPL(clk_register_fractional_divider);