1fcaf2036SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
211f68120SShawn Guo /*
311f68120SShawn Guo * Copyright 2012 Freescale Semiconductor, Inc.
411f68120SShawn Guo * Copyright 2012 Linaro Ltd.
511f68120SShawn Guo */
611f68120SShawn Guo
711f68120SShawn Guo #include <linux/clk-provider.h>
8*64ea30d1SArnd Bergmann #include <linux/export.h>
911f68120SShawn Guo #include <linux/io.h>
1011f68120SShawn Guo #include <linux/slab.h>
1111f68120SShawn Guo #include <linux/err.h>
1211f68120SShawn Guo #include "clk.h"
1311f68120SShawn Guo
1411f68120SShawn Guo /**
1511f68120SShawn Guo * struct clk_pfd - IMX PFD clock
16cca87e5cSKrzysztof Kozlowski * @hw: clock source
1711f68120SShawn Guo * @reg: PFD register address
1811f68120SShawn Guo * @idx: the index of PFD encoded in the register
1911f68120SShawn Guo *
2011f68120SShawn Guo * PFD clock found on i.MX6 series. Each register for PFD has 4 clk_pfd
2111f68120SShawn Guo * data encoded, and member idx is used to specify the one. And each
2211f68120SShawn Guo * register has SET, CLR and TOG registers at offset 0x4 0x8 and 0xc.
2311f68120SShawn Guo */
2411f68120SShawn Guo struct clk_pfd {
2511f68120SShawn Guo struct clk_hw hw;
2611f68120SShawn Guo void __iomem *reg;
2711f68120SShawn Guo u8 idx;
2811f68120SShawn Guo };
2911f68120SShawn Guo
3011f68120SShawn Guo #define to_clk_pfd(_hw) container_of(_hw, struct clk_pfd, hw)
3111f68120SShawn Guo
3211f68120SShawn Guo #define SET 0x4
3311f68120SShawn Guo #define CLR 0x8
3411f68120SShawn Guo #define OTG 0xc
3511f68120SShawn Guo
clk_pfd_enable(struct clk_hw * hw)3611f68120SShawn Guo static int clk_pfd_enable(struct clk_hw *hw)
3711f68120SShawn Guo {
3811f68120SShawn Guo struct clk_pfd *pfd = to_clk_pfd(hw);
3911f68120SShawn Guo
4011f68120SShawn Guo writel_relaxed(1 << ((pfd->idx + 1) * 8 - 1), pfd->reg + CLR);
4111f68120SShawn Guo
4211f68120SShawn Guo return 0;
4311f68120SShawn Guo }
4411f68120SShawn Guo
clk_pfd_disable(struct clk_hw * hw)4511f68120SShawn Guo static void clk_pfd_disable(struct clk_hw *hw)
4611f68120SShawn Guo {
4711f68120SShawn Guo struct clk_pfd *pfd = to_clk_pfd(hw);
4811f68120SShawn Guo
4911f68120SShawn Guo writel_relaxed(1 << ((pfd->idx + 1) * 8 - 1), pfd->reg + SET);
5011f68120SShawn Guo }
5111f68120SShawn Guo
clk_pfd_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)5211f68120SShawn Guo static unsigned long clk_pfd_recalc_rate(struct clk_hw *hw,
5311f68120SShawn Guo unsigned long parent_rate)
5411f68120SShawn Guo {
5511f68120SShawn Guo struct clk_pfd *pfd = to_clk_pfd(hw);
5611f68120SShawn Guo u64 tmp = parent_rate;
5711f68120SShawn Guo u8 frac = (readl_relaxed(pfd->reg) >> (pfd->idx * 8)) & 0x3f;
5811f68120SShawn Guo
5911f68120SShawn Guo tmp *= 18;
6011f68120SShawn Guo do_div(tmp, frac);
6111f68120SShawn Guo
6211f68120SShawn Guo return tmp;
6311f68120SShawn Guo }
6411f68120SShawn Guo
clk_pfd_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)6511f68120SShawn Guo static long clk_pfd_round_rate(struct clk_hw *hw, unsigned long rate,
6611f68120SShawn Guo unsigned long *prate)
6711f68120SShawn Guo {
6811f68120SShawn Guo u64 tmp = *prate;
6911f68120SShawn Guo u8 frac;
7011f68120SShawn Guo
7111f68120SShawn Guo tmp = tmp * 18 + rate / 2;
7211f68120SShawn Guo do_div(tmp, rate);
7311f68120SShawn Guo frac = tmp;
7411f68120SShawn Guo if (frac < 12)
7511f68120SShawn Guo frac = 12;
7611f68120SShawn Guo else if (frac > 35)
7711f68120SShawn Guo frac = 35;
7811f68120SShawn Guo tmp = *prate;
7911f68120SShawn Guo tmp *= 18;
8011f68120SShawn Guo do_div(tmp, frac);
8111f68120SShawn Guo
8211f68120SShawn Guo return tmp;
8311f68120SShawn Guo }
8411f68120SShawn Guo
clk_pfd_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)8511f68120SShawn Guo static int clk_pfd_set_rate(struct clk_hw *hw, unsigned long rate,
8611f68120SShawn Guo unsigned long parent_rate)
8711f68120SShawn Guo {
8811f68120SShawn Guo struct clk_pfd *pfd = to_clk_pfd(hw);
8911f68120SShawn Guo u64 tmp = parent_rate;
9011f68120SShawn Guo u8 frac;
9111f68120SShawn Guo
9211f68120SShawn Guo tmp = tmp * 18 + rate / 2;
9311f68120SShawn Guo do_div(tmp, rate);
9411f68120SShawn Guo frac = tmp;
9511f68120SShawn Guo if (frac < 12)
9611f68120SShawn Guo frac = 12;
9711f68120SShawn Guo else if (frac > 35)
9811f68120SShawn Guo frac = 35;
9911f68120SShawn Guo
10011f68120SShawn Guo writel_relaxed(0x3f << (pfd->idx * 8), pfd->reg + CLR);
10111f68120SShawn Guo writel_relaxed(frac << (pfd->idx * 8), pfd->reg + SET);
10211f68120SShawn Guo
10311f68120SShawn Guo return 0;
10411f68120SShawn Guo }
10511f68120SShawn Guo
clk_pfd_is_enabled(struct clk_hw * hw)10611f68120SShawn Guo static int clk_pfd_is_enabled(struct clk_hw *hw)
10711f68120SShawn Guo {
10811f68120SShawn Guo struct clk_pfd *pfd = to_clk_pfd(hw);
10911f68120SShawn Guo
11011f68120SShawn Guo if (readl_relaxed(pfd->reg) & (1 << ((pfd->idx + 1) * 8 - 1)))
11111f68120SShawn Guo return 0;
11211f68120SShawn Guo
11311f68120SShawn Guo return 1;
11411f68120SShawn Guo }
11511f68120SShawn Guo
11611f68120SShawn Guo static const struct clk_ops clk_pfd_ops = {
11711f68120SShawn Guo .enable = clk_pfd_enable,
11811f68120SShawn Guo .disable = clk_pfd_disable,
11911f68120SShawn Guo .recalc_rate = clk_pfd_recalc_rate,
12011f68120SShawn Guo .round_rate = clk_pfd_round_rate,
12111f68120SShawn Guo .set_rate = clk_pfd_set_rate,
12211f68120SShawn Guo .is_enabled = clk_pfd_is_enabled,
12311f68120SShawn Guo };
12411f68120SShawn Guo
imx_clk_hw_pfd(const char * name,const char * parent_name,void __iomem * reg,u8 idx)125995087c9SAbel Vesa struct clk_hw *imx_clk_hw_pfd(const char *name, const char *parent_name,
12611f68120SShawn Guo void __iomem *reg, u8 idx)
12711f68120SShawn Guo {
12811f68120SShawn Guo struct clk_pfd *pfd;
129995087c9SAbel Vesa struct clk_hw *hw;
13011f68120SShawn Guo struct clk_init_data init;
131995087c9SAbel Vesa int ret;
13211f68120SShawn Guo
13311f68120SShawn Guo pfd = kzalloc(sizeof(*pfd), GFP_KERNEL);
13411f68120SShawn Guo if (!pfd)
13511f68120SShawn Guo return ERR_PTR(-ENOMEM);
13611f68120SShawn Guo
13711f68120SShawn Guo pfd->reg = reg;
13811f68120SShawn Guo pfd->idx = idx;
13911f68120SShawn Guo
14011f68120SShawn Guo init.name = name;
14111f68120SShawn Guo init.ops = &clk_pfd_ops;
14211f68120SShawn Guo init.flags = 0;
14311f68120SShawn Guo init.parent_names = &parent_name;
14411f68120SShawn Guo init.num_parents = 1;
14511f68120SShawn Guo
14611f68120SShawn Guo pfd->hw.init = &init;
147995087c9SAbel Vesa hw = &pfd->hw;
14811f68120SShawn Guo
149995087c9SAbel Vesa ret = clk_hw_register(NULL, hw);
150995087c9SAbel Vesa if (ret) {
15111f68120SShawn Guo kfree(pfd);
152995087c9SAbel Vesa return ERR_PTR(ret);
153995087c9SAbel Vesa }
15411f68120SShawn Guo
155995087c9SAbel Vesa return hw;
15611f68120SShawn Guo }
157*64ea30d1SArnd Bergmann EXPORT_SYMBOL_GPL(imx_clk_hw_pfd);
158