18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2014, The Linux Foundation. All rights reserved. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/kernel.h> 78c2ecf20Sopenharmony_ci#include <linux/bitops.h> 88c2ecf20Sopenharmony_ci#include <linux/regmap.h> 98c2ecf20Sopenharmony_ci#include <linux/export.h> 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include "clk-regmap-divider.h" 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_cistatic inline struct clk_regmap_div *to_clk_regmap_div(struct clk_hw *hw) 148c2ecf20Sopenharmony_ci{ 158c2ecf20Sopenharmony_ci return container_of(to_clk_regmap(hw), struct clk_regmap_div, clkr); 168c2ecf20Sopenharmony_ci} 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_cistatic long div_round_ro_rate(struct clk_hw *hw, unsigned long rate, 198c2ecf20Sopenharmony_ci unsigned long *prate) 208c2ecf20Sopenharmony_ci{ 218c2ecf20Sopenharmony_ci struct clk_regmap_div *divider = to_clk_regmap_div(hw); 228c2ecf20Sopenharmony_ci struct clk_regmap *clkr = ÷r->clkr; 238c2ecf20Sopenharmony_ci u32 val; 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci regmap_read(clkr->regmap, divider->reg, &val); 268c2ecf20Sopenharmony_ci val >>= divider->shift; 278c2ecf20Sopenharmony_ci val &= BIT(divider->width) - 1; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ci return divider_ro_round_rate(hw, rate, prate, NULL, divider->width, 308c2ecf20Sopenharmony_ci CLK_DIVIDER_ROUND_CLOSEST, val); 318c2ecf20Sopenharmony_ci} 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistatic long div_round_rate(struct clk_hw *hw, unsigned long rate, 348c2ecf20Sopenharmony_ci unsigned long *prate) 358c2ecf20Sopenharmony_ci{ 368c2ecf20Sopenharmony_ci struct clk_regmap_div *divider = to_clk_regmap_div(hw); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci return divider_round_rate(hw, rate, prate, NULL, divider->width, 398c2ecf20Sopenharmony_ci CLK_DIVIDER_ROUND_CLOSEST); 408c2ecf20Sopenharmony_ci} 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic int div_set_rate(struct clk_hw *hw, unsigned long rate, 438c2ecf20Sopenharmony_ci unsigned long parent_rate) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci struct clk_regmap_div *divider = to_clk_regmap_div(hw); 468c2ecf20Sopenharmony_ci struct clk_regmap *clkr = ÷r->clkr; 478c2ecf20Sopenharmony_ci u32 div; 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci div = divider_get_val(rate, parent_rate, NULL, divider->width, 508c2ecf20Sopenharmony_ci CLK_DIVIDER_ROUND_CLOSEST); 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci return regmap_update_bits(clkr->regmap, divider->reg, 538c2ecf20Sopenharmony_ci (BIT(divider->width) - 1) << divider->shift, 548c2ecf20Sopenharmony_ci div << divider->shift); 558c2ecf20Sopenharmony_ci} 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_cistatic unsigned long div_recalc_rate(struct clk_hw *hw, 588c2ecf20Sopenharmony_ci unsigned long parent_rate) 598c2ecf20Sopenharmony_ci{ 608c2ecf20Sopenharmony_ci struct clk_regmap_div *divider = to_clk_regmap_div(hw); 618c2ecf20Sopenharmony_ci struct clk_regmap *clkr = ÷r->clkr; 628c2ecf20Sopenharmony_ci u32 div; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci regmap_read(clkr->regmap, divider->reg, &div); 658c2ecf20Sopenharmony_ci div >>= divider->shift; 668c2ecf20Sopenharmony_ci div &= BIT(divider->width) - 1; 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ci return divider_recalc_rate(hw, parent_rate, div, NULL, 698c2ecf20Sopenharmony_ci CLK_DIVIDER_ROUND_CLOSEST, divider->width); 708c2ecf20Sopenharmony_ci} 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ciconst struct clk_ops clk_regmap_div_ops = { 738c2ecf20Sopenharmony_ci .round_rate = div_round_rate, 748c2ecf20Sopenharmony_ci .set_rate = div_set_rate, 758c2ecf20Sopenharmony_ci .recalc_rate = div_recalc_rate, 768c2ecf20Sopenharmony_ci}; 778c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_regmap_div_ops); 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ciconst struct clk_ops clk_regmap_div_ro_ops = { 808c2ecf20Sopenharmony_ci .round_rate = div_round_ro_rate, 818c2ecf20Sopenharmony_ci .recalc_rate = div_recalc_rate, 828c2ecf20Sopenharmony_ci}; 838c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(clk_regmap_div_ro_ops); 84