pwrseq-qcom-wcn.c 9.1 KB

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