clk-apmixed.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2015 MediaTek Inc.
  3. * Author: James Liao <jamesjj.liao@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/delay.h>
  15. #include <linux/of_address.h>
  16. #include <linux/slab.h>
  17. #include "clk-mtk.h"
  18. #define REF2USB_TX_EN BIT(0)
  19. #define REF2USB_TX_LPF_EN BIT(1)
  20. #define REF2USB_TX_OUT_EN BIT(2)
  21. #define REF2USB_EN_MASK (REF2USB_TX_EN | REF2USB_TX_LPF_EN | \
  22. REF2USB_TX_OUT_EN)
  23. struct mtk_ref2usb_tx {
  24. struct clk_hw hw;
  25. void __iomem *base_addr;
  26. };
  27. static inline struct mtk_ref2usb_tx *to_mtk_ref2usb_tx(struct clk_hw *hw)
  28. {
  29. return container_of(hw, struct mtk_ref2usb_tx, hw);
  30. }
  31. static int mtk_ref2usb_tx_is_prepared(struct clk_hw *hw)
  32. {
  33. struct mtk_ref2usb_tx *tx = to_mtk_ref2usb_tx(hw);
  34. return (readl(tx->base_addr) & REF2USB_EN_MASK) == REF2USB_EN_MASK;
  35. }
  36. static int mtk_ref2usb_tx_prepare(struct clk_hw *hw)
  37. {
  38. struct mtk_ref2usb_tx *tx = to_mtk_ref2usb_tx(hw);
  39. u32 val;
  40. val = readl(tx->base_addr);
  41. val |= REF2USB_TX_EN;
  42. writel(val, tx->base_addr);
  43. udelay(100);
  44. val |= REF2USB_TX_LPF_EN;
  45. writel(val, tx->base_addr);
  46. val |= REF2USB_TX_OUT_EN;
  47. writel(val, tx->base_addr);
  48. return 0;
  49. }
  50. static void mtk_ref2usb_tx_unprepare(struct clk_hw *hw)
  51. {
  52. struct mtk_ref2usb_tx *tx = to_mtk_ref2usb_tx(hw);
  53. u32 val;
  54. val = readl(tx->base_addr);
  55. val &= ~REF2USB_EN_MASK;
  56. writel(val, tx->base_addr);
  57. }
  58. static const struct clk_ops mtk_ref2usb_tx_ops = {
  59. .is_prepared = mtk_ref2usb_tx_is_prepared,
  60. .prepare = mtk_ref2usb_tx_prepare,
  61. .unprepare = mtk_ref2usb_tx_unprepare,
  62. };
  63. struct clk * __init mtk_clk_register_ref2usb_tx(const char *name,
  64. const char *parent_name, void __iomem *reg)
  65. {
  66. struct mtk_ref2usb_tx *tx;
  67. struct clk_init_data init = {};
  68. struct clk *clk;
  69. tx = kzalloc(sizeof(*tx), GFP_KERNEL);
  70. if (!tx)
  71. return ERR_PTR(-ENOMEM);
  72. tx->base_addr = reg;
  73. tx->hw.init = &init;
  74. init.name = name;
  75. init.ops = &mtk_ref2usb_tx_ops;
  76. init.parent_names = &parent_name;
  77. init.num_parents = 1;
  78. clk = clk_register(NULL, &tx->hw);
  79. if (IS_ERR(clk)) {
  80. pr_err("Failed to register clk %s: %ld\n", name, PTR_ERR(clk));
  81. kfree(tx);
  82. }
  83. return clk;
  84. }