]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/clk/rockchip/clk-mmc-phase.c
Merge tag 'berlin64-for-v4.3-1' of git://git.infradead.org/users/hesselba/linux-berli...
[karo-tx-linux.git] / drivers / clk / rockchip / clk-mmc-phase.c
1 /*
2  * Copyright 2014 Google, Inc
3  * Author: Alexandru M Stan <amstan@chromium.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include <linux/slab.h>
17 #include <linux/clk-provider.h>
18 #include <linux/io.h>
19 #include <linux/kernel.h>
20 #include "clk.h"
21
22 struct rockchip_mmc_clock {
23         struct clk_hw   hw;
24         void __iomem    *reg;
25         int             id;
26         int             shift;
27 };
28
29 #define to_mmc_clock(_hw) container_of(_hw, struct rockchip_mmc_clock, hw)
30
31 #define RK3288_MMC_CLKGEN_DIV 2
32
33 static unsigned long rockchip_mmc_recalc(struct clk_hw *hw,
34                                          unsigned long parent_rate)
35 {
36         return parent_rate / RK3288_MMC_CLKGEN_DIV;
37 }
38
39 #define ROCKCHIP_MMC_DELAY_SEL BIT(10)
40 #define ROCKCHIP_MMC_DEGREE_MASK 0x3
41 #define ROCKCHIP_MMC_DELAYNUM_OFFSET 2
42 #define ROCKCHIP_MMC_DELAYNUM_MASK (0xff << ROCKCHIP_MMC_DELAYNUM_OFFSET)
43
44 #define PSECS_PER_SEC 1000000000000LL
45
46 /*
47  * Each fine delay is between 40ps-80ps. Assume each fine delay is 60ps to
48  * simplify calculations. So 45degs could be anywhere between 33deg and 66deg.
49  */
50 #define ROCKCHIP_MMC_DELAY_ELEMENT_PSEC 60
51
52 static int rockchip_mmc_get_phase(struct clk_hw *hw)
53 {
54         struct rockchip_mmc_clock *mmc_clock = to_mmc_clock(hw);
55         unsigned long rate = clk_get_rate(hw->clk);
56         u32 raw_value;
57         u16 degrees;
58         u32 delay_num = 0;
59
60         raw_value = readl(mmc_clock->reg) >> (mmc_clock->shift);
61
62         degrees = (raw_value & ROCKCHIP_MMC_DEGREE_MASK) * 90;
63
64         if (raw_value & ROCKCHIP_MMC_DELAY_SEL) {
65                 /* degrees/delaynum * 10000 */
66                 unsigned long factor = (ROCKCHIP_MMC_DELAY_ELEMENT_PSEC / 10) *
67                                         36 * (rate / 1000000);
68
69                 delay_num = (raw_value & ROCKCHIP_MMC_DELAYNUM_MASK);
70                 delay_num >>= ROCKCHIP_MMC_DELAYNUM_OFFSET;
71                 degrees += delay_num * factor / 10000;
72         }
73
74         return degrees % 360;
75 }
76
77 static int rockchip_mmc_set_phase(struct clk_hw *hw, int degrees)
78 {
79         struct rockchip_mmc_clock *mmc_clock = to_mmc_clock(hw);
80         unsigned long rate = clk_get_rate(hw->clk);
81         u8 nineties, remainder;
82         u8 delay_num;
83         u32 raw_value;
84         u64 delay;
85
86         /* allow 22 to be 22.5 */
87         degrees++;
88         /* floor to 22.5 increment */
89         degrees -= ((degrees) * 10 % 225) / 10;
90
91         nineties = degrees / 90;
92         /* 22.5 multiples */
93         remainder = (degrees % 90) / 22;
94
95         delay = PSECS_PER_SEC;
96         do_div(delay, rate);
97         /* / 360 / 22.5 */
98         do_div(delay, 16);
99         do_div(delay, ROCKCHIP_MMC_DELAY_ELEMENT_PSEC);
100
101         delay *= remainder;
102         delay_num = (u8) min(delay, 255ULL);
103
104         raw_value = delay_num ? ROCKCHIP_MMC_DELAY_SEL : 0;
105         raw_value |= delay_num << ROCKCHIP_MMC_DELAYNUM_OFFSET;
106         raw_value |= nineties;
107         writel(HIWORD_UPDATE(raw_value, 0x07ff, mmc_clock->shift), mmc_clock->reg);
108
109         pr_debug("%s->set_phase(%d) delay_nums=%u reg[0x%p]=0x%03x actual_degrees=%d\n",
110                 __clk_get_name(hw->clk), degrees, delay_num,
111                 mmc_clock->reg, raw_value>>(mmc_clock->shift),
112                 rockchip_mmc_get_phase(hw)
113         );
114
115         return 0;
116 }
117
118 static const struct clk_ops rockchip_mmc_clk_ops = {
119         .recalc_rate    = rockchip_mmc_recalc,
120         .get_phase      = rockchip_mmc_get_phase,
121         .set_phase      = rockchip_mmc_set_phase,
122 };
123
124 struct clk *rockchip_clk_register_mmc(const char *name,
125                                 const char *const *parent_names, u8 num_parents,
126                                 void __iomem *reg, int shift)
127 {
128         struct clk_init_data init;
129         struct rockchip_mmc_clock *mmc_clock;
130         struct clk *clk;
131
132         mmc_clock = kmalloc(sizeof(*mmc_clock), GFP_KERNEL);
133         if (!mmc_clock)
134                 return NULL;
135
136         init.name = name;
137         init.num_parents = num_parents;
138         init.parent_names = parent_names;
139         init.ops = &rockchip_mmc_clk_ops;
140
141         mmc_clock->hw.init = &init;
142         mmc_clock->reg = reg;
143         mmc_clock->shift = shift;
144
145         clk = clk_register(NULL, &mmc_clock->hw);
146         if (IS_ERR(clk))
147                 goto err_free;
148
149         return clk;
150
151 err_free:
152         kfree(mmc_clock);
153         return NULL;
154 }