clk-i2s-mux.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2018 Microchip Technology Inc,
  4. * Codrin Ciubotariu <codrin.ciubotariu@microchip.com>
  5. *
  6. *
  7. */
  8. #include <linux/clk-provider.h>
  9. #include <linux/of.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/regmap.h>
  12. #include <linux/slab.h>
  13. #include <soc/at91/atmel-sfr.h>
  14. #define I2S_BUS_NR 2
  15. struct clk_i2s_mux {
  16. struct clk_hw hw;
  17. struct regmap *regmap;
  18. u8 bus_id;
  19. };
  20. #define to_clk_i2s_mux(hw) container_of(hw, struct clk_i2s_mux, hw)
  21. static u8 clk_i2s_mux_get_parent(struct clk_hw *hw)
  22. {
  23. struct clk_i2s_mux *mux = to_clk_i2s_mux(hw);
  24. u32 val;
  25. regmap_read(mux->regmap, AT91_SFR_I2SCLKSEL, &val);
  26. return (val & BIT(mux->bus_id)) >> mux->bus_id;
  27. }
  28. static int clk_i2s_mux_set_parent(struct clk_hw *hw, u8 index)
  29. {
  30. struct clk_i2s_mux *mux = to_clk_i2s_mux(hw);
  31. return regmap_update_bits(mux->regmap, AT91_SFR_I2SCLKSEL,
  32. BIT(mux->bus_id), index << mux->bus_id);
  33. }
  34. static const struct clk_ops clk_i2s_mux_ops = {
  35. .get_parent = clk_i2s_mux_get_parent,
  36. .set_parent = clk_i2s_mux_set_parent,
  37. .determine_rate = __clk_mux_determine_rate,
  38. };
  39. static struct clk_hw * __init
  40. at91_clk_i2s_mux_register(struct regmap *regmap, const char *name,
  41. const char * const *parent_names,
  42. unsigned int num_parents, u8 bus_id)
  43. {
  44. struct clk_init_data init = {};
  45. struct clk_i2s_mux *i2s_ck;
  46. int ret;
  47. i2s_ck = kzalloc(sizeof(*i2s_ck), GFP_KERNEL);
  48. if (!i2s_ck)
  49. return ERR_PTR(-ENOMEM);
  50. init.name = name;
  51. init.ops = &clk_i2s_mux_ops;
  52. init.parent_names = parent_names;
  53. init.num_parents = num_parents;
  54. i2s_ck->hw.init = &init;
  55. i2s_ck->bus_id = bus_id;
  56. i2s_ck->regmap = regmap;
  57. ret = clk_hw_register(NULL, &i2s_ck->hw);
  58. if (ret) {
  59. kfree(i2s_ck);
  60. return ERR_PTR(ret);
  61. }
  62. return &i2s_ck->hw;
  63. }
  64. static void __init of_sama5d2_clk_i2s_mux_setup(struct device_node *np)
  65. {
  66. struct regmap *regmap_sfr;
  67. u8 bus_id;
  68. const char *parent_names[2];
  69. struct device_node *i2s_mux_np;
  70. struct clk_hw *hw;
  71. int ret;
  72. regmap_sfr = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
  73. if (IS_ERR(regmap_sfr))
  74. return;
  75. for_each_child_of_node(np, i2s_mux_np) {
  76. if (of_property_read_u8(i2s_mux_np, "reg", &bus_id))
  77. continue;
  78. if (bus_id > I2S_BUS_NR)
  79. continue;
  80. ret = of_clk_parent_fill(i2s_mux_np, parent_names, 2);
  81. if (ret != 2)
  82. continue;
  83. hw = at91_clk_i2s_mux_register(regmap_sfr, i2s_mux_np->name,
  84. parent_names, 2, bus_id);
  85. if (IS_ERR(hw))
  86. continue;
  87. of_clk_add_hw_provider(i2s_mux_np, of_clk_hw_simple_get, hw);
  88. }
  89. }
  90. CLK_OF_DECLARE(sama5d2_clk_i2s_mux, "atmel,sama5d2-clk-i2s-mux",
  91. of_sama5d2_clk_i2s_mux_setup);