clk-sun8i-apb0.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2014 Chen-Yu Tsai
  4. * Author: Chen-Yu Tsai <[email protected]>
  5. *
  6. * Allwinner A23 APB0 clock driver
  7. *
  8. * Based on clk-sun6i-apb0.c
  9. * Allwinner A31 APB0 clock driver
  10. *
  11. * Copyright (C) 2014 Free Electrons
  12. * Author: Boris BREZILLON <[email protected]>
  13. */
  14. #include <linux/clk-provider.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/platform_device.h>
  20. static struct clk *sun8i_a23_apb0_register(struct device_node *node,
  21. void __iomem *reg)
  22. {
  23. const char *clk_name = node->name;
  24. const char *clk_parent;
  25. struct clk *clk;
  26. int ret;
  27. clk_parent = of_clk_get_parent_name(node, 0);
  28. if (!clk_parent)
  29. return ERR_PTR(-EINVAL);
  30. of_property_read_string(node, "clock-output-names", &clk_name);
  31. /* The A23 APB0 clock is a standard 2 bit wide divider clock */
  32. clk = clk_register_divider(NULL, clk_name, clk_parent, 0, reg,
  33. 0, 2, 0, NULL);
  34. if (IS_ERR(clk))
  35. return clk;
  36. ret = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  37. if (ret)
  38. goto err_unregister;
  39. return clk;
  40. err_unregister:
  41. clk_unregister_divider(clk);
  42. return ERR_PTR(ret);
  43. }
  44. static void sun8i_a23_apb0_setup(struct device_node *node)
  45. {
  46. void __iomem *reg;
  47. struct resource res;
  48. struct clk *clk;
  49. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  50. if (IS_ERR(reg)) {
  51. /*
  52. * This happens with clk nodes instantiated through mfd,
  53. * as those do not have their resources assigned in the
  54. * device tree. Do not print an error in this case.
  55. */
  56. if (PTR_ERR(reg) != -EINVAL)
  57. pr_err("Could not get registers for a23-apb0-clk\n");
  58. return;
  59. }
  60. clk = sun8i_a23_apb0_register(node, reg);
  61. if (IS_ERR(clk))
  62. goto err_unmap;
  63. return;
  64. err_unmap:
  65. iounmap(reg);
  66. of_address_to_resource(node, 0, &res);
  67. release_mem_region(res.start, resource_size(&res));
  68. }
  69. CLK_OF_DECLARE_DRIVER(sun8i_a23_apb0, "allwinner,sun8i-a23-apb0-clk",
  70. sun8i_a23_apb0_setup);
  71. static int sun8i_a23_apb0_clk_probe(struct platform_device *pdev)
  72. {
  73. struct device_node *np = pdev->dev.of_node;
  74. void __iomem *reg;
  75. struct clk *clk;
  76. reg = devm_platform_ioremap_resource(pdev, 0);
  77. if (IS_ERR(reg))
  78. return PTR_ERR(reg);
  79. clk = sun8i_a23_apb0_register(np, reg);
  80. return PTR_ERR_OR_ZERO(clk);
  81. }
  82. static const struct of_device_id sun8i_a23_apb0_clk_dt_ids[] = {
  83. { .compatible = "allwinner,sun8i-a23-apb0-clk" },
  84. { /* sentinel */ }
  85. };
  86. static struct platform_driver sun8i_a23_apb0_clk_driver = {
  87. .driver = {
  88. .name = "sun8i-a23-apb0-clk",
  89. .of_match_table = sun8i_a23_apb0_clk_dt_ids,
  90. },
  91. .probe = sun8i_a23_apb0_clk_probe,
  92. };
  93. builtin_platform_driver(sun8i_a23_apb0_clk_driver);