1 // SPDX-License-Identifier: GPL-2.0
3 * Qualcomm Ramp Controller driver
4 * Copyright (c) 2022, AngeloGioacchino Del Regno
8 #include <linux/bitfield.h>
9 #include <linux/kernel.h>
10 #include <linux/module.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
14 #include <linux/regmap.h>
15 #include <linux/types.h>
17 #define RC_UPDATE_EN BIT(0)
18 #define RC_ROOT_EN BIT(1)
20 #define RC_REG_CFG_UPDATE 0x60
21 #define RC_CFG_UPDATE_EN BIT(8)
22 #define RC_CFG_ACK GENMASK(31, 16)
24 #define RC_DCVS_CFG_SID 2
29 #define RC_UPDATE_TIMEOUT_US 500
32 * struct qcom_ramp_controller_desc - SoC specific parameters
33 * @cfg_dfs_sid: Dynamic Frequency Scaling SID configuration
34 * @cfg_link_sid: Link SID configuration
35 * @cfg_lmh_sid: Limits Management hardware SID configuration
36 * @cfg_ramp_en: Ramp Controller enable sequence
37 * @cfg_ramp_dis: Ramp Controller disable sequence
38 * @cmd_reg: Command register offset
39 * @num_dfs_sids: Number of DFS SIDs (max 8)
40 * @num_link_sids: Number of Link SIDs (max 3)
41 * @num_lmh_sids: Number of LMh SIDs (max 8)
42 * @num_ramp_en: Number of entries in enable sequence
43 * @num_ramp_dis: Number of entries in disable sequence
45 struct qcom_ramp_controller_desc {
46 const struct reg_sequence *cfg_dfs_sid;
47 const struct reg_sequence *cfg_link_sid;
48 const struct reg_sequence *cfg_lmh_sid;
49 const struct reg_sequence *cfg_ramp_en;
50 const struct reg_sequence *cfg_ramp_dis;
60 * struct qcom_ramp_controller - Main driver structure
61 * @regmap: Regmap handle
62 * @desc: SoC specific parameters
64 struct qcom_ramp_controller {
65 struct regmap *regmap;
66 const struct qcom_ramp_controller_desc *desc;
70 * rc_wait_for_update() - Wait for Ramp Controller root update
71 * @qrc: Main driver structure
73 * Return: Zero for success or negative number for failure
75 static int rc_wait_for_update(struct qcom_ramp_controller *qrc)
77 const struct qcom_ramp_controller_desc *d = qrc->desc;
78 struct regmap *r = qrc->regmap;
82 ret = regmap_set_bits(r, d->cmd_reg, RC_ROOT_EN);
86 return regmap_read_poll_timeout(r, d->cmd_reg, val, !(val & RC_UPDATE_EN),
87 1, RC_UPDATE_TIMEOUT_US);
91 * rc_set_cfg_update() - Ramp Controller configuration update
92 * @qrc: Main driver structure
93 * @ce: Configuration entry to update
95 * Return: Zero for success or negative number for failure
97 static int rc_set_cfg_update(struct qcom_ramp_controller *qrc, u8 ce)
99 const struct qcom_ramp_controller_desc *d = qrc->desc;
100 struct regmap *r = qrc->regmap;
104 /* The ack bit is between bits 16-31 of RC_REG_CFG_UPDATE */
105 ack = FIELD_PREP(RC_CFG_ACK, BIT(ce));
107 /* Write the configuration type first... */
108 ret = regmap_set_bits(r, d->cmd_reg + RC_REG_CFG_UPDATE, ce);
112 /* ...and after that, enable the update bit to sync the changes */
113 ret = regmap_set_bits(r, d->cmd_reg + RC_REG_CFG_UPDATE, RC_CFG_UPDATE_EN);
117 /* Wait for the changes to go through */
118 ret = regmap_read_poll_timeout(r, d->cmd_reg + RC_REG_CFG_UPDATE, val,
119 val & ack, 1, RC_UPDATE_TIMEOUT_US);
124 * Configuration update success! The CFG_UPDATE register will not be
125 * cleared automatically upon applying the configuration, so we have
126 * to do that manually in order to leave the ramp controller in a
127 * predictable and clean state.
129 ret = regmap_write(r, d->cmd_reg + RC_REG_CFG_UPDATE, 0);
133 /* Wait for the update bit cleared ack */
134 return regmap_read_poll_timeout(r, d->cmd_reg + RC_REG_CFG_UPDATE,
135 val, !(val & RC_CFG_ACK), 1,
136 RC_UPDATE_TIMEOUT_US);
140 * rc_write_cfg - Send configuration sequence
141 * @qrc: Main driver structure
142 * @seq: Register sequence to send before asking for update
143 * @ce: Configuration SID
144 * @nsids: Total number of SIDs
146 * Returns: Zero for success or negative number for error
148 static int rc_write_cfg(struct qcom_ramp_controller *qrc,
149 const struct reg_sequence *seq,
155 /* Check if, and wait until the ramp controller is ready */
156 ret = rc_wait_for_update(qrc);
160 /* Write the sequence */
161 ret = regmap_multi_reg_write(qrc->regmap, seq, nsids);
165 /* Pull the trigger: do config update starting from the last sid */
166 for (i = 0; i < nsids; i++) {
167 ret = rc_set_cfg_update(qrc, (u8)ce - i);
176 * rc_ramp_ctrl_enable() - Enable Ramp up/down Control
177 * @qrc: Main driver structure
179 * Return: Zero for success or negative number for error
181 static int rc_ramp_ctrl_enable(struct qcom_ramp_controller *qrc)
183 const struct qcom_ramp_controller_desc *d = qrc->desc;
186 for (i = 0; i < d->num_ramp_en; i++) {
187 ret = rc_write_cfg(qrc, &d->cfg_ramp_en[i], RC_DCVS_CFG_SID, 1);
196 * qcom_ramp_controller_start() - Initialize and start the ramp controller
197 * @qrc: Main driver structure
199 * The Ramp Controller needs to be initialized by programming the relevant
200 * registers with SoC-specific configuration: once programming is done,
201 * the hardware will take care of the rest (no further handling required).
203 * Return: Zero for success or negative number for error
205 static int qcom_ramp_controller_start(struct qcom_ramp_controller *qrc)
207 const struct qcom_ramp_controller_desc *d = qrc->desc;
210 /* Program LMH, DFS, Link SIDs */
211 ret = rc_write_cfg(qrc, d->cfg_lmh_sid, RC_LMH_SID, d->num_lmh_sids);
215 ret = rc_write_cfg(qrc, d->cfg_dfs_sid, RC_DFS_SID, d->num_dfs_sids);
219 ret = rc_write_cfg(qrc, d->cfg_link_sid, RC_LINK_SID, d->num_link_sids);
223 /* Everything is ready! Enable the ramp up/down control */
224 return rc_ramp_ctrl_enable(qrc);
227 static const struct regmap_config qrc_regmap_config = {
231 .max_register = 0x68,
235 static const struct reg_sequence msm8976_cfg_dfs_sid[] = {
236 { 0x10, 0xfefebff7 },
237 { 0x14, 0xfdff7fef },
238 { 0x18, 0xfbffdefb },
239 { 0x1c, 0xb69b5555 },
240 { 0x20, 0x24929249 },
241 { 0x24, 0x49241112 },
242 { 0x28, 0x11112111 },
246 static const struct reg_sequence msm8976_cfg_link_sid[] = {
250 static const struct reg_sequence msm8976_cfg_lmh_sid[] = {
256 static const struct reg_sequence msm8976_cfg_ramp_en[] = {
257 { 0x50, 0x800 }, /* pre_en */
258 { 0x50, 0xc00 }, /* en */
259 { 0x50, 0x400 } /* post_en */
262 static const struct reg_sequence msm8976_cfg_ramp_dis[] = {
266 static const struct qcom_ramp_controller_desc msm8976_rc_cfg = {
267 .cfg_dfs_sid = msm8976_cfg_dfs_sid,
268 .num_dfs_sids = ARRAY_SIZE(msm8976_cfg_dfs_sid),
270 .cfg_link_sid = msm8976_cfg_link_sid,
271 .num_link_sids = ARRAY_SIZE(msm8976_cfg_link_sid),
273 .cfg_lmh_sid = msm8976_cfg_lmh_sid,
274 .num_lmh_sids = ARRAY_SIZE(msm8976_cfg_lmh_sid),
276 .cfg_ramp_en = msm8976_cfg_ramp_en,
277 .num_ramp_en = ARRAY_SIZE(msm8976_cfg_ramp_en),
279 .cfg_ramp_dis = msm8976_cfg_ramp_dis,
280 .num_ramp_dis = ARRAY_SIZE(msm8976_cfg_ramp_dis),
285 static int qcom_ramp_controller_probe(struct platform_device *pdev)
287 struct qcom_ramp_controller *qrc;
290 base = devm_platform_ioremap_resource(pdev, 0);
292 return PTR_ERR(base);
294 qrc = devm_kmalloc(&pdev->dev, sizeof(*qrc), GFP_KERNEL);
298 qrc->desc = device_get_match_data(&pdev->dev);
302 qrc->regmap = devm_regmap_init_mmio(&pdev->dev, base, &qrc_regmap_config);
303 if (IS_ERR(qrc->regmap))
304 return PTR_ERR(qrc->regmap);
306 platform_set_drvdata(pdev, qrc);
308 return qcom_ramp_controller_start(qrc);
311 static void qcom_ramp_controller_remove(struct platform_device *pdev)
313 struct qcom_ramp_controller *qrc = platform_get_drvdata(pdev);
316 ret = rc_write_cfg(qrc, qrc->desc->cfg_ramp_dis,
317 RC_DCVS_CFG_SID, qrc->desc->num_ramp_dis);
319 dev_err(&pdev->dev, "Failed to send disable sequence\n");
322 static const struct of_device_id qcom_ramp_controller_match_table[] = {
323 { .compatible = "qcom,msm8976-ramp-controller", .data = &msm8976_rc_cfg },
326 MODULE_DEVICE_TABLE(of, qcom_ramp_controller_match_table);
328 static struct platform_driver qcom_ramp_controller_driver = {
330 .name = "qcom-ramp-controller",
331 .of_match_table = qcom_ramp_controller_match_table,
332 .suppress_bind_attrs = true,
334 .probe = qcom_ramp_controller_probe,
335 .remove = qcom_ramp_controller_remove,
338 static int __init qcom_ramp_controller_init(void)
340 return platform_driver_register(&qcom_ramp_controller_driver);
342 arch_initcall(qcom_ramp_controller_init);
345 MODULE_DESCRIPTION("Qualcomm Ramp Controller driver");
346 MODULE_LICENSE("GPL");