mux.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // Spreadtrum multiplexer clock driver
  4. //
  5. // Copyright (C) 2017 Spreadtrum, Inc.
  6. // Author: Chunyan Zhang <[email protected]>
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/regmap.h>
  10. #include "mux.h"
  11. u8 sprd_mux_helper_get_parent(const struct sprd_clk_common *common,
  12. const struct sprd_mux_ssel *mux)
  13. {
  14. unsigned int reg;
  15. u8 parent;
  16. int num_parents;
  17. int i;
  18. regmap_read(common->regmap, common->reg, &reg);
  19. parent = reg >> mux->shift;
  20. parent &= (1 << mux->width) - 1;
  21. if (!mux->table)
  22. return parent;
  23. num_parents = clk_hw_get_num_parents(&common->hw);
  24. for (i = 0; i < num_parents - 1; i++)
  25. if (parent >= mux->table[i] && parent < mux->table[i + 1])
  26. return i;
  27. return num_parents - 1;
  28. }
  29. EXPORT_SYMBOL_GPL(sprd_mux_helper_get_parent);
  30. static u8 sprd_mux_get_parent(struct clk_hw *hw)
  31. {
  32. struct sprd_mux *cm = hw_to_sprd_mux(hw);
  33. return sprd_mux_helper_get_parent(&cm->common, &cm->mux);
  34. }
  35. int sprd_mux_helper_set_parent(const struct sprd_clk_common *common,
  36. const struct sprd_mux_ssel *mux,
  37. u8 index)
  38. {
  39. unsigned int reg;
  40. if (mux->table)
  41. index = mux->table[index];
  42. regmap_read(common->regmap, common->reg, &reg);
  43. reg &= ~GENMASK(mux->width + mux->shift - 1, mux->shift);
  44. regmap_write(common->regmap, common->reg,
  45. reg | (index << mux->shift));
  46. return 0;
  47. }
  48. EXPORT_SYMBOL_GPL(sprd_mux_helper_set_parent);
  49. static int sprd_mux_set_parent(struct clk_hw *hw, u8 index)
  50. {
  51. struct sprd_mux *cm = hw_to_sprd_mux(hw);
  52. return sprd_mux_helper_set_parent(&cm->common, &cm->mux, index);
  53. }
  54. const struct clk_ops sprd_mux_ops = {
  55. .get_parent = sprd_mux_get_parent,
  56. .set_parent = sprd_mux_set_parent,
  57. .determine_rate = __clk_mux_determine_rate,
  58. };
  59. EXPORT_SYMBOL_GPL(sprd_mux_ops);