apcs-sdx55.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Qualcomm SDX55 APCS clock controller driver
  4. *
  5. * Copyright (c) 2020, Linaro Limited
  6. * Author: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/cpu.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_domain.h>
  15. #include <linux/regmap.h>
  16. #include <linux/slab.h>
  17. #include "clk-regmap.h"
  18. #include "clk-regmap-mux-div.h"
  19. static const u32 apcs_mux_clk_parent_map[] = { 0, 1, 5 };
  20. static const struct clk_parent_data pdata[] = {
  21. { .fw_name = "ref" },
  22. { .fw_name = "aux" },
  23. { .fw_name = "pll" },
  24. };
  25. /*
  26. * We use the notifier function for switching to a temporary safe configuration
  27. * (mux and divider), while the A7 PLL is reconfigured.
  28. */
  29. static int a7cc_notifier_cb(struct notifier_block *nb, unsigned long event,
  30. void *data)
  31. {
  32. int ret = 0;
  33. struct clk_regmap_mux_div *md = container_of(nb,
  34. struct clk_regmap_mux_div,
  35. clk_nb);
  36. if (event == PRE_RATE_CHANGE)
  37. /* set the mux and divider to safe frequency (400mhz) */
  38. ret = mux_div_set_src_div(md, 1, 2);
  39. return notifier_from_errno(ret);
  40. }
  41. static int qcom_apcs_sdx55_clk_probe(struct platform_device *pdev)
  42. {
  43. struct device *dev = &pdev->dev;
  44. struct device *parent = dev->parent;
  45. struct device *cpu_dev;
  46. struct clk_regmap_mux_div *a7cc;
  47. struct regmap *regmap;
  48. struct clk_init_data init = { };
  49. int ret;
  50. regmap = dev_get_regmap(parent, NULL);
  51. if (!regmap) {
  52. dev_err(dev, "Failed to get parent regmap\n");
  53. return -ENODEV;
  54. }
  55. a7cc = devm_kzalloc(dev, sizeof(*a7cc), GFP_KERNEL);
  56. if (!a7cc)
  57. return -ENOMEM;
  58. init.name = "a7mux";
  59. init.parent_data = pdata;
  60. init.num_parents = ARRAY_SIZE(pdata);
  61. init.ops = &clk_regmap_mux_div_ops;
  62. a7cc->clkr.hw.init = &init;
  63. a7cc->clkr.regmap = regmap;
  64. a7cc->reg_offset = 0x8;
  65. a7cc->hid_width = 5;
  66. a7cc->hid_shift = 0;
  67. a7cc->src_width = 3;
  68. a7cc->src_shift = 8;
  69. a7cc->parent_map = apcs_mux_clk_parent_map;
  70. a7cc->pclk = devm_clk_get(parent, "pll");
  71. if (IS_ERR(a7cc->pclk))
  72. return dev_err_probe(dev, PTR_ERR(a7cc->pclk),
  73. "Failed to get PLL clk\n");
  74. a7cc->clk_nb.notifier_call = a7cc_notifier_cb;
  75. ret = clk_notifier_register(a7cc->pclk, &a7cc->clk_nb);
  76. if (ret)
  77. return dev_err_probe(dev, ret,
  78. "Failed to register clock notifier\n");
  79. ret = devm_clk_register_regmap(dev, &a7cc->clkr);
  80. if (ret) {
  81. dev_err_probe(dev, ret, "Failed to register regmap clock\n");
  82. goto err;
  83. }
  84. ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_simple_get,
  85. &a7cc->clkr.hw);
  86. if (ret) {
  87. dev_err_probe(dev, ret, "Failed to add clock provider\n");
  88. goto err;
  89. }
  90. platform_set_drvdata(pdev, a7cc);
  91. /*
  92. * Attach the power domain to cpudev. Since there is no dedicated driver
  93. * for CPUs and the SDX55 platform lacks hardware specific CPUFreq
  94. * driver, there seems to be no better place to do this. So do it here!
  95. */
  96. cpu_dev = get_cpu_device(0);
  97. dev_pm_domain_attach(cpu_dev, true);
  98. return 0;
  99. err:
  100. clk_notifier_unregister(a7cc->pclk, &a7cc->clk_nb);
  101. return ret;
  102. }
  103. static void qcom_apcs_sdx55_clk_remove(struct platform_device *pdev)
  104. {
  105. struct device *cpu_dev = get_cpu_device(0);
  106. struct clk_regmap_mux_div *a7cc = platform_get_drvdata(pdev);
  107. clk_notifier_unregister(a7cc->pclk, &a7cc->clk_nb);
  108. dev_pm_domain_detach(cpu_dev, true);
  109. }
  110. static struct platform_driver qcom_apcs_sdx55_clk_driver = {
  111. .probe = qcom_apcs_sdx55_clk_probe,
  112. .remove = qcom_apcs_sdx55_clk_remove,
  113. .driver = {
  114. .name = "qcom-sdx55-acps-clk",
  115. },
  116. };
  117. module_platform_driver(qcom_apcs_sdx55_clk_driver);
  118. MODULE_AUTHOR("Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>");
  119. MODULE_LICENSE("GPL v2");
  120. MODULE_DESCRIPTION("Qualcomm SDX55 APCS clock driver");