10c6ab1b8SGeorgi Djakov // SPDX-License-Identifier: GPL-2.0
20c6ab1b8SGeorgi Djakov /*
30c6ab1b8SGeorgi Djakov * Qualcomm A53 PLL driver
40c6ab1b8SGeorgi Djakov *
50c6ab1b8SGeorgi Djakov * Copyright (c) 2017, Linaro Limited
60c6ab1b8SGeorgi Djakov * Author: Georgi Djakov <georgi.djakov@linaro.org>
70c6ab1b8SGeorgi Djakov */
80c6ab1b8SGeorgi Djakov
95d9bc010SShawn Guo #include <linux/clk.h>
100c6ab1b8SGeorgi Djakov #include <linux/clk-provider.h>
110c6ab1b8SGeorgi Djakov #include <linux/kernel.h>
120c6ab1b8SGeorgi Djakov #include <linux/platform_device.h>
135d9bc010SShawn Guo #include <linux/pm_opp.h>
140c6ab1b8SGeorgi Djakov #include <linux/regmap.h>
150c6ab1b8SGeorgi Djakov #include <linux/module.h>
160c6ab1b8SGeorgi Djakov
170c6ab1b8SGeorgi Djakov #include "clk-pll.h"
180c6ab1b8SGeorgi Djakov #include "clk-regmap.h"
190c6ab1b8SGeorgi Djakov
200c6ab1b8SGeorgi Djakov static const struct pll_freq_tbl a53pll_freq[] = {
210c6ab1b8SGeorgi Djakov { 998400000, 52, 0x0, 0x1, 0 },
220c6ab1b8SGeorgi Djakov { 1094400000, 57, 0x0, 0x1, 0 },
230c6ab1b8SGeorgi Djakov { 1152000000, 62, 0x0, 0x1, 0 },
240c6ab1b8SGeorgi Djakov { 1209600000, 63, 0x0, 0x1, 0 },
250c6ab1b8SGeorgi Djakov { 1248000000, 65, 0x0, 0x1, 0 },
260c6ab1b8SGeorgi Djakov { 1363200000, 71, 0x0, 0x1, 0 },
270c6ab1b8SGeorgi Djakov { 1401600000, 73, 0x0, 0x1, 0 },
280c6ab1b8SGeorgi Djakov { }
290c6ab1b8SGeorgi Djakov };
300c6ab1b8SGeorgi Djakov
310c6ab1b8SGeorgi Djakov static const struct regmap_config a53pll_regmap_config = {
320c6ab1b8SGeorgi Djakov .reg_bits = 32,
330c6ab1b8SGeorgi Djakov .reg_stride = 4,
340c6ab1b8SGeorgi Djakov .val_bits = 32,
350c6ab1b8SGeorgi Djakov .max_register = 0x40,
360c6ab1b8SGeorgi Djakov .fast_io = true,
370c6ab1b8SGeorgi Djakov };
380c6ab1b8SGeorgi Djakov
qcom_a53pll_get_freq_tbl(struct device * dev)395d9bc010SShawn Guo static struct pll_freq_tbl *qcom_a53pll_get_freq_tbl(struct device *dev)
405d9bc010SShawn Guo {
415d9bc010SShawn Guo struct pll_freq_tbl *freq_tbl;
425d9bc010SShawn Guo unsigned long xo_freq;
435d9bc010SShawn Guo unsigned long freq;
445d9bc010SShawn Guo struct clk *xo_clk;
455d9bc010SShawn Guo int count;
465d9bc010SShawn Guo int ret;
475d9bc010SShawn Guo int i;
485d9bc010SShawn Guo
495d9bc010SShawn Guo xo_clk = devm_clk_get(dev, "xo");
505d9bc010SShawn Guo if (IS_ERR(xo_clk))
515d9bc010SShawn Guo return NULL;
525d9bc010SShawn Guo
535d9bc010SShawn Guo xo_freq = clk_get_rate(xo_clk);
545d9bc010SShawn Guo
555d9bc010SShawn Guo ret = devm_pm_opp_of_add_table(dev);
565d9bc010SShawn Guo if (ret)
575d9bc010SShawn Guo return NULL;
585d9bc010SShawn Guo
595d9bc010SShawn Guo count = dev_pm_opp_get_opp_count(dev);
605d9bc010SShawn Guo if (count <= 0)
615d9bc010SShawn Guo return NULL;
625d9bc010SShawn Guo
635d9bc010SShawn Guo freq_tbl = devm_kcalloc(dev, count + 1, sizeof(*freq_tbl), GFP_KERNEL);
645d9bc010SShawn Guo if (!freq_tbl)
655d9bc010SShawn Guo return NULL;
665d9bc010SShawn Guo
675d9bc010SShawn Guo for (i = 0, freq = 0; i < count; i++, freq++) {
685d9bc010SShawn Guo struct dev_pm_opp *opp;
695d9bc010SShawn Guo
705d9bc010SShawn Guo opp = dev_pm_opp_find_freq_ceil(dev, &freq);
715d9bc010SShawn Guo if (IS_ERR(opp))
725d9bc010SShawn Guo return NULL;
735d9bc010SShawn Guo
745d9bc010SShawn Guo /* Skip the freq that is not divisible */
755d9bc010SShawn Guo if (freq % xo_freq)
765d9bc010SShawn Guo continue;
775d9bc010SShawn Guo
785d9bc010SShawn Guo freq_tbl[i].freq = freq;
795d9bc010SShawn Guo freq_tbl[i].l = freq / xo_freq;
805d9bc010SShawn Guo freq_tbl[i].n = 1;
815d9bc010SShawn Guo
825d9bc010SShawn Guo dev_pm_opp_put(opp);
835d9bc010SShawn Guo }
845d9bc010SShawn Guo
855d9bc010SShawn Guo return freq_tbl;
865d9bc010SShawn Guo }
875d9bc010SShawn Guo
qcom_a53pll_probe(struct platform_device * pdev)880c6ab1b8SGeorgi Djakov static int qcom_a53pll_probe(struct platform_device *pdev)
890c6ab1b8SGeorgi Djakov {
900c6ab1b8SGeorgi Djakov struct device *dev = &pdev->dev;
9105cc560cSShawn Guo struct device_node *np = dev->of_node;
920c6ab1b8SGeorgi Djakov struct regmap *regmap;
930c6ab1b8SGeorgi Djakov struct clk_pll *pll;
940c6ab1b8SGeorgi Djakov void __iomem *base;
950c6ab1b8SGeorgi Djakov struct clk_init_data init = { };
960c6ab1b8SGeorgi Djakov int ret;
970c6ab1b8SGeorgi Djakov
980c6ab1b8SGeorgi Djakov pll = devm_kzalloc(dev, sizeof(*pll), GFP_KERNEL);
990c6ab1b8SGeorgi Djakov if (!pll)
1000c6ab1b8SGeorgi Djakov return -ENOMEM;
1010c6ab1b8SGeorgi Djakov
102aacbbe6bSCai Huoqing base = devm_platform_ioremap_resource(pdev, 0);
1030c6ab1b8SGeorgi Djakov if (IS_ERR(base))
1040c6ab1b8SGeorgi Djakov return PTR_ERR(base);
1050c6ab1b8SGeorgi Djakov
1060c6ab1b8SGeorgi Djakov regmap = devm_regmap_init_mmio(dev, base, &a53pll_regmap_config);
1070c6ab1b8SGeorgi Djakov if (IS_ERR(regmap))
1080c6ab1b8SGeorgi Djakov return PTR_ERR(regmap);
1090c6ab1b8SGeorgi Djakov
1100c6ab1b8SGeorgi Djakov pll->l_reg = 0x04;
1110c6ab1b8SGeorgi Djakov pll->m_reg = 0x08;
1120c6ab1b8SGeorgi Djakov pll->n_reg = 0x0c;
1130c6ab1b8SGeorgi Djakov pll->config_reg = 0x14;
1140c6ab1b8SGeorgi Djakov pll->mode_reg = 0x00;
1150c6ab1b8SGeorgi Djakov pll->status_reg = 0x1c;
1160c6ab1b8SGeorgi Djakov pll->status_bit = 16;
1175d9bc010SShawn Guo
1185d9bc010SShawn Guo pll->freq_tbl = qcom_a53pll_get_freq_tbl(dev);
1195d9bc010SShawn Guo if (!pll->freq_tbl) {
1205d9bc010SShawn Guo /* Fall on a53pll_freq if no freq_tbl is found from OPP */
1210c6ab1b8SGeorgi Djakov pll->freq_tbl = a53pll_freq;
1225d9bc010SShawn Guo }
1230c6ab1b8SGeorgi Djakov
12405cc560cSShawn Guo /* Use an unique name by appending @unit-address */
12505cc560cSShawn Guo init.name = devm_kasprintf(dev, GFP_KERNEL, "a53pll%s",
12605cc560cSShawn Guo strchrnul(np->full_name, '@'));
12705cc560cSShawn Guo if (!init.name)
12805cc560cSShawn Guo return -ENOMEM;
12905cc560cSShawn Guo
130*867bc326SDmitry Baryshkov init.parent_data = &(const struct clk_parent_data){
131*867bc326SDmitry Baryshkov .fw_name = "xo", .name = "xo_board",
132*867bc326SDmitry Baryshkov };
1330c6ab1b8SGeorgi Djakov init.num_parents = 1;
1340c6ab1b8SGeorgi Djakov init.ops = &clk_pll_sr2_ops;
1350c6ab1b8SGeorgi Djakov pll->clkr.hw.init = &init;
1360c6ab1b8SGeorgi Djakov
1370c6ab1b8SGeorgi Djakov ret = devm_clk_register_regmap(dev, &pll->clkr);
1380c6ab1b8SGeorgi Djakov if (ret) {
1390c6ab1b8SGeorgi Djakov dev_err(dev, "failed to register regmap clock: %d\n", ret);
1400c6ab1b8SGeorgi Djakov return ret;
1410c6ab1b8SGeorgi Djakov }
1420c6ab1b8SGeorgi Djakov
1430c6ab1b8SGeorgi Djakov ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
1440c6ab1b8SGeorgi Djakov &pll->clkr.hw);
1450c6ab1b8SGeorgi Djakov if (ret) {
1460c6ab1b8SGeorgi Djakov dev_err(dev, "failed to add clock provider: %d\n", ret);
1470c6ab1b8SGeorgi Djakov return ret;
1480c6ab1b8SGeorgi Djakov }
1490c6ab1b8SGeorgi Djakov
1500c6ab1b8SGeorgi Djakov return 0;
1510c6ab1b8SGeorgi Djakov }
1520c6ab1b8SGeorgi Djakov
1530c6ab1b8SGeorgi Djakov static const struct of_device_id qcom_a53pll_match_table[] = {
1540c6ab1b8SGeorgi Djakov { .compatible = "qcom,msm8916-a53pll" },
1555d9bc010SShawn Guo { .compatible = "qcom,msm8939-a53pll" },
1560c6ab1b8SGeorgi Djakov { }
1570c6ab1b8SGeorgi Djakov };
158790b516aSChen Hui MODULE_DEVICE_TABLE(of, qcom_a53pll_match_table);
1590c6ab1b8SGeorgi Djakov
1600c6ab1b8SGeorgi Djakov static struct platform_driver qcom_a53pll_driver = {
1610c6ab1b8SGeorgi Djakov .probe = qcom_a53pll_probe,
1620c6ab1b8SGeorgi Djakov .driver = {
1630c6ab1b8SGeorgi Djakov .name = "qcom-a53pll",
1640c6ab1b8SGeorgi Djakov .of_match_table = qcom_a53pll_match_table,
1650c6ab1b8SGeorgi Djakov },
1660c6ab1b8SGeorgi Djakov };
1670c6ab1b8SGeorgi Djakov module_platform_driver(qcom_a53pll_driver);
1680c6ab1b8SGeorgi Djakov
1690c6ab1b8SGeorgi Djakov MODULE_DESCRIPTION("Qualcomm A53 PLL Driver");
1700c6ab1b8SGeorgi Djakov MODULE_LICENSE("GPL v2");
171