clk-regmap-mux.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #include "clk-debug.h"
  11. static inline struct clk_regmap_mux *to_clk_regmap_mux(struct clk_hw *hw)
  12. {
  13. return container_of(to_clk_regmap(hw), struct clk_regmap_mux, clkr);
  14. }
  15. static u8 mux_get_parent(struct clk_hw *hw)
  16. {
  17. struct clk_regmap_mux *mux = to_clk_regmap_mux(hw);
  18. struct clk_regmap *clkr = to_clk_regmap(hw);
  19. unsigned int mask = GENMASK(mux->width - 1, 0);
  20. unsigned int val;
  21. regmap_read(clkr->regmap, mux->reg, &val);
  22. val >>= mux->shift;
  23. val &= mask;
  24. if (mux->parent_map)
  25. return qcom_find_cfg_index(hw, mux->parent_map, val);
  26. return val;
  27. }
  28. static int mux_set_parent(struct clk_hw *hw, u8 index)
  29. {
  30. struct clk_regmap_mux *mux = to_clk_regmap_mux(hw);
  31. struct clk_regmap *clkr = to_clk_regmap(hw);
  32. unsigned int mask = GENMASK(mux->width + mux->shift - 1, mux->shift);
  33. unsigned int val;
  34. if (mux->parent_map)
  35. index = mux->parent_map[index].cfg;
  36. val = index;
  37. val <<= mux->shift;
  38. return regmap_update_bits(clkr->regmap, mux->reg, mask, val);
  39. }
  40. static void clk_regmap_mux_list_registers(struct seq_file *f, struct clk_hw *hw)
  41. {
  42. struct clk_regmap_mux *mux = to_clk_regmap_mux(hw);
  43. int val;
  44. regmap_read(mux->clkr.regmap, mux->reg, &val);
  45. clock_debug_output(f, "%20s: 0x%.8x\n", "MUXR", val);
  46. }
  47. static struct clk_regmap_ops clk_regmap_mux_regmap_ops = {
  48. .list_registers = clk_regmap_mux_list_registers,
  49. };
  50. static int clk_regmap_mux_init(struct clk_hw *hw)
  51. {
  52. struct clk_regmap *rclk = to_clk_regmap(hw);
  53. if (!rclk->ops)
  54. rclk->ops = &clk_regmap_mux_regmap_ops;
  55. return 0;
  56. }
  57. const struct clk_ops clk_regmap_mux_closest_ops = {
  58. .get_parent = mux_get_parent,
  59. .set_parent = mux_set_parent,
  60. .determine_rate = __clk_mux_determine_rate_closest,
  61. .init = clk_regmap_mux_init,
  62. .debug_init = clk_common_debug_init,
  63. };
  64. EXPORT_SYMBOL_GPL(clk_regmap_mux_closest_ops);