1 /*
2  * Copyright (c) 2006-2024 RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author        Notes
8  * 2024-08-16     zhujiale     first version
9  */
10 #ifndef __SDHCI_MISC_H__
11 #define __SDHCI_MISC_H__
12 
13 #include "sdhci_host.h"
14 
15 #define __BF_FIELD_CHECK(...)
16 #define __bf_shf(x) (__builtin_ffsll(x) - 1)
17 #define FIELD_GET(_mask, _reg)                                  \
18     ({                                                          \
19         __BF_FIELD_CHECK(_mask, _reg, 0U, "FIELD_GET: ");       \
20         (typeof(_mask))(((_reg) & (_mask)) >> __bf_shf(_mask)); \
21     })
22 
23 #define FIELD_PREP(_mask, _val)                               \
24     ({                                                        \
25         __BF_FIELD_CHECK(_mask, 0ULL, _val, "FIELD_PREP: ");  \
26         ((typeof(_mask))(_val) << __bf_shf(_mask)) & (_mask); \
27     })
28 
29 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
30 
31 #define min_t(type, x, y) (((type)x < (type)y) ? x : y)
32 #define max_t(type, x, y) (((type)x > (type)y) ? x : y)
33 #define min(x, y)         ((x) < (y) ? (x) : (y))
34 
35 #define from_timer(var, callback_timer, timer_fieldname) \
36     container_of(callback_timer, typeof(*var), timer_fieldname)
37 
38 
39 #define le32_to_cpu(x)   (x)
40 #define le16_to_cpu(x)   (x)
41 #define cpu_to_le16(x)   (x)
42 #define cpu_to_le32(x)   (x)
43 #define lower_32_bits(n) ((rt_uint32_t)((n) & 0xffffffff))
44 #define upper_32_bits(n) ((rt_uint32_t)(((n) >> 16) >> 16))
45 
46 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
47 
48 #define do_div(n, base) ({            \
49     uint32_t __base = (base);         \
50     uint32_t __rem;                   \
51     __rem = ((uint64_t)(n)) % __base; \
52     (n)   = ((uint64_t)(n)) / __base; \
53     __rem;                            \
54 })
55 
56 #define fallthrough \
57     do {            \
58     } while (0)
59 
60 int       regulator_is_supported_voltage(struct regulator *regulator,
61                                          int min_uV, int max_uV);
62 int       regulator_enable(struct regulator *regulator);
63 rt_bool_t mmc_can_gpio_cd(struct mmc_host *host);
64 
65 struct regulator
66 {
67     const char *supply_name;
68 };
69 
70 int regulator_get_current_limit(struct regulator *regulator);
71 
72 int regulator_enable(struct regulator *regulator);
73 
74 void regulator_disable(struct regulator *regulator);
75 
76 #endif
77