1 /* 2 * Sysctrl clock implementation for ux500 platform. 3 * 4 * Copyright (C) 2013 ST-Ericsson SA 5 * Author: Ulf Hansson <ulf.hansson@linaro.org> 6 * 7 * License terms: GNU General Public License (GPL) version 2 8 */ 9 10 #include <linux/clk-provider.h> 11 #include <linux/mfd/abx500/ab8500-sysctrl.h> 12 #include <linux/device.h> 13 #include <linux/slab.h> 14 #include <linux/delay.h> 15 #include <linux/io.h> 16 #include <linux/err.h> 17 #include "clk.h" 18 19 #define SYSCTRL_MAX_NUM_PARENTS 4 20 21 #define to_clk_sysctrl(_hw) container_of(_hw, struct clk_sysctrl, hw) 22 23 struct clk_sysctrl { 24 struct clk_hw hw; 25 struct device *dev; 26 u8 parent_index; 27 u16 reg_sel[SYSCTRL_MAX_NUM_PARENTS]; 28 u8 reg_mask[SYSCTRL_MAX_NUM_PARENTS]; 29 u8 reg_bits[SYSCTRL_MAX_NUM_PARENTS]; 30 unsigned long rate; 31 unsigned long enable_delay_us; 32 }; 33 34 /* Sysctrl clock operations. */ 35 36 static int clk_sysctrl_prepare(struct clk_hw *hw) 37 { 38 int ret; 39 struct clk_sysctrl *clk = to_clk_sysctrl(hw); 40 41 ret = ab8500_sysctrl_write(clk->reg_sel[0], clk->reg_mask[0], 42 clk->reg_bits[0]); 43 44 if (!ret && clk->enable_delay_us) 45 usleep_range(clk->enable_delay_us, clk->enable_delay_us + 46 (clk->enable_delay_us >> 2)); 47 48 return ret; 49 } 50 51 static void clk_sysctrl_unprepare(struct clk_hw *hw) 52 { 53 struct clk_sysctrl *clk = to_clk_sysctrl(hw); 54 if (ab8500_sysctrl_clear(clk->reg_sel[0], clk->reg_mask[0])) 55 dev_err(clk->dev, "clk_sysctrl: %s fail to clear %s.\n", 56 __func__, clk_hw_get_name(hw)); 57 } 58 59 static unsigned long clk_sysctrl_recalc_rate(struct clk_hw *hw, 60 unsigned long parent_rate) 61 { 62 struct clk_sysctrl *clk = to_clk_sysctrl(hw); 63 return clk->rate; 64 } 65 66 static int clk_sysctrl_set_parent(struct clk_hw *hw, u8 index) 67 { 68 struct clk_sysctrl *clk = to_clk_sysctrl(hw); 69 u8 old_index = clk->parent_index; 70 int ret = 0; 71 72 if (clk->reg_sel[old_index]) { 73 ret = ab8500_sysctrl_clear(clk->reg_sel[old_index], 74 clk->reg_mask[old_index]); 75 if (ret) 76 return ret; 77 } 78 79 if (clk->reg_sel[index]) { 80 ret = ab8500_sysctrl_write(clk->reg_sel[index], 81 clk->reg_mask[index], 82 clk->reg_bits[index]); 83 if (ret) { 84 if (clk->reg_sel[old_index]) 85 ab8500_sysctrl_write(clk->reg_sel[old_index], 86 clk->reg_mask[old_index], 87 clk->reg_bits[old_index]); 88 return ret; 89 } 90 } 91 clk->parent_index = index; 92 93 return ret; 94 } 95 96 static u8 clk_sysctrl_get_parent(struct clk_hw *hw) 97 { 98 struct clk_sysctrl *clk = to_clk_sysctrl(hw); 99 return clk->parent_index; 100 } 101 102 static const struct clk_ops clk_sysctrl_gate_ops = { 103 .prepare = clk_sysctrl_prepare, 104 .unprepare = clk_sysctrl_unprepare, 105 }; 106 107 static const struct clk_ops clk_sysctrl_gate_fixed_rate_ops = { 108 .prepare = clk_sysctrl_prepare, 109 .unprepare = clk_sysctrl_unprepare, 110 .recalc_rate = clk_sysctrl_recalc_rate, 111 }; 112 113 static const struct clk_ops clk_sysctrl_set_parent_ops = { 114 .set_parent = clk_sysctrl_set_parent, 115 .get_parent = clk_sysctrl_get_parent, 116 }; 117 118 static struct clk *clk_reg_sysctrl(struct device *dev, 119 const char *name, 120 const char **parent_names, 121 u8 num_parents, 122 u16 *reg_sel, 123 u8 *reg_mask, 124 u8 *reg_bits, 125 unsigned long rate, 126 unsigned long enable_delay_us, 127 unsigned long flags, 128 const struct clk_ops *clk_sysctrl_ops) 129 { 130 struct clk_sysctrl *clk; 131 struct clk_init_data clk_sysctrl_init; 132 struct clk *clk_reg; 133 int i; 134 135 if (!dev) 136 return ERR_PTR(-EINVAL); 137 138 if (!name || (num_parents > SYSCTRL_MAX_NUM_PARENTS)) { 139 dev_err(dev, "clk_sysctrl: invalid arguments passed\n"); 140 return ERR_PTR(-EINVAL); 141 } 142 143 clk = devm_kzalloc(dev, sizeof(*clk), GFP_KERNEL); 144 if (!clk) 145 return ERR_PTR(-ENOMEM); 146 147 /* set main clock registers */ 148 clk->reg_sel[0] = reg_sel[0]; 149 clk->reg_bits[0] = reg_bits[0]; 150 clk->reg_mask[0] = reg_mask[0]; 151 152 /* handle clocks with more than one parent */ 153 for (i = 1; i < num_parents; i++) { 154 clk->reg_sel[i] = reg_sel[i]; 155 clk->reg_bits[i] = reg_bits[i]; 156 clk->reg_mask[i] = reg_mask[i]; 157 } 158 159 clk->parent_index = 0; 160 clk->rate = rate; 161 clk->enable_delay_us = enable_delay_us; 162 clk->dev = dev; 163 164 clk_sysctrl_init.name = name; 165 clk_sysctrl_init.ops = clk_sysctrl_ops; 166 clk_sysctrl_init.flags = flags; 167 clk_sysctrl_init.parent_names = parent_names; 168 clk_sysctrl_init.num_parents = num_parents; 169 clk->hw.init = &clk_sysctrl_init; 170 171 clk_reg = devm_clk_register(clk->dev, &clk->hw); 172 if (IS_ERR(clk_reg)) 173 dev_err(dev, "clk_sysctrl: clk_register failed\n"); 174 175 return clk_reg; 176 } 177 178 struct clk *clk_reg_sysctrl_gate(struct device *dev, 179 const char *name, 180 const char *parent_name, 181 u16 reg_sel, 182 u8 reg_mask, 183 u8 reg_bits, 184 unsigned long enable_delay_us, 185 unsigned long flags) 186 { 187 const char **parent_names = (parent_name ? &parent_name : NULL); 188 u8 num_parents = (parent_name ? 1 : 0); 189 190 return clk_reg_sysctrl(dev, name, parent_names, num_parents, 191 ®_sel, ®_mask, ®_bits, 0, enable_delay_us, 192 flags, &clk_sysctrl_gate_ops); 193 } 194 195 struct clk *clk_reg_sysctrl_gate_fixed_rate(struct device *dev, 196 const char *name, 197 const char *parent_name, 198 u16 reg_sel, 199 u8 reg_mask, 200 u8 reg_bits, 201 unsigned long rate, 202 unsigned long enable_delay_us, 203 unsigned long flags) 204 { 205 const char **parent_names = (parent_name ? &parent_name : NULL); 206 u8 num_parents = (parent_name ? 1 : 0); 207 208 return clk_reg_sysctrl(dev, name, parent_names, num_parents, 209 ®_sel, ®_mask, ®_bits, 210 rate, enable_delay_us, flags, 211 &clk_sysctrl_gate_fixed_rate_ops); 212 } 213 214 struct clk *clk_reg_sysctrl_set_parent(struct device *dev, 215 const char *name, 216 const char **parent_names, 217 u8 num_parents, 218 u16 *reg_sel, 219 u8 *reg_mask, 220 u8 *reg_bits, 221 unsigned long flags) 222 { 223 return clk_reg_sysctrl(dev, name, parent_names, num_parents, 224 reg_sel, reg_mask, reg_bits, 0, 0, flags, 225 &clk_sysctrl_set_parent_ops); 226 } 227