clk-apmixed.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2015 MediaTek Inc.
  4. * Author: James Liao <jamesjj.liao@mediatek.com>
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/of_address.h>
  8. #include <linux/slab.h>
  9. #include "clk-mtk.h"
  10. #define REF2USB_TX_EN BIT(0)
  11. #define REF2USB_TX_LPF_EN BIT(1)
  12. #define REF2USB_TX_OUT_EN BIT(2)
  13. #define REF2USB_EN_MASK (REF2USB_TX_EN | REF2USB_TX_LPF_EN | \
  14. REF2USB_TX_OUT_EN)
  15. struct mtk_ref2usb_tx {
  16. struct clk_hw hw;
  17. void __iomem *base_addr;
  18. };
  19. static inline struct mtk_ref2usb_tx *to_mtk_ref2usb_tx(struct clk_hw *hw)
  20. {
  21. return container_of(hw, struct mtk_ref2usb_tx, hw);
  22. }
  23. static int mtk_ref2usb_tx_is_prepared(struct clk_hw *hw)
  24. {
  25. struct mtk_ref2usb_tx *tx = to_mtk_ref2usb_tx(hw);
  26. return (readl(tx->base_addr) & REF2USB_EN_MASK) == REF2USB_EN_MASK;
  27. }
  28. static int mtk_ref2usb_tx_prepare(struct clk_hw *hw)
  29. {
  30. struct mtk_ref2usb_tx *tx = to_mtk_ref2usb_tx(hw);
  31. u32 val;
  32. val = readl(tx->base_addr);
  33. val |= REF2USB_TX_EN;
  34. writel(val, tx->base_addr);
  35. udelay(100);
  36. val |= REF2USB_TX_LPF_EN;
  37. writel(val, tx->base_addr);
  38. val |= REF2USB_TX_OUT_EN;
  39. writel(val, tx->base_addr);
  40. return 0;
  41. }
  42. static void mtk_ref2usb_tx_unprepare(struct clk_hw *hw)
  43. {
  44. struct mtk_ref2usb_tx *tx = to_mtk_ref2usb_tx(hw);
  45. u32 val;
  46. val = readl(tx->base_addr);
  47. val &= ~REF2USB_EN_MASK;
  48. writel(val, tx->base_addr);
  49. }
  50. static const struct clk_ops mtk_ref2usb_tx_ops = {
  51. .is_prepared = mtk_ref2usb_tx_is_prepared,
  52. .prepare = mtk_ref2usb_tx_prepare,
  53. .unprepare = mtk_ref2usb_tx_unprepare,
  54. };
  55. struct clk * __init mtk_clk_register_ref2usb_tx(const char *name,
  56. const char *parent_name, void __iomem *reg)
  57. {
  58. struct mtk_ref2usb_tx *tx;
  59. struct clk_init_data init = {};
  60. struct clk *clk;
  61. tx = kzalloc(sizeof(*tx), GFP_KERNEL);
  62. if (!tx)
  63. return ERR_PTR(-ENOMEM);
  64. tx->base_addr = reg;
  65. tx->hw.init = &init;
  66. init.name = name;
  67. init.ops = &mtk_ref2usb_tx_ops;
  68. init.parent_names = &parent_name;
  69. init.num_parents = 1;
  70. clk = clk_register(NULL, &tx->hw);
  71. if (IS_ERR(clk)) {
  72. pr_err("Failed to register clk %s: %ld\n", name, PTR_ERR(clk));
  73. kfree(tx);
  74. }
  75. return clk;
  76. }