18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Lochnagar clock control 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2017-2018 Cirrus Logic, Inc. and 68c2ecf20Sopenharmony_ci * Cirrus Logic International Semiconductor Ltd. 78c2ecf20Sopenharmony_ci * 88c2ecf20Sopenharmony_ci * Author: Charles Keepax <ckeepax@opensource.cirrus.com> 98c2ecf20Sopenharmony_ci */ 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci#include <linux/clk-provider.h> 128c2ecf20Sopenharmony_ci#include <linux/device.h> 138c2ecf20Sopenharmony_ci#include <linux/module.h> 148c2ecf20Sopenharmony_ci#include <linux/of.h> 158c2ecf20Sopenharmony_ci#include <linux/of_device.h> 168c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 178c2ecf20Sopenharmony_ci#include <linux/regmap.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#include <linux/mfd/lochnagar1_regs.h> 208c2ecf20Sopenharmony_ci#include <linux/mfd/lochnagar2_regs.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include <dt-bindings/clk/lochnagar.h> 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define LOCHNAGAR_NUM_CLOCKS (LOCHNAGAR_SPDIF_CLKOUT + 1) 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_cistruct lochnagar_clk { 278c2ecf20Sopenharmony_ci const char * const name; 288c2ecf20Sopenharmony_ci struct clk_hw hw; 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci struct lochnagar_clk_priv *priv; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci u16 cfg_reg; 338c2ecf20Sopenharmony_ci u16 ena_mask; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci u16 src_reg; 368c2ecf20Sopenharmony_ci u16 src_mask; 378c2ecf20Sopenharmony_ci}; 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_cistruct lochnagar_clk_priv { 408c2ecf20Sopenharmony_ci struct device *dev; 418c2ecf20Sopenharmony_ci struct regmap *regmap; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_ci struct lochnagar_clk lclks[LOCHNAGAR_NUM_CLOCKS]; 448c2ecf20Sopenharmony_ci}; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci#define LN_PARENT(NAME) { .name = NAME, .fw_name = NAME } 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_cistatic const struct clk_parent_data lochnagar1_clk_parents[] = { 498c2ecf20Sopenharmony_ci LN_PARENT("ln-none"), 508c2ecf20Sopenharmony_ci LN_PARENT("ln-spdif-mclk"), 518c2ecf20Sopenharmony_ci LN_PARENT("ln-psia1-mclk"), 528c2ecf20Sopenharmony_ci LN_PARENT("ln-psia2-mclk"), 538c2ecf20Sopenharmony_ci LN_PARENT("ln-cdc-clkout"), 548c2ecf20Sopenharmony_ci LN_PARENT("ln-dsp-clkout"), 558c2ecf20Sopenharmony_ci LN_PARENT("ln-pmic-32k"), 568c2ecf20Sopenharmony_ci LN_PARENT("ln-gf-mclk1"), 578c2ecf20Sopenharmony_ci LN_PARENT("ln-gf-mclk3"), 588c2ecf20Sopenharmony_ci LN_PARENT("ln-gf-mclk2"), 598c2ecf20Sopenharmony_ci LN_PARENT("ln-gf-mclk4"), 608c2ecf20Sopenharmony_ci}; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_cistatic const struct clk_parent_data lochnagar2_clk_parents[] = { 638c2ecf20Sopenharmony_ci LN_PARENT("ln-none"), 648c2ecf20Sopenharmony_ci LN_PARENT("ln-cdc-clkout"), 658c2ecf20Sopenharmony_ci LN_PARENT("ln-dsp-clkout"), 668c2ecf20Sopenharmony_ci LN_PARENT("ln-pmic-32k"), 678c2ecf20Sopenharmony_ci LN_PARENT("ln-spdif-mclk"), 688c2ecf20Sopenharmony_ci LN_PARENT("ln-clk-12m"), 698c2ecf20Sopenharmony_ci LN_PARENT("ln-clk-11m"), 708c2ecf20Sopenharmony_ci LN_PARENT("ln-clk-24m"), 718c2ecf20Sopenharmony_ci LN_PARENT("ln-clk-22m"), 728c2ecf20Sopenharmony_ci LN_PARENT("ln-clk-8m"), 738c2ecf20Sopenharmony_ci LN_PARENT("ln-usb-clk-24m"), 748c2ecf20Sopenharmony_ci LN_PARENT("ln-gf-mclk1"), 758c2ecf20Sopenharmony_ci LN_PARENT("ln-gf-mclk3"), 768c2ecf20Sopenharmony_ci LN_PARENT("ln-gf-mclk2"), 778c2ecf20Sopenharmony_ci LN_PARENT("ln-psia1-mclk"), 788c2ecf20Sopenharmony_ci LN_PARENT("ln-psia2-mclk"), 798c2ecf20Sopenharmony_ci LN_PARENT("ln-spdif-clkout"), 808c2ecf20Sopenharmony_ci LN_PARENT("ln-adat-mclk"), 818c2ecf20Sopenharmony_ci LN_PARENT("ln-usb-clk-12m"), 828c2ecf20Sopenharmony_ci}; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci#define LN1_CLK(ID, NAME, REG) \ 858c2ecf20Sopenharmony_ci [LOCHNAGAR_##ID] = { \ 868c2ecf20Sopenharmony_ci .name = NAME, \ 878c2ecf20Sopenharmony_ci .cfg_reg = LOCHNAGAR1_##REG, \ 888c2ecf20Sopenharmony_ci .ena_mask = LOCHNAGAR1_##ID##_ENA_MASK, \ 898c2ecf20Sopenharmony_ci .src_reg = LOCHNAGAR1_##ID##_SEL, \ 908c2ecf20Sopenharmony_ci .src_mask = LOCHNAGAR1_SRC_MASK, \ 918c2ecf20Sopenharmony_ci } 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci#define LN2_CLK(ID, NAME) \ 948c2ecf20Sopenharmony_ci [LOCHNAGAR_##ID] = { \ 958c2ecf20Sopenharmony_ci .name = NAME, \ 968c2ecf20Sopenharmony_ci .cfg_reg = LOCHNAGAR2_##ID##_CTRL, \ 978c2ecf20Sopenharmony_ci .src_reg = LOCHNAGAR2_##ID##_CTRL, \ 988c2ecf20Sopenharmony_ci .ena_mask = LOCHNAGAR2_CLK_ENA_MASK, \ 998c2ecf20Sopenharmony_ci .src_mask = LOCHNAGAR2_CLK_SRC_MASK, \ 1008c2ecf20Sopenharmony_ci } 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistatic const struct lochnagar_clk lochnagar1_clks[LOCHNAGAR_NUM_CLOCKS] = { 1038c2ecf20Sopenharmony_ci LN1_CLK(CDC_MCLK1, "ln-cdc-mclk1", CDC_AIF_CTRL2), 1048c2ecf20Sopenharmony_ci LN1_CLK(CDC_MCLK2, "ln-cdc-mclk2", CDC_AIF_CTRL2), 1058c2ecf20Sopenharmony_ci LN1_CLK(DSP_CLKIN, "ln-dsp-clkin", DSP_AIF), 1068c2ecf20Sopenharmony_ci LN1_CLK(GF_CLKOUT1, "ln-gf-clkout1", GF_AIF1), 1078c2ecf20Sopenharmony_ci}; 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_cistatic const struct lochnagar_clk lochnagar2_clks[LOCHNAGAR_NUM_CLOCKS] = { 1108c2ecf20Sopenharmony_ci LN2_CLK(CDC_MCLK1, "ln-cdc-mclk1"), 1118c2ecf20Sopenharmony_ci LN2_CLK(CDC_MCLK2, "ln-cdc-mclk2"), 1128c2ecf20Sopenharmony_ci LN2_CLK(DSP_CLKIN, "ln-dsp-clkin"), 1138c2ecf20Sopenharmony_ci LN2_CLK(GF_CLKOUT1, "ln-gf-clkout1"), 1148c2ecf20Sopenharmony_ci LN2_CLK(GF_CLKOUT2, "ln-gf-clkout2"), 1158c2ecf20Sopenharmony_ci LN2_CLK(PSIA1_MCLK, "ln-psia1-mclk"), 1168c2ecf20Sopenharmony_ci LN2_CLK(PSIA2_MCLK, "ln-psia2-mclk"), 1178c2ecf20Sopenharmony_ci LN2_CLK(SPDIF_MCLK, "ln-spdif-mclk"), 1188c2ecf20Sopenharmony_ci LN2_CLK(ADAT_MCLK, "ln-adat-mclk"), 1198c2ecf20Sopenharmony_ci LN2_CLK(SOUNDCARD_MCLK, "ln-soundcard-mclk"), 1208c2ecf20Sopenharmony_ci}; 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cistruct lochnagar_config { 1238c2ecf20Sopenharmony_ci const struct clk_parent_data *parents; 1248c2ecf20Sopenharmony_ci int nparents; 1258c2ecf20Sopenharmony_ci const struct lochnagar_clk *clks; 1268c2ecf20Sopenharmony_ci}; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_cistatic const struct lochnagar_config lochnagar1_conf = { 1298c2ecf20Sopenharmony_ci .parents = lochnagar1_clk_parents, 1308c2ecf20Sopenharmony_ci .nparents = ARRAY_SIZE(lochnagar1_clk_parents), 1318c2ecf20Sopenharmony_ci .clks = lochnagar1_clks, 1328c2ecf20Sopenharmony_ci}; 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_cistatic const struct lochnagar_config lochnagar2_conf = { 1358c2ecf20Sopenharmony_ci .parents = lochnagar2_clk_parents, 1368c2ecf20Sopenharmony_ci .nparents = ARRAY_SIZE(lochnagar2_clk_parents), 1378c2ecf20Sopenharmony_ci .clks = lochnagar2_clks, 1388c2ecf20Sopenharmony_ci}; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistatic inline struct lochnagar_clk *lochnagar_hw_to_lclk(struct clk_hw *hw) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci return container_of(hw, struct lochnagar_clk, hw); 1438c2ecf20Sopenharmony_ci} 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_cistatic int lochnagar_clk_prepare(struct clk_hw *hw) 1468c2ecf20Sopenharmony_ci{ 1478c2ecf20Sopenharmony_ci struct lochnagar_clk *lclk = lochnagar_hw_to_lclk(hw); 1488c2ecf20Sopenharmony_ci struct lochnagar_clk_priv *priv = lclk->priv; 1498c2ecf20Sopenharmony_ci struct regmap *regmap = priv->regmap; 1508c2ecf20Sopenharmony_ci int ret; 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci ret = regmap_update_bits(regmap, lclk->cfg_reg, 1538c2ecf20Sopenharmony_ci lclk->ena_mask, lclk->ena_mask); 1548c2ecf20Sopenharmony_ci if (ret < 0) 1558c2ecf20Sopenharmony_ci dev_dbg(priv->dev, "Failed to prepare %s: %d\n", 1568c2ecf20Sopenharmony_ci lclk->name, ret); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci return ret; 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_cistatic void lochnagar_clk_unprepare(struct clk_hw *hw) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci struct lochnagar_clk *lclk = lochnagar_hw_to_lclk(hw); 1648c2ecf20Sopenharmony_ci struct lochnagar_clk_priv *priv = lclk->priv; 1658c2ecf20Sopenharmony_ci struct regmap *regmap = priv->regmap; 1668c2ecf20Sopenharmony_ci int ret; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci ret = regmap_update_bits(regmap, lclk->cfg_reg, lclk->ena_mask, 0); 1698c2ecf20Sopenharmony_ci if (ret < 0) 1708c2ecf20Sopenharmony_ci dev_dbg(priv->dev, "Failed to unprepare %s: %d\n", 1718c2ecf20Sopenharmony_ci lclk->name, ret); 1728c2ecf20Sopenharmony_ci} 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_cistatic int lochnagar_clk_set_parent(struct clk_hw *hw, u8 index) 1758c2ecf20Sopenharmony_ci{ 1768c2ecf20Sopenharmony_ci struct lochnagar_clk *lclk = lochnagar_hw_to_lclk(hw); 1778c2ecf20Sopenharmony_ci struct lochnagar_clk_priv *priv = lclk->priv; 1788c2ecf20Sopenharmony_ci struct regmap *regmap = priv->regmap; 1798c2ecf20Sopenharmony_ci int ret; 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci ret = regmap_update_bits(regmap, lclk->src_reg, lclk->src_mask, index); 1828c2ecf20Sopenharmony_ci if (ret < 0) 1838c2ecf20Sopenharmony_ci dev_dbg(priv->dev, "Failed to reparent %s: %d\n", 1848c2ecf20Sopenharmony_ci lclk->name, ret); 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci return ret; 1878c2ecf20Sopenharmony_ci} 1888c2ecf20Sopenharmony_ci 1898c2ecf20Sopenharmony_cistatic u8 lochnagar_clk_get_parent(struct clk_hw *hw) 1908c2ecf20Sopenharmony_ci{ 1918c2ecf20Sopenharmony_ci struct lochnagar_clk *lclk = lochnagar_hw_to_lclk(hw); 1928c2ecf20Sopenharmony_ci struct lochnagar_clk_priv *priv = lclk->priv; 1938c2ecf20Sopenharmony_ci struct regmap *regmap = priv->regmap; 1948c2ecf20Sopenharmony_ci unsigned int val; 1958c2ecf20Sopenharmony_ci int ret; 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci ret = regmap_read(regmap, lclk->src_reg, &val); 1988c2ecf20Sopenharmony_ci if (ret < 0) { 1998c2ecf20Sopenharmony_ci dev_dbg(priv->dev, "Failed to read parent of %s: %d\n", 2008c2ecf20Sopenharmony_ci lclk->name, ret); 2018c2ecf20Sopenharmony_ci return clk_hw_get_num_parents(hw); 2028c2ecf20Sopenharmony_ci } 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci val &= lclk->src_mask; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci return val; 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_cistatic const struct clk_ops lochnagar_clk_ops = { 2108c2ecf20Sopenharmony_ci .prepare = lochnagar_clk_prepare, 2118c2ecf20Sopenharmony_ci .unprepare = lochnagar_clk_unprepare, 2128c2ecf20Sopenharmony_ci .set_parent = lochnagar_clk_set_parent, 2138c2ecf20Sopenharmony_ci .get_parent = lochnagar_clk_get_parent, 2148c2ecf20Sopenharmony_ci}; 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_cistatic struct clk_hw * 2178c2ecf20Sopenharmony_cilochnagar_of_clk_hw_get(struct of_phandle_args *clkspec, void *data) 2188c2ecf20Sopenharmony_ci{ 2198c2ecf20Sopenharmony_ci struct lochnagar_clk_priv *priv = data; 2208c2ecf20Sopenharmony_ci unsigned int idx = clkspec->args[0]; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci if (idx >= ARRAY_SIZE(priv->lclks)) { 2238c2ecf20Sopenharmony_ci dev_err(priv->dev, "Invalid index %u\n", idx); 2248c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 2258c2ecf20Sopenharmony_ci } 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ci return &priv->lclks[idx].hw; 2288c2ecf20Sopenharmony_ci} 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_cistatic const struct of_device_id lochnagar_of_match[] = { 2318c2ecf20Sopenharmony_ci { .compatible = "cirrus,lochnagar1-clk", .data = &lochnagar1_conf }, 2328c2ecf20Sopenharmony_ci { .compatible = "cirrus,lochnagar2-clk", .data = &lochnagar2_conf }, 2338c2ecf20Sopenharmony_ci {} 2348c2ecf20Sopenharmony_ci}; 2358c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, lochnagar_of_match); 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_cistatic int lochnagar_clk_probe(struct platform_device *pdev) 2388c2ecf20Sopenharmony_ci{ 2398c2ecf20Sopenharmony_ci struct clk_init_data clk_init = { 2408c2ecf20Sopenharmony_ci .ops = &lochnagar_clk_ops, 2418c2ecf20Sopenharmony_ci }; 2428c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 2438c2ecf20Sopenharmony_ci struct lochnagar_clk_priv *priv; 2448c2ecf20Sopenharmony_ci const struct of_device_id *of_id; 2458c2ecf20Sopenharmony_ci struct lochnagar_clk *lclk; 2468c2ecf20Sopenharmony_ci struct lochnagar_config *conf; 2478c2ecf20Sopenharmony_ci int ret, i; 2488c2ecf20Sopenharmony_ci 2498c2ecf20Sopenharmony_ci of_id = of_match_device(lochnagar_of_match, dev); 2508c2ecf20Sopenharmony_ci if (!of_id) 2518c2ecf20Sopenharmony_ci return -EINVAL; 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_ci priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 2548c2ecf20Sopenharmony_ci if (!priv) 2558c2ecf20Sopenharmony_ci return -ENOMEM; 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci priv->dev = dev; 2588c2ecf20Sopenharmony_ci priv->regmap = dev_get_regmap(dev->parent, NULL); 2598c2ecf20Sopenharmony_ci conf = (struct lochnagar_config *)of_id->data; 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_ci memcpy(priv->lclks, conf->clks, sizeof(priv->lclks)); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci clk_init.parent_data = conf->parents; 2648c2ecf20Sopenharmony_ci clk_init.num_parents = conf->nparents; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci for (i = 0; i < ARRAY_SIZE(priv->lclks); i++) { 2678c2ecf20Sopenharmony_ci lclk = &priv->lclks[i]; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci if (!lclk->name) 2708c2ecf20Sopenharmony_ci continue; 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci clk_init.name = lclk->name; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci lclk->priv = priv; 2758c2ecf20Sopenharmony_ci lclk->hw.init = &clk_init; 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_ci ret = devm_clk_hw_register(dev, &lclk->hw); 2788c2ecf20Sopenharmony_ci if (ret) { 2798c2ecf20Sopenharmony_ci dev_err(dev, "Failed to register %s: %d\n", 2808c2ecf20Sopenharmony_ci lclk->name, ret); 2818c2ecf20Sopenharmony_ci return ret; 2828c2ecf20Sopenharmony_ci } 2838c2ecf20Sopenharmony_ci } 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci ret = devm_of_clk_add_hw_provider(dev, lochnagar_of_clk_hw_get, priv); 2868c2ecf20Sopenharmony_ci if (ret < 0) 2878c2ecf20Sopenharmony_ci dev_err(dev, "Failed to register provider: %d\n", ret); 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci return ret; 2908c2ecf20Sopenharmony_ci} 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_cistatic struct platform_driver lochnagar_clk_driver = { 2938c2ecf20Sopenharmony_ci .driver = { 2948c2ecf20Sopenharmony_ci .name = "lochnagar-clk", 2958c2ecf20Sopenharmony_ci .of_match_table = lochnagar_of_match, 2968c2ecf20Sopenharmony_ci }, 2978c2ecf20Sopenharmony_ci .probe = lochnagar_clk_probe, 2988c2ecf20Sopenharmony_ci}; 2998c2ecf20Sopenharmony_cimodule_platform_driver(lochnagar_clk_driver); 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ciMODULE_AUTHOR("Charles Keepax <ckeepax@opensource.cirrus.com>"); 3028c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Clock driver for Cirrus Logic Lochnagar Board"); 3038c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 304