xref: /openbmc/linux/drivers/clk/qcom/clk-rpmh.c (revision 04053f4d)
19c7e4702STaniya Das // SPDX-License-Identifier: GPL-2.0
29c7e4702STaniya Das /*
39c7e4702STaniya Das  * Copyright (c) 2018, 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>
159c7e4702STaniya Das 
169c7e4702STaniya Das #include <dt-bindings/clock/qcom,rpmh.h>
179c7e4702STaniya Das 
189c7e4702STaniya Das #define CLK_RPMH_ARC_EN_OFFSET		0
199c7e4702STaniya Das #define CLK_RPMH_VRM_EN_OFFSET		4
209c7e4702STaniya Das 
2104053f4dSDavid Dai #define BCM_TCS_CMD_COMMIT_MASK		0x40000000
2204053f4dSDavid Dai #define BCM_TCS_CMD_VALID_SHIFT		29
2304053f4dSDavid Dai #define BCM_TCS_CMD_VOTE_MASK		0x3fff
2404053f4dSDavid Dai #define BCM_TCS_CMD_VOTE_SHIFT		0
2504053f4dSDavid Dai 
2604053f4dSDavid Dai #define BCM_TCS_CMD(valid, vote)				\
2704053f4dSDavid Dai 	(BCM_TCS_CMD_COMMIT_MASK |				\
2804053f4dSDavid Dai 	((valid) << BCM_TCS_CMD_VALID_SHIFT) |			\
2904053f4dSDavid Dai 	((vote & BCM_TCS_CMD_VOTE_MASK)				\
3004053f4dSDavid Dai 	<< BCM_TCS_CMD_VOTE_SHIFT))
3104053f4dSDavid Dai 
3204053f4dSDavid Dai /**
3304053f4dSDavid Dai  * struct bcm_db - Auxiliary data pertaining to each Bus Clock Manager(BCM)
3404053f4dSDavid Dai  * @unit: divisor used to convert Hz value to an RPMh msg
3504053f4dSDavid Dai  * @width: multiplier used to convert Hz value to an RPMh msg
3604053f4dSDavid Dai  * @vcd: virtual clock domain that this bcm belongs to
3704053f4dSDavid Dai  * @reserved: reserved to pad the struct
3804053f4dSDavid Dai  */
3904053f4dSDavid Dai struct bcm_db {
4004053f4dSDavid Dai 	__le32 unit;
4104053f4dSDavid Dai 	__le16 width;
4204053f4dSDavid Dai 	u8 vcd;
4304053f4dSDavid Dai 	u8 reserved;
4404053f4dSDavid Dai };
4504053f4dSDavid Dai 
469c7e4702STaniya Das /**
479c7e4702STaniya Das  * struct clk_rpmh - individual rpmh clock data structure
489c7e4702STaniya Das  * @hw:			handle between common and hardware-specific interfaces
499c7e4702STaniya Das  * @res_name:		resource name for the rpmh clock
509c7e4702STaniya Das  * @div:		clock divider to compute the clock rate
519c7e4702STaniya Das  * @res_addr:		base address of the rpmh resource within the RPMh
529c7e4702STaniya Das  * @res_on_val:		rpmh clock enable value
539c7e4702STaniya Das  * @state:		rpmh clock requested state
549c7e4702STaniya Das  * @aggr_state:		rpmh clock aggregated state
559c7e4702STaniya Das  * @last_sent_aggr_state: rpmh clock last aggr state sent to RPMh
569c7e4702STaniya Das  * @valid_state_mask:	mask to determine the state of the rpmh clock
5704053f4dSDavid Dai  * @unit:		divisor to convert rate to rpmh msg in magnitudes of Khz
589c7e4702STaniya Das  * @dev:		device to which it is attached
599c7e4702STaniya Das  * @peer:		pointer to the clock rpmh sibling
609c7e4702STaniya Das  */
619c7e4702STaniya Das struct clk_rpmh {
629c7e4702STaniya Das 	struct clk_hw hw;
639c7e4702STaniya Das 	const char *res_name;
649c7e4702STaniya Das 	u8 div;
659c7e4702STaniya Das 	u32 res_addr;
669c7e4702STaniya Das 	u32 res_on_val;
679c7e4702STaniya Das 	u32 state;
689c7e4702STaniya Das 	u32 aggr_state;
699c7e4702STaniya Das 	u32 last_sent_aggr_state;
709c7e4702STaniya Das 	u32 valid_state_mask;
7104053f4dSDavid Dai 	u32 unit;
729c7e4702STaniya Das 	struct device *dev;
739c7e4702STaniya Das 	struct clk_rpmh *peer;
749c7e4702STaniya Das };
759c7e4702STaniya Das 
769c7e4702STaniya Das struct clk_rpmh_desc {
779c7e4702STaniya Das 	struct clk_hw **clks;
789c7e4702STaniya Das 	size_t num_clks;
799c7e4702STaniya Das };
809c7e4702STaniya Das 
819c7e4702STaniya Das static DEFINE_MUTEX(rpmh_clk_lock);
829c7e4702STaniya Das 
839c7e4702STaniya Das #define __DEFINE_CLK_RPMH(_platform, _name, _name_active, _res_name,	\
849c7e4702STaniya Das 			  _res_en_offset, _res_on, _div)		\
859c7e4702STaniya Das 	static struct clk_rpmh _platform##_##_name_active;		\
869c7e4702STaniya Das 	static struct clk_rpmh _platform##_##_name = {			\
879c7e4702STaniya Das 		.res_name = _res_name,					\
889c7e4702STaniya Das 		.res_addr = _res_en_offset,				\
899c7e4702STaniya Das 		.res_on_val = _res_on,					\
909c7e4702STaniya Das 		.div = _div,						\
919c7e4702STaniya Das 		.peer = &_platform##_##_name_active,			\
929c7e4702STaniya Das 		.valid_state_mask = (BIT(RPMH_WAKE_ONLY_STATE) |	\
939c7e4702STaniya Das 				      BIT(RPMH_ACTIVE_ONLY_STATE) |	\
949c7e4702STaniya Das 				      BIT(RPMH_SLEEP_STATE)),		\
959c7e4702STaniya Das 		.hw.init = &(struct clk_init_data){			\
969c7e4702STaniya Das 			.ops = &clk_rpmh_ops,				\
979c7e4702STaniya Das 			.name = #_name,					\
989c7e4702STaniya Das 			.parent_names = (const char *[]){ "xo_board" },	\
999c7e4702STaniya Das 			.num_parents = 1,				\
1009c7e4702STaniya Das 		},							\
1019c7e4702STaniya Das 	};								\
1029c7e4702STaniya Das 	static struct clk_rpmh _platform##_##_name_active = {		\
1039c7e4702STaniya Das 		.res_name = _res_name,					\
1049c7e4702STaniya Das 		.res_addr = _res_en_offset,				\
1059c7e4702STaniya Das 		.res_on_val = _res_on,					\
1069c7e4702STaniya Das 		.div = _div,						\
1079c7e4702STaniya Das 		.peer = &_platform##_##_name,				\
1089c7e4702STaniya Das 		.valid_state_mask = (BIT(RPMH_WAKE_ONLY_STATE) |	\
1099c7e4702STaniya Das 					BIT(RPMH_ACTIVE_ONLY_STATE)),	\
1109c7e4702STaniya Das 		.hw.init = &(struct clk_init_data){			\
1119c7e4702STaniya Das 			.ops = &clk_rpmh_ops,				\
1129c7e4702STaniya Das 			.name = #_name_active,				\
1139c7e4702STaniya Das 			.parent_names = (const char *[]){ "xo_board" },	\
1149c7e4702STaniya Das 			.num_parents = 1,				\
1159c7e4702STaniya Das 		},							\
1169c7e4702STaniya Das 	}
1179c7e4702STaniya Das 
1189c7e4702STaniya Das #define DEFINE_CLK_RPMH_ARC(_platform, _name, _name_active, _res_name,	\
1199c7e4702STaniya Das 			    _res_on, _div)				\
1209c7e4702STaniya Das 	__DEFINE_CLK_RPMH(_platform, _name, _name_active, _res_name,	\
1219c7e4702STaniya Das 			  CLK_RPMH_ARC_EN_OFFSET, _res_on, _div)
1229c7e4702STaniya Das 
1239c7e4702STaniya Das #define DEFINE_CLK_RPMH_VRM(_platform, _name, _name_active, _res_name,	\
1249c7e4702STaniya Das 				_div)					\
1259c7e4702STaniya Das 	__DEFINE_CLK_RPMH(_platform, _name, _name_active, _res_name,	\
1269c7e4702STaniya Das 			  CLK_RPMH_VRM_EN_OFFSET, 1, _div)
1279c7e4702STaniya Das 
12804053f4dSDavid Dai #define DEFINE_CLK_RPMH_BCM(_platform, _name, _res_name)		\
12904053f4dSDavid Dai 	static struct clk_rpmh _platform##_##_name = {			\
13004053f4dSDavid Dai 		.res_name = _res_name,					\
13104053f4dSDavid Dai 		.valid_state_mask = BIT(RPMH_ACTIVE_ONLY_STATE),	\
13204053f4dSDavid Dai 		.div = 1,						\
13304053f4dSDavid Dai 		.hw.init = &(struct clk_init_data){			\
13404053f4dSDavid Dai 			.ops = &clk_rpmh_bcm_ops,			\
13504053f4dSDavid Dai 			.name = #_name,					\
13604053f4dSDavid Dai 		},							\
13704053f4dSDavid Dai 	}
13804053f4dSDavid Dai 
1399c7e4702STaniya Das static inline struct clk_rpmh *to_clk_rpmh(struct clk_hw *_hw)
1409c7e4702STaniya Das {
1419c7e4702STaniya Das 	return container_of(_hw, struct clk_rpmh, hw);
1429c7e4702STaniya Das }
1439c7e4702STaniya Das 
1449c7e4702STaniya Das static inline bool has_state_changed(struct clk_rpmh *c, u32 state)
1459c7e4702STaniya Das {
1469c7e4702STaniya Das 	return (c->last_sent_aggr_state & BIT(state))
1479c7e4702STaniya Das 		!= (c->aggr_state & BIT(state));
1489c7e4702STaniya Das }
1499c7e4702STaniya Das 
1509c7e4702STaniya Das static int clk_rpmh_send_aggregate_command(struct clk_rpmh *c)
1519c7e4702STaniya Das {
1529c7e4702STaniya Das 	struct tcs_cmd cmd = { 0 };
1539c7e4702STaniya Das 	u32 cmd_state, on_val;
1549c7e4702STaniya Das 	enum rpmh_state state = RPMH_SLEEP_STATE;
1559c7e4702STaniya Das 	int ret;
1569c7e4702STaniya Das 
1579c7e4702STaniya Das 	cmd.addr = c->res_addr;
1589c7e4702STaniya Das 	cmd_state = c->aggr_state;
1599c7e4702STaniya Das 	on_val = c->res_on_val;
1609c7e4702STaniya Das 
1619c7e4702STaniya Das 	for (; state <= RPMH_ACTIVE_ONLY_STATE; state++) {
1629c7e4702STaniya Das 		if (has_state_changed(c, state)) {
1639c7e4702STaniya Das 			if (cmd_state & BIT(state))
1649c7e4702STaniya Das 				cmd.data = on_val;
1659c7e4702STaniya Das 
1669c7e4702STaniya Das 			ret = rpmh_write_async(c->dev, state, &cmd, 1);
1679c7e4702STaniya Das 			if (ret) {
1689c7e4702STaniya Das 				dev_err(c->dev, "set %s state of %s failed: (%d)\n",
1699c7e4702STaniya Das 					!state ? "sleep" :
1709c7e4702STaniya Das 					state == RPMH_WAKE_ONLY_STATE	?
1719c7e4702STaniya Das 					"wake" : "active", c->res_name, ret);
1729c7e4702STaniya Das 				return ret;
1739c7e4702STaniya Das 			}
1749c7e4702STaniya Das 		}
1759c7e4702STaniya Das 	}
1769c7e4702STaniya Das 
1779c7e4702STaniya Das 	c->last_sent_aggr_state = c->aggr_state;
1789c7e4702STaniya Das 	c->peer->last_sent_aggr_state =  c->last_sent_aggr_state;
1799c7e4702STaniya Das 
1809c7e4702STaniya Das 	return 0;
1819c7e4702STaniya Das }
1829c7e4702STaniya Das 
1839c7e4702STaniya Das /*
1849c7e4702STaniya Das  * Update state and aggregate state values based on enable value.
1859c7e4702STaniya Das  */
1869c7e4702STaniya Das static int clk_rpmh_aggregate_state_send_command(struct clk_rpmh *c,
1879c7e4702STaniya Das 						bool enable)
1889c7e4702STaniya Das {
1899c7e4702STaniya Das 	int ret;
1909c7e4702STaniya Das 
1919c7e4702STaniya Das 	/* Nothing required to be done if already off or on */
1929c7e4702STaniya Das 	if (enable == c->state)
1939c7e4702STaniya Das 		return 0;
1949c7e4702STaniya Das 
1959c7e4702STaniya Das 	c->state = enable ? c->valid_state_mask : 0;
1969c7e4702STaniya Das 	c->aggr_state = c->state | c->peer->state;
1979c7e4702STaniya Das 	c->peer->aggr_state = c->aggr_state;
1989c7e4702STaniya Das 
1999c7e4702STaniya Das 	ret = clk_rpmh_send_aggregate_command(c);
2009c7e4702STaniya Das 	if (!ret)
2019c7e4702STaniya Das 		return 0;
2029c7e4702STaniya Das 
2039c7e4702STaniya Das 	if (ret && enable)
2049c7e4702STaniya Das 		c->state = 0;
2059c7e4702STaniya Das 	else if (ret)
2069c7e4702STaniya Das 		c->state = c->valid_state_mask;
2079c7e4702STaniya Das 
2089c7e4702STaniya Das 	WARN(1, "clk: %s failed to %s\n", c->res_name,
2099c7e4702STaniya Das 	     enable ? "enable" : "disable");
2109c7e4702STaniya Das 	return ret;
2119c7e4702STaniya Das }
2129c7e4702STaniya Das 
2139c7e4702STaniya Das static int clk_rpmh_prepare(struct clk_hw *hw)
2149c7e4702STaniya Das {
2159c7e4702STaniya Das 	struct clk_rpmh *c = to_clk_rpmh(hw);
2169c7e4702STaniya Das 	int ret = 0;
2179c7e4702STaniya Das 
2189c7e4702STaniya Das 	mutex_lock(&rpmh_clk_lock);
2199c7e4702STaniya Das 	ret = clk_rpmh_aggregate_state_send_command(c, true);
2209c7e4702STaniya Das 	mutex_unlock(&rpmh_clk_lock);
2219c7e4702STaniya Das 
2229c7e4702STaniya Das 	return ret;
2239c7e4702STaniya Das };
2249c7e4702STaniya Das 
2259c7e4702STaniya Das static void clk_rpmh_unprepare(struct clk_hw *hw)
2269c7e4702STaniya Das {
2279c7e4702STaniya Das 	struct clk_rpmh *c = to_clk_rpmh(hw);
2289c7e4702STaniya Das 
2299c7e4702STaniya Das 	mutex_lock(&rpmh_clk_lock);
2309c7e4702STaniya Das 	clk_rpmh_aggregate_state_send_command(c, false);
2319c7e4702STaniya Das 	mutex_unlock(&rpmh_clk_lock);
2329c7e4702STaniya Das };
2339c7e4702STaniya Das 
2349c7e4702STaniya Das static unsigned long clk_rpmh_recalc_rate(struct clk_hw *hw,
2359c7e4702STaniya Das 					unsigned long prate)
2369c7e4702STaniya Das {
2379c7e4702STaniya Das 	struct clk_rpmh *r = to_clk_rpmh(hw);
2389c7e4702STaniya Das 
2399c7e4702STaniya Das 	/*
2409c7e4702STaniya Das 	 * RPMh clocks have a fixed rate. Return static rate.
2419c7e4702STaniya Das 	 */
2429c7e4702STaniya Das 	return prate / r->div;
2439c7e4702STaniya Das }
2449c7e4702STaniya Das 
2459c7e4702STaniya Das static const struct clk_ops clk_rpmh_ops = {
2469c7e4702STaniya Das 	.prepare	= clk_rpmh_prepare,
2479c7e4702STaniya Das 	.unprepare	= clk_rpmh_unprepare,
2489c7e4702STaniya Das 	.recalc_rate	= clk_rpmh_recalc_rate,
2499c7e4702STaniya Das };
2509c7e4702STaniya Das 
25104053f4dSDavid Dai static int clk_rpmh_bcm_send_cmd(struct clk_rpmh *c, bool enable)
25204053f4dSDavid Dai {
25304053f4dSDavid Dai 	struct tcs_cmd cmd = { 0 };
25404053f4dSDavid Dai 	u32 cmd_state;
25504053f4dSDavid Dai 	int ret;
25604053f4dSDavid Dai 
25704053f4dSDavid Dai 	mutex_lock(&rpmh_clk_lock);
25804053f4dSDavid Dai 
25904053f4dSDavid Dai 	cmd_state = 0;
26004053f4dSDavid Dai 	if (enable) {
26104053f4dSDavid Dai 		cmd_state = 1;
26204053f4dSDavid Dai 		if (c->aggr_state)
26304053f4dSDavid Dai 			cmd_state = c->aggr_state;
26404053f4dSDavid Dai 	}
26504053f4dSDavid Dai 
26604053f4dSDavid Dai 	if (c->last_sent_aggr_state == cmd_state) {
26704053f4dSDavid Dai 		mutex_unlock(&rpmh_clk_lock);
26804053f4dSDavid Dai 		return 0;
26904053f4dSDavid Dai 	}
27004053f4dSDavid Dai 
27104053f4dSDavid Dai 	cmd.addr = c->res_addr;
27204053f4dSDavid Dai 	cmd.data = BCM_TCS_CMD(enable, cmd_state);
27304053f4dSDavid Dai 
27404053f4dSDavid Dai 	ret = rpmh_write_async(c->dev, RPMH_ACTIVE_ONLY_STATE, &cmd, 1);
27504053f4dSDavid Dai 	if (ret) {
27604053f4dSDavid Dai 		dev_err(c->dev, "set active state of %s failed: (%d)\n",
27704053f4dSDavid Dai 			c->res_name, ret);
27804053f4dSDavid Dai 		mutex_unlock(&rpmh_clk_lock);
27904053f4dSDavid Dai 		return ret;
28004053f4dSDavid Dai 	}
28104053f4dSDavid Dai 
28204053f4dSDavid Dai 	c->last_sent_aggr_state = cmd_state;
28304053f4dSDavid Dai 
28404053f4dSDavid Dai 	mutex_unlock(&rpmh_clk_lock);
28504053f4dSDavid Dai 
28604053f4dSDavid Dai 	return 0;
28704053f4dSDavid Dai }
28804053f4dSDavid Dai 
28904053f4dSDavid Dai static int clk_rpmh_bcm_prepare(struct clk_hw *hw)
29004053f4dSDavid Dai {
29104053f4dSDavid Dai 	struct clk_rpmh *c = to_clk_rpmh(hw);
29204053f4dSDavid Dai 
29304053f4dSDavid Dai 	return clk_rpmh_bcm_send_cmd(c, true);
29404053f4dSDavid Dai };
29504053f4dSDavid Dai 
29604053f4dSDavid Dai static void clk_rpmh_bcm_unprepare(struct clk_hw *hw)
29704053f4dSDavid Dai {
29804053f4dSDavid Dai 	struct clk_rpmh *c = to_clk_rpmh(hw);
29904053f4dSDavid Dai 
30004053f4dSDavid Dai 	clk_rpmh_bcm_send_cmd(c, false);
30104053f4dSDavid Dai };
30204053f4dSDavid Dai 
30304053f4dSDavid Dai static int clk_rpmh_bcm_set_rate(struct clk_hw *hw, unsigned long rate,
30404053f4dSDavid Dai 				 unsigned long parent_rate)
30504053f4dSDavid Dai {
30604053f4dSDavid Dai 	struct clk_rpmh *c = to_clk_rpmh(hw);
30704053f4dSDavid Dai 
30804053f4dSDavid Dai 	c->aggr_state = rate / c->unit;
30904053f4dSDavid Dai 	/*
31004053f4dSDavid Dai 	 * Since any non-zero value sent to hw would result in enabling the
31104053f4dSDavid Dai 	 * clock, only send the value if the clock has already been prepared.
31204053f4dSDavid Dai 	 */
31304053f4dSDavid Dai 	if (clk_hw_is_prepared(hw))
31404053f4dSDavid Dai 		clk_rpmh_bcm_send_cmd(c, true);
31504053f4dSDavid Dai 
31604053f4dSDavid Dai 	return 0;
31704053f4dSDavid Dai };
31804053f4dSDavid Dai 
31904053f4dSDavid Dai static long clk_rpmh_round_rate(struct clk_hw *hw, unsigned long rate,
32004053f4dSDavid Dai 				unsigned long *parent_rate)
32104053f4dSDavid Dai {
32204053f4dSDavid Dai 	return rate;
32304053f4dSDavid Dai }
32404053f4dSDavid Dai 
32504053f4dSDavid Dai static unsigned long clk_rpmh_bcm_recalc_rate(struct clk_hw *hw,
32604053f4dSDavid Dai 					unsigned long prate)
32704053f4dSDavid Dai {
32804053f4dSDavid Dai 	struct clk_rpmh *c = to_clk_rpmh(hw);
32904053f4dSDavid Dai 
33004053f4dSDavid Dai 	return c->aggr_state * c->unit;
33104053f4dSDavid Dai }
33204053f4dSDavid Dai 
33304053f4dSDavid Dai static const struct clk_ops clk_rpmh_bcm_ops = {
33404053f4dSDavid Dai 	.prepare	= clk_rpmh_bcm_prepare,
33504053f4dSDavid Dai 	.unprepare	= clk_rpmh_bcm_unprepare,
33604053f4dSDavid Dai 	.set_rate	= clk_rpmh_bcm_set_rate,
33704053f4dSDavid Dai 	.round_rate	= clk_rpmh_round_rate,
33804053f4dSDavid Dai 	.recalc_rate	= clk_rpmh_bcm_recalc_rate,
33904053f4dSDavid Dai };
34004053f4dSDavid Dai 
3419c7e4702STaniya Das /* Resource name must match resource id present in cmd-db. */
3429c7e4702STaniya Das DEFINE_CLK_RPMH_ARC(sdm845, bi_tcxo, bi_tcxo_ao, "xo.lvl", 0x3, 2);
3439c7e4702STaniya Das DEFINE_CLK_RPMH_VRM(sdm845, ln_bb_clk2, ln_bb_clk2_ao, "lnbclka2", 2);
3449c7e4702STaniya Das DEFINE_CLK_RPMH_VRM(sdm845, ln_bb_clk3, ln_bb_clk3_ao, "lnbclka3", 2);
3459c7e4702STaniya Das DEFINE_CLK_RPMH_VRM(sdm845, rf_clk1, rf_clk1_ao, "rfclka1", 1);
3469c7e4702STaniya Das DEFINE_CLK_RPMH_VRM(sdm845, rf_clk2, rf_clk2_ao, "rfclka2", 1);
3479c7e4702STaniya Das DEFINE_CLK_RPMH_VRM(sdm845, rf_clk3, rf_clk3_ao, "rfclka3", 1);
34804053f4dSDavid Dai DEFINE_CLK_RPMH_BCM(sdm845, ipa, "IP0");
3499c7e4702STaniya Das 
3509c7e4702STaniya Das static struct clk_hw *sdm845_rpmh_clocks[] = {
3519c7e4702STaniya Das 	[RPMH_CXO_CLK]		= &sdm845_bi_tcxo.hw,
3529c7e4702STaniya Das 	[RPMH_CXO_CLK_A]	= &sdm845_bi_tcxo_ao.hw,
3539c7e4702STaniya Das 	[RPMH_LN_BB_CLK2]	= &sdm845_ln_bb_clk2.hw,
3549c7e4702STaniya Das 	[RPMH_LN_BB_CLK2_A]	= &sdm845_ln_bb_clk2_ao.hw,
3559c7e4702STaniya Das 	[RPMH_LN_BB_CLK3]	= &sdm845_ln_bb_clk3.hw,
3569c7e4702STaniya Das 	[RPMH_LN_BB_CLK3_A]	= &sdm845_ln_bb_clk3_ao.hw,
3579c7e4702STaniya Das 	[RPMH_RF_CLK1]		= &sdm845_rf_clk1.hw,
3589c7e4702STaniya Das 	[RPMH_RF_CLK1_A]	= &sdm845_rf_clk1_ao.hw,
3599c7e4702STaniya Das 	[RPMH_RF_CLK2]		= &sdm845_rf_clk2.hw,
3609c7e4702STaniya Das 	[RPMH_RF_CLK2_A]	= &sdm845_rf_clk2_ao.hw,
3619c7e4702STaniya Das 	[RPMH_RF_CLK3]		= &sdm845_rf_clk3.hw,
3629c7e4702STaniya Das 	[RPMH_RF_CLK3_A]	= &sdm845_rf_clk3_ao.hw,
36304053f4dSDavid Dai 	[RPMH_IPA_CLK]		= &sdm845_ipa.hw,
3649c7e4702STaniya Das };
3659c7e4702STaniya Das 
3669c7e4702STaniya Das static const struct clk_rpmh_desc clk_rpmh_sdm845 = {
3679c7e4702STaniya Das 	.clks = sdm845_rpmh_clocks,
3689c7e4702STaniya Das 	.num_clks = ARRAY_SIZE(sdm845_rpmh_clocks),
3699c7e4702STaniya Das };
3709c7e4702STaniya Das 
3719c7e4702STaniya Das static struct clk_hw *of_clk_rpmh_hw_get(struct of_phandle_args *clkspec,
3729c7e4702STaniya Das 					 void *data)
3739c7e4702STaniya Das {
3749c7e4702STaniya Das 	struct clk_rpmh_desc *rpmh = data;
3759c7e4702STaniya Das 	unsigned int idx = clkspec->args[0];
3769c7e4702STaniya Das 
3779c7e4702STaniya Das 	if (idx >= rpmh->num_clks) {
3789c7e4702STaniya Das 		pr_err("%s: invalid index %u\n", __func__, idx);
3799c7e4702STaniya Das 		return ERR_PTR(-EINVAL);
3809c7e4702STaniya Das 	}
3819c7e4702STaniya Das 
3829c7e4702STaniya Das 	return rpmh->clks[idx];
3839c7e4702STaniya Das }
3849c7e4702STaniya Das 
3859c7e4702STaniya Das static int clk_rpmh_probe(struct platform_device *pdev)
3869c7e4702STaniya Das {
3879c7e4702STaniya Das 	struct clk_hw **hw_clks;
3889c7e4702STaniya Das 	struct clk_rpmh *rpmh_clk;
3899c7e4702STaniya Das 	const struct clk_rpmh_desc *desc;
3909c7e4702STaniya Das 	int ret, i;
3919c7e4702STaniya Das 
3929c7e4702STaniya Das 	desc = of_device_get_match_data(&pdev->dev);
3939c7e4702STaniya Das 	if (!desc)
3949c7e4702STaniya Das 		return -ENODEV;
3959c7e4702STaniya Das 
3969c7e4702STaniya Das 	hw_clks = desc->clks;
3979c7e4702STaniya Das 
3989c7e4702STaniya Das 	for (i = 0; i < desc->num_clks; i++) {
3999c7e4702STaniya Das 		u32 res_addr;
40004053f4dSDavid Dai 		size_t aux_data_len;
40104053f4dSDavid Dai 		const struct bcm_db *data;
4029c7e4702STaniya Das 
4039c7e4702STaniya Das 		rpmh_clk = to_clk_rpmh(hw_clks[i]);
4049c7e4702STaniya Das 		res_addr = cmd_db_read_addr(rpmh_clk->res_name);
4059c7e4702STaniya Das 		if (!res_addr) {
4069c7e4702STaniya Das 			dev_err(&pdev->dev, "missing RPMh resource address for %s\n",
4079c7e4702STaniya Das 				rpmh_clk->res_name);
4089c7e4702STaniya Das 			return -ENODEV;
4099c7e4702STaniya Das 		}
41004053f4dSDavid Dai 
41104053f4dSDavid Dai 		data = cmd_db_read_aux_data(rpmh_clk->res_name, &aux_data_len);
41204053f4dSDavid Dai 		if (IS_ERR(data)) {
41304053f4dSDavid Dai 			ret = PTR_ERR(data);
41404053f4dSDavid Dai 			dev_err(&pdev->dev,
41504053f4dSDavid Dai 				"error reading RPMh aux data for %s (%d)\n",
41604053f4dSDavid Dai 				rpmh_clk->res_name, ret);
41704053f4dSDavid Dai 			return ret;
41804053f4dSDavid Dai 		}
41904053f4dSDavid Dai 
42004053f4dSDavid Dai 		/* Convert unit from Khz to Hz */
42104053f4dSDavid Dai 		if (aux_data_len == sizeof(*data))
42204053f4dSDavid Dai 			rpmh_clk->unit = le32_to_cpu(data->unit) * 1000ULL;
42304053f4dSDavid Dai 
4249c7e4702STaniya Das 		rpmh_clk->res_addr += res_addr;
4259c7e4702STaniya Das 		rpmh_clk->dev = &pdev->dev;
4269c7e4702STaniya Das 
4279c7e4702STaniya Das 		ret = devm_clk_hw_register(&pdev->dev, hw_clks[i]);
4289c7e4702STaniya Das 		if (ret) {
4299c7e4702STaniya Das 			dev_err(&pdev->dev, "failed to register %s\n",
4309c7e4702STaniya Das 				hw_clks[i]->init->name);
4319c7e4702STaniya Das 			return ret;
4329c7e4702STaniya Das 		}
4339c7e4702STaniya Das 	}
4349c7e4702STaniya Das 
4359c7e4702STaniya Das 	/* typecast to silence compiler warning */
4369c7e4702STaniya Das 	ret = devm_of_clk_add_hw_provider(&pdev->dev, of_clk_rpmh_hw_get,
4379c7e4702STaniya Das 					  (void *)desc);
4389c7e4702STaniya Das 	if (ret) {
4399c7e4702STaniya Das 		dev_err(&pdev->dev, "Failed to add clock provider\n");
4409c7e4702STaniya Das 		return ret;
4419c7e4702STaniya Das 	}
4429c7e4702STaniya Das 
4439c7e4702STaniya Das 	dev_dbg(&pdev->dev, "Registered RPMh clocks\n");
4449c7e4702STaniya Das 
4459c7e4702STaniya Das 	return 0;
4469c7e4702STaniya Das }
4479c7e4702STaniya Das 
4489c7e4702STaniya Das static const struct of_device_id clk_rpmh_match_table[] = {
4499c7e4702STaniya Das 	{ .compatible = "qcom,sdm845-rpmh-clk", .data = &clk_rpmh_sdm845},
4509c7e4702STaniya Das 	{ }
4519c7e4702STaniya Das };
4529c7e4702STaniya Das MODULE_DEVICE_TABLE(of, clk_rpmh_match_table);
4539c7e4702STaniya Das 
4549c7e4702STaniya Das static struct platform_driver clk_rpmh_driver = {
4559c7e4702STaniya Das 	.probe		= clk_rpmh_probe,
4569c7e4702STaniya Das 	.driver		= {
4579c7e4702STaniya Das 		.name	= "clk-rpmh",
4589c7e4702STaniya Das 		.of_match_table = clk_rpmh_match_table,
4599c7e4702STaniya Das 	},
4609c7e4702STaniya Das };
4619c7e4702STaniya Das 
4629c7e4702STaniya Das static int __init clk_rpmh_init(void)
4639c7e4702STaniya Das {
4649c7e4702STaniya Das 	return platform_driver_register(&clk_rpmh_driver);
4659c7e4702STaniya Das }
4669c7e4702STaniya Das subsys_initcall(clk_rpmh_init);
4679c7e4702STaniya Das 
4689c7e4702STaniya Das static void __exit clk_rpmh_exit(void)
4699c7e4702STaniya Das {
4709c7e4702STaniya Das 	platform_driver_unregister(&clk_rpmh_driver);
4719c7e4702STaniya Das }
4729c7e4702STaniya Das module_exit(clk_rpmh_exit);
4739c7e4702STaniya Das 
4749c7e4702STaniya Das MODULE_DESCRIPTION("QCOM RPMh Clock Driver");
4759c7e4702STaniya Das MODULE_LICENSE("GPL v2");
476