clk-sun8i-mbus.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2014 Chen-Yu Tsai
  4. *
  5. * Chen-Yu Tsai <[email protected]>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/io.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/of_address.h>
  13. #define SUN8I_MBUS_ENABLE 31
  14. #define SUN8I_MBUS_MUX_SHIFT 24
  15. #define SUN8I_MBUS_MUX_MASK 0x3
  16. #define SUN8I_MBUS_DIV_SHIFT 0
  17. #define SUN8I_MBUS_DIV_WIDTH 3
  18. #define SUN8I_MBUS_MAX_PARENTS 4
  19. static DEFINE_SPINLOCK(sun8i_a23_mbus_lock);
  20. static void __init sun8i_a23_mbus_setup(struct device_node *node)
  21. {
  22. int num_parents = of_clk_get_parent_count(node);
  23. const char **parents;
  24. const char *clk_name = node->name;
  25. struct resource res;
  26. struct clk_divider *div;
  27. struct clk_gate *gate;
  28. struct clk_mux *mux;
  29. struct clk *clk;
  30. void __iomem *reg;
  31. int err;
  32. parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
  33. if (!parents)
  34. return;
  35. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  36. if (IS_ERR(reg)) {
  37. pr_err("Could not get registers for sun8i-mbus-clk\n");
  38. goto err_free_parents;
  39. }
  40. div = kzalloc(sizeof(*div), GFP_KERNEL);
  41. if (!div)
  42. goto err_unmap;
  43. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  44. if (!mux)
  45. goto err_free_div;
  46. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  47. if (!gate)
  48. goto err_free_mux;
  49. of_property_read_string(node, "clock-output-names", &clk_name);
  50. of_clk_parent_fill(node, parents, num_parents);
  51. gate->reg = reg;
  52. gate->bit_idx = SUN8I_MBUS_ENABLE;
  53. gate->lock = &sun8i_a23_mbus_lock;
  54. div->reg = reg;
  55. div->shift = SUN8I_MBUS_DIV_SHIFT;
  56. div->width = SUN8I_MBUS_DIV_WIDTH;
  57. div->lock = &sun8i_a23_mbus_lock;
  58. mux->reg = reg;
  59. mux->shift = SUN8I_MBUS_MUX_SHIFT;
  60. mux->mask = SUN8I_MBUS_MUX_MASK;
  61. mux->lock = &sun8i_a23_mbus_lock;
  62. /* The MBUS clocks needs to be always enabled */
  63. clk = clk_register_composite(NULL, clk_name, parents, num_parents,
  64. &mux->hw, &clk_mux_ops,
  65. &div->hw, &clk_divider_ops,
  66. &gate->hw, &clk_gate_ops,
  67. CLK_IS_CRITICAL);
  68. if (IS_ERR(clk))
  69. goto err_free_gate;
  70. err = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  71. if (err)
  72. goto err_unregister_clk;
  73. kfree(parents); /* parents is deep copied */
  74. return;
  75. err_unregister_clk:
  76. /* TODO: The composite clock stuff will leak a bit here. */
  77. clk_unregister(clk);
  78. err_free_gate:
  79. kfree(gate);
  80. err_free_mux:
  81. kfree(mux);
  82. err_free_div:
  83. kfree(div);
  84. err_unmap:
  85. iounmap(reg);
  86. of_address_to_resource(node, 0, &res);
  87. release_mem_region(res.start, resource_size(&res));
  88. err_free_parents:
  89. kfree(parents);
  90. }
  91. CLK_OF_DECLARE(sun8i_a23_mbus, "allwinner,sun8i-a23-mbus-clk", sun8i_a23_mbus_setup);