Line data Source code
1 1 : /*
2 : * Copyright (c) 2021 Carlo Caione <ccaione@baylibre.com>
3 : *
4 : * SPDX-License-Identifier: Apache-2.0
5 : */
6 :
7 : /**
8 : * @file
9 : * @brief Public SYSCON driver APIs
10 : */
11 :
12 : #ifndef ZEPHYR_INCLUDE_DRIVERS_SYSCON_H_
13 : #define ZEPHYR_INCLUDE_DRIVERS_SYSCON_H_
14 :
15 : /**
16 : * @brief SYSCON Interface
17 : * @defgroup syscon_interface SYSCON Interface
18 : * @ingroup io_interfaces
19 : * @{
20 : */
21 :
22 : #include <errno.h>
23 :
24 : #include <zephyr/types.h>
25 : #include <zephyr/device.h>
26 :
27 : #ifdef __cplusplus
28 : extern "C" {
29 : #endif
30 :
31 : /**
32 : * API template to get the base address of the syscon region.
33 : *
34 : * @see syscon_get_base
35 : */
36 1 : typedef int (*syscon_api_get_base)(const struct device *dev, uintptr_t *addr);
37 :
38 : /**
39 : * API template to read a single register.
40 : *
41 : * @see syscon_read_reg
42 : */
43 1 : typedef int (*syscon_api_read_reg)(const struct device *dev, uint16_t reg, uint32_t *val);
44 :
45 : /**
46 : * API template to write a single register.
47 : *
48 : * @see syscon_write_reg
49 : */
50 1 : typedef int (*syscon_api_write_reg)(const struct device *dev, uint16_t reg, uint32_t val);
51 :
52 : /**
53 : * API template to get the size of the syscon register.
54 : *
55 : * @see syscon_get_size
56 : */
57 1 : typedef int (*syscon_api_get_size)(const struct device *dev, size_t *size);
58 :
59 : /**
60 : * @brief System Control (syscon) register driver API
61 : */
62 1 : __subsystem struct syscon_driver_api {
63 0 : syscon_api_read_reg read;
64 0 : syscon_api_write_reg write;
65 0 : syscon_api_get_base get_base;
66 0 : syscon_api_get_size get_size;
67 : };
68 :
69 : /**
70 : * @brief Get the syscon base address
71 : *
72 : * @param dev The device to get the register size for.
73 : * @param addr Where to write the base address.
74 : * @return 0 When @a addr was written to.
75 : * @return -ENOSYS If the API or function isn't implemented.
76 : */
77 1 : __syscall int syscon_get_base(const struct device *dev, uintptr_t *addr);
78 :
79 : static inline int z_impl_syscon_get_base(const struct device *dev, uintptr_t *addr)
80 : {
81 : const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api;
82 :
83 : if ((api == NULL) || (api->get_base == NULL)) {
84 : return -ENOSYS;
85 : }
86 :
87 : return api->get_base(dev, addr);
88 : }
89 :
90 :
91 : /**
92 : * @brief Read from syscon register
93 : *
94 : * This function reads from a specific register in the syscon area
95 : *
96 : * @param dev The device to get the register size for.
97 : * @param reg The register offset
98 : * @param val The returned value read from the syscon register
99 : *
100 : * @return 0 on success.
101 : * @return -ENOSYS If the API or function isn't implemented.
102 : */
103 1 : __syscall int syscon_read_reg(const struct device *dev, uint16_t reg, uint32_t *val);
104 :
105 : static inline int z_impl_syscon_read_reg(const struct device *dev, uint16_t reg, uint32_t *val)
106 : {
107 : const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api;
108 :
109 : if ((api == NULL) || (api->read == NULL)) {
110 : return -ENOSYS;
111 : }
112 :
113 : return api->read(dev, reg, val);
114 : }
115 :
116 :
117 : /**
118 : * @brief Write to syscon register
119 : *
120 : * This function writes to a specific register in the syscon area
121 : *
122 : * @param dev The device to get the register size for.
123 : * @param reg The register offset
124 : * @param val The value to be written in the register
125 : *
126 : * @return 0 on success.
127 : * @return -ENOSYS If the API or function isn't implemented.
128 : */
129 1 : __syscall int syscon_write_reg(const struct device *dev, uint16_t reg, uint32_t val);
130 :
131 : static inline int z_impl_syscon_write_reg(const struct device *dev, uint16_t reg, uint32_t val)
132 : {
133 : const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api;
134 :
135 : if ((api == NULL) || (api->write == NULL)) {
136 : return -ENOSYS;
137 : }
138 :
139 : return api->write(dev, reg, val);
140 : }
141 :
142 : /**
143 : * Get the size of the syscon register in bytes.
144 : *
145 : * @param dev The device to get the register size for.
146 : * @param size Pointer to write the size to.
147 : * @return 0 for success.
148 : * @return -ENOSYS If the API or function isn't implemented.
149 : */
150 1 : __syscall int syscon_get_size(const struct device *dev, size_t *size);
151 :
152 : static inline int z_impl_syscon_get_size(const struct device *dev, size_t *size)
153 : {
154 : const struct syscon_driver_api *api = (const struct syscon_driver_api *)dev->api;
155 :
156 : if ((api == NULL) || (api->get_size == NULL)) {
157 : return -ENOSYS;
158 : }
159 :
160 : return api->get_size(dev, size);
161 : }
162 :
163 : /**
164 : * @}
165 : */
166 :
167 : #ifdef __cplusplus
168 : }
169 : #endif
170 :
171 : #include <zephyr/syscalls/syscon.h>
172 :
173 : #endif /* ZEPHYR_INCLUDE_DRIVERS_SYSCON_H_ */
|