summaryrefslogtreecommitdiff
path: root/drivers/clk/sophgo/clk-common.h
blob: 95b82e968d04aefbe586b58a7cce8c682d164026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/* SPDX-License-Identifier: GPL-2.0+ */
/*
 * Copyright (c) 2024, Kongyang Liu <seashell11234455@gmail.com>
 *
 */

#ifndef __CLK_SOPHGO_COMMON_H__
#define __CLK_SOPHGO_COMMON_H__

#include <linux/bitops.h>
#include <linux/io.h>

#define CV1800B_CLK_OSC 1
#define CV1800B_CLK_BYPASS 2
#define CV1800B_CLK_ID_TRANSFORM(_id) ((_id) + 3)

struct cv1800b_clk_regbit {
	u32 offset;
	u8 shift;
};

struct cv1800b_clk_regfield {
	u32 offset;
	u8 shift;
	u8 width;
};

#define CV1800B_CLK_REGBIT(_offset, _shift)	\
	{					\
		.offset = _offset,		\
		.shift = _shift,		\
	}

#define CV1800B_CLK_REGFIELD(_offset, _shift, _width)	\
	{						\
		.offset = _offset,			\
		.shift = _shift,			\
		.width = _width,			\
	}

static inline u32 cv1800b_clk_getbit(void *base, struct cv1800b_clk_regbit *bit)
{
	return readl(base + bit->offset) & (BIT(bit->shift));
}

static inline u32 cv1800b_clk_setbit(void *base, struct cv1800b_clk_regbit *bit)
{
	return setbits_le32(base + bit->offset, BIT(bit->shift));
}

static inline u32 cv1800b_clk_clrbit(void *base, struct cv1800b_clk_regbit *bit)
{
	return clrbits_le32(base + bit->offset, BIT(bit->shift));
}

static inline u32 cv1800b_clk_getfield(void *base,
				       struct cv1800b_clk_regfield *field)
{
	u32 mask = GENMASK(field->shift + field->width - 1, field->shift);

	return (readl(base + field->offset) & mask) >> field->shift;
}

static inline void
cv1800b_clk_setfield(void *base, struct cv1800b_clk_regfield *field, u32 val)
{
	u32 mask = GENMASK(field->shift + field->width - 1, field->shift);
	u32 new_val = (readl(base + field->offset) & ~mask) |
		      ((val << field->shift) & mask);

	return writel(new_val, base + field->offset);
}

#endif /* __CLK_SOPHGO_COMMON_H__ */