18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com> 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 78c2ecf20Sopenharmony_ci#include <linux/clkdev.h> 88c2ecf20Sopenharmony_ci#include <linux/clk/at91_pmc.h> 98c2ecf20Sopenharmony_ci#include <linux/of.h> 108c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h> 118c2ecf20Sopenharmony_ci#include <linux/regmap.h> 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#include "pmc.h" 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#define to_clk_plldiv(hw) container_of(hw, struct clk_plldiv, hw) 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_cistruct clk_plldiv { 188c2ecf20Sopenharmony_ci struct clk_hw hw; 198c2ecf20Sopenharmony_ci struct regmap *regmap; 208c2ecf20Sopenharmony_ci}; 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_cistatic unsigned long clk_plldiv_recalc_rate(struct clk_hw *hw, 238c2ecf20Sopenharmony_ci unsigned long parent_rate) 248c2ecf20Sopenharmony_ci{ 258c2ecf20Sopenharmony_ci struct clk_plldiv *plldiv = to_clk_plldiv(hw); 268c2ecf20Sopenharmony_ci unsigned int mckr; 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci regmap_read(plldiv->regmap, AT91_PMC_MCKR, &mckr); 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci if (mckr & AT91_PMC_PLLADIV2) 318c2ecf20Sopenharmony_ci return parent_rate / 2; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci return parent_rate; 348c2ecf20Sopenharmony_ci} 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_cistatic long clk_plldiv_round_rate(struct clk_hw *hw, unsigned long rate, 378c2ecf20Sopenharmony_ci unsigned long *parent_rate) 388c2ecf20Sopenharmony_ci{ 398c2ecf20Sopenharmony_ci unsigned long div; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci if (rate > *parent_rate) 428c2ecf20Sopenharmony_ci return *parent_rate; 438c2ecf20Sopenharmony_ci div = *parent_rate / 2; 448c2ecf20Sopenharmony_ci if (rate < div) 458c2ecf20Sopenharmony_ci return div; 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci if (rate - div < *parent_rate - rate) 488c2ecf20Sopenharmony_ci return div; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci return *parent_rate; 518c2ecf20Sopenharmony_ci} 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic int clk_plldiv_set_rate(struct clk_hw *hw, unsigned long rate, 548c2ecf20Sopenharmony_ci unsigned long parent_rate) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci struct clk_plldiv *plldiv = to_clk_plldiv(hw); 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci if ((parent_rate != rate) && (parent_rate / 2 != rate)) 598c2ecf20Sopenharmony_ci return -EINVAL; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci regmap_update_bits(plldiv->regmap, AT91_PMC_MCKR, AT91_PMC_PLLADIV2, 628c2ecf20Sopenharmony_ci parent_rate != rate ? AT91_PMC_PLLADIV2 : 0); 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci return 0; 658c2ecf20Sopenharmony_ci} 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cistatic const struct clk_ops plldiv_ops = { 688c2ecf20Sopenharmony_ci .recalc_rate = clk_plldiv_recalc_rate, 698c2ecf20Sopenharmony_ci .round_rate = clk_plldiv_round_rate, 708c2ecf20Sopenharmony_ci .set_rate = clk_plldiv_set_rate, 718c2ecf20Sopenharmony_ci}; 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistruct clk_hw * __init 748c2ecf20Sopenharmony_ciat91_clk_register_plldiv(struct regmap *regmap, const char *name, 758c2ecf20Sopenharmony_ci const char *parent_name) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci struct clk_plldiv *plldiv; 788c2ecf20Sopenharmony_ci struct clk_hw *hw; 798c2ecf20Sopenharmony_ci struct clk_init_data init; 808c2ecf20Sopenharmony_ci int ret; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci plldiv = kzalloc(sizeof(*plldiv), GFP_KERNEL); 838c2ecf20Sopenharmony_ci if (!plldiv) 848c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci init.name = name; 878c2ecf20Sopenharmony_ci init.ops = &plldiv_ops; 888c2ecf20Sopenharmony_ci init.parent_names = parent_name ? &parent_name : NULL; 898c2ecf20Sopenharmony_ci init.num_parents = parent_name ? 1 : 0; 908c2ecf20Sopenharmony_ci init.flags = CLK_SET_RATE_GATE; 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci plldiv->hw.init = &init; 938c2ecf20Sopenharmony_ci plldiv->regmap = regmap; 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci hw = &plldiv->hw; 968c2ecf20Sopenharmony_ci ret = clk_hw_register(NULL, &plldiv->hw); 978c2ecf20Sopenharmony_ci if (ret) { 988c2ecf20Sopenharmony_ci kfree(plldiv); 998c2ecf20Sopenharmony_ci hw = ERR_PTR(ret); 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci return hw; 1038c2ecf20Sopenharmony_ci} 104