clk-fixed-mmio.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Memory Mapped IO Fixed clock driver
  4. *
  5. * Copyright (C) 2018 Cadence Design Systems, Inc.
  6. *
  7. * Authors:
  8. * Jan Kotas <[email protected]>
  9. */
  10. #include <linux/clk-provider.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <linux/of_address.h>
  14. #include <linux/platform_device.h>
  15. static struct clk_hw *fixed_mmio_clk_setup(struct device_node *node)
  16. {
  17. struct clk_hw *clk;
  18. const char *clk_name = node->name;
  19. void __iomem *base;
  20. u32 freq;
  21. int ret;
  22. base = of_iomap(node, 0);
  23. if (!base) {
  24. pr_err("%pOFn: failed to map address\n", node);
  25. return ERR_PTR(-EIO);
  26. }
  27. freq = readl(base);
  28. iounmap(base);
  29. of_property_read_string(node, "clock-output-names", &clk_name);
  30. clk = clk_hw_register_fixed_rate(NULL, clk_name, NULL, 0, freq);
  31. if (IS_ERR(clk)) {
  32. pr_err("%pOFn: failed to register fixed rate clock\n", node);
  33. return clk;
  34. }
  35. ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, clk);
  36. if (ret) {
  37. pr_err("%pOFn: failed to add clock provider\n", node);
  38. clk_hw_unregister(clk);
  39. clk = ERR_PTR(ret);
  40. }
  41. return clk;
  42. }
  43. static void __init of_fixed_mmio_clk_setup(struct device_node *node)
  44. {
  45. fixed_mmio_clk_setup(node);
  46. }
  47. CLK_OF_DECLARE(fixed_mmio_clk, "fixed-mmio-clock", of_fixed_mmio_clk_setup);
  48. /*
  49. * This is not executed when of_fixed_mmio_clk_setup succeeded.
  50. */
  51. static int of_fixed_mmio_clk_probe(struct platform_device *pdev)
  52. {
  53. struct clk_hw *clk;
  54. clk = fixed_mmio_clk_setup(pdev->dev.of_node);
  55. if (IS_ERR(clk))
  56. return PTR_ERR(clk);
  57. platform_set_drvdata(pdev, clk);
  58. return 0;
  59. }
  60. static int of_fixed_mmio_clk_remove(struct platform_device *pdev)
  61. {
  62. struct clk_hw *clk = platform_get_drvdata(pdev);
  63. of_clk_del_provider(pdev->dev.of_node);
  64. clk_hw_unregister_fixed_rate(clk);
  65. return 0;
  66. }
  67. static const struct of_device_id of_fixed_mmio_clk_ids[] = {
  68. { .compatible = "fixed-mmio-clock" },
  69. { }
  70. };
  71. MODULE_DEVICE_TABLE(of, of_fixed_mmio_clk_ids);
  72. static struct platform_driver of_fixed_mmio_clk_driver = {
  73. .driver = {
  74. .name = "of_fixed_mmio_clk",
  75. .of_match_table = of_fixed_mmio_clk_ids,
  76. },
  77. .probe = of_fixed_mmio_clk_probe,
  78. .remove = of_fixed_mmio_clk_remove,
  79. };
  80. module_platform_driver(of_fixed_mmio_clk_driver);
  81. MODULE_AUTHOR("Jan Kotas <[email protected]>");
  82. MODULE_DESCRIPTION("Memory Mapped IO Fixed clock driver");
  83. MODULE_LICENSE("GPL v2");