2 * Copyright 2014 Google, Inc
3 * Author: Alexandru M Stan <amstan@chromium.org>
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.
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.
16 #include <linux/slab.h>
17 #include <linux/clk-provider.h>
19 #include <linux/kernel.h>
22 struct rockchip_mmc_clock {
29 #define to_mmc_clock(_hw) container_of(_hw, struct rockchip_mmc_clock, hw)
31 #define RK3288_MMC_CLKGEN_DIV 2
33 static unsigned long rockchip_mmc_recalc(struct clk_hw *hw,
34 unsigned long parent_rate)
36 return parent_rate / RK3288_MMC_CLKGEN_DIV;
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)
44 #define PSECS_PER_SEC 1000000000000LL
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.
50 #define ROCKCHIP_MMC_DELAY_ELEMENT_PSEC 60
52 static int rockchip_mmc_get_phase(struct clk_hw *hw)
54 struct rockchip_mmc_clock *mmc_clock = to_mmc_clock(hw);
55 unsigned long rate = clk_get_rate(hw->clk);
60 raw_value = readl(mmc_clock->reg) >> (mmc_clock->shift);
62 degrees = (raw_value & ROCKCHIP_MMC_DEGREE_MASK) * 90;
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);
69 delay_num = (raw_value & ROCKCHIP_MMC_DELAYNUM_MASK);
70 delay_num >>= ROCKCHIP_MMC_DELAYNUM_OFFSET;
71 degrees += delay_num * factor / 10000;
77 static int rockchip_mmc_set_phase(struct clk_hw *hw, int degrees)
79 struct rockchip_mmc_clock *mmc_clock = to_mmc_clock(hw);
80 unsigned long rate = clk_get_rate(hw->clk);
81 u8 nineties, remainder;
86 /* allow 22 to be 22.5 */
88 /* floor to 22.5 increment */
89 degrees -= ((degrees) * 10 % 225) / 10;
91 nineties = degrees / 90;
93 remainder = (degrees % 90) / 22;
95 delay = PSECS_PER_SEC;
99 do_div(delay, ROCKCHIP_MMC_DELAY_ELEMENT_PSEC);
102 delay_num = (u8) min(delay, 255ULL);
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);
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)
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,
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)
128 struct clk_init_data init;
129 struct rockchip_mmc_clock *mmc_clock;
132 mmc_clock = kmalloc(sizeof(*mmc_clock), GFP_KERNEL);
137 init.num_parents = num_parents;
138 init.parent_names = parent_names;
139 init.ops = &rockchip_mmc_clk_ops;
141 mmc_clock->hw.init = &init;
142 mmc_clock->reg = reg;
143 mmc_clock->shift = shift;
145 clk = clk_register(NULL, &mmc_clock->hw);