Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * This software is licensed under the terms of the GNU General Public |
| 5 | * License version 2, as published by the Free Software Foundation, and |
| 6 | * may be copied, distributed, and modified under those terms. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/bitops.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/bug.h> |
| 18 | #include <linux/export.h> |
| 19 | #include <linux/clk-provider.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/regmap.h> |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 22 | #include <linux/math64.h> |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 23 | |
| 24 | #include <asm/div64.h> |
| 25 | |
| 26 | #include "clk-rcg.h" |
Stephen Boyd | 50c6a50 | 2014-09-04 13:21:50 -0700 | [diff] [blame] | 27 | #include "common.h" |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 28 | |
| 29 | #define CMD_REG 0x0 |
| 30 | #define CMD_UPDATE BIT(0) |
| 31 | #define CMD_ROOT_EN BIT(1) |
| 32 | #define CMD_DIRTY_CFG BIT(4) |
| 33 | #define CMD_DIRTY_N BIT(5) |
| 34 | #define CMD_DIRTY_M BIT(6) |
| 35 | #define CMD_DIRTY_D BIT(7) |
| 36 | #define CMD_ROOT_OFF BIT(31) |
| 37 | |
| 38 | #define CFG_REG 0x4 |
| 39 | #define CFG_SRC_DIV_SHIFT 0 |
| 40 | #define CFG_SRC_SEL_SHIFT 8 |
| 41 | #define CFG_SRC_SEL_MASK (0x7 << CFG_SRC_SEL_SHIFT) |
| 42 | #define CFG_MODE_SHIFT 12 |
| 43 | #define CFG_MODE_MASK (0x3 << CFG_MODE_SHIFT) |
| 44 | #define CFG_MODE_DUAL_EDGE (0x2 << CFG_MODE_SHIFT) |
| 45 | |
| 46 | #define M_REG 0x8 |
| 47 | #define N_REG 0xc |
| 48 | #define D_REG 0x10 |
| 49 | |
| 50 | static int clk_rcg2_is_enabled(struct clk_hw *hw) |
| 51 | { |
| 52 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 53 | u32 cmd; |
| 54 | int ret; |
| 55 | |
| 56 | ret = regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG, &cmd); |
| 57 | if (ret) |
| 58 | return ret; |
| 59 | |
Stephen Boyd | aa01414 | 2014-05-16 16:07:08 -0700 | [diff] [blame] | 60 | return (cmd & CMD_ROOT_OFF) == 0; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static u8 clk_rcg2_get_parent(struct clk_hw *hw) |
| 64 | { |
| 65 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 66 | int num_parents = __clk_get_num_parents(hw->clk); |
| 67 | u32 cfg; |
| 68 | int i, ret; |
| 69 | |
| 70 | ret = regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &cfg); |
| 71 | if (ret) |
Georgi Djakov | 7f21897 | 2015-03-20 18:30:24 +0200 | [diff] [blame] | 72 | goto err; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 73 | |
| 74 | cfg &= CFG_SRC_SEL_MASK; |
| 75 | cfg >>= CFG_SRC_SEL_SHIFT; |
| 76 | |
| 77 | for (i = 0; i < num_parents; i++) |
| 78 | if (cfg == rcg->parent_map[i]) |
| 79 | return i; |
| 80 | |
Georgi Djakov | 7f21897 | 2015-03-20 18:30:24 +0200 | [diff] [blame] | 81 | err: |
| 82 | pr_debug("%s: Clock %s has invalid parent, using default.\n", |
| 83 | __func__, __clk_get_name(hw->clk)); |
| 84 | return 0; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static int update_config(struct clk_rcg2 *rcg) |
| 88 | { |
| 89 | int count, ret; |
| 90 | u32 cmd; |
| 91 | struct clk_hw *hw = &rcg->clkr.hw; |
| 92 | const char *name = __clk_get_name(hw->clk); |
| 93 | |
| 94 | ret = regmap_update_bits(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG, |
| 95 | CMD_UPDATE, CMD_UPDATE); |
| 96 | if (ret) |
| 97 | return ret; |
| 98 | |
| 99 | /* Wait for update to take effect */ |
| 100 | for (count = 500; count > 0; count--) { |
| 101 | ret = regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CMD_REG, &cmd); |
| 102 | if (ret) |
| 103 | return ret; |
| 104 | if (!(cmd & CMD_UPDATE)) |
| 105 | return 0; |
| 106 | udelay(1); |
| 107 | } |
| 108 | |
| 109 | WARN(1, "%s: rcg didn't update its configuration.", name); |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int clk_rcg2_set_parent(struct clk_hw *hw, u8 index) |
| 114 | { |
| 115 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 116 | int ret; |
| 117 | |
| 118 | ret = regmap_update_bits(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, |
| 119 | CFG_SRC_SEL_MASK, |
| 120 | rcg->parent_map[index] << CFG_SRC_SEL_SHIFT); |
| 121 | if (ret) |
| 122 | return ret; |
| 123 | |
| 124 | return update_config(rcg); |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Calculate m/n:d rate |
| 129 | * |
| 130 | * parent_rate m |
| 131 | * rate = ----------- x --- |
| 132 | * hid_div n |
| 133 | */ |
| 134 | static unsigned long |
| 135 | calc_rate(unsigned long rate, u32 m, u32 n, u32 mode, u32 hid_div) |
| 136 | { |
| 137 | if (hid_div) { |
| 138 | rate *= 2; |
| 139 | rate /= hid_div + 1; |
| 140 | } |
| 141 | |
| 142 | if (mode) { |
| 143 | u64 tmp = rate; |
| 144 | tmp *= m; |
| 145 | do_div(tmp, n); |
| 146 | rate = tmp; |
| 147 | } |
| 148 | |
| 149 | return rate; |
| 150 | } |
| 151 | |
| 152 | static unsigned long |
| 153 | clk_rcg2_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) |
| 154 | { |
| 155 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 156 | u32 cfg, hid_div, m = 0, n = 0, mode = 0, mask; |
| 157 | |
| 158 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, &cfg); |
| 159 | |
| 160 | if (rcg->mnd_width) { |
| 161 | mask = BIT(rcg->mnd_width) - 1; |
| 162 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + M_REG, &m); |
| 163 | m &= mask; |
| 164 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + N_REG, &n); |
| 165 | n = ~n; |
| 166 | n &= mask; |
| 167 | n += m; |
| 168 | mode = cfg & CFG_MODE_MASK; |
| 169 | mode >>= CFG_MODE_SHIFT; |
| 170 | } |
| 171 | |
| 172 | mask = BIT(rcg->hid_width) - 1; |
| 173 | hid_div = cfg >> CFG_SRC_DIV_SHIFT; |
| 174 | hid_div &= mask; |
| 175 | |
| 176 | return calc_rate(parent_rate, m, n, mode, hid_div); |
| 177 | } |
| 178 | |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 179 | static long _freq_tbl_determine_rate(struct clk_hw *hw, |
| 180 | const struct freq_tbl *f, unsigned long rate, |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 181 | unsigned long *p_rate, struct clk_hw **p_hw) |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 182 | { |
| 183 | unsigned long clk_flags; |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 184 | struct clk *p; |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 185 | |
Stephen Boyd | 50c6a50 | 2014-09-04 13:21:50 -0700 | [diff] [blame] | 186 | f = qcom_find_freq(f, rate); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 187 | if (!f) |
| 188 | return -EINVAL; |
| 189 | |
| 190 | clk_flags = __clk_get_flags(hw->clk); |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 191 | p = clk_get_parent_by_index(hw->clk, f->src); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 192 | if (clk_flags & CLK_SET_RATE_PARENT) { |
| 193 | if (f->pre_div) { |
| 194 | rate /= 2; |
| 195 | rate *= f->pre_div + 1; |
| 196 | } |
| 197 | |
| 198 | if (f->n) { |
| 199 | u64 tmp = rate; |
| 200 | tmp = tmp * f->n; |
| 201 | do_div(tmp, f->m); |
| 202 | rate = tmp; |
| 203 | } |
| 204 | } else { |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 205 | rate = __clk_get_rate(p); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 206 | } |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 207 | *p_hw = __clk_get_hw(p); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 208 | *p_rate = rate; |
| 209 | |
| 210 | return f->freq; |
| 211 | } |
| 212 | |
| 213 | static long clk_rcg2_determine_rate(struct clk_hw *hw, unsigned long rate, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 214 | unsigned long min_rate, unsigned long max_rate, |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 215 | unsigned long *p_rate, struct clk_hw **p) |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 216 | { |
| 217 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 218 | |
| 219 | return _freq_tbl_determine_rate(hw, rcg->freq_tbl, rate, p_rate, p); |
| 220 | } |
| 221 | |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 222 | static int clk_rcg2_configure(struct clk_rcg2 *rcg, const struct freq_tbl *f) |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 223 | { |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 224 | u32 cfg, mask; |
| 225 | int ret; |
| 226 | |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 227 | if (rcg->mnd_width && f->n) { |
| 228 | mask = BIT(rcg->mnd_width) - 1; |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 229 | ret = regmap_update_bits(rcg->clkr.regmap, |
| 230 | rcg->cmd_rcgr + M_REG, mask, f->m); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 231 | if (ret) |
| 232 | return ret; |
| 233 | |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 234 | ret = regmap_update_bits(rcg->clkr.regmap, |
| 235 | rcg->cmd_rcgr + N_REG, mask, ~(f->n - f->m)); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 236 | if (ret) |
| 237 | return ret; |
| 238 | |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 239 | ret = regmap_update_bits(rcg->clkr.regmap, |
| 240 | rcg->cmd_rcgr + D_REG, mask, ~f->n); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 241 | if (ret) |
| 242 | return ret; |
| 243 | } |
| 244 | |
| 245 | mask = BIT(rcg->hid_width) - 1; |
| 246 | mask |= CFG_SRC_SEL_MASK | CFG_MODE_MASK; |
| 247 | cfg = f->pre_div << CFG_SRC_DIV_SHIFT; |
| 248 | cfg |= rcg->parent_map[f->src] << CFG_SRC_SEL_SHIFT; |
Archit Taneja | 0b21503 | 2015-03-04 15:19:35 +0530 | [diff] [blame] | 249 | if (rcg->mnd_width && f->n && (f->m != f->n)) |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 250 | cfg |= CFG_MODE_DUAL_EDGE; |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 251 | ret = regmap_update_bits(rcg->clkr.regmap, |
| 252 | rcg->cmd_rcgr + CFG_REG, mask, cfg); |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 253 | if (ret) |
| 254 | return ret; |
| 255 | |
| 256 | return update_config(rcg); |
| 257 | } |
| 258 | |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 259 | static int __clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate) |
| 260 | { |
| 261 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 262 | const struct freq_tbl *f; |
| 263 | |
Stephen Boyd | 50c6a50 | 2014-09-04 13:21:50 -0700 | [diff] [blame] | 264 | f = qcom_find_freq(rcg->freq_tbl, rate); |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 265 | if (!f) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | return clk_rcg2_configure(rcg, f); |
| 269 | } |
| 270 | |
Stephen Boyd | bcd61c0 | 2014-01-15 10:47:25 -0800 | [diff] [blame] | 271 | static int clk_rcg2_set_rate(struct clk_hw *hw, unsigned long rate, |
| 272 | unsigned long parent_rate) |
| 273 | { |
| 274 | return __clk_rcg2_set_rate(hw, rate); |
| 275 | } |
| 276 | |
| 277 | static int clk_rcg2_set_rate_and_parent(struct clk_hw *hw, |
| 278 | unsigned long rate, unsigned long parent_rate, u8 index) |
| 279 | { |
| 280 | return __clk_rcg2_set_rate(hw, rate); |
| 281 | } |
| 282 | |
| 283 | const struct clk_ops clk_rcg2_ops = { |
| 284 | .is_enabled = clk_rcg2_is_enabled, |
| 285 | .get_parent = clk_rcg2_get_parent, |
| 286 | .set_parent = clk_rcg2_set_parent, |
| 287 | .recalc_rate = clk_rcg2_recalc_rate, |
| 288 | .determine_rate = clk_rcg2_determine_rate, |
| 289 | .set_rate = clk_rcg2_set_rate, |
| 290 | .set_rate_and_parent = clk_rcg2_set_rate_and_parent, |
| 291 | }; |
| 292 | EXPORT_SYMBOL_GPL(clk_rcg2_ops); |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 293 | |
| 294 | struct frac_entry { |
| 295 | int num; |
| 296 | int den; |
| 297 | }; |
| 298 | |
| 299 | static const struct frac_entry frac_table_675m[] = { /* link rate of 270M */ |
| 300 | { 52, 295 }, /* 119 M */ |
| 301 | { 11, 57 }, /* 130.25 M */ |
| 302 | { 63, 307 }, /* 138.50 M */ |
| 303 | { 11, 50 }, /* 148.50 M */ |
| 304 | { 47, 206 }, /* 154 M */ |
| 305 | { 31, 100 }, /* 205.25 M */ |
| 306 | { 107, 269 }, /* 268.50 M */ |
| 307 | { }, |
| 308 | }; |
| 309 | |
| 310 | static struct frac_entry frac_table_810m[] = { /* Link rate of 162M */ |
| 311 | { 31, 211 }, /* 119 M */ |
| 312 | { 32, 199 }, /* 130.25 M */ |
| 313 | { 63, 307 }, /* 138.50 M */ |
| 314 | { 11, 60 }, /* 148.50 M */ |
| 315 | { 50, 263 }, /* 154 M */ |
| 316 | { 31, 120 }, /* 205.25 M */ |
| 317 | { 119, 359 }, /* 268.50 M */ |
| 318 | { }, |
| 319 | }; |
| 320 | |
| 321 | static int clk_edp_pixel_set_rate(struct clk_hw *hw, unsigned long rate, |
| 322 | unsigned long parent_rate) |
| 323 | { |
| 324 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 325 | struct freq_tbl f = *rcg->freq_tbl; |
| 326 | const struct frac_entry *frac; |
| 327 | int delta = 100000; |
| 328 | s64 src_rate = parent_rate; |
| 329 | s64 request; |
| 330 | u32 mask = BIT(rcg->hid_width) - 1; |
| 331 | u32 hid_div; |
| 332 | |
| 333 | if (src_rate == 810000000) |
| 334 | frac = frac_table_810m; |
| 335 | else |
| 336 | frac = frac_table_675m; |
| 337 | |
| 338 | for (; frac->num; frac++) { |
| 339 | request = rate; |
| 340 | request *= frac->den; |
| 341 | request = div_s64(request, frac->num); |
| 342 | if ((src_rate < (request - delta)) || |
| 343 | (src_rate > (request + delta))) |
| 344 | continue; |
| 345 | |
| 346 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, |
| 347 | &hid_div); |
| 348 | f.pre_div = hid_div; |
| 349 | f.pre_div >>= CFG_SRC_DIV_SHIFT; |
| 350 | f.pre_div &= mask; |
| 351 | f.m = frac->num; |
| 352 | f.n = frac->den; |
| 353 | |
| 354 | return clk_rcg2_configure(rcg, &f); |
| 355 | } |
| 356 | |
| 357 | return -EINVAL; |
| 358 | } |
| 359 | |
| 360 | static int clk_edp_pixel_set_rate_and_parent(struct clk_hw *hw, |
| 361 | unsigned long rate, unsigned long parent_rate, u8 index) |
| 362 | { |
| 363 | /* Parent index is set statically in frequency table */ |
| 364 | return clk_edp_pixel_set_rate(hw, rate, parent_rate); |
| 365 | } |
| 366 | |
| 367 | static long clk_edp_pixel_determine_rate(struct clk_hw *hw, unsigned long rate, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 368 | unsigned long min_rate, |
| 369 | unsigned long max_rate, |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 370 | unsigned long *p_rate, struct clk_hw **p) |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 371 | { |
| 372 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 373 | const struct freq_tbl *f = rcg->freq_tbl; |
| 374 | const struct frac_entry *frac; |
| 375 | int delta = 100000; |
| 376 | s64 src_rate = *p_rate; |
| 377 | s64 request; |
| 378 | u32 mask = BIT(rcg->hid_width) - 1; |
| 379 | u32 hid_div; |
| 380 | |
| 381 | /* Force the correct parent */ |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 382 | *p = __clk_get_hw(clk_get_parent_by_index(hw->clk, f->src)); |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 383 | |
| 384 | if (src_rate == 810000000) |
| 385 | frac = frac_table_810m; |
| 386 | else |
| 387 | frac = frac_table_675m; |
| 388 | |
| 389 | for (; frac->num; frac++) { |
| 390 | request = rate; |
| 391 | request *= frac->den; |
| 392 | request = div_s64(request, frac->num); |
| 393 | if ((src_rate < (request - delta)) || |
| 394 | (src_rate > (request + delta))) |
| 395 | continue; |
| 396 | |
| 397 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, |
| 398 | &hid_div); |
| 399 | hid_div >>= CFG_SRC_DIV_SHIFT; |
| 400 | hid_div &= mask; |
| 401 | |
| 402 | return calc_rate(src_rate, frac->num, frac->den, !!frac->den, |
| 403 | hid_div); |
| 404 | } |
| 405 | |
| 406 | return -EINVAL; |
| 407 | } |
| 408 | |
| 409 | const struct clk_ops clk_edp_pixel_ops = { |
| 410 | .is_enabled = clk_rcg2_is_enabled, |
| 411 | .get_parent = clk_rcg2_get_parent, |
| 412 | .set_parent = clk_rcg2_set_parent, |
| 413 | .recalc_rate = clk_rcg2_recalc_rate, |
| 414 | .set_rate = clk_edp_pixel_set_rate, |
| 415 | .set_rate_and_parent = clk_edp_pixel_set_rate_and_parent, |
| 416 | .determine_rate = clk_edp_pixel_determine_rate, |
| 417 | }; |
| 418 | EXPORT_SYMBOL_GPL(clk_edp_pixel_ops); |
| 419 | |
| 420 | static long clk_byte_determine_rate(struct clk_hw *hw, unsigned long rate, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 421 | unsigned long min_rate, unsigned long max_rate, |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 422 | unsigned long *p_rate, struct clk_hw **p_hw) |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 423 | { |
| 424 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 425 | const struct freq_tbl *f = rcg->freq_tbl; |
| 426 | unsigned long parent_rate, div; |
| 427 | u32 mask = BIT(rcg->hid_width) - 1; |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 428 | struct clk *p; |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 429 | |
| 430 | if (rate == 0) |
| 431 | return -EINVAL; |
| 432 | |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 433 | p = clk_get_parent_by_index(hw->clk, f->src); |
| 434 | *p_hw = __clk_get_hw(p); |
| 435 | *p_rate = parent_rate = __clk_round_rate(p, rate); |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 436 | |
| 437 | div = DIV_ROUND_UP((2 * parent_rate), rate) - 1; |
| 438 | div = min_t(u32, div, mask); |
| 439 | |
| 440 | return calc_rate(parent_rate, 0, 0, 0, div); |
| 441 | } |
| 442 | |
| 443 | static int clk_byte_set_rate(struct clk_hw *hw, unsigned long rate, |
| 444 | unsigned long parent_rate) |
| 445 | { |
| 446 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 447 | struct freq_tbl f = *rcg->freq_tbl; |
| 448 | unsigned long div; |
| 449 | u32 mask = BIT(rcg->hid_width) - 1; |
| 450 | |
| 451 | div = DIV_ROUND_UP((2 * parent_rate), rate) - 1; |
| 452 | div = min_t(u32, div, mask); |
| 453 | |
| 454 | f.pre_div = div; |
| 455 | |
| 456 | return clk_rcg2_configure(rcg, &f); |
| 457 | } |
| 458 | |
| 459 | static int clk_byte_set_rate_and_parent(struct clk_hw *hw, |
| 460 | unsigned long rate, unsigned long parent_rate, u8 index) |
| 461 | { |
| 462 | /* Parent index is set statically in frequency table */ |
| 463 | return clk_byte_set_rate(hw, rate, parent_rate); |
| 464 | } |
| 465 | |
| 466 | const struct clk_ops clk_byte_ops = { |
| 467 | .is_enabled = clk_rcg2_is_enabled, |
| 468 | .get_parent = clk_rcg2_get_parent, |
| 469 | .set_parent = clk_rcg2_set_parent, |
| 470 | .recalc_rate = clk_rcg2_recalc_rate, |
| 471 | .set_rate = clk_byte_set_rate, |
| 472 | .set_rate_and_parent = clk_byte_set_rate_and_parent, |
| 473 | .determine_rate = clk_byte_determine_rate, |
| 474 | }; |
| 475 | EXPORT_SYMBOL_GPL(clk_byte_ops); |
| 476 | |
| 477 | static const struct frac_entry frac_table_pixel[] = { |
| 478 | { 3, 8 }, |
| 479 | { 2, 9 }, |
| 480 | { 4, 9 }, |
| 481 | { 1, 1 }, |
| 482 | { } |
| 483 | }; |
| 484 | |
| 485 | static long clk_pixel_determine_rate(struct clk_hw *hw, unsigned long rate, |
Tomeu Vizoso | 1c8e600 | 2015-01-23 12:03:31 +0100 | [diff] [blame] | 486 | unsigned long min_rate, |
| 487 | unsigned long max_rate, |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 488 | unsigned long *p_rate, struct clk_hw **p) |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 489 | { |
| 490 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 491 | unsigned long request, src_rate; |
| 492 | int delta = 100000; |
| 493 | const struct freq_tbl *f = rcg->freq_tbl; |
| 494 | const struct frac_entry *frac = frac_table_pixel; |
Tomeu Vizoso | 646cafc | 2014-12-02 08:54:22 +0100 | [diff] [blame] | 495 | struct clk *parent = clk_get_parent_by_index(hw->clk, f->src); |
| 496 | |
| 497 | *p = __clk_get_hw(parent); |
Stephen Boyd | 99cbd06 | 2014-05-16 16:07:11 -0700 | [diff] [blame] | 498 | |
| 499 | for (; frac->num; frac++) { |
| 500 | request = (rate * frac->den) / frac->num; |
| 501 | |
| 502 | src_rate = __clk_round_rate(parent, request); |
| 503 | if ((src_rate < (request - delta)) || |
| 504 | (src_rate > (request + delta))) |
| 505 | continue; |
| 506 | |
| 507 | *p_rate = src_rate; |
| 508 | return (src_rate * frac->num) / frac->den; |
| 509 | } |
| 510 | |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | |
| 514 | static int clk_pixel_set_rate(struct clk_hw *hw, unsigned long rate, |
| 515 | unsigned long parent_rate) |
| 516 | { |
| 517 | struct clk_rcg2 *rcg = to_clk_rcg2(hw); |
| 518 | struct freq_tbl f = *rcg->freq_tbl; |
| 519 | const struct frac_entry *frac = frac_table_pixel; |
| 520 | unsigned long request, src_rate; |
| 521 | int delta = 100000; |
| 522 | u32 mask = BIT(rcg->hid_width) - 1; |
| 523 | u32 hid_div; |
| 524 | struct clk *parent = clk_get_parent_by_index(hw->clk, f.src); |
| 525 | |
| 526 | for (; frac->num; frac++) { |
| 527 | request = (rate * frac->den) / frac->num; |
| 528 | |
| 529 | src_rate = __clk_round_rate(parent, request); |
| 530 | if ((src_rate < (request - delta)) || |
| 531 | (src_rate > (request + delta))) |
| 532 | continue; |
| 533 | |
| 534 | regmap_read(rcg->clkr.regmap, rcg->cmd_rcgr + CFG_REG, |
| 535 | &hid_div); |
| 536 | f.pre_div = hid_div; |
| 537 | f.pre_div >>= CFG_SRC_DIV_SHIFT; |
| 538 | f.pre_div &= mask; |
| 539 | f.m = frac->num; |
| 540 | f.n = frac->den; |
| 541 | |
| 542 | return clk_rcg2_configure(rcg, &f); |
| 543 | } |
| 544 | return -EINVAL; |
| 545 | } |
| 546 | |
| 547 | static int clk_pixel_set_rate_and_parent(struct clk_hw *hw, unsigned long rate, |
| 548 | unsigned long parent_rate, u8 index) |
| 549 | { |
| 550 | /* Parent index is set statically in frequency table */ |
| 551 | return clk_pixel_set_rate(hw, rate, parent_rate); |
| 552 | } |
| 553 | |
| 554 | const struct clk_ops clk_pixel_ops = { |
| 555 | .is_enabled = clk_rcg2_is_enabled, |
| 556 | .get_parent = clk_rcg2_get_parent, |
| 557 | .set_parent = clk_rcg2_set_parent, |
| 558 | .recalc_rate = clk_rcg2_recalc_rate, |
| 559 | .set_rate = clk_pixel_set_rate, |
| 560 | .set_rate_and_parent = clk_pixel_set_rate_and_parent, |
| 561 | .determine_rate = clk_pixel_determine_rate, |
| 562 | }; |
| 563 | EXPORT_SYMBOL_GPL(clk_pixel_ops); |