1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2015 MediaTek Inc.
4 * Author: James Liao <jamesjj.liao@mediatek.com>
7 #include <linux/delay.h>
8 #include <linux/module.h>
9 #include <linux/of_address.h>
10 #include <linux/slab.h>
14 #define REF2USB_TX_EN BIT(0)
15 #define REF2USB_TX_LPF_EN BIT(1)
16 #define REF2USB_TX_OUT_EN BIT(2)
17 #define REF2USB_EN_MASK (REF2USB_TX_EN | REF2USB_TX_LPF_EN | \
20 struct mtk_ref2usb_tx
{
22 void __iomem
*base_addr
;
25 static inline struct mtk_ref2usb_tx
*to_mtk_ref2usb_tx(struct clk_hw
*hw
)
27 return container_of(hw
, struct mtk_ref2usb_tx
, hw
);
30 static int mtk_ref2usb_tx_is_prepared(struct clk_hw
*hw
)
32 struct mtk_ref2usb_tx
*tx
= to_mtk_ref2usb_tx(hw
);
34 return (readl(tx
->base_addr
) & REF2USB_EN_MASK
) == REF2USB_EN_MASK
;
37 static int mtk_ref2usb_tx_prepare(struct clk_hw
*hw
)
39 struct mtk_ref2usb_tx
*tx
= to_mtk_ref2usb_tx(hw
);
42 val
= readl(tx
->base_addr
);
45 writel(val
, tx
->base_addr
);
48 val
|= REF2USB_TX_LPF_EN
;
49 writel(val
, tx
->base_addr
);
51 val
|= REF2USB_TX_OUT_EN
;
52 writel(val
, tx
->base_addr
);
57 static void mtk_ref2usb_tx_unprepare(struct clk_hw
*hw
)
59 struct mtk_ref2usb_tx
*tx
= to_mtk_ref2usb_tx(hw
);
62 val
= readl(tx
->base_addr
);
63 val
&= ~REF2USB_EN_MASK
;
64 writel(val
, tx
->base_addr
);
67 static const struct clk_ops mtk_ref2usb_tx_ops
= {
68 .is_prepared
= mtk_ref2usb_tx_is_prepared
,
69 .prepare
= mtk_ref2usb_tx_prepare
,
70 .unprepare
= mtk_ref2usb_tx_unprepare
,
73 struct clk_hw
*mtk_clk_register_ref2usb_tx(const char *name
,
74 const char *parent_name
, void __iomem
*reg
)
76 struct mtk_ref2usb_tx
*tx
;
77 struct clk_init_data init
= {};
80 tx
= kzalloc(sizeof(*tx
), GFP_KERNEL
);
82 return ERR_PTR(-ENOMEM
);
88 init
.ops
= &mtk_ref2usb_tx_ops
;
89 init
.parent_names
= &parent_name
;
92 ret
= clk_hw_register(NULL
, &tx
->hw
);
101 EXPORT_SYMBOL_GPL(mtk_clk_register_ref2usb_tx
);
103 void mtk_clk_unregister_ref2usb_tx(struct clk_hw
*hw
)
105 struct mtk_ref2usb_tx
*tx
= to_mtk_ref2usb_tx(hw
);
107 clk_hw_unregister(hw
);
110 EXPORT_SYMBOL_GPL(mtk_clk_unregister_ref2usb_tx
);
112 MODULE_LICENSE("GPL");