clk-regmap-mux.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/bitops.h>
  7. #include <linux/regmap.h>
  8. #include <linux/export.h>
  9. #include "clk-regmap-mux.h"
  10. static inline struct clk_regmap_mux *to_clk_regmap_mux(struct clk_hw *hw)
  11. {
  12. return container_of(to_clk_regmap(hw), struct clk_regmap_mux, clkr);
  13. }
  14. static u8 mux_get_parent(struct clk_hw *hw)
  15. {
  16. struct clk_regmap_mux *mux = to_clk_regmap_mux(hw);
  17. struct clk_regmap *clkr = to_clk_regmap(hw);
  18. unsigned int mask = GENMASK(mux->width - 1, 0);
  19. unsigned int val;
  20. regmap_read(clkr->regmap, mux->reg, &val);
  21. val >>= mux->shift;
  22. val &= mask;
  23. if (mux->parent_map)
  24. return qcom_find_cfg_index(hw, mux->parent_map, val);
  25. return val;
  26. }
  27. static int mux_set_parent(struct clk_hw *hw, u8 index)
  28. {
  29. struct clk_regmap_mux *mux = to_clk_regmap_mux(hw);
  30. struct clk_regmap *clkr = to_clk_regmap(hw);
  31. unsigned int mask = GENMASK(mux->width + mux->shift - 1, mux->shift);
  32. unsigned int val;
  33. if (mux->parent_map)
  34. index = mux->parent_map[index].cfg;
  35. val = index;
  36. val <<= mux->shift;
  37. return regmap_update_bits(clkr->regmap, mux->reg, mask, val);
  38. }
  39. const struct clk_ops clk_regmap_mux_closest_ops = {
  40. .get_parent = mux_get_parent,
  41. .set_parent = mux_set_parent,
  42. .determine_rate = __clk_mux_determine_rate_closest,
  43. };
  44. EXPORT_SYMBOL_GPL(clk_regmap_mux_closest_ops);