blob: 95b82e968d04aefbe586b58a7cce8c682d164026 [file] [log] [blame]
Kongyang Liu5f364e02024-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{
48 return setbits_le32(base + bit->offset, BIT(bit->shift));
49}
50
51static inline u32 cv1800b_clk_clrbit(void *base, struct cv1800b_clk_regbit *bit)
52{
53 return clrbits_le32(base + bit->offset, BIT(bit->shift));
54}
55
56static inline u32 cv1800b_clk_getfield(void *base,
57 struct cv1800b_clk_regfield *field)
58{
59 u32 mask = GENMASK(field->shift + field->width - 1, field->shift);
60
61 return (readl(base + field->offset) & mask) >> field->shift;
62}
63
64static inline void
65cv1800b_clk_setfield(void *base, struct cv1800b_clk_regfield *field, u32 val)
66{
67 u32 mask = GENMASK(field->shift + field->width - 1, field->shift);
68 u32 new_val = (readl(base + field->offset) & ~mask) |
69 ((val << field->shift) & mask);
70
71 return writel(new_val, base + field->offset);
72}
73
74#endif /* __CLK_SOPHGO_COMMON_H__ */