19c7e4702STaniya Das // SPDX-License-Identifier: GPL-2.0 29c7e4702STaniya Das /* 36311b652SJordan Crouse * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved. 49c7e4702STaniya Das */ 59c7e4702STaniya Das 69c7e4702STaniya Das #include <linux/clk-provider.h> 79c7e4702STaniya Das #include <linux/err.h> 89c7e4702STaniya Das #include <linux/kernel.h> 99c7e4702STaniya Das #include <linux/module.h> 109c7e4702STaniya Das #include <linux/of.h> 119c7e4702STaniya Das #include <linux/of_device.h> 129c7e4702STaniya Das #include <linux/platform_device.h> 139c7e4702STaniya Das #include <soc/qcom/cmd-db.h> 149c7e4702STaniya Das #include <soc/qcom/rpmh.h> 156311b652SJordan Crouse #include <soc/qcom/tcs.h> 169c7e4702STaniya Das 179c7e4702STaniya Das #include <dt-bindings/clock/qcom,rpmh.h> 189c7e4702STaniya Das 199c7e4702STaniya Das #define CLK_RPMH_ARC_EN_OFFSET 0 209c7e4702STaniya Das #define CLK_RPMH_VRM_EN_OFFSET 4 219c7e4702STaniya Das 2204053f4dSDavid Dai /** 2304053f4dSDavid Dai * struct bcm_db - Auxiliary data pertaining to each Bus Clock Manager(BCM) 2404053f4dSDavid Dai * @unit: divisor used to convert Hz value to an RPMh msg 2504053f4dSDavid Dai * @width: multiplier used to convert Hz value to an RPMh msg 2604053f4dSDavid Dai * @vcd: virtual clock domain that this bcm belongs to 2704053f4dSDavid Dai * @reserved: reserved to pad the struct 2804053f4dSDavid Dai */ 2904053f4dSDavid Dai struct bcm_db { 3004053f4dSDavid Dai __le32 unit; 3104053f4dSDavid Dai __le16 width; 3204053f4dSDavid Dai u8 vcd; 3304053f4dSDavid Dai u8 reserved; 3404053f4dSDavid Dai }; 3504053f4dSDavid Dai 369c7e4702STaniya Das /** 379c7e4702STaniya Das * struct clk_rpmh - individual rpmh clock data structure 389c7e4702STaniya Das * @hw: handle between common and hardware-specific interfaces 399c7e4702STaniya Das * @res_name: resource name for the rpmh clock 409c7e4702STaniya Das * @div: clock divider to compute the clock rate 419c7e4702STaniya Das * @res_addr: base address of the rpmh resource within the RPMh 429c7e4702STaniya Das * @res_on_val: rpmh clock enable value 439c7e4702STaniya Das * @state: rpmh clock requested state 449c7e4702STaniya Das * @aggr_state: rpmh clock aggregated state 459c7e4702STaniya Das * @last_sent_aggr_state: rpmh clock last aggr state sent to RPMh 469c7e4702STaniya Das * @valid_state_mask: mask to determine the state of the rpmh clock 4704053f4dSDavid Dai * @unit: divisor to convert rate to rpmh msg in magnitudes of Khz 489c7e4702STaniya Das * @dev: device to which it is attached 499c7e4702STaniya Das * @peer: pointer to the clock rpmh sibling 509c7e4702STaniya Das */ 519c7e4702STaniya Das struct clk_rpmh { 529c7e4702STaniya Das struct clk_hw hw; 539c7e4702STaniya Das const char *res_name; 549c7e4702STaniya Das u8 div; 559c7e4702STaniya Das u32 res_addr; 569c7e4702STaniya Das u32 res_on_val; 579c7e4702STaniya Das u32 state; 589c7e4702STaniya Das u32 aggr_state; 599c7e4702STaniya Das u32 last_sent_aggr_state; 609c7e4702STaniya Das u32 valid_state_mask; 6104053f4dSDavid Dai u32 unit; 629c7e4702STaniya Das struct device *dev; 639c7e4702STaniya Das struct clk_rpmh *peer; 649c7e4702STaniya Das }; 659c7e4702STaniya Das 669c7e4702STaniya Das struct clk_rpmh_desc { 679c7e4702STaniya Das struct clk_hw **clks; 689c7e4702STaniya Das size_t num_clks; 699c7e4702STaniya Das }; 709c7e4702STaniya Das 719c7e4702STaniya Das static DEFINE_MUTEX(rpmh_clk_lock); 729c7e4702STaniya Das 739c7e4702STaniya Das #define __DEFINE_CLK_RPMH(_platform, _name, _name_active, _res_name, \ 749c7e4702STaniya Das _res_en_offset, _res_on, _div) \ 759c7e4702STaniya Das static struct clk_rpmh _platform##_##_name_active; \ 769c7e4702STaniya Das static struct clk_rpmh _platform##_##_name = { \ 779c7e4702STaniya Das .res_name = _res_name, \ 789c7e4702STaniya Das .res_addr = _res_en_offset, \ 799c7e4702STaniya Das .res_on_val = _res_on, \ 809c7e4702STaniya Das .div = _div, \ 819c7e4702STaniya Das .peer = &_platform##_##_name_active, \ 829c7e4702STaniya Das .valid_state_mask = (BIT(RPMH_WAKE_ONLY_STATE) | \ 839c7e4702STaniya Das BIT(RPMH_ACTIVE_ONLY_STATE) | \ 849c7e4702STaniya Das BIT(RPMH_SLEEP_STATE)), \ 859c7e4702STaniya Das .hw.init = &(struct clk_init_data){ \ 869c7e4702STaniya Das .ops = &clk_rpmh_ops, \ 879c7e4702STaniya Das .name = #_name, \ 88a64a9e51SVinod Koul .parent_data = &(const struct clk_parent_data){ \ 89a64a9e51SVinod Koul .fw_name = "xo", \ 90a64a9e51SVinod Koul .name = "xo_board", \ 91a64a9e51SVinod Koul }, \ 929c7e4702STaniya Das .num_parents = 1, \ 939c7e4702STaniya Das }, \ 949c7e4702STaniya Das }; \ 959c7e4702STaniya Das static struct clk_rpmh _platform##_##_name_active = { \ 969c7e4702STaniya Das .res_name = _res_name, \ 979c7e4702STaniya Das .res_addr = _res_en_offset, \ 989c7e4702STaniya Das .res_on_val = _res_on, \ 999c7e4702STaniya Das .div = _div, \ 1009c7e4702STaniya Das .peer = &_platform##_##_name, \ 1019c7e4702STaniya Das .valid_state_mask = (BIT(RPMH_WAKE_ONLY_STATE) | \ 1029c7e4702STaniya Das BIT(RPMH_ACTIVE_ONLY_STATE)), \ 1039c7e4702STaniya Das .hw.init = &(struct clk_init_data){ \ 1049c7e4702STaniya Das .ops = &clk_rpmh_ops, \ 1059c7e4702STaniya Das .name = #_name_active, \ 106a64a9e51SVinod Koul .parent_data = &(const struct clk_parent_data){ \ 107a64a9e51SVinod Koul .fw_name = "xo", \ 108a64a9e51SVinod Koul .name = "xo_board", \ 109a64a9e51SVinod Koul }, \ 1109c7e4702STaniya Das .num_parents = 1, \ 1119c7e4702STaniya Das }, \ 1129c7e4702STaniya Das } 1139c7e4702STaniya Das 1149c7e4702STaniya Das #define DEFINE_CLK_RPMH_ARC(_platform, _name, _name_active, _res_name, \ 1159c7e4702STaniya Das _res_on, _div) \ 1169c7e4702STaniya Das __DEFINE_CLK_RPMH(_platform, _name, _name_active, _res_name, \ 1179c7e4702STaniya Das CLK_RPMH_ARC_EN_OFFSET, _res_on, _div) 1189c7e4702STaniya Das 1199c7e4702STaniya Das #define DEFINE_CLK_RPMH_VRM(_platform, _name, _name_active, _res_name, \ 1209c7e4702STaniya Das _div) \ 1219c7e4702STaniya Das __DEFINE_CLK_RPMH(_platform, _name, _name_active, _res_name, \ 1229c7e4702STaniya Das CLK_RPMH_VRM_EN_OFFSET, 1, _div) 1239c7e4702STaniya Das 12404053f4dSDavid Dai #define DEFINE_CLK_RPMH_BCM(_platform, _name, _res_name) \ 12504053f4dSDavid Dai static struct clk_rpmh _platform##_##_name = { \ 12604053f4dSDavid Dai .res_name = _res_name, \ 12704053f4dSDavid Dai .valid_state_mask = BIT(RPMH_ACTIVE_ONLY_STATE), \ 12804053f4dSDavid Dai .div = 1, \ 12904053f4dSDavid Dai .hw.init = &(struct clk_init_data){ \ 13004053f4dSDavid Dai .ops = &clk_rpmh_bcm_ops, \ 13104053f4dSDavid Dai .name = #_name, \ 13204053f4dSDavid Dai }, \ 13304053f4dSDavid Dai } 13404053f4dSDavid Dai 1359c7e4702STaniya Das static inline struct clk_rpmh *to_clk_rpmh(struct clk_hw *_hw) 1369c7e4702STaniya Das { 1379c7e4702STaniya Das return container_of(_hw, struct clk_rpmh, hw); 1389c7e4702STaniya Das } 1399c7e4702STaniya Das 1409c7e4702STaniya Das static inline bool has_state_changed(struct clk_rpmh *c, u32 state) 1419c7e4702STaniya Das { 1429c7e4702STaniya Das return (c->last_sent_aggr_state & BIT(state)) 1439c7e4702STaniya Das != (c->aggr_state & BIT(state)); 1449c7e4702STaniya Das } 1459c7e4702STaniya Das 1469c7e4702STaniya Das static int clk_rpmh_send_aggregate_command(struct clk_rpmh *c) 1479c7e4702STaniya Das { 1489c7e4702STaniya Das struct tcs_cmd cmd = { 0 }; 1499c7e4702STaniya Das u32 cmd_state, on_val; 1509c7e4702STaniya Das enum rpmh_state state = RPMH_SLEEP_STATE; 1519c7e4702STaniya Das int ret; 1529c7e4702STaniya Das 1539c7e4702STaniya Das cmd.addr = c->res_addr; 1549c7e4702STaniya Das cmd_state = c->aggr_state; 1559c7e4702STaniya Das on_val = c->res_on_val; 1569c7e4702STaniya Das 1579c7e4702STaniya Das for (; state <= RPMH_ACTIVE_ONLY_STATE; state++) { 1589c7e4702STaniya Das if (has_state_changed(c, state)) { 1599c7e4702STaniya Das if (cmd_state & BIT(state)) 1609c7e4702STaniya Das cmd.data = on_val; 1619c7e4702STaniya Das 1629c7e4702STaniya Das ret = rpmh_write_async(c->dev, state, &cmd, 1); 1639c7e4702STaniya Das if (ret) { 1649c7e4702STaniya Das dev_err(c->dev, "set %s state of %s failed: (%d)\n", 1659c7e4702STaniya Das !state ? "sleep" : 1669c7e4702STaniya Das state == RPMH_WAKE_ONLY_STATE ? 1679c7e4702STaniya Das "wake" : "active", c->res_name, ret); 1689c7e4702STaniya Das return ret; 1699c7e4702STaniya Das } 1709c7e4702STaniya Das } 1719c7e4702STaniya Das } 1729c7e4702STaniya Das 1739c7e4702STaniya Das c->last_sent_aggr_state = c->aggr_state; 1749c7e4702STaniya Das c->peer->last_sent_aggr_state = c->last_sent_aggr_state; 1759c7e4702STaniya Das 1769c7e4702STaniya Das return 0; 1779c7e4702STaniya Das } 1789c7e4702STaniya Das 1799c7e4702STaniya Das /* 1809c7e4702STaniya Das * Update state and aggregate state values based on enable value. 1819c7e4702STaniya Das */ 1829c7e4702STaniya Das static int clk_rpmh_aggregate_state_send_command(struct clk_rpmh *c, 1839c7e4702STaniya Das bool enable) 1849c7e4702STaniya Das { 1859c7e4702STaniya Das int ret; 1869c7e4702STaniya Das 1879c7e4702STaniya Das /* Nothing required to be done if already off or on */ 1889c7e4702STaniya Das if (enable == c->state) 1899c7e4702STaniya Das return 0; 1909c7e4702STaniya Das 1919c7e4702STaniya Das c->state = enable ? c->valid_state_mask : 0; 1929c7e4702STaniya Das c->aggr_state = c->state | c->peer->state; 1939c7e4702STaniya Das c->peer->aggr_state = c->aggr_state; 1949c7e4702STaniya Das 1959c7e4702STaniya Das ret = clk_rpmh_send_aggregate_command(c); 1969c7e4702STaniya Das if (!ret) 1979c7e4702STaniya Das return 0; 1989c7e4702STaniya Das 1999c7e4702STaniya Das if (ret && enable) 2009c7e4702STaniya Das c->state = 0; 2019c7e4702STaniya Das else if (ret) 2029c7e4702STaniya Das c->state = c->valid_state_mask; 2039c7e4702STaniya Das 2049c7e4702STaniya Das WARN(1, "clk: %s failed to %s\n", c->res_name, 2059c7e4702STaniya Das enable ? "enable" : "disable"); 2069c7e4702STaniya Das return ret; 2079c7e4702STaniya Das } 2089c7e4702STaniya Das 2099c7e4702STaniya Das static int clk_rpmh_prepare(struct clk_hw *hw) 2109c7e4702STaniya Das { 2119c7e4702STaniya Das struct clk_rpmh *c = to_clk_rpmh(hw); 2129c7e4702STaniya Das int ret = 0; 2139c7e4702STaniya Das 2149c7e4702STaniya Das mutex_lock(&rpmh_clk_lock); 2159c7e4702STaniya Das ret = clk_rpmh_aggregate_state_send_command(c, true); 2169c7e4702STaniya Das mutex_unlock(&rpmh_clk_lock); 2179c7e4702STaniya Das 2189c7e4702STaniya Das return ret; 2199c7e4702STaniya Das }; 2209c7e4702STaniya Das 2219c7e4702STaniya Das static void clk_rpmh_unprepare(struct clk_hw *hw) 2229c7e4702STaniya Das { 2239c7e4702STaniya Das struct clk_rpmh *c = to_clk_rpmh(hw); 2249c7e4702STaniya Das 2259c7e4702STaniya Das mutex_lock(&rpmh_clk_lock); 2269c7e4702STaniya Das clk_rpmh_aggregate_state_send_command(c, false); 2279c7e4702STaniya Das mutex_unlock(&rpmh_clk_lock); 2289c7e4702STaniya Das }; 2299c7e4702STaniya Das 2309c7e4702STaniya Das static unsigned long clk_rpmh_recalc_rate(struct clk_hw *hw, 2319c7e4702STaniya Das unsigned long prate) 2329c7e4702STaniya Das { 2339c7e4702STaniya Das struct clk_rpmh *r = to_clk_rpmh(hw); 2349c7e4702STaniya Das 2359c7e4702STaniya Das /* 2369c7e4702STaniya Das * RPMh clocks have a fixed rate. Return static rate. 2379c7e4702STaniya Das */ 2389c7e4702STaniya Das return prate / r->div; 2399c7e4702STaniya Das } 2409c7e4702STaniya Das 2419c7e4702STaniya Das static const struct clk_ops clk_rpmh_ops = { 2429c7e4702STaniya Das .prepare = clk_rpmh_prepare, 2439c7e4702STaniya Das .unprepare = clk_rpmh_unprepare, 2449c7e4702STaniya Das .recalc_rate = clk_rpmh_recalc_rate, 2459c7e4702STaniya Das }; 2469c7e4702STaniya Das 24704053f4dSDavid Dai static int clk_rpmh_bcm_send_cmd(struct clk_rpmh *c, bool enable) 24804053f4dSDavid Dai { 24904053f4dSDavid Dai struct tcs_cmd cmd = { 0 }; 25004053f4dSDavid Dai u32 cmd_state; 25104053f4dSDavid Dai int ret; 25204053f4dSDavid Dai 25304053f4dSDavid Dai mutex_lock(&rpmh_clk_lock); 25404053f4dSDavid Dai 25504053f4dSDavid Dai cmd_state = 0; 25604053f4dSDavid Dai if (enable) { 25704053f4dSDavid Dai cmd_state = 1; 25804053f4dSDavid Dai if (c->aggr_state) 25904053f4dSDavid Dai cmd_state = c->aggr_state; 26004053f4dSDavid Dai } 26104053f4dSDavid Dai 26204053f4dSDavid Dai if (c->last_sent_aggr_state == cmd_state) { 26304053f4dSDavid Dai mutex_unlock(&rpmh_clk_lock); 26404053f4dSDavid Dai return 0; 26504053f4dSDavid Dai } 26604053f4dSDavid Dai 26704053f4dSDavid Dai cmd.addr = c->res_addr; 2686311b652SJordan Crouse cmd.data = BCM_TCS_CMD(1, enable, 0, cmd_state); 26904053f4dSDavid Dai 27004053f4dSDavid Dai ret = rpmh_write_async(c->dev, RPMH_ACTIVE_ONLY_STATE, &cmd, 1); 27104053f4dSDavid Dai if (ret) { 27204053f4dSDavid Dai dev_err(c->dev, "set active state of %s failed: (%d)\n", 27304053f4dSDavid Dai c->res_name, ret); 27404053f4dSDavid Dai mutex_unlock(&rpmh_clk_lock); 27504053f4dSDavid Dai return ret; 27604053f4dSDavid Dai } 27704053f4dSDavid Dai 27804053f4dSDavid Dai c->last_sent_aggr_state = cmd_state; 27904053f4dSDavid Dai 28004053f4dSDavid Dai mutex_unlock(&rpmh_clk_lock); 28104053f4dSDavid Dai 28204053f4dSDavid Dai return 0; 28304053f4dSDavid Dai } 28404053f4dSDavid Dai 28504053f4dSDavid Dai static int clk_rpmh_bcm_prepare(struct clk_hw *hw) 28604053f4dSDavid Dai { 28704053f4dSDavid Dai struct clk_rpmh *c = to_clk_rpmh(hw); 28804053f4dSDavid Dai 28904053f4dSDavid Dai return clk_rpmh_bcm_send_cmd(c, true); 29004053f4dSDavid Dai }; 29104053f4dSDavid Dai 29204053f4dSDavid Dai static void clk_rpmh_bcm_unprepare(struct clk_hw *hw) 29304053f4dSDavid Dai { 29404053f4dSDavid Dai struct clk_rpmh *c = to_clk_rpmh(hw); 29504053f4dSDavid Dai 29604053f4dSDavid Dai clk_rpmh_bcm_send_cmd(c, false); 29704053f4dSDavid Dai }; 29804053f4dSDavid Dai 29904053f4dSDavid Dai static int clk_rpmh_bcm_set_rate(struct clk_hw *hw, unsigned long rate, 30004053f4dSDavid Dai unsigned long parent_rate) 30104053f4dSDavid Dai { 30204053f4dSDavid Dai struct clk_rpmh *c = to_clk_rpmh(hw); 30304053f4dSDavid Dai 30404053f4dSDavid Dai c->aggr_state = rate / c->unit; 30504053f4dSDavid Dai /* 30604053f4dSDavid Dai * Since any non-zero value sent to hw would result in enabling the 30704053f4dSDavid Dai * clock, only send the value if the clock has already been prepared. 30804053f4dSDavid Dai */ 30904053f4dSDavid Dai if (clk_hw_is_prepared(hw)) 31004053f4dSDavid Dai clk_rpmh_bcm_send_cmd(c, true); 31104053f4dSDavid Dai 31204053f4dSDavid Dai return 0; 31304053f4dSDavid Dai }; 31404053f4dSDavid Dai 31504053f4dSDavid Dai static long clk_rpmh_round_rate(struct clk_hw *hw, unsigned long rate, 31604053f4dSDavid Dai unsigned long *parent_rate) 31704053f4dSDavid Dai { 31804053f4dSDavid Dai return rate; 31904053f4dSDavid Dai } 32004053f4dSDavid Dai 32104053f4dSDavid Dai static unsigned long clk_rpmh_bcm_recalc_rate(struct clk_hw *hw, 32204053f4dSDavid Dai unsigned long prate) 32304053f4dSDavid Dai { 32404053f4dSDavid Dai struct clk_rpmh *c = to_clk_rpmh(hw); 32504053f4dSDavid Dai 32604053f4dSDavid Dai return c->aggr_state * c->unit; 32704053f4dSDavid Dai } 32804053f4dSDavid Dai 32904053f4dSDavid Dai static const struct clk_ops clk_rpmh_bcm_ops = { 33004053f4dSDavid Dai .prepare = clk_rpmh_bcm_prepare, 33104053f4dSDavid Dai .unprepare = clk_rpmh_bcm_unprepare, 33204053f4dSDavid Dai .set_rate = clk_rpmh_bcm_set_rate, 33304053f4dSDavid Dai .round_rate = clk_rpmh_round_rate, 33404053f4dSDavid Dai .recalc_rate = clk_rpmh_bcm_recalc_rate, 33504053f4dSDavid Dai }; 33604053f4dSDavid Dai 337f5790382SStephen Boyd /* Resource name must match resource id present in cmd-db */ 3389c7e4702STaniya Das DEFINE_CLK_RPMH_ARC(sdm845, bi_tcxo, bi_tcxo_ao, "xo.lvl", 0x3, 2); 3399c7e4702STaniya Das DEFINE_CLK_RPMH_VRM(sdm845, ln_bb_clk2, ln_bb_clk2_ao, "lnbclka2", 2); 3409c7e4702STaniya Das DEFINE_CLK_RPMH_VRM(sdm845, ln_bb_clk3, ln_bb_clk3_ao, "lnbclka3", 2); 3419c7e4702STaniya Das DEFINE_CLK_RPMH_VRM(sdm845, rf_clk1, rf_clk1_ao, "rfclka1", 1); 3429c7e4702STaniya Das DEFINE_CLK_RPMH_VRM(sdm845, rf_clk2, rf_clk2_ao, "rfclka2", 1); 3439c7e4702STaniya Das DEFINE_CLK_RPMH_VRM(sdm845, rf_clk3, rf_clk3_ao, "rfclka3", 1); 344f5790382SStephen Boyd DEFINE_CLK_RPMH_VRM(sm8150, rf_clk3, rf_clk3_ao, "rfclka3", 1); 34504053f4dSDavid Dai DEFINE_CLK_RPMH_BCM(sdm845, ipa, "IP0"); 3469c7e4702STaniya Das 3479c7e4702STaniya Das static struct clk_hw *sdm845_rpmh_clocks[] = { 3489c7e4702STaniya Das [RPMH_CXO_CLK] = &sdm845_bi_tcxo.hw, 3499c7e4702STaniya Das [RPMH_CXO_CLK_A] = &sdm845_bi_tcxo_ao.hw, 3509c7e4702STaniya Das [RPMH_LN_BB_CLK2] = &sdm845_ln_bb_clk2.hw, 3519c7e4702STaniya Das [RPMH_LN_BB_CLK2_A] = &sdm845_ln_bb_clk2_ao.hw, 3529c7e4702STaniya Das [RPMH_LN_BB_CLK3] = &sdm845_ln_bb_clk3.hw, 3539c7e4702STaniya Das [RPMH_LN_BB_CLK3_A] = &sdm845_ln_bb_clk3_ao.hw, 3549c7e4702STaniya Das [RPMH_RF_CLK1] = &sdm845_rf_clk1.hw, 3559c7e4702STaniya Das [RPMH_RF_CLK1_A] = &sdm845_rf_clk1_ao.hw, 3569c7e4702STaniya Das [RPMH_RF_CLK2] = &sdm845_rf_clk2.hw, 3579c7e4702STaniya Das [RPMH_RF_CLK2_A] = &sdm845_rf_clk2_ao.hw, 3589c7e4702STaniya Das [RPMH_RF_CLK3] = &sdm845_rf_clk3.hw, 3599c7e4702STaniya Das [RPMH_RF_CLK3_A] = &sdm845_rf_clk3_ao.hw, 36004053f4dSDavid Dai [RPMH_IPA_CLK] = &sdm845_ipa.hw, 3619c7e4702STaniya Das }; 3629c7e4702STaniya Das 3639c7e4702STaniya Das static const struct clk_rpmh_desc clk_rpmh_sdm845 = { 3649c7e4702STaniya Das .clks = sdm845_rpmh_clocks, 3659c7e4702STaniya Das .num_clks = ARRAY_SIZE(sdm845_rpmh_clocks), 3669c7e4702STaniya Das }; 3679c7e4702STaniya Das 3682243fd41SVinod Koul static struct clk_hw *sm8150_rpmh_clocks[] = { 369f5790382SStephen Boyd [RPMH_CXO_CLK] = &sdm845_bi_tcxo.hw, 370f5790382SStephen Boyd [RPMH_CXO_CLK_A] = &sdm845_bi_tcxo_ao.hw, 371f5790382SStephen Boyd [RPMH_LN_BB_CLK2] = &sdm845_ln_bb_clk2.hw, 372f5790382SStephen Boyd [RPMH_LN_BB_CLK2_A] = &sdm845_ln_bb_clk2_ao.hw, 373f5790382SStephen Boyd [RPMH_LN_BB_CLK3] = &sdm845_ln_bb_clk3.hw, 374f5790382SStephen Boyd [RPMH_LN_BB_CLK3_A] = &sdm845_ln_bb_clk3_ao.hw, 375f5790382SStephen Boyd [RPMH_RF_CLK1] = &sdm845_rf_clk1.hw, 376f5790382SStephen Boyd [RPMH_RF_CLK1_A] = &sdm845_rf_clk1_ao.hw, 377f5790382SStephen Boyd [RPMH_RF_CLK2] = &sdm845_rf_clk2.hw, 378f5790382SStephen Boyd [RPMH_RF_CLK2_A] = &sdm845_rf_clk2_ao.hw, 379f5790382SStephen Boyd [RPMH_RF_CLK3] = &sdm845_rf_clk3.hw, 380f5790382SStephen Boyd [RPMH_RF_CLK3_A] = &sdm845_rf_clk3_ao.hw, 3812243fd41SVinod Koul }; 3822243fd41SVinod Koul 3832243fd41SVinod Koul static const struct clk_rpmh_desc clk_rpmh_sm8150 = { 3842243fd41SVinod Koul .clks = sm8150_rpmh_clocks, 3852243fd41SVinod Koul .num_clks = ARRAY_SIZE(sm8150_rpmh_clocks), 3862243fd41SVinod Koul }; 3872243fd41SVinod Koul 388eee28109STaniya Das static struct clk_hw *sc7180_rpmh_clocks[] = { 389eee28109STaniya Das [RPMH_CXO_CLK] = &sdm845_bi_tcxo.hw, 390eee28109STaniya Das [RPMH_CXO_CLK_A] = &sdm845_bi_tcxo_ao.hw, 391eee28109STaniya Das [RPMH_LN_BB_CLK2] = &sdm845_ln_bb_clk2.hw, 392eee28109STaniya Das [RPMH_LN_BB_CLK2_A] = &sdm845_ln_bb_clk2_ao.hw, 393eee28109STaniya Das [RPMH_LN_BB_CLK3] = &sdm845_ln_bb_clk3.hw, 394eee28109STaniya Das [RPMH_LN_BB_CLK3_A] = &sdm845_ln_bb_clk3_ao.hw, 395eee28109STaniya Das [RPMH_RF_CLK1] = &sdm845_rf_clk1.hw, 396eee28109STaniya Das [RPMH_RF_CLK1_A] = &sdm845_rf_clk1_ao.hw, 397eee28109STaniya Das [RPMH_RF_CLK2] = &sdm845_rf_clk2.hw, 398eee28109STaniya Das [RPMH_RF_CLK2_A] = &sdm845_rf_clk2_ao.hw, 399eee28109STaniya Das }; 400eee28109STaniya Das 401eee28109STaniya Das static const struct clk_rpmh_desc clk_rpmh_sc7180 = { 402eee28109STaniya Das .clks = sc7180_rpmh_clocks, 403eee28109STaniya Das .num_clks = ARRAY_SIZE(sc7180_rpmh_clocks), 404eee28109STaniya Das }; 405eee28109STaniya Das 4069c7e4702STaniya Das static struct clk_hw *of_clk_rpmh_hw_get(struct of_phandle_args *clkspec, 4079c7e4702STaniya Das void *data) 4089c7e4702STaniya Das { 4099c7e4702STaniya Das struct clk_rpmh_desc *rpmh = data; 4109c7e4702STaniya Das unsigned int idx = clkspec->args[0]; 4119c7e4702STaniya Das 4129c7e4702STaniya Das if (idx >= rpmh->num_clks) { 4139c7e4702STaniya Das pr_err("%s: invalid index %u\n", __func__, idx); 4149c7e4702STaniya Das return ERR_PTR(-EINVAL); 4159c7e4702STaniya Das } 4169c7e4702STaniya Das 4179c7e4702STaniya Das return rpmh->clks[idx]; 4189c7e4702STaniya Das } 4199c7e4702STaniya Das 4209c7e4702STaniya Das static int clk_rpmh_probe(struct platform_device *pdev) 4219c7e4702STaniya Das { 4229c7e4702STaniya Das struct clk_hw **hw_clks; 4239c7e4702STaniya Das struct clk_rpmh *rpmh_clk; 4249c7e4702STaniya Das const struct clk_rpmh_desc *desc; 4259c7e4702STaniya Das int ret, i; 4269c7e4702STaniya Das 4279c7e4702STaniya Das desc = of_device_get_match_data(&pdev->dev); 4289c7e4702STaniya Das if (!desc) 4299c7e4702STaniya Das return -ENODEV; 4309c7e4702STaniya Das 4319c7e4702STaniya Das hw_clks = desc->clks; 4329c7e4702STaniya Das 4339c7e4702STaniya Das for (i = 0; i < desc->num_clks; i++) { 434af884a5dSStephen Boyd const char *name = hw_clks[i]->init->name; 4359c7e4702STaniya Das u32 res_addr; 43604053f4dSDavid Dai size_t aux_data_len; 43704053f4dSDavid Dai const struct bcm_db *data; 4389c7e4702STaniya Das 4399c7e4702STaniya Das rpmh_clk = to_clk_rpmh(hw_clks[i]); 4409c7e4702STaniya Das res_addr = cmd_db_read_addr(rpmh_clk->res_name); 4419c7e4702STaniya Das if (!res_addr) { 4429c7e4702STaniya Das dev_err(&pdev->dev, "missing RPMh resource address for %s\n", 4439c7e4702STaniya Das rpmh_clk->res_name); 4449c7e4702STaniya Das return -ENODEV; 4459c7e4702STaniya Das } 44604053f4dSDavid Dai 44704053f4dSDavid Dai data = cmd_db_read_aux_data(rpmh_clk->res_name, &aux_data_len); 44804053f4dSDavid Dai if (IS_ERR(data)) { 44904053f4dSDavid Dai ret = PTR_ERR(data); 45004053f4dSDavid Dai dev_err(&pdev->dev, 45104053f4dSDavid Dai "error reading RPMh aux data for %s (%d)\n", 45204053f4dSDavid Dai rpmh_clk->res_name, ret); 45304053f4dSDavid Dai return ret; 45404053f4dSDavid Dai } 45504053f4dSDavid Dai 45604053f4dSDavid Dai /* Convert unit from Khz to Hz */ 45704053f4dSDavid Dai if (aux_data_len == sizeof(*data)) 45804053f4dSDavid Dai rpmh_clk->unit = le32_to_cpu(data->unit) * 1000ULL; 45904053f4dSDavid Dai 4609c7e4702STaniya Das rpmh_clk->res_addr += res_addr; 4619c7e4702STaniya Das rpmh_clk->dev = &pdev->dev; 4629c7e4702STaniya Das 4639c7e4702STaniya Das ret = devm_clk_hw_register(&pdev->dev, hw_clks[i]); 4649c7e4702STaniya Das if (ret) { 465af884a5dSStephen Boyd dev_err(&pdev->dev, "failed to register %s\n", name); 4669c7e4702STaniya Das return ret; 4679c7e4702STaniya Das } 4689c7e4702STaniya Das } 4699c7e4702STaniya Das 4709c7e4702STaniya Das /* typecast to silence compiler warning */ 4719c7e4702STaniya Das ret = devm_of_clk_add_hw_provider(&pdev->dev, of_clk_rpmh_hw_get, 4729c7e4702STaniya Das (void *)desc); 4739c7e4702STaniya Das if (ret) { 4749c7e4702STaniya Das dev_err(&pdev->dev, "Failed to add clock provider\n"); 4759c7e4702STaniya Das return ret; 4769c7e4702STaniya Das } 4779c7e4702STaniya Das 4789c7e4702STaniya Das dev_dbg(&pdev->dev, "Registered RPMh clocks\n"); 4799c7e4702STaniya Das 4809c7e4702STaniya Das return 0; 4819c7e4702STaniya Das } 4829c7e4702STaniya Das 4839c7e4702STaniya Das static const struct of_device_id clk_rpmh_match_table[] = { 4849c7e4702STaniya Das { .compatible = "qcom,sdm845-rpmh-clk", .data = &clk_rpmh_sdm845}, 4852243fd41SVinod Koul { .compatible = "qcom,sm8150-rpmh-clk", .data = &clk_rpmh_sm8150}, 486eee28109STaniya Das { .compatible = "qcom,sc7180-rpmh-clk", .data = &clk_rpmh_sc7180}, 4879c7e4702STaniya Das { } 4889c7e4702STaniya Das }; 4899c7e4702STaniya Das MODULE_DEVICE_TABLE(of, clk_rpmh_match_table); 4909c7e4702STaniya Das 4919c7e4702STaniya Das static struct platform_driver clk_rpmh_driver = { 4929c7e4702STaniya Das .probe = clk_rpmh_probe, 4939c7e4702STaniya Das .driver = { 4949c7e4702STaniya Das .name = "clk-rpmh", 4959c7e4702STaniya Das .of_match_table = clk_rpmh_match_table, 4969c7e4702STaniya Das }, 4979c7e4702STaniya Das }; 4989c7e4702STaniya Das 4999c7e4702STaniya Das static int __init clk_rpmh_init(void) 5009c7e4702STaniya Das { 5019c7e4702STaniya Das return platform_driver_register(&clk_rpmh_driver); 5029c7e4702STaniya Das } 5039c7e4702STaniya Das subsys_initcall(clk_rpmh_init); 5049c7e4702STaniya Das 5059c7e4702STaniya Das static void __exit clk_rpmh_exit(void) 5069c7e4702STaniya Das { 5079c7e4702STaniya Das platform_driver_unregister(&clk_rpmh_driver); 5089c7e4702STaniya Das } 5099c7e4702STaniya Das module_exit(clk_rpmh_exit); 5109c7e4702STaniya Das 5119c7e4702STaniya Das MODULE_DESCRIPTION("QCOM RPMh Clock Driver"); 5129c7e4702STaniya Das MODULE_LICENSE("GPL v2"); 513