| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* RZ/G2L Clock Pulse Generator |
| 4 |
* |
| 5 |
* Copyright (C) 2021 Renesas Electronics Corp. |
| 6 |
* |
| 7 |
* Based on renesas-cpg-mssr.c |
| 8 |
* |
| 9 |
* Copyright (C) 2015 Glider bvba |
| 10 |
* Copyright (C) 2013 Ideas On Board SPRL |
| 11 |
* Copyright (C) 2015 Renesas Electronics Corp. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <linux/atomic.h> |
| 15 |
#include <linux/bitfield.h> |
| 16 |
#include <linux/cleanup.h> |
| 17 |
#include <linux/clk.h> |
| 18 |
#include <linux/clk-provider.h> |
| 19 |
#include <linux/clk/renesas.h> |
| 20 |
#include <linux/debugfs.h> |
| 21 |
#include <linux/delay.h> |
| 22 |
#include <linux/device.h> |
| 23 |
#include <linux/init.h> |
| 24 |
#include <linux/iopoll.h> |
| 25 |
#include <linux/math64.h> |
| 26 |
#include <linux/mod_devicetable.h> |
| 27 |
#include <linux/module.h> |
| 28 |
#include <linux/of.h> |
| 29 |
#include <linux/platform_device.h> |
| 30 |
#include <linux/pm_clock.h> |
| 31 |
#include <linux/pm_domain.h> |
| 32 |
#include <linux/reset-controller.h> |
| 33 |
#include <linux/slab.h> |
| 34 |
#include <linux/string_choices.h> |
| 35 |
#include <linux/units.h> |
| 36 |
|
| 37 |
#include <dt-bindings/clock/renesas-cpg-mssr.h> |
| 38 |
|
| 39 |
#include "rzg2l-cpg.h" |
| 40 |
|
| 41 |
#ifdef DEBUG |
| 42 |
#define WARN_DEBUG(x) WARN_ON(x) |
| 43 |
#else |
| 44 |
#define WARN_DEBUG(x) do { } while (0) |
| 45 |
#endif |
| 46 |
|
| 47 |
#define GET_SHIFT(val) ((val >> 12) & 0xff) |
| 48 |
#define GET_WIDTH(val) ((val >> 8) & 0xf) |
| 49 |
|
| 50 |
#define KDIV(val) ((s16)FIELD_GET(GENMASK(31, 16), val)) |
| 51 |
#define MDIV(val) FIELD_GET(GENMASK(15, 6), val) |
| 52 |
#define PDIV(val) FIELD_GET(GENMASK(5, 0), val) |
| 53 |
#define SDIV(val) FIELD_GET(GENMASK(2, 0), val) |
| 54 |
|
| 55 |
#define RZG3S_DIV_P GENMASK(28, 26) |
| 56 |
#define RZG3S_DIV_M GENMASK(25, 22) |
| 57 |
#define RZG3S_DIV_NI GENMASK(21, 13) |
| 58 |
#define RZG3S_DIV_NF GENMASK(12, 1) |
| 59 |
#define RZG3S_SEL_PLL BIT(0) |
| 60 |
|
| 61 |
#define CLK_ON_R(reg) (reg) |
| 62 |
#define CLK_MON_R(reg) (0x180 + (reg)) |
| 63 |
#define CLK_RST_R(reg) (reg) |
| 64 |
#define CLK_MRST_R(reg) (0x180 + (reg)) |
| 65 |
|
| 66 |
#define GET_REG_OFFSET(val) ((val >> 20) & 0xfff) |
| 67 |
#define GET_REG_SAMPLL_CLK1(val) ((val >> 22) & 0xfff) |
| 68 |
#define GET_REG_SAMPLL_CLK2(val) ((val >> 12) & 0xfff) |
| 69 |
#define GET_REG_SAMPLL_SETTING(val) ((val) & 0xfff) |
| 70 |
|
| 71 |
#define CPG_WEN_BIT BIT(16) |
| 72 |
|
| 73 |
#define MAX_VCLK_FREQ (148500000) |
| 74 |
|
| 75 |
#define MSTOP_OFF(conf) FIELD_GET(GENMASK(31, 16), (conf)) |
| 76 |
#define MSTOP_MASK(conf) FIELD_GET(GENMASK(15, 0), (conf)) |
| 77 |
|
| 78 |
#define PLL5_FOUTVCO_MIN 800000000 |
| 79 |
#define PLL5_FOUTVCO_MAX 3000000000 |
| 80 |
#define PLL5_POSTDIV_MIN 1 |
| 81 |
#define PLL5_POSTDIV_MAX 7 |
| 82 |
#define PLL5_REFDIV_MIN 1 |
| 83 |
#define PLL5_REFDIV_MAX 2 |
| 84 |
#define PLL5_INTIN_MIN 20 |
| 85 |
#define PLL5_INTIN_MAX 320 |
| 86 |
#define PLL5_HSCLK_MIN 10000000 |
| 87 |
#define PLL5_HSCLK_MAX 187500000 |
| 88 |
|
| 89 |
/** |
| 90 |
* struct clk_hw_data - clock hardware data |
| 91 |
* @hw: clock hw |
| 92 |
* @conf: clock configuration (register offset, shift, width) |
| 93 |
* @sconf: clock status configuration (register offset, shift, width) |
| 94 |
* @priv: CPG private data structure |
| 95 |
*/ |
| 96 |
struct clk_hw_data { |
| 97 |
struct clk_hw hw; |
| 98 |
u32 conf; |
| 99 |
u32 sconf; |
| 100 |
struct rzg2l_cpg_priv *priv; |
| 101 |
}; |
| 102 |
|
| 103 |
#define to_clk_hw_data(_hw) container_of(_hw, struct clk_hw_data, hw) |
| 104 |
|
| 105 |
/** |
| 106 |
* struct sd_mux_hw_data - SD MUX clock hardware data |
| 107 |
* @hw_data: clock hw data |
| 108 |
* @mtable: clock mux table |
| 109 |
*/ |
| 110 |
struct sd_mux_hw_data { |
| 111 |
struct clk_hw_data hw_data; |
| 112 |
const u32 *mtable; |
| 113 |
}; |
| 114 |
|
| 115 |
#define to_sd_mux_hw_data(_hw) container_of(_hw, struct sd_mux_hw_data, hw_data) |
| 116 |
|
| 117 |
/** |
| 118 |
* struct div_hw_data - divider clock hardware data |
| 119 |
* @hw_data: clock hw data |
| 120 |
* @dtable: pointer to divider table |
| 121 |
* @invalid_rate: invalid rate for divider |
| 122 |
* @max_rate: maximum rate for divider |
| 123 |
* @width: divider width |
| 124 |
*/ |
| 125 |
struct div_hw_data { |
| 126 |
struct clk_hw_data hw_data; |
| 127 |
const struct clk_div_table *dtable; |
| 128 |
unsigned long invalid_rate; |
| 129 |
unsigned long max_rate; |
| 130 |
u32 width; |
| 131 |
}; |
| 132 |
|
| 133 |
#define to_div_hw_data(_hw) container_of(_hw, struct div_hw_data, hw_data) |
| 134 |
|
| 135 |
struct rzg2l_pll5_param { |
| 136 |
u32 pl5_fracin; |
| 137 |
u16 pl5_intin; |
| 138 |
u8 pl5_refdiv; |
| 139 |
u8 pl5_postdiv1; |
| 140 |
u8 pl5_postdiv2; |
| 141 |
u8 pl5_spread; |
| 142 |
}; |
| 143 |
|
| 144 |
/* PLL5 output will be used for DPI or MIPI-DSI */ |
| 145 |
static int dsi_div_target = PLL5_TARGET_DPI; |
| 146 |
|
| 147 |
/* Required division ratio for MIPI D-PHY clock depending on number of lanes and bpp. */ |
| 148 |
static u8 dsi_div_ab_desired; |
| 149 |
|
| 150 |
struct rzg2l_pll5_mux_dsi_div_param { |
| 151 |
u8 clksrc; |
| 152 |
u8 dsi_div_a; |
| 153 |
u8 dsi_div_b; |
| 154 |
}; |
| 155 |
|
| 156 |
/** |
| 157 |
* struct rzg2l_cpg_priv - Clock Pulse Generator Private Data |
| 158 |
* |
| 159 |
* @rcdev: Reset controller entity |
| 160 |
* @dev: CPG device |
| 161 |
* @base: CPG register block base address |
| 162 |
* @rmw_lock: protects register accesses |
| 163 |
* @clks: Array containing all Core and Module Clocks |
| 164 |
* @num_core_clks: Number of Core Clocks in clks[] |
| 165 |
* @num_mod_clks: Number of Module Clocks in clks[] |
| 166 |
* @num_resets: Number of Module Resets in info->resets[] |
| 167 |
* @last_dt_core_clk: ID of the last Core Clock exported to DT |
| 168 |
* @info: Pointer to platform data |
| 169 |
* @genpd: PM domain |
| 170 |
* @mux_dsi_div_params: pll5 mux and dsi div parameters |
| 171 |
*/ |
| 172 |
struct rzg2l_cpg_priv { |
| 173 |
struct reset_controller_dev rcdev; |
| 174 |
struct device *dev; |
| 175 |
void __iomem *base; |
| 176 |
spinlock_t rmw_lock; |
| 177 |
|
| 178 |
struct clk **clks; |
| 179 |
unsigned int num_core_clks; |
| 180 |
unsigned int num_mod_clks; |
| 181 |
unsigned int num_resets; |
| 182 |
unsigned int last_dt_core_clk; |
| 183 |
|
| 184 |
const struct rzg2l_cpg_info *info; |
| 185 |
|
| 186 |
struct generic_pm_domain genpd; |
| 187 |
|
| 188 |
struct rzg2l_pll5_mux_dsi_div_param mux_dsi_div_params; |
| 189 |
}; |
| 190 |
|
| 191 |
static inline u8 rzg2l_cpg_div_ab(u8 a, u8 b) |
| 192 |
{ |
| 193 |
return (b + 1) << a; |
| 194 |
} |
| 195 |
|
| 196 |
static void rzg2l_cpg_del_clk_provider(void *data) |
| 197 |
{ |
| 198 |
of_clk_del_provider(data); |
| 199 |
} |
| 200 |
|
| 201 |
/* Must be called in atomic context. */ |
| 202 |
static int rzg2l_cpg_wait_clk_update_done(void __iomem *base, u32 conf) |
| 203 |
{ |
| 204 |
u32 bitmask = GENMASK(GET_WIDTH(conf) - 1, 0) << GET_SHIFT(conf); |
| 205 |
u32 off = GET_REG_OFFSET(conf); |
| 206 |
u32 val; |
| 207 |
|
| 208 |
return readl_poll_timeout_atomic(base + off, val, !(val & bitmask), 10, 200); |
| 209 |
} |
| 210 |
|
| 211 |
int rzg2l_cpg_sd_clk_mux_notifier(struct notifier_block *nb, unsigned long event, |
| 212 |
void *data) |
| 213 |
{ |
| 214 |
struct clk_notifier_data *cnd = data; |
| 215 |
struct clk_hw *hw = __clk_get_hw(cnd->clk); |
| 216 |
struct clk_hw_data *clk_hw_data = to_clk_hw_data(hw); |
| 217 |
struct rzg2l_cpg_priv *priv = clk_hw_data->priv; |
| 218 |
u32 off = GET_REG_OFFSET(clk_hw_data->conf); |
| 219 |
u32 shift = GET_SHIFT(clk_hw_data->conf); |
| 220 |
const u32 clk_src_266 = 3; |
| 221 |
unsigned long flags; |
| 222 |
int ret; |
| 223 |
|
| 224 |
if (event != PRE_RATE_CHANGE || (cnd->new_rate / MEGA == 266)) |
| 225 |
return NOTIFY_DONE; |
| 226 |
|
| 227 |
spin_lock_irqsave(&priv->rmw_lock, flags); |
| 228 |
|
| 229 |
/* |
| 230 |
* As per the HW manual, we should not directly switch from 533 MHz to |
| 231 |
* 400 MHz and vice versa. To change the setting from 2’b01 (533 MHz) |
| 232 |
* to 2’b10 (400 MHz) or vice versa, Switch to 2’b11 (266 MHz) first, |
| 233 |
* and then switch to the target setting (2’b01 (533 MHz) or 2’b10 |
| 234 |
* (400 MHz)). |
| 235 |
* Setting a value of '0' to the SEL_SDHI0_SET or SEL_SDHI1_SET clock |
| 236 |
* switching register is prohibited. |
| 237 |
* The clock mux has 3 input clocks(533 MHz, 400 MHz, and 266 MHz), and |
| 238 |
* the index to value mapping is done by adding 1 to the index. |
| 239 |
*/ |
| 240 |
|
| 241 |
writel((CPG_WEN_BIT | clk_src_266) << shift, priv->base + off); |
| 242 |
|
| 243 |
/* Wait for the update done. */ |
| 244 |
ret = rzg2l_cpg_wait_clk_update_done(priv->base, clk_hw_data->sconf); |
| 245 |
|
| 246 |
spin_unlock_irqrestore(&priv->rmw_lock, flags); |
| 247 |
|
| 248 |
if (ret) |
| 249 |
dev_err(priv->dev, "failed to switch to safe clk source\n"); |
| 250 |
|
| 251 |
return notifier_from_errno(ret); |
| 252 |
} |
| 253 |
|
| 254 |
int rzg3s_cpg_div_clk_notifier(struct notifier_block *nb, unsigned long event, |
| 255 |
void *data) |
| 256 |
{ |
| 257 |
struct clk_notifier_data *cnd = data; |
| 258 |
struct clk_hw *hw = __clk_get_hw(cnd->clk); |
| 259 |
struct clk_hw_data *clk_hw_data = to_clk_hw_data(hw); |
| 260 |
struct div_hw_data *div_hw_data = to_div_hw_data(clk_hw_data); |
| 261 |
struct rzg2l_cpg_priv *priv = clk_hw_data->priv; |
| 262 |
u32 off = GET_REG_OFFSET(clk_hw_data->conf); |
| 263 |
u32 shift = GET_SHIFT(clk_hw_data->conf); |
| 264 |
unsigned long flags; |
| 265 |
int ret = 0; |
| 266 |
u32 val; |
| 267 |
|
| 268 |
if (event != PRE_RATE_CHANGE || !div_hw_data->invalid_rate || |
| 269 |
div_hw_data->invalid_rate % cnd->new_rate) |
| 270 |
return NOTIFY_DONE; |
| 271 |
|
| 272 |
spin_lock_irqsave(&priv->rmw_lock, flags); |
| 273 |
|
| 274 |
val = readl(priv->base + off); |
| 275 |
val >>= shift; |
| 276 |
val &= GENMASK(GET_WIDTH(clk_hw_data->conf) - 1, 0); |
| 277 |
|
| 278 |
/* |
| 279 |
* There are different constraints for the user of this notifiers as follows: |
| 280 |
* 1/ SD div cannot be 1 (val == 0) if parent rate is 800MHz |
| 281 |
* 2/ OCTA / SPI div cannot be 1 (val == 0) if parent rate is 400MHz |
| 282 |
* As SD can have only one parent having 800MHz and OCTA div can have |
| 283 |
* only one parent having 400MHz we took into account the parent rate |
| 284 |
* at the beginning of function (by checking invalid_rate % new_rate). |
| 285 |
* Now it is time to check the hardware divider and update it accordingly. |
| 286 |
*/ |
| 287 |
if (!val) { |
| 288 |
writel((CPG_WEN_BIT | 1) << shift, priv->base + off); |
| 289 |
/* Wait for the update done. */ |
| 290 |
ret = rzg2l_cpg_wait_clk_update_done(priv->base, clk_hw_data->sconf); |
| 291 |
} |
| 292 |
|
| 293 |
spin_unlock_irqrestore(&priv->rmw_lock, flags); |
| 294 |
|
| 295 |
if (ret) |
| 296 |
dev_err(priv->dev, "Failed to downgrade the div\n"); |
| 297 |
|
| 298 |
return notifier_from_errno(ret); |
| 299 |
} |
| 300 |
|
| 301 |
static int rzg2l_register_notifier(struct clk_hw *hw, const struct cpg_core_clk *core, |
| 302 |
struct rzg2l_cpg_priv *priv) |
| 303 |
{ |
| 304 |
struct notifier_block *nb; |
| 305 |
|
| 306 |
if (!core->notifier) |
| 307 |
return 0; |
| 308 |
|
| 309 |
nb = devm_kzalloc(priv->dev, sizeof(*nb), GFP_KERNEL); |
| 310 |
if (!nb) |
| 311 |
return -ENOMEM; |
| 312 |
|
| 313 |
nb->notifier_call = core->notifier; |
| 314 |
|
| 315 |
return clk_notifier_register(hw->clk, nb); |
| 316 |
} |
| 317 |
|
| 318 |
static unsigned long rzg3s_div_clk_recalc_rate(struct clk_hw *hw, |
| 319 |
unsigned long parent_rate) |
| 320 |
{ |
| 321 |
struct clk_hw_data *clk_hw_data = to_clk_hw_data(hw); |
| 322 |
struct div_hw_data *div_hw_data = to_div_hw_data(clk_hw_data); |
| 323 |
struct rzg2l_cpg_priv *priv = clk_hw_data->priv; |
| 324 |
u32 val; |
| 325 |
|
| 326 |
val = readl(priv->base + GET_REG_OFFSET(clk_hw_data->conf)); |
| 327 |
val >>= GET_SHIFT(clk_hw_data->conf); |
| 328 |
val &= GENMASK(GET_WIDTH(clk_hw_data->conf) - 1, 0); |
| 329 |
|
| 330 |
return divider_recalc_rate(hw, parent_rate, val, div_hw_data->dtable, |
| 331 |
CLK_DIVIDER_ROUND_CLOSEST, div_hw_data->width); |
| 332 |
} |
| 333 |
|
| 334 |
static int rzg3s_div_clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req) |
| 335 |
{ |
| 336 |
struct clk_hw_data *clk_hw_data = to_clk_hw_data(hw); |
| 337 |
struct div_hw_data *div_hw_data = to_div_hw_data(clk_hw_data); |
| 338 |
|
| 339 |
if (div_hw_data->max_rate && req->rate > div_hw_data->max_rate) |
| 340 |
req->rate = div_hw_data->max_rate; |
| 341 |
|
| 342 |
return divider_determine_rate(hw, req, div_hw_data->dtable, div_hw_data->width, |
| 343 |
CLK_DIVIDER_ROUND_CLOSEST); |
| 344 |
} |
| 345 |
|
| 346 |
static int rzg3s_div_clk_set_rate(struct clk_hw *hw, unsigned long rate, |
| 347 |
unsigned long parent_rate) |
| 348 |
{ |
| 349 |
struct clk_hw_data *clk_hw_data = to_clk_hw_data(hw); |
| 350 |
struct div_hw_data *div_hw_data = to_div_hw_data(clk_hw_data); |
| 351 |
struct rzg2l_cpg_priv *priv = clk_hw_data->priv; |
| 352 |
u32 off = GET_REG_OFFSET(clk_hw_data->conf); |
| 353 |
u32 shift = GET_SHIFT(clk_hw_data->conf); |
| 354 |
unsigned long flags; |
| 355 |
u32 val; |
| 356 |
int ret; |
| 357 |
|
| 358 |
val = divider_get_val(rate, parent_rate, div_hw_data->dtable, div_hw_data->width, |
| 359 |
CLK_DIVIDER_ROUND_CLOSEST); |
| 360 |
|
| 361 |
spin_lock_irqsave(&priv->rmw_lock, flags); |
| 362 |
writel((CPG_WEN_BIT | val) << shift, priv->base + off); |
| 363 |
/* Wait for the update done. */ |
| 364 |
ret = rzg2l_cpg_wait_clk_update_done(priv->base, clk_hw_data->sconf); |
| 365 |
spin_unlock_irqrestore(&priv->rmw_lock, flags); |
| 366 |
|
| 367 |
return ret; |
| 368 |
} |
| 369 |
|
| 370 |
static const struct clk_ops rzg3s_div_clk_ops = { |
| 371 |
.recalc_rate = rzg3s_div_clk_recalc_rate, |
| 372 |
.determine_rate = rzg3s_div_clk_determine_rate, |
| 373 |
.set_rate = rzg3s_div_clk_set_rate, |
| 374 |
}; |
| 375 |
|
| 376 |
static struct clk * __init |
| 377 |
rzg3s_cpg_div_clk_register(const struct cpg_core_clk *core, struct rzg2l_cpg_priv *priv) |
| 378 |
{ |
| 379 |
struct div_hw_data *div_hw_data; |
| 380 |
struct clk_init_data init = {}; |
| 381 |
const struct clk_div_table *clkt; |
| 382 |
struct clk_hw *clk_hw; |
| 383 |
const struct clk *parent; |
| 384 |
const char *parent_name; |
| 385 |
u32 max = 0; |
| 386 |
int ret; |
| 387 |
|
| 388 |
parent = priv->clks[core->parent]; |
| 389 |
if (IS_ERR(parent)) |
| 390 |
return ERR_CAST(parent); |
| 391 |
|
| 392 |
parent_name = __clk_get_name(parent); |
| 393 |
|
| 394 |
div_hw_data = devm_kzalloc(priv->dev, sizeof(*div_hw_data), GFP_KERNEL); |
| 395 |
if (!div_hw_data) |
| 396 |
return ERR_PTR(-ENOMEM); |
| 397 |
|
| 398 |
init.name = core->name; |
| 399 |
init.flags = core->flag; |
| 400 |
init.ops = &rzg3s_div_clk_ops; |
| 401 |
init.parent_names = &parent_name; |
| 402 |
init.num_parents = 1; |
| 403 |
|
| 404 |
/* Get the maximum divider to retrieve div width. */ |
| 405 |
for (clkt = core->dtable; clkt->div; clkt++) { |
| 406 |
if (max < clkt->div) |
| 407 |
max = clkt->div; |
| 408 |
} |
| 409 |
|
| 410 |
div_hw_data->hw_data.priv = priv; |
| 411 |
div_hw_data->hw_data.conf = core->conf; |
| 412 |
div_hw_data->hw_data.sconf = core->sconf; |
| 413 |
div_hw_data->dtable = core->dtable; |
| 414 |
div_hw_data->invalid_rate = core->invalid_rate; |
| 415 |
div_hw_data->max_rate = core->max_rate; |
| 416 |
div_hw_data->width = fls(max) - 1; |
| 417 |
|
| 418 |
clk_hw = &div_hw_data->hw_data.hw; |
| 419 |
clk_hw->init = &init; |
| 420 |
|
| 421 |
ret = devm_clk_hw_register(priv->dev, clk_hw); |
| 422 |
if (ret) |
| 423 |
return ERR_PTR(ret); |
| 424 |
|
| 425 |
ret = rzg2l_register_notifier(clk_hw, core, priv); |
| 426 |
if (ret) { |
| 427 |
dev_err(priv->dev, "Failed to register notifier for %s\n", |
| 428 |
core->name); |
| 429 |
return ERR_PTR(ret); |
| 430 |
} |
| 431 |
|
| 432 |
return clk_hw->clk; |
| 433 |
} |
| 434 |
|
| 435 |
static struct clk * __init |
| 436 |
rzg2l_cpg_div_clk_register(const struct cpg_core_clk *core, |
| 437 |
struct rzg2l_cpg_priv *priv) |
| 438 |
{ |
| 439 |
void __iomem *base = priv->base; |
| 440 |
struct device *dev = priv->dev; |
| 441 |
const struct clk *parent; |
| 442 |
const char *parent_name; |
| 443 |
struct clk_hw *clk_hw; |
| 444 |
|
| 445 |
parent = priv->clks[core->parent]; |
| 446 |
if (IS_ERR(parent)) |
| 447 |
return ERR_CAST(parent); |
| 448 |
|
| 449 |
parent_name = __clk_get_name(parent); |
| 450 |
|
| 451 |
if (core->dtable) |
| 452 |
clk_hw = clk_hw_register_divider_table(dev, core->name, |
| 453 |
parent_name, 0, |
| 454 |
base + GET_REG_OFFSET(core->conf), |
| 455 |
GET_SHIFT(core->conf), |
| 456 |
GET_WIDTH(core->conf), |
| 457 |
core->flag, |
| 458 |
core->dtable, |
| 459 |
&priv->rmw_lock); |
| 460 |
else |
| 461 |
clk_hw = clk_hw_register_divider(dev, core->name, |
| 462 |
parent_name, 0, |
| 463 |
base + GET_REG_OFFSET(core->conf), |
| 464 |
GET_SHIFT(core->conf), |
| 465 |
GET_WIDTH(core->conf), |
| 466 |
core->flag, &priv->rmw_lock); |
| 467 |
|
| 468 |
if (IS_ERR(clk_hw)) |
| 469 |
return ERR_CAST(clk_hw); |
| 470 |
|
| 471 |
return clk_hw->clk; |
| 472 |
} |
| 473 |
|
| 474 |
static struct clk * __init |
| 475 |
rzg2l_cpg_mux_clk_register(const struct cpg_core_clk *core, |
| 476 |
struct rzg2l_cpg_priv *priv) |
| 477 |
{ |
| 478 |
const struct clk_hw *clk_hw; |
| 479 |
|
| 480 |
clk_hw = devm_clk_hw_register_mux(priv->dev, core->name, |
| 481 |
core->parent_names, core->num_parents, |
| 482 |
core->flag, |
| 483 |
priv->base + GET_REG_OFFSET(core->conf), |
| 484 |
GET_SHIFT(core->conf), |
| 485 |
GET_WIDTH(core->conf), |
| 486 |
core->mux_flags, &priv->rmw_lock); |
| 487 |
if (IS_ERR(clk_hw)) |
| 488 |
return ERR_CAST(clk_hw); |
| 489 |
|
| 490 |
return clk_hw->clk; |
| 491 |
} |
| 492 |
|
| 493 |
static int rzg2l_cpg_sd_clk_mux_set_parent(struct clk_hw *hw, u8 index) |
| 494 |
{ |
| 495 |
struct clk_hw_data *clk_hw_data = to_clk_hw_data(hw); |
| 496 |
struct sd_mux_hw_data *sd_mux_hw_data = to_sd_mux_hw_data(clk_hw_data); |
| 497 |
struct rzg2l_cpg_priv *priv = clk_hw_data->priv; |
| 498 |
u32 off = GET_REG_OFFSET(clk_hw_data->conf); |
| 499 |
u32 shift = GET_SHIFT(clk_hw_data->conf); |
| 500 |
unsigned long flags; |
| 501 |
u32 val; |
| 502 |
int ret; |
| 503 |
|
| 504 |
val = clk_mux_index_to_val(sd_mux_hw_data->mtable, CLK_MUX_ROUND_CLOSEST, index); |
| 505 |
|
| 506 |
spin_lock_irqsave(&priv->rmw_lock, flags); |
| 507 |
|
| 508 |
writel((CPG_WEN_BIT | val) << shift, priv->base + off); |
| 509 |
|
| 510 |
/* Wait for the update done. */ |
| 511 |
ret = rzg2l_cpg_wait_clk_update_done(priv->base, clk_hw_data->sconf); |
| 512 |
|
| 513 |
spin_unlock_irqrestore(&priv->rmw_lock, flags); |
| 514 |
|
| 515 |
if (ret) |
| 516 |
dev_err(priv->dev, "Failed to switch parent\n"); |
| 517 |
|
| 518 |
return ret; |
| 519 |
} |
| 520 |
|
| 521 |
static u8 rzg2l_cpg_sd_clk_mux_get_parent(struct clk_hw *hw) |
| 522 |
{ |
| 523 |
struct clk_hw_data *clk_hw_data = to_clk_hw_data(hw); |
| 524 |
struct sd_mux_hw_data *sd_mux_hw_data = to_sd_mux_hw_data(clk_hw_data); |
| 525 |
struct rzg2l_cpg_priv *priv = clk_hw_data->priv; |
| 526 |
u32 val; |
| 527 |
|
| 528 |
val = readl(priv->base + GET_REG_OFFSET(clk_hw_data->conf)); |
| 529 |
val >>= GET_SHIFT(clk_hw_data->conf); |
| 530 |
val &= GENMASK(GET_WIDTH(clk_hw_data->conf) - 1, 0); |
| 531 |
|
| 532 |
return clk_mux_val_to_index(hw, sd_mux_hw_data->mtable, CLK_MUX_ROUND_CLOSEST, val); |
| 533 |
} |
| 534 |
|
| 535 |
static const struct clk_ops rzg2l_cpg_sd_clk_mux_ops = { |
| 536 |
.determine_rate = __clk_mux_determine_rate_closest, |
| 537 |
.set_parent = rzg2l_cpg_sd_clk_mux_set_parent, |
| 538 |
.get_parent = rzg2l_cpg_sd_clk_mux_get_parent, |
| 539 |
}; |
| 540 |
|
| 541 |
static struct clk * __init |
| 542 |
rzg2l_cpg_sd_mux_clk_register(const struct cpg_core_clk *core, |
| 543 |
struct rzg2l_cpg_priv *priv) |
| 544 |
{ |
| 545 |
struct sd_mux_hw_data *sd_mux_hw_data; |
| 546 |
struct clk_init_data init; |
| 547 |
struct clk_hw *clk_hw; |
| 548 |
int ret; |
| 549 |
|
| 550 |
sd_mux_hw_data = devm_kzalloc(priv->dev, sizeof(*sd_mux_hw_data), GFP_KERNEL); |
| 551 |
if (!sd_mux_hw_data) |
| 552 |
return ERR_PTR(-ENOMEM); |
| 553 |
|
| 554 |
sd_mux_hw_data->hw_data.priv = priv; |
| 555 |
sd_mux_hw_data->hw_data.conf = core->conf; |
| 556 |
sd_mux_hw_data->hw_data.sconf = core->sconf; |
| 557 |
sd_mux_hw_data->mtable = core->mtable; |
| 558 |
|
| 559 |
init.name = core->name; |
| 560 |
init.ops = &rzg2l_cpg_sd_clk_mux_ops; |
| 561 |
init.flags = core->flag; |
| 562 |
init.num_parents = core->num_parents; |
| 563 |
init.parent_names = core->parent_names; |
| 564 |
|
| 565 |
clk_hw = &sd_mux_hw_data->hw_data.hw; |
| 566 |
clk_hw->init = &init; |
| 567 |
|
| 568 |
ret = devm_clk_hw_register(priv->dev, clk_hw); |
| 569 |
if (ret) |
| 570 |
return ERR_PTR(ret); |
| 571 |
|
| 572 |
ret = rzg2l_register_notifier(clk_hw, core, priv); |
| 573 |
if (ret) { |
| 574 |
dev_err(priv->dev, "Failed to register notifier for %s\n", |
| 575 |
core->name); |
| 576 |
return ERR_PTR(ret); |
| 577 |
} |
| 578 |
|
| 579 |
return clk_hw->clk; |
| 580 |
} |
| 581 |
|
| 582 |
/* |
| 583 |
* VCO-->[POSTDIV1,2]--FOUTPOSTDIV--------------->| |
| 584 |
* | |-->[1/(DSI DIV A * B)]--> MIPI_DSI_VCLK |
| 585 |
* |-->[1/2]--FOUT1PH0-->| |
| 586 |
* | |
| 587 |
* |------->[1/16]--------------------------------> hsclk (MIPI-PHY) |
| 588 |
*/ |
| 589 |
static unsigned long |
| 590 |
rzg2l_cpg_get_foutpostdiv_rate(struct rzg2l_cpg_priv *priv, |
| 591 |
struct rzg2l_pll5_param *params, |
| 592 |
unsigned long rate) |
| 593 |
{ |
| 594 |
const u32 extal_hz = EXTAL_FREQ_IN_MEGA_HZ * MEGA; |
| 595 |
unsigned long foutpostdiv_rate; |
| 596 |
unsigned int a, b, odd; |
| 597 |
unsigned long hsclk; |
| 598 |
u8 dsi_div_ab_calc; |
| 599 |
u64 foutvco_rate; |
| 600 |
|
| 601 |
if (dsi_div_target == PLL5_TARGET_DSI) { |
| 602 |
/* Check hsclk */ |
| 603 |
hsclk = rate * dsi_div_ab_desired / 16; |
| 604 |
if (hsclk < PLL5_HSCLK_MIN || hsclk > PLL5_HSCLK_MAX) { |
| 605 |
dev_err(priv->dev, "hsclk out of range\n"); |
| 606 |
return 0; |
| 607 |
} |
| 608 |
|
| 609 |
/* Determine the correct clock source based on even/odd of the divider */ |
| 610 |
odd = dsi_div_ab_desired & 1; |
| 611 |
if (odd) { |
| 612 |
priv->mux_dsi_div_params.clksrc = 0; /* FOUTPOSTDIV */ |
| 613 |
dsi_div_ab_calc = dsi_div_ab_desired; |
| 614 |
} else { |
| 615 |
priv->mux_dsi_div_params.clksrc = 1; /* FOUT1PH0 */ |
| 616 |
dsi_div_ab_calc = dsi_div_ab_desired / 2; |
| 617 |
} |
| 618 |
|
| 619 |
/* Calculate the DIV_DSI_A and DIV_DSI_B based on the desired divider */ |
| 620 |
for (a = 0; a < 4; a++) { |
| 621 |
/* FOUT1PH0: Max output of DIV_DSI_A is 750MHz so at least 1/2 to be safe */ |
| 622 |
if (!odd && a == 0) |
| 623 |
continue; |
| 624 |
|
| 625 |
/* FOUTPOSTDIV: DIV_DSI_A must always be 1/1 */ |
| 626 |
if (odd && a != 0) |
| 627 |
break; |
| 628 |
|
| 629 |
for (b = 0; b < 16; b++) { |
| 630 |
/* FOUTPOSTDIV: DIV_DSI_B must always be odd divider 1/(b+1) */ |
| 631 |
if (odd && b & 1) |
| 632 |
continue; |
| 633 |
|
| 634 |
if (rzg2l_cpg_div_ab(a, b) == dsi_div_ab_calc) { |
| 635 |
priv->mux_dsi_div_params.dsi_div_a = a; |
| 636 |
priv->mux_dsi_div_params.dsi_div_b = b; |
| 637 |
goto calc_pll_clk; |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
dev_err(priv->dev, "Failed to calculate DIV_DSI_A,B\n"); |
| 643 |
|
| 644 |
return 0; |
| 645 |
} else if (dsi_div_target == PLL5_TARGET_DPI) { |
| 646 |
/* Fixed settings for DPI */ |
| 647 |
priv->mux_dsi_div_params.clksrc = 0; |
| 648 |
priv->mux_dsi_div_params.dsi_div_a = 3; /* Divided by 8 */ |
| 649 |
priv->mux_dsi_div_params.dsi_div_b = 0; /* Divided by 1 */ |
| 650 |
dsi_div_ab_desired = rzg2l_cpg_div_ab(priv->mux_dsi_div_params.dsi_div_a, |
| 651 |
priv->mux_dsi_div_params.dsi_div_b); |
| 652 |
} |
| 653 |
|
| 654 |
calc_pll_clk: |
| 655 |
/* PLL5 (MIPI_DSI_PLLCLK) = VCO / POSTDIV1 / POSTDIV2 */ |
| 656 |
for (params->pl5_postdiv1 = PLL5_POSTDIV_MIN; |
| 657 |
params->pl5_postdiv1 <= PLL5_POSTDIV_MAX; |
| 658 |
params->pl5_postdiv1++) { |
| 659 |
for (params->pl5_postdiv2 = PLL5_POSTDIV_MIN; |
| 660 |
params->pl5_postdiv2 <= PLL5_POSTDIV_MAX; |
| 661 |
params->pl5_postdiv2++) { |
| 662 |
foutvco_rate = rate * params->pl5_postdiv1 * params->pl5_postdiv2 * |
| 663 |
dsi_div_ab_desired; |
| 664 |
if (foutvco_rate <= PLL5_FOUTVCO_MIN || foutvco_rate >= PLL5_FOUTVCO_MAX) |
| 665 |
continue; |
| 666 |
|
| 667 |
for (params->pl5_refdiv = PLL5_REFDIV_MIN; |
| 668 |
params->pl5_refdiv <= PLL5_REFDIV_MAX; |
| 669 |
params->pl5_refdiv++) { |
| 670 |
u32 rem; |
| 671 |
|
| 672 |
params->pl5_intin = div_u64_rem(foutvco_rate * params->pl5_refdiv, |
| 673 |
extal_hz, &rem); |
| 674 |
|
| 675 |
if (params->pl5_intin < PLL5_INTIN_MIN || |
| 676 |
params->pl5_intin > PLL5_INTIN_MAX) |
| 677 |
continue; |
| 678 |
|
| 679 |
params->pl5_fracin = div_u64((u64)rem << 24, extal_hz); |
| 680 |
|
| 681 |
goto clk_valid; |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
dev_err(priv->dev, "Failed to calculate PLL5 settings\n"); |
| 687 |
return 0; |
| 688 |
|
| 689 |
clk_valid: |
| 690 |
params->pl5_spread = 0x16; |
| 691 |
|
| 692 |
foutvco_rate = div_u64(mul_u32_u32(EXTAL_FREQ_IN_MEGA_HZ * MEGA, |
| 693 |
(params->pl5_intin << 24) + params->pl5_fracin), |
| 694 |
params->pl5_refdiv) >> 24; |
| 695 |
foutpostdiv_rate = DIV_U64_ROUND_CLOSEST(foutvco_rate, |
| 696 |
params->pl5_postdiv1 * params->pl5_postdiv2); |
| 697 |
|
| 698 |
return foutpostdiv_rate; |
| 699 |
} |
| 700 |
|
| 701 |
struct dsi_div_hw_data { |
| 702 |
struct clk_hw hw; |
| 703 |
u32 conf; |
| 704 |
unsigned long rate; |
| 705 |
struct rzg2l_cpg_priv *priv; |
| 706 |
}; |
| 707 |
|
| 708 |
#define to_dsi_div_hw_data(_hw) container_of(_hw, struct dsi_div_hw_data, hw) |
| 709 |
|
| 710 |
static unsigned long rzg2l_cpg_dsi_div_recalc_rate(struct clk_hw *hw, |
| 711 |
unsigned long parent_rate) |
| 712 |
{ |
| 713 |
struct dsi_div_hw_data *dsi_div = to_dsi_div_hw_data(hw); |
| 714 |
unsigned long rate = dsi_div->rate; |
| 715 |
|
| 716 |
if (!rate) |
| 717 |
rate = parent_rate; |
| 718 |
|
| 719 |
return rate; |
| 720 |
} |
| 721 |
|
| 722 |
static unsigned long rzg2l_cpg_get_vclk_parent_rate(struct clk_hw *hw, |
| 723 |
unsigned long rate) |
| 724 |
{ |
| 725 |
struct dsi_div_hw_data *dsi_div = to_dsi_div_hw_data(hw); |
| 726 |
struct rzg2l_cpg_priv *priv = dsi_div->priv; |
| 727 |
struct rzg2l_pll5_param params; |
| 728 |
unsigned long parent_rate; |
| 729 |
|
| 730 |
parent_rate = rzg2l_cpg_get_foutpostdiv_rate(priv, ¶ms, rate); |
| 731 |
|
| 732 |
if (priv->mux_dsi_div_params.clksrc) |
| 733 |
parent_rate /= 2; |
| 734 |
|
| 735 |
return parent_rate; |
| 736 |
} |
| 737 |
|
| 738 |
static int rzg2l_cpg_dsi_div_determine_rate(struct clk_hw *hw, |
| 739 |
struct clk_rate_request *req) |
| 740 |
{ |
| 741 |
if (req->rate > MAX_VCLK_FREQ) |
| 742 |
req->rate = MAX_VCLK_FREQ; |
| 743 |
|
| 744 |
req->best_parent_rate = rzg2l_cpg_get_vclk_parent_rate(hw, req->rate); |
| 745 |
|
| 746 |
if (!req->best_parent_rate) |
| 747 |
return -EINVAL; |
| 748 |
|
| 749 |
return 0; |
| 750 |
} |
| 751 |
|
| 752 |
void rzg2l_cpg_dsi_div_set_divider(u8 divider, int target) |
| 753 |
{ |
| 754 |
dsi_div_ab_desired = divider; |
| 755 |
dsi_div_target = target; |
| 756 |
} |
| 757 |
EXPORT_SYMBOL_GPL(rzg2l_cpg_dsi_div_set_divider); |
| 758 |
|
| 759 |
static int rzg2l_cpg_dsi_div_set_rate(struct clk_hw *hw, |
| 760 |
unsigned long rate, |
| 761 |
unsigned long parent_rate) |
| 762 |
{ |
| 763 |
struct dsi_div_hw_data *dsi_div = to_dsi_div_hw_data(hw); |
| 764 |
struct rzg2l_cpg_priv *priv = dsi_div->priv; |
| 765 |
|
| 766 |
/* |
| 767 |
* MUX -->DIV_DSI_{A,B} -->M3 -->VCLK |
| 768 |
* |
| 769 |
* Based on the dot clock, the DSI divider clock sets the divider value, |
| 770 |
* calculates the pll parameters for generating FOUTPOSTDIV and the clk |
| 771 |
* source for the MUX and propagates that info to the parents. |
| 772 |
*/ |
| 773 |
|
| 774 |
if (!rate || rate > MAX_VCLK_FREQ) |
| 775 |
return -EINVAL; |
| 776 |
|
| 777 |
dsi_div->rate = rate; |
| 778 |
writel(CPG_PL5_SDIV_DIV_DSI_A_WEN | CPG_PL5_SDIV_DIV_DSI_B_WEN | |
| 779 |
(priv->mux_dsi_div_params.dsi_div_a << 0) | |
| 780 |
(priv->mux_dsi_div_params.dsi_div_b << 8), |
| 781 |
priv->base + CPG_PL5_SDIV); |
| 782 |
|
| 783 |
return 0; |
| 784 |
} |
| 785 |
|
| 786 |
static const struct clk_ops rzg2l_cpg_dsi_div_ops = { |
| 787 |
.recalc_rate = rzg2l_cpg_dsi_div_recalc_rate, |
| 788 |
.determine_rate = rzg2l_cpg_dsi_div_determine_rate, |
| 789 |
.set_rate = rzg2l_cpg_dsi_div_set_rate, |
| 790 |
}; |
| 791 |
|
| 792 |
static struct clk * __init |
| 793 |
rzg2l_cpg_dsi_div_clk_register(const struct cpg_core_clk *core, |
| 794 |
struct rzg2l_cpg_priv *priv) |
| 795 |
{ |
| 796 |
struct dsi_div_hw_data *clk_hw_data; |
| 797 |
const struct clk *parent; |
| 798 |
const char *parent_name; |
| 799 |
struct clk_init_data init; |
| 800 |
struct clk_hw *clk_hw; |
| 801 |
int ret; |
| 802 |
|
| 803 |
parent = priv->clks[core->parent]; |
| 804 |
if (IS_ERR(parent)) |
| 805 |
return ERR_CAST(parent); |
| 806 |
|
| 807 |
clk_hw_data = devm_kzalloc(priv->dev, sizeof(*clk_hw_data), GFP_KERNEL); |
| 808 |
if (!clk_hw_data) |
| 809 |
return ERR_PTR(-ENOMEM); |
| 810 |
|
| 811 |
clk_hw_data->priv = priv; |
| 812 |
|
| 813 |
parent_name = __clk_get_name(parent); |
| 814 |
init.name = core->name; |
| 815 |
init.ops = &rzg2l_cpg_dsi_div_ops; |
| 816 |
init.flags = CLK_SET_RATE_PARENT; |
| 817 |
init.parent_names = &parent_name; |
| 818 |
init.num_parents = 1; |
| 819 |
|
| 820 |
clk_hw = &clk_hw_data->hw; |
| 821 |
clk_hw->init = &init; |
| 822 |
|
| 823 |
ret = devm_clk_hw_register(priv->dev, clk_hw); |
| 824 |
if (ret) |
| 825 |
return ERR_PTR(ret); |
| 826 |
|
| 827 |
return clk_hw->clk; |
| 828 |
} |
| 829 |
|
| 830 |
struct pll5_mux_hw_data { |
| 831 |
struct clk_hw hw; |
| 832 |
u32 conf; |
| 833 |
unsigned long rate; |
| 834 |
struct rzg2l_cpg_priv *priv; |
| 835 |
}; |
| 836 |
|
| 837 |
#define to_pll5_mux_hw_data(_hw) container_of(_hw, struct pll5_mux_hw_data, hw) |
| 838 |
|
| 839 |
static int rzg2l_cpg_pll5_4_clk_mux_determine_rate(struct clk_hw *hw, |
| 840 |
struct clk_rate_request *req) |
| 841 |
{ |
| 842 |
struct clk_hw *parent; |
| 843 |
struct pll5_mux_hw_data *hwdata = to_pll5_mux_hw_data(hw); |
| 844 |
struct rzg2l_cpg_priv *priv = hwdata->priv; |
| 845 |
|
| 846 |
parent = clk_hw_get_parent_by_index(hw, priv->mux_dsi_div_params.clksrc); |
| 847 |
req->best_parent_hw = parent; |
| 848 |
req->best_parent_rate = req->rate; |
| 849 |
|
| 850 |
return 0; |
| 851 |
} |
| 852 |
|
| 853 |
static int rzg2l_cpg_pll5_4_clk_mux_set_parent(struct clk_hw *hw, u8 index) |
| 854 |
{ |
| 855 |
struct pll5_mux_hw_data *hwdata = to_pll5_mux_hw_data(hw); |
| 856 |
struct rzg2l_cpg_priv *priv = hwdata->priv; |
| 857 |
|
| 858 |
/* |
| 859 |
* FOUTPOSTDIV--->| |
| 860 |
* | | -->MUX -->DIV_DSIA_B -->M3 -->VCLK |
| 861 |
* |--FOUT1PH0-->| |
| 862 |
* |
| 863 |
* Based on the dot clock, the DSI divider clock calculates the parent |
| 864 |
* rate and clk source for the MUX. It propagates that info to |
| 865 |
* pll5_4_clk_mux which sets the clock source for DSI divider clock. |
| 866 |
*/ |
| 867 |
|
| 868 |
writel(CPG_OTHERFUNC1_REG_RES0_ON_WEN | index, |
| 869 |
priv->base + CPG_OTHERFUNC1_REG); |
| 870 |
|
| 871 |
return 0; |
| 872 |
} |
| 873 |
|
| 874 |
static u8 rzg2l_cpg_pll5_4_clk_mux_get_parent(struct clk_hw *hw) |
| 875 |
{ |
| 876 |
struct pll5_mux_hw_data *hwdata = to_pll5_mux_hw_data(hw); |
| 877 |
struct rzg2l_cpg_priv *priv = hwdata->priv; |
| 878 |
|
| 879 |
return readl(priv->base + GET_REG_OFFSET(hwdata->conf)); |
| 880 |
} |
| 881 |
|
| 882 |
static const struct clk_ops rzg2l_cpg_pll5_4_clk_mux_ops = { |
| 883 |
.determine_rate = rzg2l_cpg_pll5_4_clk_mux_determine_rate, |
| 884 |
.set_parent = rzg2l_cpg_pll5_4_clk_mux_set_parent, |
| 885 |
.get_parent = rzg2l_cpg_pll5_4_clk_mux_get_parent, |
| 886 |
}; |
| 887 |
|
| 888 |
static struct clk * __init |
| 889 |
rzg2l_cpg_pll5_4_mux_clk_register(const struct cpg_core_clk *core, |
| 890 |
struct rzg2l_cpg_priv *priv) |
| 891 |
{ |
| 892 |
struct pll5_mux_hw_data *clk_hw_data; |
| 893 |
struct clk_init_data init; |
| 894 |
struct clk_hw *clk_hw; |
| 895 |
int ret; |
| 896 |
|
| 897 |
clk_hw_data = devm_kzalloc(priv->dev, sizeof(*clk_hw_data), GFP_KERNEL); |
| 898 |
if (!clk_hw_data) |
| 899 |
return ERR_PTR(-ENOMEM); |
| 900 |
|
| 901 |
clk_hw_data->priv = priv; |
| 902 |
clk_hw_data->conf = core->conf; |
| 903 |
|
| 904 |
init.name = core->name; |
| 905 |
init.ops = &rzg2l_cpg_pll5_4_clk_mux_ops; |
| 906 |
init.flags = CLK_SET_RATE_PARENT; |
| 907 |
init.num_parents = core->num_parents; |
| 908 |
init.parent_names = core->parent_names; |
| 909 |
|
| 910 |
clk_hw = &clk_hw_data->hw; |
| 911 |
clk_hw->init = &init; |
| 912 |
|
| 913 |
ret = devm_clk_hw_register(priv->dev, clk_hw); |
| 914 |
if (ret) |
| 915 |
return ERR_PTR(ret); |
| 916 |
|
| 917 |
return clk_hw->clk; |
| 918 |
} |
| 919 |
|
| 920 |
struct sipll5 { |
| 921 |
struct clk_hw hw; |
| 922 |
u32 conf; |
| 923 |
unsigned long foutpostdiv_rate; |
| 924 |
struct rzg2l_cpg_priv *priv; |
| 925 |
}; |
| 926 |
|
| 927 |
#define to_sipll5(_hw) container_of(_hw, struct sipll5, hw) |
| 928 |
|
| 929 |
static unsigned long rzg2l_cpg_sipll5_recalc_rate(struct clk_hw *hw, |
| 930 |
unsigned long parent_rate) |
| 931 |
{ |
| 932 |
struct sipll5 *sipll5 = to_sipll5(hw); |
| 933 |
unsigned long pll5_rate = sipll5->foutpostdiv_rate; |
| 934 |
|
| 935 |
if (!pll5_rate) |
| 936 |
pll5_rate = parent_rate; |
| 937 |
|
| 938 |
return pll5_rate; |
| 939 |
} |
| 940 |
|
| 941 |
static int rzg2l_cpg_sipll5_determine_rate(struct clk_hw *hw, |
| 942 |
struct clk_rate_request *req) |
| 943 |
{ |
| 944 |
return 0; |
| 945 |
} |
| 946 |
|
| 947 |
static int rzg2l_cpg_sipll5_set_rate(struct clk_hw *hw, |
| 948 |
unsigned long rate, |
| 949 |
unsigned long parent_rate) |
| 950 |
{ |
| 951 |
struct sipll5 *sipll5 = to_sipll5(hw); |
| 952 |
struct rzg2l_cpg_priv *priv = sipll5->priv; |
| 953 |
struct rzg2l_pll5_param params; |
| 954 |
unsigned long vclk_rate; |
| 955 |
int ret; |
| 956 |
u32 val; |
| 957 |
|
| 958 |
/* |
| 959 |
* OSC --> PLL5 --> FOUTPOSTDIV-->| |
| 960 |
* | | -->MUX -->DIV_DSIA_B -->M3 -->VCLK |
| 961 |
* |--FOUT1PH0-->| |
| 962 |
* |
| 963 |
* Based on the dot clock, the DSI divider clock calculates the parent |
| 964 |
* rate and the pll5 parameters for generating FOUTPOSTDIV. It propagates |
| 965 |
* that info to sipll5 which sets parameters for generating FOUTPOSTDIV. |
| 966 |
* |
| 967 |
* OSC --> PLL5 --> FOUTPOSTDIV |
| 968 |
*/ |
| 969 |
|
| 970 |
if (!rate) |
| 971 |
return -EINVAL; |
| 972 |
|
| 973 |
vclk_rate = rate / dsi_div_ab_desired; |
| 974 |
sipll5->foutpostdiv_rate = |
| 975 |
rzg2l_cpg_get_foutpostdiv_rate(priv, ¶ms, vclk_rate); |
| 976 |
|
| 977 |
/* Put PLL5 into standby mode */ |
| 978 |
writel(CPG_SIPLL5_STBY_RESETB_WEN, priv->base + CPG_SIPLL5_STBY); |
| 979 |
ret = readl_poll_timeout(priv->base + CPG_SIPLL5_MON, val, |
| 980 |
!(val & CPG_SIPLL5_MON_PLL5_LOCK), 100, 250000); |
| 981 |
if (ret) { |
| 982 |
dev_err(priv->dev, "failed to release pll5 lock\n"); |
| 983 |
return ret; |
| 984 |
} |
| 985 |
|
| 986 |
/* Output clock setting 1 */ |
| 987 |
writel((params.pl5_postdiv1 << 0) | (params.pl5_postdiv2 << 4) | |
| 988 |
(params.pl5_refdiv << 8), priv->base + CPG_SIPLL5_CLK1); |
| 989 |
|
| 990 |
/* Output clock setting, SSCG modulation value setting 3 */ |
| 991 |
writel((params.pl5_fracin << 8), priv->base + CPG_SIPLL5_CLK3); |
| 992 |
|
| 993 |
/* Output clock setting 4 */ |
| 994 |
writel(CPG_SIPLL5_CLK4_RESV_LSB | (params.pl5_intin << 16), |
| 995 |
priv->base + CPG_SIPLL5_CLK4); |
| 996 |
|
| 997 |
/* Output clock setting 5 */ |
| 998 |
writel(params.pl5_spread, priv->base + CPG_SIPLL5_CLK5); |
| 999 |
|
| 1000 |
/* PLL normal mode setting */ |
| 1001 |
writel(CPG_SIPLL5_STBY_DOWNSPREAD_WEN | CPG_SIPLL5_STBY_SSCG_EN_WEN | |
| 1002 |
CPG_SIPLL5_STBY_RESETB_WEN | CPG_SIPLL5_STBY_RESETB, |
| 1003 |
priv->base + CPG_SIPLL5_STBY); |
| 1004 |
|
| 1005 |
/* PLL normal mode transition, output clock stability check */ |
| 1006 |
ret = readl_poll_timeout(priv->base + CPG_SIPLL5_MON, val, |
| 1007 |
(val & CPG_SIPLL5_MON_PLL5_LOCK), 100, 250000); |
| 1008 |
if (ret) { |
| 1009 |
dev_err(priv->dev, "failed to lock pll5\n"); |
| 1010 |
return ret; |
| 1011 |
} |
| 1012 |
|
| 1013 |
return 0; |
| 1014 |
} |
| 1015 |
|
| 1016 |
static const struct clk_ops rzg2l_cpg_sipll5_ops = { |
| 1017 |
.recalc_rate = rzg2l_cpg_sipll5_recalc_rate, |
| 1018 |
.determine_rate = rzg2l_cpg_sipll5_determine_rate, |
| 1019 |
.set_rate = rzg2l_cpg_sipll5_set_rate, |
| 1020 |
}; |
| 1021 |
|
| 1022 |
static struct clk * __init |
| 1023 |
rzg2l_cpg_sipll5_register(const struct cpg_core_clk *core, |
| 1024 |
struct rzg2l_cpg_priv *priv) |
| 1025 |
{ |
| 1026 |
const struct clk *parent; |
| 1027 |
struct clk_init_data init; |
| 1028 |
const char *parent_name; |
| 1029 |
struct sipll5 *sipll5; |
| 1030 |
struct clk_hw *clk_hw; |
| 1031 |
int ret; |
| 1032 |
|
| 1033 |
parent = priv->clks[core->parent]; |
| 1034 |
if (IS_ERR(parent)) |
| 1035 |
return ERR_CAST(parent); |
| 1036 |
|
| 1037 |
sipll5 = devm_kzalloc(priv->dev, sizeof(*sipll5), GFP_KERNEL); |
| 1038 |
if (!sipll5) |
| 1039 |
return ERR_PTR(-ENOMEM); |
| 1040 |
|
| 1041 |
init.name = core->name; |
| 1042 |
parent_name = __clk_get_name(parent); |
| 1043 |
init.ops = &rzg2l_cpg_sipll5_ops; |
| 1044 |
init.flags = 0; |
| 1045 |
init.parent_names = &parent_name; |
| 1046 |
init.num_parents = 1; |
| 1047 |
|
| 1048 |
sipll5->hw.init = &init; |
| 1049 |
sipll5->conf = core->conf; |
| 1050 |
sipll5->priv = priv; |
| 1051 |
|
| 1052 |
writel(CPG_SIPLL5_STBY_SSCG_EN_WEN | CPG_SIPLL5_STBY_RESETB_WEN | |
| 1053 |
CPG_SIPLL5_STBY_RESETB, priv->base + CPG_SIPLL5_STBY); |
| 1054 |
|
| 1055 |
clk_hw = &sipll5->hw; |
| 1056 |
clk_hw->init = &init; |
| 1057 |
|
| 1058 |
ret = devm_clk_hw_register(priv->dev, clk_hw); |
| 1059 |
if (ret) |
| 1060 |
return ERR_PTR(ret); |
| 1061 |
|
| 1062 |
rzg2l_cpg_dsi_div_set_divider(8, PLL5_TARGET_DPI); |
| 1063 |
|
| 1064 |
return clk_hw->clk; |
| 1065 |
} |
| 1066 |
|
| 1067 |
struct pll_clk { |
| 1068 |
struct clk_hw hw; |
| 1069 |
unsigned long default_rate; |
| 1070 |
unsigned int conf; |
| 1071 |
unsigned int type; |
| 1072 |
void __iomem *base; |
| 1073 |
struct rzg2l_cpg_priv *priv; |
| 1074 |
}; |
| 1075 |
|
| 1076 |
#define to_pll(_hw) container_of(_hw, struct pll_clk, hw) |
| 1077 |
|
| 1078 |
static unsigned long rzg2l_cpg_pll_clk_recalc_rate(struct clk_hw *hw, |
| 1079 |
unsigned long parent_rate) |
| 1080 |
{ |
| 1081 |
struct pll_clk *pll_clk = to_pll(hw); |
| 1082 |
struct rzg2l_cpg_priv *priv = pll_clk->priv; |
| 1083 |
unsigned int val1, val2; |
| 1084 |
u64 rate; |
| 1085 |
|
| 1086 |
if (pll_clk->type != CLK_TYPE_SAM_PLL) |
| 1087 |
return parent_rate; |
| 1088 |
|
| 1089 |
val1 = readl(priv->base + GET_REG_SAMPLL_CLK1(pll_clk->conf)); |
| 1090 |
val2 = readl(priv->base + GET_REG_SAMPLL_CLK2(pll_clk->conf)); |
| 1091 |
|
| 1092 |
rate = mul_u64_u32_shr(parent_rate, (MDIV(val1) << 16) + KDIV(val1), |
| 1093 |
16 + SDIV(val2)); |
| 1094 |
|
| 1095 |
return DIV_ROUND_CLOSEST_ULL(rate, PDIV(val1)); |
| 1096 |
} |
| 1097 |
|
| 1098 |
static const struct clk_ops rzg2l_cpg_pll_ops = { |
| 1099 |
.recalc_rate = rzg2l_cpg_pll_clk_recalc_rate, |
| 1100 |
}; |
| 1101 |
|
| 1102 |
static unsigned long rzg3s_cpg_pll_clk_recalc_rate(struct clk_hw *hw, |
| 1103 |
unsigned long parent_rate) |
| 1104 |
{ |
| 1105 |
struct pll_clk *pll_clk = to_pll(hw); |
| 1106 |
struct rzg2l_cpg_priv *priv = pll_clk->priv; |
| 1107 |
u32 nir, nfr, mr, pr, val, setting; |
| 1108 |
u64 rate; |
| 1109 |
|
| 1110 |
if (pll_clk->type != CLK_TYPE_G3S_PLL) |
| 1111 |
return parent_rate; |
| 1112 |
|
| 1113 |
setting = GET_REG_SAMPLL_SETTING(pll_clk->conf); |
| 1114 |
if (setting) { |
| 1115 |
val = readl(priv->base + setting); |
| 1116 |
if (val & RZG3S_SEL_PLL) |
| 1117 |
return pll_clk->default_rate; |
| 1118 |
} |
| 1119 |
|
| 1120 |
val = readl(priv->base + GET_REG_SAMPLL_CLK1(pll_clk->conf)); |
| 1121 |
|
| 1122 |
pr = 1 << FIELD_GET(RZG3S_DIV_P, val); |
| 1123 |
/* Hardware interprets values higher than 8 as p = 16. */ |
| 1124 |
if (pr > 8) |
| 1125 |
pr = 16; |
| 1126 |
|
| 1127 |
mr = FIELD_GET(RZG3S_DIV_M, val) + 1; |
| 1128 |
nir = FIELD_GET(RZG3S_DIV_NI, val) + 1; |
| 1129 |
nfr = FIELD_GET(RZG3S_DIV_NF, val); |
| 1130 |
|
| 1131 |
rate = mul_u64_u32_shr(parent_rate, 4096 * nir + nfr, 12); |
| 1132 |
|
| 1133 |
return DIV_ROUND_CLOSEST_ULL(rate, (mr * pr)); |
| 1134 |
} |
| 1135 |
|
| 1136 |
static const struct clk_ops rzg3s_cpg_pll_ops = { |
| 1137 |
.recalc_rate = rzg3s_cpg_pll_clk_recalc_rate, |
| 1138 |
}; |
| 1139 |
|
| 1140 |
static struct clk * __init |
| 1141 |
rzg2l_cpg_pll_clk_register(const struct cpg_core_clk *core, |
| 1142 |
struct rzg2l_cpg_priv *priv, |
| 1143 |
const struct clk_ops *ops) |
| 1144 |
{ |
| 1145 |
struct device *dev = priv->dev; |
| 1146 |
const struct clk *parent; |
| 1147 |
struct clk_init_data init; |
| 1148 |
const char *parent_name; |
| 1149 |
struct pll_clk *pll_clk; |
| 1150 |
int ret; |
| 1151 |
|
| 1152 |
parent = priv->clks[core->parent]; |
| 1153 |
if (IS_ERR(parent)) |
| 1154 |
return ERR_CAST(parent); |
| 1155 |
|
| 1156 |
pll_clk = devm_kzalloc(dev, sizeof(*pll_clk), GFP_KERNEL); |
| 1157 |
if (!pll_clk) |
| 1158 |
return ERR_PTR(-ENOMEM); |
| 1159 |
|
| 1160 |
parent_name = __clk_get_name(parent); |
| 1161 |
init.name = core->name; |
| 1162 |
init.ops = ops; |
| 1163 |
init.flags = 0; |
| 1164 |
init.parent_names = &parent_name; |
| 1165 |
init.num_parents = 1; |
| 1166 |
|
| 1167 |
pll_clk->hw.init = &init; |
| 1168 |
pll_clk->conf = core->conf; |
| 1169 |
pll_clk->base = priv->base; |
| 1170 |
pll_clk->priv = priv; |
| 1171 |
pll_clk->type = core->type; |
| 1172 |
pll_clk->default_rate = core->default_rate; |
| 1173 |
|
| 1174 |
ret = devm_clk_hw_register(dev, &pll_clk->hw); |
| 1175 |
if (ret) |
| 1176 |
return ERR_PTR(ret); |
| 1177 |
|
| 1178 |
return pll_clk->hw.clk; |
| 1179 |
} |
| 1180 |
|
| 1181 |
static struct clk |
| 1182 |
*rzg2l_cpg_clk_src_twocell_get(struct of_phandle_args *clkspec, |
| 1183 |
void *data) |
| 1184 |
{ |
| 1185 |
unsigned int clkidx = clkspec->args[1]; |
| 1186 |
struct rzg2l_cpg_priv *priv = data; |
| 1187 |
struct device *dev = priv->dev; |
| 1188 |
const char *type; |
| 1189 |
struct clk *clk; |
| 1190 |
|
| 1191 |
switch (clkspec->args[0]) { |
| 1192 |
case CPG_CORE: |
| 1193 |
type = "core"; |
| 1194 |
if (clkidx > priv->last_dt_core_clk) { |
| 1195 |
dev_err(dev, "Invalid %s clock index %u\n", type, clkidx); |
| 1196 |
return ERR_PTR(-EINVAL); |
| 1197 |
} |
| 1198 |
clk = priv->clks[clkidx]; |
| 1199 |
break; |
| 1200 |
|
| 1201 |
case CPG_MOD: |
| 1202 |
type = "module"; |
| 1203 |
if (clkidx >= priv->num_mod_clks) { |
| 1204 |
dev_err(dev, "Invalid %s clock index %u\n", type, |
| 1205 |
clkidx); |
| 1206 |
return ERR_PTR(-EINVAL); |
| 1207 |
} |
| 1208 |
clk = priv->clks[priv->num_core_clks + clkidx]; |
| 1209 |
break; |
| 1210 |
|
| 1211 |
default: |
| 1212 |
dev_err(dev, "Invalid CPG clock type %u\n", clkspec->args[0]); |
| 1213 |
return ERR_PTR(-EINVAL); |
| 1214 |
} |
| 1215 |
|
| 1216 |
if (IS_ERR(clk)) |
| 1217 |
dev_err(dev, "Cannot get %s clock %u: %ld\n", type, clkidx, |
| 1218 |
PTR_ERR(clk)); |
| 1219 |
else |
| 1220 |
dev_dbg(dev, "clock (%u, %u) is %pC at %lu Hz\n", |
| 1221 |
clkspec->args[0], clkspec->args[1], clk, |
| 1222 |
clk_get_rate(clk)); |
| 1223 |
return clk; |
| 1224 |
} |
| 1225 |
|
| 1226 |
static void __init |
| 1227 |
rzg2l_cpg_register_core_clk(const struct cpg_core_clk *core, |
| 1228 |
const struct rzg2l_cpg_info *info, |
| 1229 |
struct rzg2l_cpg_priv *priv) |
| 1230 |
{ |
| 1231 |
struct clk *clk = ERR_PTR(-EOPNOTSUPP), *parent; |
| 1232 |
struct device *dev = priv->dev; |
| 1233 |
unsigned int id = core->id, div = core->div; |
| 1234 |
const char *parent_name; |
| 1235 |
struct clk_hw *clk_hw; |
| 1236 |
|
| 1237 |
WARN_DEBUG(id >= priv->num_core_clks); |
| 1238 |
WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT); |
| 1239 |
|
| 1240 |
switch (core->type) { |
| 1241 |
case CLK_TYPE_IN: |
| 1242 |
clk = of_clk_get_by_name(priv->dev->of_node, core->name); |
| 1243 |
break; |
| 1244 |
case CLK_TYPE_FF: |
| 1245 |
WARN_DEBUG(core->parent >= priv->num_core_clks); |
| 1246 |
parent = priv->clks[core->parent]; |
| 1247 |
if (IS_ERR(parent)) { |
| 1248 |
clk = parent; |
| 1249 |
goto fail; |
| 1250 |
} |
| 1251 |
|
| 1252 |
parent_name = __clk_get_name(parent); |
| 1253 |
clk_hw = devm_clk_hw_register_fixed_factor(dev, core->name, parent_name, |
| 1254 |
CLK_SET_RATE_PARENT, |
| 1255 |
core->mult, div); |
| 1256 |
if (IS_ERR(clk_hw)) |
| 1257 |
clk = ERR_CAST(clk_hw); |
| 1258 |
else |
| 1259 |
clk = clk_hw->clk; |
| 1260 |
break; |
| 1261 |
case CLK_TYPE_SAM_PLL: |
| 1262 |
clk = rzg2l_cpg_pll_clk_register(core, priv, &rzg2l_cpg_pll_ops); |
| 1263 |
break; |
| 1264 |
case CLK_TYPE_G3S_PLL: |
| 1265 |
clk = rzg2l_cpg_pll_clk_register(core, priv, &rzg3s_cpg_pll_ops); |
| 1266 |
break; |
| 1267 |
case CLK_TYPE_SIPLL5: |
| 1268 |
clk = rzg2l_cpg_sipll5_register(core, priv); |
| 1269 |
break; |
| 1270 |
case CLK_TYPE_DIV: |
| 1271 |
clk = rzg2l_cpg_div_clk_register(core, priv); |
| 1272 |
break; |
| 1273 |
case CLK_TYPE_G3S_DIV: |
| 1274 |
clk = rzg3s_cpg_div_clk_register(core, priv); |
| 1275 |
break; |
| 1276 |
case CLK_TYPE_MUX: |
| 1277 |
clk = rzg2l_cpg_mux_clk_register(core, priv); |
| 1278 |
break; |
| 1279 |
case CLK_TYPE_SD_MUX: |
| 1280 |
clk = rzg2l_cpg_sd_mux_clk_register(core, priv); |
| 1281 |
break; |
| 1282 |
case CLK_TYPE_PLL5_4_MUX: |
| 1283 |
clk = rzg2l_cpg_pll5_4_mux_clk_register(core, priv); |
| 1284 |
break; |
| 1285 |
case CLK_TYPE_DSI_DIV: |
| 1286 |
clk = rzg2l_cpg_dsi_div_clk_register(core, priv); |
| 1287 |
break; |
| 1288 |
default: |
| 1289 |
goto fail; |
| 1290 |
} |
| 1291 |
|
| 1292 |
if (IS_ERR(clk)) |
| 1293 |
goto fail; |
| 1294 |
|
| 1295 |
dev_dbg(dev, "Core clock %pC at %lu Hz\n", clk, clk_get_rate(clk)); |
| 1296 |
priv->clks[id] = clk; |
| 1297 |
return; |
| 1298 |
|
| 1299 |
fail: |
| 1300 |
dev_err(dev, "Failed to register %s clock %s: %ld\n", "core", |
| 1301 |
core->name, PTR_ERR(clk)); |
| 1302 |
} |
| 1303 |
|
| 1304 |
/** |
| 1305 |
* struct mstop - MSTOP specific data structure |
| 1306 |
* @usecnt: Usage counter for MSTOP settings (when zero the settings |
| 1307 |
* are applied to register) |
| 1308 |
* @conf: MSTOP configuration (register offset, setup bits) |
| 1309 |
*/ |
| 1310 |
struct mstop { |
| 1311 |
atomic_t usecnt; |
| 1312 |
u32 conf; |
| 1313 |
}; |
| 1314 |
|
| 1315 |
/** |
| 1316 |
* struct mod_clock - Module clock |
| 1317 |
* |
| 1318 |
* @hw: handle between common and hardware-specific interfaces |
| 1319 |
* @priv: CPG/MSTP private data |
| 1320 |
* @sibling: pointer to the other coupled clock |
| 1321 |
* @mstop: MSTOP configuration |
| 1322 |
* @shared_mstop_clks: clocks sharing the MSTOP with this clock |
| 1323 |
* @off: register offset |
| 1324 |
* @bit: ON/MON bit |
| 1325 |
* @num_shared_mstop_clks: number of the clocks sharing MSTOP with this clock |
| 1326 |
* @enabled: soft state of the clock, if it is coupled with another clock |
| 1327 |
*/ |
| 1328 |
struct mod_clock { |
| 1329 |
struct clk_hw hw; |
| 1330 |
struct rzg2l_cpg_priv *priv; |
| 1331 |
struct mod_clock *sibling; |
| 1332 |
struct mstop *mstop; |
| 1333 |
struct mod_clock **shared_mstop_clks; |
| 1334 |
u16 off; |
| 1335 |
u8 bit; |
| 1336 |
u8 num_shared_mstop_clks; |
| 1337 |
bool enabled; |
| 1338 |
}; |
| 1339 |
|
| 1340 |
#define to_mod_clock(_hw) container_of(_hw, struct mod_clock, hw) |
| 1341 |
|
| 1342 |
#define for_each_mod_clock(mod_clock, hw, priv) \ |
| 1343 |
for (unsigned int i = 0; (priv) && i < (priv)->num_mod_clks; i++) \ |
| 1344 |
if ((priv)->clks[(priv)->num_core_clks + i] == ERR_PTR(-ENOENT)) \ |
| 1345 |
continue; \ |
| 1346 |
else if (((hw) = __clk_get_hw((priv)->clks[(priv)->num_core_clks + i])) && \ |
| 1347 |
((mod_clock) = to_mod_clock(hw))) |
| 1348 |
|
| 1349 |
/* Need to be called with a lock held to avoid concurrent access to mstop->usecnt. */ |
| 1350 |
static void rzg2l_mod_clock_module_set_state(struct mod_clock *clock, |
| 1351 |
bool standby) |
| 1352 |
{ |
| 1353 |
struct rzg2l_cpg_priv *priv = clock->priv; |
| 1354 |
struct mstop *mstop = clock->mstop; |
| 1355 |
bool update = false; |
| 1356 |
u32 value; |
| 1357 |
|
| 1358 |
if (!mstop) |
| 1359 |
return; |
| 1360 |
|
| 1361 |
value = MSTOP_MASK(mstop->conf) << 16; |
| 1362 |
|
| 1363 |
if (standby) { |
| 1364 |
unsigned int criticals = 0; |
| 1365 |
|
| 1366 |
for (unsigned int i = 0; i < clock->num_shared_mstop_clks; i++) { |
| 1367 |
struct mod_clock *clk = clock->shared_mstop_clks[i]; |
| 1368 |
|
| 1369 |
if (clk_hw_get_flags(&clk->hw) & CLK_IS_CRITICAL) |
| 1370 |
criticals++; |
| 1371 |
} |
| 1372 |
|
| 1373 |
if (!clock->num_shared_mstop_clks && |
| 1374 |
clk_hw_get_flags(&clock->hw) & CLK_IS_CRITICAL) |
| 1375 |
criticals++; |
| 1376 |
|
| 1377 |
/* |
| 1378 |
* If this is a shared MSTOP and it is shared with critical clocks, |
| 1379 |
* and the system boots up with this clock enabled but no driver |
| 1380 |
* uses it the CCF will disable it (as it is unused). As we don't |
| 1381 |
* increment reference counter for it at registration (to avoid |
| 1382 |
* messing with clocks enabled at probe but later used by drivers) |
| 1383 |
* do not set the MSTOP here too if it is shared with critical |
| 1384 |
* clocks and ref counted only by those critical clocks. |
| 1385 |
*/ |
| 1386 |
if (criticals && criticals == atomic_read(&mstop->usecnt)) |
| 1387 |
return; |
| 1388 |
|
| 1389 |
value |= MSTOP_MASK(mstop->conf); |
| 1390 |
|
| 1391 |
/* Allow updates on probe when usecnt = 0. */ |
| 1392 |
if (!atomic_read(&mstop->usecnt)) |
| 1393 |
update = true; |
| 1394 |
else |
| 1395 |
update = atomic_dec_and_test(&mstop->usecnt); |
| 1396 |
} else { |
| 1397 |
if (!atomic_read(&mstop->usecnt)) |
| 1398 |
update = true; |
| 1399 |
atomic_inc(&mstop->usecnt); |
| 1400 |
} |
| 1401 |
|
| 1402 |
if (update) |
| 1403 |
writel(value, priv->base + MSTOP_OFF(mstop->conf)); |
| 1404 |
} |
| 1405 |
|
| 1406 |
static int rzg2l_mod_clock_mstop_show(struct seq_file *s, void *what) |
| 1407 |
{ |
| 1408 |
struct rzg2l_cpg_priv *priv = s->private; |
| 1409 |
struct mod_clock *clk; |
| 1410 |
struct clk_hw *hw; |
| 1411 |
|
| 1412 |
seq_printf(s, "%-20s %-5s %-10s\n", "", "", "MSTOP"); |
| 1413 |
seq_printf(s, "%-20s %-5s %-10s\n", "", "clk", "-------------------------"); |
| 1414 |
seq_printf(s, "%-20s %-5s %-5s %-5s %-6s %-6s\n", |
| 1415 |
"clk_name", "cnt", "cnt", "off", "val", "shared"); |
| 1416 |
seq_printf(s, "%-20s %-5s %-5s %-5s %-6s %-6s\n", |
| 1417 |
"--------", "-----", "-----", "-----", "------", "------"); |
| 1418 |
|
| 1419 |
for_each_mod_clock(clk, hw, priv) { |
| 1420 |
u32 val; |
| 1421 |
|
| 1422 |
if (!clk->mstop) |
| 1423 |
continue; |
| 1424 |
|
| 1425 |
val = readl(priv->base + MSTOP_OFF(clk->mstop->conf)) & |
| 1426 |
MSTOP_MASK(clk->mstop->conf); |
| 1427 |
|
| 1428 |
seq_printf(s, "%-20s %-5d %-5d 0x%-3lx 0x%-4x", clk_hw_get_name(hw), |
| 1429 |
__clk_get_enable_count(hw->clk), atomic_read(&clk->mstop->usecnt), |
| 1430 |
MSTOP_OFF(clk->mstop->conf), val); |
| 1431 |
|
| 1432 |
for (unsigned int i = 0; i < clk->num_shared_mstop_clks; i++) |
| 1433 |
seq_printf(s, " %pC", clk->shared_mstop_clks[i]->hw.clk); |
| 1434 |
|
| 1435 |
seq_puts(s, "\n"); |
| 1436 |
} |
| 1437 |
|
| 1438 |
return 0; |
| 1439 |
} |
| 1440 |
DEFINE_SHOW_ATTRIBUTE(rzg2l_mod_clock_mstop); |
| 1441 |
|
| 1442 |
static int rzg2l_mod_clock_endisable(struct clk_hw *hw, bool enable) |
| 1443 |
{ |
| 1444 |
struct mod_clock *clock = to_mod_clock(hw); |
| 1445 |
struct rzg2l_cpg_priv *priv = clock->priv; |
| 1446 |
unsigned int reg = clock->off; |
| 1447 |
struct device *dev = priv->dev; |
| 1448 |
u32 bitmask = BIT(clock->bit); |
| 1449 |
u32 value; |
| 1450 |
int error; |
| 1451 |
|
| 1452 |
if (!clock->off) { |
| 1453 |
dev_dbg(dev, "%pC does not support ON/OFF\n", hw->clk); |
| 1454 |
return 0; |
| 1455 |
} |
| 1456 |
|
| 1457 |
dev_dbg(dev, "CLK_ON 0x%x/%pC %s\n", CLK_ON_R(reg), hw->clk, |
| 1458 |
str_on_off(enable)); |
| 1459 |
|
| 1460 |
value = bitmask << 16; |
| 1461 |
if (enable) |
| 1462 |
value |= bitmask; |
| 1463 |
|
| 1464 |
scoped_guard(spinlock_irqsave, &priv->rmw_lock) { |
| 1465 |
if (enable) { |
| 1466 |
writel(value, priv->base + CLK_ON_R(reg)); |
| 1467 |
rzg2l_mod_clock_module_set_state(clock, false); |
| 1468 |
} else { |
| 1469 |
rzg2l_mod_clock_module_set_state(clock, true); |
| 1470 |
writel(value, priv->base + CLK_ON_R(reg)); |
| 1471 |
} |
| 1472 |
} |
| 1473 |
|
| 1474 |
if (!enable) |
| 1475 |
return 0; |
| 1476 |
|
| 1477 |
if (!priv->info->has_clk_mon_regs) |
| 1478 |
return 0; |
| 1479 |
|
| 1480 |
error = readl_poll_timeout_atomic(priv->base + CLK_MON_R(reg), value, |
| 1481 |
value & bitmask, 0, 10); |
| 1482 |
if (error) |
| 1483 |
dev_err(dev, "Failed to enable CLK_ON 0x%x/%pC\n", |
| 1484 |
CLK_ON_R(reg), hw->clk); |
| 1485 |
|
| 1486 |
return error; |
| 1487 |
} |
| 1488 |
|
| 1489 |
static int rzg2l_mod_clock_enable(struct clk_hw *hw) |
| 1490 |
{ |
| 1491 |
struct mod_clock *clock = to_mod_clock(hw); |
| 1492 |
|
| 1493 |
if (clock->sibling) { |
| 1494 |
struct rzg2l_cpg_priv *priv = clock->priv; |
| 1495 |
unsigned long flags; |
| 1496 |
bool enabled; |
| 1497 |
|
| 1498 |
spin_lock_irqsave(&priv->rmw_lock, flags); |
| 1499 |
enabled = clock->sibling->enabled; |
| 1500 |
clock->enabled = true; |
| 1501 |
spin_unlock_irqrestore(&priv->rmw_lock, flags); |
| 1502 |
if (enabled) |
| 1503 |
return 0; |
| 1504 |
} |
| 1505 |
|
| 1506 |
return rzg2l_mod_clock_endisable(hw, true); |
| 1507 |
} |
| 1508 |
|
| 1509 |
static void rzg2l_mod_clock_disable(struct clk_hw *hw) |
| 1510 |
{ |
| 1511 |
struct mod_clock *clock = to_mod_clock(hw); |
| 1512 |
|
| 1513 |
if (clock->sibling) { |
| 1514 |
struct rzg2l_cpg_priv *priv = clock->priv; |
| 1515 |
unsigned long flags; |
| 1516 |
bool enabled; |
| 1517 |
|
| 1518 |
spin_lock_irqsave(&priv->rmw_lock, flags); |
| 1519 |
enabled = clock->sibling->enabled; |
| 1520 |
clock->enabled = false; |
| 1521 |
spin_unlock_irqrestore(&priv->rmw_lock, flags); |
| 1522 |
if (enabled) |
| 1523 |
return; |
| 1524 |
} |
| 1525 |
|
| 1526 |
rzg2l_mod_clock_endisable(hw, false); |
| 1527 |
} |
| 1528 |
|
| 1529 |
static int rzg2l_mod_clock_is_enabled(struct clk_hw *hw) |
| 1530 |
{ |
| 1531 |
struct mod_clock *clock = to_mod_clock(hw); |
| 1532 |
struct rzg2l_cpg_priv *priv = clock->priv; |
| 1533 |
u32 bitmask = BIT(clock->bit); |
| 1534 |
u32 value; |
| 1535 |
|
| 1536 |
if (!clock->off) { |
| 1537 |
dev_dbg(priv->dev, "%pC does not support ON/OFF\n", hw->clk); |
| 1538 |
return 1; |
| 1539 |
} |
| 1540 |
|
| 1541 |
if (clock->sibling) |
| 1542 |
return clock->enabled; |
| 1543 |
|
| 1544 |
if (priv->info->has_clk_mon_regs) |
| 1545 |
value = readl(priv->base + CLK_MON_R(clock->off)); |
| 1546 |
else |
| 1547 |
value = readl(priv->base + clock->off); |
| 1548 |
|
| 1549 |
return value & bitmask; |
| 1550 |
} |
| 1551 |
|
| 1552 |
static const struct clk_ops rzg2l_mod_clock_ops = { |
| 1553 |
.enable = rzg2l_mod_clock_enable, |
| 1554 |
.disable = rzg2l_mod_clock_disable, |
| 1555 |
.is_enabled = rzg2l_mod_clock_is_enabled, |
| 1556 |
}; |
| 1557 |
|
| 1558 |
static struct mod_clock |
| 1559 |
*rzg2l_mod_clock_get_sibling(struct mod_clock *clock, |
| 1560 |
struct rzg2l_cpg_priv *priv) |
| 1561 |
{ |
| 1562 |
struct mod_clock *clk; |
| 1563 |
struct clk_hw *hw; |
| 1564 |
|
| 1565 |
for_each_mod_clock(clk, hw, priv) { |
| 1566 |
if (clock->off == clk->off && clock->bit == clk->bit) |
| 1567 |
return clk; |
| 1568 |
} |
| 1569 |
|
| 1570 |
return NULL; |
| 1571 |
} |
| 1572 |
|
| 1573 |
static struct mstop *rzg2l_mod_clock_get_mstop(struct rzg2l_cpg_priv *priv, u32 conf) |
| 1574 |
{ |
| 1575 |
struct mod_clock *clk; |
| 1576 |
struct clk_hw *hw; |
| 1577 |
|
| 1578 |
for_each_mod_clock(clk, hw, priv) { |
| 1579 |
if (!clk->mstop) |
| 1580 |
continue; |
| 1581 |
|
| 1582 |
if (clk->mstop->conf == conf) |
| 1583 |
return clk->mstop; |
| 1584 |
} |
| 1585 |
|
| 1586 |
return NULL; |
| 1587 |
} |
| 1588 |
|
| 1589 |
static void rzg2l_mod_clock_init_mstop(struct rzg2l_cpg_priv *priv) |
| 1590 |
{ |
| 1591 |
struct mod_clock *clk; |
| 1592 |
struct clk_hw *hw; |
| 1593 |
|
| 1594 |
for_each_mod_clock(clk, hw, priv) { |
| 1595 |
if (!clk->mstop) |
| 1596 |
continue; |
| 1597 |
|
| 1598 |
/* |
| 1599 |
* Out of reset all modules are enabled. Set module state |
| 1600 |
* in case associated clocks are disabled at probe. Otherwise |
| 1601 |
* module is in invalid HW state. |
| 1602 |
*/ |
| 1603 |
scoped_guard(spinlock_irqsave, &priv->rmw_lock) { |
| 1604 |
if (!rzg2l_mod_clock_is_enabled(&clk->hw)) |
| 1605 |
rzg2l_mod_clock_module_set_state(clk, true); |
| 1606 |
} |
| 1607 |
} |
| 1608 |
} |
| 1609 |
|
| 1610 |
static int rzg2l_mod_clock_update_shared_mstop_clks(struct rzg2l_cpg_priv *priv, |
| 1611 |
struct mod_clock *clock) |
| 1612 |
{ |
| 1613 |
struct mod_clock *clk; |
| 1614 |
struct clk_hw *hw; |
| 1615 |
|
| 1616 |
if (!clock->mstop) |
| 1617 |
return 0; |
| 1618 |
|
| 1619 |
for_each_mod_clock(clk, hw, priv) { |
| 1620 |
int num_shared_mstop_clks, incr = 1; |
| 1621 |
struct mod_clock **new_clks; |
| 1622 |
|
| 1623 |
if (clk->mstop != clock->mstop) |
| 1624 |
continue; |
| 1625 |
|
| 1626 |
num_shared_mstop_clks = clk->num_shared_mstop_clks; |
| 1627 |
if (!num_shared_mstop_clks) |
| 1628 |
incr++; |
| 1629 |
|
| 1630 |
new_clks = devm_krealloc(priv->dev, clk->shared_mstop_clks, |
| 1631 |
(num_shared_mstop_clks + incr) * sizeof(*new_clks), |
| 1632 |
GFP_KERNEL); |
| 1633 |
if (!new_clks) |
| 1634 |
return -ENOMEM; |
| 1635 |
|
| 1636 |
if (!num_shared_mstop_clks) |
| 1637 |
new_clks[num_shared_mstop_clks++] = clk; |
| 1638 |
new_clks[num_shared_mstop_clks++] = clock; |
| 1639 |
|
| 1640 |
for (unsigned int i = 0; i < num_shared_mstop_clks; i++) { |
| 1641 |
new_clks[i]->shared_mstop_clks = new_clks; |
| 1642 |
new_clks[i]->num_shared_mstop_clks = num_shared_mstop_clks; |
| 1643 |
} |
| 1644 |
break; |
| 1645 |
} |
| 1646 |
|
| 1647 |
return 0; |
| 1648 |
} |
| 1649 |
|
| 1650 |
static void __init |
| 1651 |
rzg2l_cpg_register_mod_clk(const struct rzg2l_mod_clk *mod, |
| 1652 |
const struct rzg2l_cpg_info *info, |
| 1653 |
struct rzg2l_cpg_priv *priv) |
| 1654 |
{ |
| 1655 |
struct mod_clock *clock = NULL; |
| 1656 |
struct device *dev = priv->dev; |
| 1657 |
unsigned int id = mod->id; |
| 1658 |
struct clk_init_data init; |
| 1659 |
struct clk *parent, *clk; |
| 1660 |
const char *parent_name; |
| 1661 |
unsigned int i; |
| 1662 |
int ret; |
| 1663 |
|
| 1664 |
WARN_DEBUG(id < priv->num_core_clks); |
| 1665 |
WARN_DEBUG(id >= priv->num_core_clks + priv->num_mod_clks); |
| 1666 |
WARN_DEBUG(mod->parent >= priv->num_core_clks + priv->num_mod_clks); |
| 1667 |
WARN_DEBUG(PTR_ERR(priv->clks[id]) != -ENOENT); |
| 1668 |
|
| 1669 |
parent = priv->clks[mod->parent]; |
| 1670 |
if (IS_ERR(parent)) { |
| 1671 |
clk = parent; |
| 1672 |
goto fail; |
| 1673 |
} |
| 1674 |
|
| 1675 |
clock = devm_kzalloc(dev, sizeof(*clock), GFP_KERNEL); |
| 1676 |
if (!clock) { |
| 1677 |
clk = ERR_PTR(-ENOMEM); |
| 1678 |
goto fail; |
| 1679 |
} |
| 1680 |
|
| 1681 |
init.name = mod->name; |
| 1682 |
init.ops = &rzg2l_mod_clock_ops; |
| 1683 |
init.flags = CLK_SET_RATE_PARENT; |
| 1684 |
for (i = 0; i < info->num_crit_mod_clks; i++) |
| 1685 |
if (id == info->crit_mod_clks[i]) { |
| 1686 |
dev_dbg(dev, "CPG %s setting CLK_IS_CRITICAL\n", |
| 1687 |
mod->name); |
| 1688 |
init.flags |= CLK_IS_CRITICAL; |
| 1689 |
break; |
| 1690 |
} |
| 1691 |
|
| 1692 |
parent_name = __clk_get_name(parent); |
| 1693 |
init.parent_names = &parent_name; |
| 1694 |
init.num_parents = 1; |
| 1695 |
|
| 1696 |
clock->off = mod->off; |
| 1697 |
clock->bit = mod->bit; |
| 1698 |
clock->priv = priv; |
| 1699 |
clock->hw.init = &init; |
| 1700 |
|
| 1701 |
if (mod->mstop_conf) { |
| 1702 |
struct mstop *mstop = rzg2l_mod_clock_get_mstop(priv, mod->mstop_conf); |
| 1703 |
|
| 1704 |
if (!mstop) { |
| 1705 |
mstop = devm_kzalloc(dev, sizeof(*mstop), GFP_KERNEL); |
| 1706 |
if (!mstop) { |
| 1707 |
clk = ERR_PTR(-ENOMEM); |
| 1708 |
goto fail; |
| 1709 |
} |
| 1710 |
mstop->conf = mod->mstop_conf; |
| 1711 |
atomic_set(&mstop->usecnt, 0); |
| 1712 |
} |
| 1713 |
clock->mstop = mstop; |
| 1714 |
} |
| 1715 |
|
| 1716 |
ret = devm_clk_hw_register(dev, &clock->hw); |
| 1717 |
if (ret) { |
| 1718 |
clk = ERR_PTR(ret); |
| 1719 |
goto fail; |
| 1720 |
} |
| 1721 |
|
| 1722 |
if (mod->is_coupled) { |
| 1723 |
struct mod_clock *sibling; |
| 1724 |
|
| 1725 |
clock->enabled = rzg2l_mod_clock_is_enabled(&clock->hw); |
| 1726 |
sibling = rzg2l_mod_clock_get_sibling(clock, priv); |
| 1727 |
if (sibling) { |
| 1728 |
clock->sibling = sibling; |
| 1729 |
sibling->sibling = clock; |
| 1730 |
} |
| 1731 |
} |
| 1732 |
|
| 1733 |
/* Keep this before priv->clks[id] is updated. */ |
| 1734 |
ret = rzg2l_mod_clock_update_shared_mstop_clks(priv, clock); |
| 1735 |
if (ret) { |
| 1736 |
clk = ERR_PTR(ret); |
| 1737 |
goto fail; |
| 1738 |
} |
| 1739 |
|
| 1740 |
clk = clock->hw.clk; |
| 1741 |
dev_dbg(dev, "Module clock %pC at %lu Hz\n", clk, clk_get_rate(clk)); |
| 1742 |
priv->clks[id] = clk; |
| 1743 |
|
| 1744 |
return; |
| 1745 |
|
| 1746 |
fail: |
| 1747 |
dev_err(dev, "Failed to register %s clock %s: %ld\n", "module", |
| 1748 |
mod->name, PTR_ERR(clk)); |
| 1749 |
} |
| 1750 |
|
| 1751 |
#define rcdev_to_priv(x) container_of(x, struct rzg2l_cpg_priv, rcdev) |
| 1752 |
|
| 1753 |
static int __rzg2l_cpg_assert(struct reset_controller_dev *rcdev, |
| 1754 |
unsigned long id, bool assert) |
| 1755 |
{ |
| 1756 |
struct rzg2l_cpg_priv *priv = rcdev_to_priv(rcdev); |
| 1757 |
const struct rzg2l_cpg_info *info = priv->info; |
| 1758 |
unsigned int reg = info->resets[id].off; |
| 1759 |
u32 mask = BIT(info->resets[id].bit); |
| 1760 |
s8 monbit = info->resets[id].monbit; |
| 1761 |
u32 value = mask << 16; |
| 1762 |
u32 mon; |
| 1763 |
int ret; |
| 1764 |
|
| 1765 |
dev_dbg(rcdev->dev, "%s id:%ld offset:0x%x\n", |
| 1766 |
assert ? "assert" : "deassert", id, CLK_RST_R(reg)); |
| 1767 |
|
| 1768 |
if (!assert) |
| 1769 |
value |= mask; |
| 1770 |
writel(value, priv->base + CLK_RST_R(reg)); |
| 1771 |
|
| 1772 |
if (info->has_clk_mon_regs) { |
| 1773 |
reg = CLK_MRST_R(reg); |
| 1774 |
} else if (monbit >= 0) { |
| 1775 |
reg = CPG_RST_MON; |
| 1776 |
mask = BIT(monbit); |
| 1777 |
} else { |
| 1778 |
/* Wait for at least one cycle of the RCLK clock (@ ca. 32 kHz) */ |
| 1779 |
udelay(35); |
| 1780 |
return 0; |
| 1781 |
} |
| 1782 |
|
| 1783 |
ret = readl_poll_timeout_atomic(priv->base + reg, mon, |
| 1784 |
assert == !!(mon & mask), 10, 200); |
| 1785 |
if (ret) { |
| 1786 |
value ^= mask; |
| 1787 |
writel(value, priv->base + CLK_RST_R(info->resets[id].off)); |
| 1788 |
} |
| 1789 |
|
| 1790 |
return ret; |
| 1791 |
} |
| 1792 |
|
| 1793 |
static int rzg2l_cpg_assert(struct reset_controller_dev *rcdev, |
| 1794 |
unsigned long id) |
| 1795 |
{ |
| 1796 |
return __rzg2l_cpg_assert(rcdev, id, true); |
| 1797 |
} |
| 1798 |
|
| 1799 |
static int rzg2l_cpg_deassert(struct reset_controller_dev *rcdev, |
| 1800 |
unsigned long id) |
| 1801 |
{ |
| 1802 |
return __rzg2l_cpg_assert(rcdev, id, false); |
| 1803 |
} |
| 1804 |
|
| 1805 |
static int rzg2l_cpg_reset(struct reset_controller_dev *rcdev, |
| 1806 |
unsigned long id) |
| 1807 |
{ |
| 1808 |
int ret; |
| 1809 |
|
| 1810 |
ret = rzg2l_cpg_assert(rcdev, id); |
| 1811 |
if (ret) |
| 1812 |
return ret; |
| 1813 |
|
| 1814 |
return rzg2l_cpg_deassert(rcdev, id); |
| 1815 |
} |
| 1816 |
|
| 1817 |
static int rzg2l_cpg_status(struct reset_controller_dev *rcdev, |
| 1818 |
unsigned long id) |
| 1819 |
{ |
| 1820 |
struct rzg2l_cpg_priv *priv = rcdev_to_priv(rcdev); |
| 1821 |
const struct rzg2l_cpg_info *info = priv->info; |
| 1822 |
s8 monbit = info->resets[id].monbit; |
| 1823 |
unsigned int reg; |
| 1824 |
u32 bitmask; |
| 1825 |
|
| 1826 |
if (info->has_clk_mon_regs) { |
| 1827 |
reg = CLK_MRST_R(info->resets[id].off); |
| 1828 |
bitmask = BIT(info->resets[id].bit); |
| 1829 |
} else if (monbit >= 0) { |
| 1830 |
reg = CPG_RST_MON; |
| 1831 |
bitmask = BIT(monbit); |
| 1832 |
} else { |
| 1833 |
return -ENOTSUPP; |
| 1834 |
} |
| 1835 |
|
| 1836 |
return !!(readl(priv->base + reg) & bitmask); |
| 1837 |
} |
| 1838 |
|
| 1839 |
static const struct reset_control_ops rzg2l_cpg_reset_ops = { |
| 1840 |
.reset = rzg2l_cpg_reset, |
| 1841 |
.assert = rzg2l_cpg_assert, |
| 1842 |
.deassert = rzg2l_cpg_deassert, |
| 1843 |
.status = rzg2l_cpg_status, |
| 1844 |
}; |
| 1845 |
|
| 1846 |
static int rzg2l_cpg_reset_xlate(struct reset_controller_dev *rcdev, |
| 1847 |
const struct of_phandle_args *reset_spec) |
| 1848 |
{ |
| 1849 |
struct rzg2l_cpg_priv *priv = rcdev_to_priv(rcdev); |
| 1850 |
const struct rzg2l_cpg_info *info = priv->info; |
| 1851 |
unsigned int id = reset_spec->args[0]; |
| 1852 |
|
| 1853 |
if (id >= rcdev->nr_resets || !info->resets[id].off) { |
| 1854 |
dev_err(rcdev->dev, "Invalid reset index %u\n", id); |
| 1855 |
return -EINVAL; |
| 1856 |
} |
| 1857 |
|
| 1858 |
return id; |
| 1859 |
} |
| 1860 |
|
| 1861 |
static int rzg2l_cpg_reset_controller_register(struct rzg2l_cpg_priv *priv) |
| 1862 |
{ |
| 1863 |
priv->rcdev.ops = &rzg2l_cpg_reset_ops; |
| 1864 |
priv->rcdev.of_node = priv->dev->of_node; |
| 1865 |
priv->rcdev.dev = priv->dev; |
| 1866 |
priv->rcdev.of_reset_n_cells = 1; |
| 1867 |
priv->rcdev.of_xlate = rzg2l_cpg_reset_xlate; |
| 1868 |
priv->rcdev.nr_resets = priv->num_resets; |
| 1869 |
|
| 1870 |
return devm_reset_controller_register(priv->dev, &priv->rcdev); |
| 1871 |
} |
| 1872 |
|
| 1873 |
static bool rzg2l_cpg_is_pm_clk(struct rzg2l_cpg_priv *priv, |
| 1874 |
const struct of_phandle_args *clkspec) |
| 1875 |
{ |
| 1876 |
if (clkspec->np != priv->genpd.dev.of_node || clkspec->args_count != 2) |
| 1877 |
return false; |
| 1878 |
|
| 1879 |
switch (clkspec->args[0]) { |
| 1880 |
case CPG_MOD: { |
| 1881 |
const struct rzg2l_cpg_info *info = priv->info; |
| 1882 |
unsigned int id = clkspec->args[1]; |
| 1883 |
|
| 1884 |
if (id >= priv->num_mod_clks) |
| 1885 |
return false; |
| 1886 |
|
| 1887 |
id += info->num_total_core_clks; |
| 1888 |
|
| 1889 |
for (unsigned int i = 0; i < info->num_no_pm_mod_clks; i++) { |
| 1890 |
if (info->no_pm_mod_clks[i] == id) |
| 1891 |
return false; |
| 1892 |
} |
| 1893 |
|
| 1894 |
return true; |
| 1895 |
} |
| 1896 |
|
| 1897 |
case CPG_CORE: |
| 1898 |
default: |
| 1899 |
return false; |
| 1900 |
} |
| 1901 |
} |
| 1902 |
|
| 1903 |
static int rzg2l_cpg_attach_dev(struct generic_pm_domain *domain, struct device *dev) |
| 1904 |
{ |
| 1905 |
struct rzg2l_cpg_priv *priv = container_of(domain, struct rzg2l_cpg_priv, genpd); |
| 1906 |
struct device_node *np = dev->of_node; |
| 1907 |
struct of_phandle_args clkspec; |
| 1908 |
bool once = true; |
| 1909 |
struct clk *clk; |
| 1910 |
unsigned int i; |
| 1911 |
int error; |
| 1912 |
|
| 1913 |
for (i = 0; !of_parse_phandle_with_args(np, "clocks", "#clock-cells", i, &clkspec); i++) { |
| 1914 |
if (!rzg2l_cpg_is_pm_clk(priv, &clkspec)) { |
| 1915 |
of_node_put(clkspec.np); |
| 1916 |
continue; |
| 1917 |
} |
| 1918 |
|
| 1919 |
if (once) { |
| 1920 |
once = false; |
| 1921 |
error = pm_clk_create(dev); |
| 1922 |
if (error) { |
| 1923 |
of_node_put(clkspec.np); |
| 1924 |
goto err; |
| 1925 |
} |
| 1926 |
} |
| 1927 |
clk = of_clk_get_from_provider(&clkspec); |
| 1928 |
of_node_put(clkspec.np); |
| 1929 |
if (IS_ERR(clk)) { |
| 1930 |
error = PTR_ERR(clk); |
| 1931 |
goto fail_destroy; |
| 1932 |
} |
| 1933 |
|
| 1934 |
error = pm_clk_add_clk(dev, clk); |
| 1935 |
if (error) { |
| 1936 |
dev_err(dev, "pm_clk_add_clk failed %d\n", error); |
| 1937 |
goto fail_put; |
| 1938 |
} |
| 1939 |
} |
| 1940 |
|
| 1941 |
return 0; |
| 1942 |
|
| 1943 |
fail_put: |
| 1944 |
clk_put(clk); |
| 1945 |
|
| 1946 |
fail_destroy: |
| 1947 |
pm_clk_destroy(dev); |
| 1948 |
err: |
| 1949 |
return error; |
| 1950 |
} |
| 1951 |
|
| 1952 |
static void rzg2l_cpg_detach_dev(struct generic_pm_domain *unused, struct device *dev) |
| 1953 |
{ |
| 1954 |
if (!pm_clk_no_clocks(dev)) |
| 1955 |
pm_clk_destroy(dev); |
| 1956 |
} |
| 1957 |
|
| 1958 |
static void rzg2l_cpg_genpd_remove(void *data) |
| 1959 |
{ |
| 1960 |
pm_genpd_remove(data); |
| 1961 |
} |
| 1962 |
|
| 1963 |
static int __init rzg2l_cpg_add_clk_domain(struct rzg2l_cpg_priv *priv) |
| 1964 |
{ |
| 1965 |
struct device *dev = priv->dev; |
| 1966 |
struct device_node *np = dev->of_node; |
| 1967 |
struct generic_pm_domain *genpd = &priv->genpd; |
| 1968 |
int ret; |
| 1969 |
|
| 1970 |
genpd->name = np->name; |
| 1971 |
genpd->flags = GENPD_FLAG_PM_CLK | GENPD_FLAG_ALWAYS_ON | |
| 1972 |
GENPD_FLAG_ACTIVE_WAKEUP; |
| 1973 |
genpd->attach_dev = rzg2l_cpg_attach_dev; |
| 1974 |
genpd->detach_dev = rzg2l_cpg_detach_dev; |
| 1975 |
ret = pm_genpd_init(genpd, &pm_domain_always_on_gov, false); |
| 1976 |
if (ret) |
| 1977 |
return ret; |
| 1978 |
|
| 1979 |
ret = devm_add_action_or_reset(dev, rzg2l_cpg_genpd_remove, genpd); |
| 1980 |
if (ret) |
| 1981 |
return ret; |
| 1982 |
|
| 1983 |
return of_genpd_add_provider_simple(np, genpd); |
| 1984 |
} |
| 1985 |
|
| 1986 |
static int __init rzg2l_cpg_probe(struct platform_device *pdev) |
| 1987 |
{ |
| 1988 |
struct device *dev = &pdev->dev; |
| 1989 |
struct device_node *np = dev->of_node; |
| 1990 |
const struct rzg2l_cpg_info *info; |
| 1991 |
struct rzg2l_cpg_priv *priv; |
| 1992 |
unsigned int nclks, i; |
| 1993 |
struct clk **clks; |
| 1994 |
int error; |
| 1995 |
|
| 1996 |
info = of_device_get_match_data(dev); |
| 1997 |
|
| 1998 |
priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); |
| 1999 |
if (!priv) |
| 2000 |
return -ENOMEM; |
| 2001 |
|
| 2002 |
priv->dev = dev; |
| 2003 |
priv->info = info; |
| 2004 |
spin_lock_init(&priv->rmw_lock); |
| 2005 |
|
| 2006 |
priv->base = devm_platform_ioremap_resource(pdev, 0); |
| 2007 |
if (IS_ERR(priv->base)) |
| 2008 |
return PTR_ERR(priv->base); |
| 2009 |
|
| 2010 |
nclks = info->num_total_core_clks + info->num_hw_mod_clks; |
| 2011 |
clks = devm_kmalloc_array(dev, nclks, sizeof(*clks), GFP_KERNEL); |
| 2012 |
if (!clks) |
| 2013 |
return -ENOMEM; |
| 2014 |
|
| 2015 |
dev_set_drvdata(dev, priv); |
| 2016 |
priv->clks = clks; |
| 2017 |
priv->num_core_clks = info->num_total_core_clks; |
| 2018 |
priv->num_mod_clks = info->num_hw_mod_clks; |
| 2019 |
priv->num_resets = info->num_resets; |
| 2020 |
priv->last_dt_core_clk = info->last_dt_core_clk; |
| 2021 |
|
| 2022 |
for (i = 0; i < nclks; i++) |
| 2023 |
clks[i] = ERR_PTR(-ENOENT); |
| 2024 |
|
| 2025 |
for (i = 0; i < info->num_core_clks; i++) |
| 2026 |
rzg2l_cpg_register_core_clk(&info->core_clks[i], info, priv); |
| 2027 |
|
| 2028 |
for (i = 0; i < info->num_mod_clks; i++) |
| 2029 |
rzg2l_cpg_register_mod_clk(&info->mod_clks[i], info, priv); |
| 2030 |
|
| 2031 |
/* |
| 2032 |
* Initialize MSTOP after all the clocks were registered to avoid |
| 2033 |
* invalid reference counting when multiple clocks (critical, |
| 2034 |
* non-critical) share the same MSTOP. |
| 2035 |
*/ |
| 2036 |
rzg2l_mod_clock_init_mstop(priv); |
| 2037 |
|
| 2038 |
error = of_clk_add_provider(np, rzg2l_cpg_clk_src_twocell_get, priv); |
| 2039 |
if (error) |
| 2040 |
return error; |
| 2041 |
|
| 2042 |
error = devm_add_action_or_reset(dev, rzg2l_cpg_del_clk_provider, np); |
| 2043 |
if (error) |
| 2044 |
return error; |
| 2045 |
|
| 2046 |
error = rzg2l_cpg_add_clk_domain(priv); |
| 2047 |
if (error) |
| 2048 |
return error; |
| 2049 |
|
| 2050 |
error = rzg2l_cpg_reset_controller_register(priv); |
| 2051 |
if (error) |
| 2052 |
return error; |
| 2053 |
|
| 2054 |
debugfs_create_file("mstop", 0444, NULL, priv, &rzg2l_mod_clock_mstop_fops); |
| 2055 |
return 0; |
| 2056 |
} |
| 2057 |
|
| 2058 |
static int rzg2l_cpg_resume(struct device *dev) |
| 2059 |
{ |
| 2060 |
struct rzg2l_cpg_priv *priv = dev_get_drvdata(dev); |
| 2061 |
|
| 2062 |
rzg2l_mod_clock_init_mstop(priv); |
| 2063 |
|
| 2064 |
return 0; |
| 2065 |
} |
| 2066 |
|
| 2067 |
static const struct dev_pm_ops rzg2l_cpg_pm_ops = { |
| 2068 |
NOIRQ_SYSTEM_SLEEP_PM_OPS(NULL, rzg2l_cpg_resume) |
| 2069 |
}; |
| 2070 |
|
| 2071 |
static const struct of_device_id rzg2l_cpg_match[] = { |
| 2072 |
#ifdef CONFIG_CLK_R9A07G043 |
| 2073 |
{ |
| 2074 |
.compatible = "renesas,r9a07g043-cpg", |
| 2075 |
.data = &r9a07g043_cpg_info, |
| 2076 |
}, |
| 2077 |
#endif |
| 2078 |
#ifdef CONFIG_CLK_R9A07G044 |
| 2079 |
{ |
| 2080 |
.compatible = "renesas,r9a07g044-cpg", |
| 2081 |
.data = &r9a07g044_cpg_info, |
| 2082 |
}, |
| 2083 |
#endif |
| 2084 |
#ifdef CONFIG_CLK_R9A07G054 |
| 2085 |
{ |
| 2086 |
.compatible = "renesas,r9a07g054-cpg", |
| 2087 |
.data = &r9a07g054_cpg_info, |
| 2088 |
}, |
| 2089 |
#endif |
| 2090 |
#ifdef CONFIG_CLK_R9A08G045 |
| 2091 |
{ |
| 2092 |
.compatible = "renesas,r9a08g045-cpg", |
| 2093 |
.data = &r9a08g045_cpg_info, |
| 2094 |
}, |
| 2095 |
#endif |
| 2096 |
#ifdef CONFIG_CLK_R9A09G011 |
| 2097 |
{ |
| 2098 |
.compatible = "renesas,r9a09g011-cpg", |
| 2099 |
.data = &r9a09g011_cpg_info, |
| 2100 |
}, |
| 2101 |
#endif |
| 2102 |
{ /* sentinel */ } |
| 2103 |
}; |
| 2104 |
|
| 2105 |
static struct platform_driver rzg2l_cpg_driver = { |
| 2106 |
.driver = { |
| 2107 |
.name = "rzg2l-cpg", |
| 2108 |
.of_match_table = rzg2l_cpg_match, |
| 2109 |
.pm = pm_sleep_ptr(&rzg2l_cpg_pm_ops), |
| 2110 |
}, |
| 2111 |
}; |
| 2112 |
|
| 2113 |
static int __init rzg2l_cpg_init(void) |
| 2114 |
{ |
| 2115 |
return platform_driver_probe(&rzg2l_cpg_driver, rzg2l_cpg_probe); |
| 2116 |
} |
| 2117 |
|
| 2118 |
subsys_initcall(rzg2l_cpg_init); |
| 2119 |
|
| 2120 |
MODULE_DESCRIPTION("Renesas RZ/G2L CPG Driver"); |