1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2024 Linaro Ltd. 4 */ 5 6 #include <linux/clk.h> 7 #include <linux/delay.h> 8 #include <linux/device.h> 9 #include <linux/gpio/consumer.h> 10 #include <linux/jiffies.h> 11 #include <linux/mod_devicetable.h> 12 #include <linux/module.h> 13 #include <linux/of.h> 14 #include <linux/platform_device.h> 15 #include <linux/regulator/consumer.h> 16 #include <linux/pwrseq/provider.h> 17 #include <linux/string.h> 18 #include <linux/types.h> 19 20 struct pwrseq_qcom_wcn_pdata { 21 const char *const *vregs; 22 size_t num_vregs; 23 unsigned int pwup_delay_ms; 24 unsigned int gpio_enable_delay_ms; 25 }; 26 27 struct pwrseq_qcom_wcn_ctx { 28 struct pwrseq_device *pwrseq; 29 struct device_node *of_node; 30 const struct pwrseq_qcom_wcn_pdata *pdata; 31 struct regulator_bulk_data *regs; 32 struct gpio_desc *bt_gpio; 33 struct gpio_desc *wlan_gpio; 34 struct clk *clk; 35 unsigned long last_gpio_enable_jf; 36 }; 37 38 static void pwrseq_qcom_wcn_ensure_gpio_delay(struct pwrseq_qcom_wcn_ctx *ctx) 39 { 40 unsigned long diff_jiffies; 41 unsigned int diff_msecs; 42 43 if (!ctx->pdata->gpio_enable_delay_ms) 44 return; 45 46 diff_jiffies = jiffies - ctx->last_gpio_enable_jf; 47 diff_msecs = jiffies_to_msecs(diff_jiffies); 48 49 if (diff_msecs < ctx->pdata->gpio_enable_delay_ms) 50 msleep(ctx->pdata->gpio_enable_delay_ms - diff_msecs); 51 } 52 53 static int pwrseq_qcom_wcn_vregs_enable(struct pwrseq_device *pwrseq) 54 { 55 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq); 56 57 return regulator_bulk_enable(ctx->pdata->num_vregs, ctx->regs); 58 } 59 60 static int pwrseq_qcom_wcn_vregs_disable(struct pwrseq_device *pwrseq) 61 { 62 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq); 63 64 return regulator_bulk_disable(ctx->pdata->num_vregs, ctx->regs); 65 } 66 67 static const struct pwrseq_unit_data pwrseq_qcom_wcn_vregs_unit_data = { 68 .name = "regulators-enable", 69 .enable = pwrseq_qcom_wcn_vregs_enable, 70 .disable = pwrseq_qcom_wcn_vregs_disable, 71 }; 72 73 static int pwrseq_qcom_wcn_clk_enable(struct pwrseq_device *pwrseq) 74 { 75 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq); 76 77 return clk_prepare_enable(ctx->clk); 78 } 79 80 static int pwrseq_qcom_wcn_clk_disable(struct pwrseq_device *pwrseq) 81 { 82 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq); 83 84 clk_disable_unprepare(ctx->clk); 85 86 return 0; 87 } 88 89 static const struct pwrseq_unit_data pwrseq_qcom_wcn_clk_unit_data = { 90 .name = "clock-enable", 91 .enable = pwrseq_qcom_wcn_clk_enable, 92 .disable = pwrseq_qcom_wcn_clk_disable, 93 }; 94 95 static const struct pwrseq_unit_data *pwrseq_qcom_wcn_unit_deps[] = { 96 &pwrseq_qcom_wcn_vregs_unit_data, 97 &pwrseq_qcom_wcn_clk_unit_data, 98 NULL 99 }; 100 101 static int pwrseq_qcom_wcn_bt_enable(struct pwrseq_device *pwrseq) 102 { 103 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq); 104 105 pwrseq_qcom_wcn_ensure_gpio_delay(ctx); 106 gpiod_set_value_cansleep(ctx->bt_gpio, 1); 107 ctx->last_gpio_enable_jf = jiffies; 108 109 return 0; 110 } 111 112 static int pwrseq_qcom_wcn_bt_disable(struct pwrseq_device *pwrseq) 113 { 114 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq); 115 116 gpiod_set_value_cansleep(ctx->bt_gpio, 0); 117 118 return 0; 119 } 120 121 static const struct pwrseq_unit_data pwrseq_qcom_wcn_bt_unit_data = { 122 .name = "bluetooth-enable", 123 .deps = pwrseq_qcom_wcn_unit_deps, 124 .enable = pwrseq_qcom_wcn_bt_enable, 125 .disable = pwrseq_qcom_wcn_bt_disable, 126 }; 127 128 static int pwrseq_qcom_wcn_wlan_enable(struct pwrseq_device *pwrseq) 129 { 130 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq); 131 132 pwrseq_qcom_wcn_ensure_gpio_delay(ctx); 133 gpiod_set_value_cansleep(ctx->wlan_gpio, 1); 134 ctx->last_gpio_enable_jf = jiffies; 135 136 return 0; 137 } 138 139 static int pwrseq_qcom_wcn_wlan_disable(struct pwrseq_device *pwrseq) 140 { 141 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq); 142 143 gpiod_set_value_cansleep(ctx->wlan_gpio, 0); 144 145 return 0; 146 } 147 148 static const struct pwrseq_unit_data pwrseq_qcom_wcn_wlan_unit_data = { 149 .name = "wlan-enable", 150 .deps = pwrseq_qcom_wcn_unit_deps, 151 .enable = pwrseq_qcom_wcn_wlan_enable, 152 .disable = pwrseq_qcom_wcn_wlan_disable, 153 }; 154 155 static int pwrseq_qcom_wcn_pwup_delay(struct pwrseq_device *pwrseq) 156 { 157 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq); 158 159 if (ctx->pdata->pwup_delay_ms) 160 msleep(ctx->pdata->pwup_delay_ms); 161 162 return 0; 163 } 164 165 static const struct pwrseq_target_data pwrseq_qcom_wcn_bt_target_data = { 166 .name = "bluetooth", 167 .unit = &pwrseq_qcom_wcn_bt_unit_data, 168 .post_enable = pwrseq_qcom_wcn_pwup_delay, 169 }; 170 171 static const struct pwrseq_target_data pwrseq_qcom_wcn_wlan_target_data = { 172 .name = "wlan", 173 .unit = &pwrseq_qcom_wcn_wlan_unit_data, 174 .post_enable = pwrseq_qcom_wcn_pwup_delay, 175 }; 176 177 static const struct pwrseq_target_data *pwrseq_qcom_wcn_targets[] = { 178 &pwrseq_qcom_wcn_bt_target_data, 179 &pwrseq_qcom_wcn_wlan_target_data, 180 NULL 181 }; 182 183 static const char *const pwrseq_qca6390_vregs[] = { 184 "vddio", 185 "vddaon", 186 "vddpmu", 187 "vddrfa0p95", 188 "vddrfa1p3", 189 "vddrfa1p9", 190 "vddpcie1p3", 191 "vddpcie1p9", 192 }; 193 194 static const struct pwrseq_qcom_wcn_pdata pwrseq_qca6390_of_data = { 195 .vregs = pwrseq_qca6390_vregs, 196 .num_vregs = ARRAY_SIZE(pwrseq_qca6390_vregs), 197 .pwup_delay_ms = 60, 198 .gpio_enable_delay_ms = 100, 199 }; 200 201 static const char *const pwrseq_wcn7850_vregs[] = { 202 "vdd", 203 "vddio", 204 "vddio1p2", 205 "vddaon", 206 "vdddig", 207 "vddrfa1p2", 208 "vddrfa1p8", 209 }; 210 211 static const struct pwrseq_qcom_wcn_pdata pwrseq_wcn7850_of_data = { 212 .vregs = pwrseq_wcn7850_vregs, 213 .num_vregs = ARRAY_SIZE(pwrseq_wcn7850_vregs), 214 .pwup_delay_ms = 50, 215 }; 216 217 static int pwrseq_qcom_wcn_match(struct pwrseq_device *pwrseq, 218 struct device *dev) 219 { 220 struct pwrseq_qcom_wcn_ctx *ctx = pwrseq_device_get_drvdata(pwrseq); 221 struct device_node *dev_node = dev->of_node; 222 223 /* 224 * The PMU supplies power to the Bluetooth and WLAN modules. both 225 * consume the PMU AON output so check the presence of the 226 * 'vddaon-supply' property and whether it leads us to the right 227 * device. 228 */ 229 if (!of_property_present(dev_node, "vddaon-supply")) 230 return 0; 231 232 struct device_node *reg_node __free(device_node) = 233 of_parse_phandle(dev_node, "vddaon-supply", 0); 234 if (!reg_node) 235 return 0; 236 237 /* 238 * `reg_node` is the PMU AON regulator, its parent is the `regulators` 239 * node and finally its grandparent is the PMU device node that we're 240 * looking for. 241 */ 242 if (!reg_node->parent || !reg_node->parent->parent || 243 reg_node->parent->parent != ctx->of_node) 244 return 0; 245 246 return 1; 247 } 248 249 static int pwrseq_qcom_wcn_probe(struct platform_device *pdev) 250 { 251 struct device *dev = &pdev->dev; 252 struct pwrseq_qcom_wcn_ctx *ctx; 253 struct pwrseq_config config; 254 int i, ret; 255 256 ctx = devm_kzalloc(dev, sizeof(*ctx), GFP_KERNEL); 257 if (!ctx) 258 return -ENOMEM; 259 260 ctx->of_node = dev->of_node; 261 262 ctx->pdata = of_device_get_match_data(dev); 263 if (!ctx->pdata) 264 return dev_err_probe(dev, -ENODEV, 265 "Failed to obtain platform data\n"); 266 267 ctx->regs = devm_kcalloc(dev, ctx->pdata->num_vregs, 268 sizeof(*ctx->regs), GFP_KERNEL); 269 if (!ctx->regs) 270 return -ENOMEM; 271 272 for (i = 0; i < ctx->pdata->num_vregs; i++) 273 ctx->regs[i].supply = ctx->pdata->vregs[i]; 274 275 ret = devm_regulator_bulk_get(dev, ctx->pdata->num_vregs, ctx->regs); 276 if (ret < 0) 277 return dev_err_probe(dev, ret, 278 "Failed to get all regulators\n"); 279 280 ctx->bt_gpio = devm_gpiod_get_optional(dev, "bt-enable", GPIOD_OUT_LOW); 281 if (IS_ERR(ctx->bt_gpio)) 282 return dev_err_probe(dev, PTR_ERR(ctx->bt_gpio), 283 "Failed to get the Bluetooth enable GPIO\n"); 284 285 ctx->wlan_gpio = devm_gpiod_get_optional(dev, "wlan-enable", 286 GPIOD_OUT_LOW); 287 if (IS_ERR(ctx->wlan_gpio)) 288 return dev_err_probe(dev, PTR_ERR(ctx->wlan_gpio), 289 "Failed to get the WLAN enable GPIO\n"); 290 291 ctx->clk = devm_clk_get_optional(dev, NULL); 292 if (IS_ERR(ctx->clk)) 293 return dev_err_probe(dev, PTR_ERR(ctx->clk), 294 "Failed to get the reference clock\n"); 295 296 memset(&config, 0, sizeof(config)); 297 298 config.parent = dev; 299 config.owner = THIS_MODULE; 300 config.drvdata = ctx; 301 config.match = pwrseq_qcom_wcn_match; 302 config.targets = pwrseq_qcom_wcn_targets; 303 304 ctx->pwrseq = devm_pwrseq_device_register(dev, &config); 305 if (IS_ERR(ctx->pwrseq)) 306 return dev_err_probe(dev, PTR_ERR(ctx->pwrseq), 307 "Failed to register the power sequencer\n"); 308 309 return 0; 310 } 311 312 static const struct of_device_id pwrseq_qcom_wcn_of_match[] = { 313 { 314 .compatible = "qcom,qca6390-pmu", 315 .data = &pwrseq_qca6390_of_data, 316 }, 317 { 318 .compatible = "qcom,wcn7850-pmu", 319 .data = &pwrseq_wcn7850_of_data, 320 }, 321 { } 322 }; 323 MODULE_DEVICE_TABLE(of, pwrseq_qcom_wcn_of_match); 324 325 static struct platform_driver pwrseq_qcom_wcn_driver = { 326 .driver = { 327 .name = "pwrseq-qcom_wcn", 328 .of_match_table = pwrseq_qcom_wcn_of_match, 329 }, 330 .probe = pwrseq_qcom_wcn_probe, 331 }; 332 module_platform_driver(pwrseq_qcom_wcn_driver); 333 334 MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>"); 335 MODULE_DESCRIPTION("Qualcomm WCN PMU power sequencing driver"); 336 MODULE_LICENSE("GPL"); 337