clk-regmap-phy-mux.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2022, Linaro Ltd.
  4. */
  5. #include <linux/clk-provider.h>
  6. #include <linux/bitfield.h>
  7. #include <linux/regmap.h>
  8. #include <linux/export.h>
  9. #include "clk-regmap.h"
  10. #include "clk-regmap-phy-mux.h"
  11. #define PHY_MUX_MASK GENMASK(1, 0)
  12. #define PHY_MUX_PHY_SRC 0
  13. #define PHY_MUX_REF_SRC 2
  14. static inline struct clk_regmap_phy_mux *to_clk_regmap_phy_mux(struct clk_regmap *clkr)
  15. {
  16. return container_of(clkr, struct clk_regmap_phy_mux, clkr);
  17. }
  18. static int phy_mux_is_enabled(struct clk_hw *hw)
  19. {
  20. struct clk_regmap *clkr = to_clk_regmap(hw);
  21. struct clk_regmap_phy_mux *phy_mux = to_clk_regmap_phy_mux(clkr);
  22. unsigned int val;
  23. regmap_read(clkr->regmap, phy_mux->reg, &val);
  24. val = FIELD_GET(PHY_MUX_MASK, val);
  25. WARN_ON(val != PHY_MUX_PHY_SRC && val != PHY_MUX_REF_SRC);
  26. return val == PHY_MUX_PHY_SRC;
  27. }
  28. static int phy_mux_enable(struct clk_hw *hw)
  29. {
  30. struct clk_regmap *clkr = to_clk_regmap(hw);
  31. struct clk_regmap_phy_mux *phy_mux = to_clk_regmap_phy_mux(clkr);
  32. return regmap_update_bits(clkr->regmap, phy_mux->reg,
  33. PHY_MUX_MASK,
  34. FIELD_PREP(PHY_MUX_MASK, PHY_MUX_PHY_SRC));
  35. }
  36. static void phy_mux_disable(struct clk_hw *hw)
  37. {
  38. struct clk_regmap *clkr = to_clk_regmap(hw);
  39. struct clk_regmap_phy_mux *phy_mux = to_clk_regmap_phy_mux(clkr);
  40. regmap_update_bits(clkr->regmap, phy_mux->reg,
  41. PHY_MUX_MASK,
  42. FIELD_PREP(PHY_MUX_MASK, PHY_MUX_REF_SRC));
  43. }
  44. const struct clk_ops clk_regmap_phy_mux_ops = {
  45. .enable = phy_mux_enable,
  46. .disable = phy_mux_disable,
  47. .is_enabled = phy_mux_is_enabled,
  48. };
  49. EXPORT_SYMBOL_GPL(clk_regmap_phy_mux_ops);