blob: a9e83d0d6899c44b52ec694797902591bbfb7e4d [file] [log] [blame]
Kongyang Liu80bf1f72024-06-11 17:41:14 +08001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright (c) 2024, Kongyang Liu <seashell11234455@gmail.com>
4 *
5 */
6
7#ifndef __CLK_SOPHGO_COMMON_H__
8#define __CLK_SOPHGO_COMMON_H__
9
10#include <linux/bitops.h>
11#include <linux/io.h>
12
13#define CV1800B_CLK_OSC 1
14#define CV1800B_CLK_BYPASS 2
15#define CV1800B_CLK_ID_TRANSFORM(_id) ((_id) + 3)
16
17struct cv1800b_clk_regbit {
18 u32 offset;
19 u8 shift;
20};
21
22struct cv1800b_clk_regfield {
23 u32 offset;
24 u8 shift;
25 u8 width;
26};
27
28#define CV1800B_CLK_REGBIT(_offset, _shift) \
29 { \
30 .offset = _offset, \
31 .shift = _shift, \
32 }
33
34#define CV1800B_CLK_REGFIELD(_offset, _shift, _width) \
35 { \
36 .offset = _offset, \
37 .shift = _shift, \
38 .width = _width, \
39 }
40
41static inline u32 cv1800b_clk_getbit(void *base, struct cv1800b_clk_regbit *bit)
42{
43 return readl(base + bit->offset) & (BIT(bit->shift));
44}
45
46static inline u32 cv1800b_clk_setbit(void *base, struct cv1800b_clk_regbit *bit)
47{
Yao Zibadc0172025-03-07 17:11:45 +000048 setbits_le32(base + bit->offset, BIT(bit->shift));
49 return 0;
Kongyang Liu80bf1f72024-06-11 17:41:14 +080050}
51
52static inline u32 cv1800b_clk_clrbit(void *base, struct cv1800b_clk_regbit *bit)
53{
Yao Zibadc0172025-03-07 17:11:45 +000054 clrbits_le32(base + bit->offset, BIT(bit->shift));
55 return 0;
Kongyang Liu80bf1f72024-06-11 17:41:14 +080056}
57
58static inline u32 cv1800b_clk_getfield(void *base,
59 struct cv1800b_clk_regfield *field)
60{
61 u32 mask = GENMASK(field->shift + field->width - 1, field->shift);
62
63 return (readl(base + field->offset) & mask) >> field->shift;
64}
65
66static inline void
67cv1800b_clk_setfield(void *base, struct cv1800b_clk_regfield *field, u32 val)
68{
69 u32 mask = GENMASK(field->shift + field->width - 1, field->shift);
70 u32 new_val = (readl(base + field->offset) & ~mask) |
71 ((val << field->shift) & mask);
72
73 return writel(new_val, base + field->offset);
74}
75
76#endif /* __CLK_SOPHGO_COMMON_H__ */