clk-imx8ulp-sim-lpav.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2025 NXP
  4. */
  5. #include <dt-bindings/clock/imx8ulp-clock.h>
  6. #include <linux/auxiliary_bus.h>
  7. #include <linux/clk-provider.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/regmap.h>
  13. #include <linux/slab.h>
  14. #define SYSCTRL0 0x8
  15. #define IMX8ULP_HIFI_CLK_GATE(gname, cname, pname, bidx) \
  16. { \
  17. .name = gname "_cg", \
  18. .id = IMX8ULP_CLK_SIM_LPAV_HIFI_##cname, \
  19. .parent = { .fw_name = pname }, \
  20. .bit = bidx, \
  21. }
  22. struct clk_imx8ulp_sim_lpav_data {
  23. spinlock_t lock; /* shared by MUX, clock gate and reset */
  24. unsigned long flags; /* for spinlock usage */
  25. struct clk_hw_onecell_data clk_data; /* keep last */
  26. };
  27. struct clk_imx8ulp_sim_lpav_gate {
  28. const char *name;
  29. int id;
  30. const struct clk_parent_data parent;
  31. u8 bit;
  32. };
  33. static struct clk_imx8ulp_sim_lpav_gate gates[] = {
  34. IMX8ULP_HIFI_CLK_GATE("hifi_core", CORE, "core", 17),
  35. IMX8ULP_HIFI_CLK_GATE("hifi_pbclk", PBCLK, "bus", 18),
  36. IMX8ULP_HIFI_CLK_GATE("hifi_plat", PLAT, "plat", 19)
  37. };
  38. static void clk_imx8ulp_sim_lpav_lock(void *arg) __acquires(&data->lock)
  39. {
  40. struct clk_imx8ulp_sim_lpav_data *data = dev_get_drvdata(arg);
  41. spin_lock_irqsave(&data->lock, data->flags);
  42. }
  43. static void clk_imx8ulp_sim_lpav_unlock(void *arg) __releases(&data->lock)
  44. {
  45. struct clk_imx8ulp_sim_lpav_data *data = dev_get_drvdata(arg);
  46. spin_unlock_irqrestore(&data->lock, data->flags);
  47. }
  48. static int clk_imx8ulp_sim_lpav_probe(struct platform_device *pdev)
  49. {
  50. const struct regmap_config regmap_config = {
  51. .reg_bits = 32,
  52. .val_bits = 32,
  53. .reg_stride = 4,
  54. .lock = clk_imx8ulp_sim_lpav_lock,
  55. .unlock = clk_imx8ulp_sim_lpav_unlock,
  56. .lock_arg = &pdev->dev,
  57. };
  58. struct clk_imx8ulp_sim_lpav_data *data;
  59. struct auxiliary_device *adev;
  60. struct regmap *regmap;
  61. void __iomem *base;
  62. struct clk_hw *hw;
  63. int i, ret;
  64. data = devm_kzalloc(&pdev->dev,
  65. struct_size(data, clk_data.hws, ARRAY_SIZE(gates)),
  66. GFP_KERNEL);
  67. if (!data)
  68. return -ENOMEM;
  69. dev_set_drvdata(&pdev->dev, data);
  70. /*
  71. * this lock is used directly by the clock gate and indirectly
  72. * by the reset and mux controller via the regmap API
  73. */
  74. spin_lock_init(&data->lock);
  75. base = devm_platform_ioremap_resource(pdev, 0);
  76. if (IS_ERR(base))
  77. return dev_err_probe(&pdev->dev, PTR_ERR(base),
  78. "failed to ioremap base\n");
  79. /*
  80. * although the clock gate doesn't use the regmap API to modify the
  81. * registers, we still need the regmap because of the reset auxiliary
  82. * driver and the MUX drivers, which use the parent device's regmap
  83. */
  84. regmap = devm_regmap_init_mmio(&pdev->dev, base, &regmap_config);
  85. if (IS_ERR(regmap))
  86. return dev_err_probe(&pdev->dev, PTR_ERR(regmap),
  87. "failed to initialize regmap\n");
  88. data->clk_data.num = ARRAY_SIZE(gates);
  89. for (i = 0; i < ARRAY_SIZE(gates); i++) {
  90. hw = devm_clk_hw_register_gate_parent_data(&pdev->dev,
  91. gates[i].name,
  92. &gates[i].parent,
  93. CLK_SET_RATE_PARENT,
  94. base + SYSCTRL0,
  95. gates[i].bit,
  96. 0x0, &data->lock);
  97. if (IS_ERR(hw))
  98. return dev_err_probe(&pdev->dev, PTR_ERR(hw),
  99. "failed to register %s gate\n",
  100. gates[i].name);
  101. data->clk_data.hws[i] = hw;
  102. }
  103. adev = devm_auxiliary_device_create(&pdev->dev, "reset", NULL);
  104. if (!adev)
  105. return dev_err_probe(&pdev->dev, -ENODEV,
  106. "failed to register aux reset\n");
  107. ret = devm_of_clk_add_hw_provider(&pdev->dev,
  108. of_clk_hw_onecell_get,
  109. &data->clk_data);
  110. if (ret)
  111. return dev_err_probe(&pdev->dev, ret,
  112. "failed to register clk hw provider\n");
  113. /* used to probe MUX child device */
  114. return devm_of_platform_populate(&pdev->dev);
  115. }
  116. static const struct of_device_id clk_imx8ulp_sim_lpav_of_match[] = {
  117. { .compatible = "fsl,imx8ulp-sim-lpav" },
  118. { }
  119. };
  120. MODULE_DEVICE_TABLE(of, clk_imx8ulp_sim_lpav_of_match);
  121. static struct platform_driver clk_imx8ulp_sim_lpav_driver = {
  122. .probe = clk_imx8ulp_sim_lpav_probe,
  123. .driver = {
  124. .name = "clk-imx8ulp-sim-lpav",
  125. .of_match_table = clk_imx8ulp_sim_lpav_of_match,
  126. },
  127. };
  128. module_platform_driver(clk_imx8ulp_sim_lpav_driver);
  129. MODULE_LICENSE("GPL");
  130. MODULE_DESCRIPTION("i.MX8ULP LPAV System Integration Module (SIM) clock driver");
  131. MODULE_AUTHOR("Laurentiu Mihalcea <laurentiu.mihalcea@nxp.com>");