clk-sun8i-mbus.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2014 Chen-Yu Tsai
  3. *
  4. * Chen-Yu Tsai <wens@csie.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/clk-provider.h>
  19. #include <linux/slab.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/of_address.h>
  22. #define SUN8I_MBUS_ENABLE 31
  23. #define SUN8I_MBUS_MUX_SHIFT 24
  24. #define SUN8I_MBUS_MUX_MASK 0x3
  25. #define SUN8I_MBUS_DIV_SHIFT 0
  26. #define SUN8I_MBUS_DIV_WIDTH 3
  27. #define SUN8I_MBUS_MAX_PARENTS 4
  28. static DEFINE_SPINLOCK(sun8i_a23_mbus_lock);
  29. static void __init sun8i_a23_mbus_setup(struct device_node *node)
  30. {
  31. int num_parents = of_clk_get_parent_count(node);
  32. const char **parents;
  33. const char *clk_name = node->name;
  34. struct resource res;
  35. struct clk_divider *div;
  36. struct clk_gate *gate;
  37. struct clk_mux *mux;
  38. struct clk *clk;
  39. void __iomem *reg;
  40. int err;
  41. parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
  42. if (!parents)
  43. return;
  44. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  45. if (IS_ERR(reg)) {
  46. pr_err("Could not get registers for sun8i-mbus-clk\n");
  47. goto err_free_parents;
  48. }
  49. div = kzalloc(sizeof(*div), GFP_KERNEL);
  50. if (!div)
  51. goto err_unmap;
  52. mux = kzalloc(sizeof(*mux), GFP_KERNEL);
  53. if (!mux)
  54. goto err_free_div;
  55. gate = kzalloc(sizeof(*gate), GFP_KERNEL);
  56. if (!gate)
  57. goto err_free_mux;
  58. of_property_read_string(node, "clock-output-names", &clk_name);
  59. of_clk_parent_fill(node, parents, num_parents);
  60. gate->reg = reg;
  61. gate->bit_idx = SUN8I_MBUS_ENABLE;
  62. gate->lock = &sun8i_a23_mbus_lock;
  63. div->reg = reg;
  64. div->shift = SUN8I_MBUS_DIV_SHIFT;
  65. div->width = SUN8I_MBUS_DIV_WIDTH;
  66. div->lock = &sun8i_a23_mbus_lock;
  67. mux->reg = reg;
  68. mux->shift = SUN8I_MBUS_MUX_SHIFT;
  69. mux->mask = SUN8I_MBUS_MUX_MASK;
  70. mux->lock = &sun8i_a23_mbus_lock;
  71. clk = clk_register_composite(NULL, clk_name, parents, num_parents,
  72. &mux->hw, &clk_mux_ops,
  73. &div->hw, &clk_divider_ops,
  74. &gate->hw, &clk_gate_ops,
  75. 0);
  76. if (IS_ERR(clk))
  77. goto err_free_gate;
  78. err = of_clk_add_provider(node, of_clk_src_simple_get, clk);
  79. if (err)
  80. goto err_unregister_clk;
  81. kfree(parents); /* parents is deep copied */
  82. /* The MBUS clocks needs to be always enabled */
  83. __clk_get(clk);
  84. clk_prepare_enable(clk);
  85. return;
  86. err_unregister_clk:
  87. /* TODO: The composite clock stuff will leak a bit here. */
  88. clk_unregister(clk);
  89. err_free_gate:
  90. kfree(gate);
  91. err_free_mux:
  92. kfree(mux);
  93. err_free_div:
  94. kfree(div);
  95. err_unmap:
  96. iounmap(reg);
  97. of_address_to_resource(node, 0, &res);
  98. release_mem_region(res.start, resource_size(&res));
  99. err_free_parents:
  100. kfree(parents);
  101. }
  102. CLK_OF_DECLARE(sun8i_a23_mbus, "allwinner,sun8i-a23-mbus-clk", sun8i_a23_mbus_setup);