Skip to content

Commit aa2eb38

Browse files
committed
device: add DEVICE[_DT[_INST]_DEINIT_DEFINE
Add a new set of macros to define devices with de-init capabilities. This allows to move gradually to de-init without introducing breaking changes. Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
1 parent ea4e48d commit aa2eb38

File tree

1 file changed

+49
-10
lines changed

1 file changed

+49
-10
lines changed

include/zephyr/device.h

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,14 @@ typedef int16_t device_handle_t;
135135
#endif
136136

137137
/**
138-
* @brief Create a device object and set it up for boot time initialization.
138+
* @brief Create a device object and set it up for boot time initialization,
139+
* with de-init capabilities.
139140
*
140141
* This macro defines a @ref device that is automatically configured by the
141142
* kernel during system initialization. This macro should only be used when the
142143
* device is not being allocated from a devicetree node. If you are allocating a
143-
* device from a devicetree node, use DEVICE_DT_DEFINE() or
144-
* DEVICE_DT_INST_DEFINE() instead.
144+
* device from a devicetree node, use DEVICE_DT_DEINIT_DEFINE() or
145+
* DEVICE_DT_INST_DEINIT_DEFINE() instead.
145146
*
146147
* @param dev_id A unique token which is used in the name of the global device
147148
* structure as a C identifier.
@@ -151,6 +152,9 @@ typedef int16_t device_handle_t;
151152
* (including terminating `NULL`) in order to be looked up from user mode.
152153
* @param init_fn Pointer to the device's initialization function, which will be
153154
* run by the kernel during system initialization. Can be `NULL`.
155+
* @param deinit_fn Pointer to the device's de-initialization function. Can be
156+
* `NULL`. It must release any acquired resources (e.g. pins, bus, clock...) and
157+
* leave the device in its reset state.
154158
* @param pm Pointer to the device's power management resources, a
155159
* @ref pm_device, which will be stored in @ref device.pm field. Use `NULL` if
156160
* the device does not use PM.
@@ -164,13 +168,23 @@ typedef int16_t device_handle_t;
164168
* SYS_INIT() for details.
165169
* @param api Pointer to the device's API structure. Can be `NULL`.
166170
*/
167-
#define DEVICE_DEFINE(dev_id, name, init_fn, pm, data, config, level, prio, \
168-
api) \
171+
#define DEVICE_DEINIT_DEFINE(dev_id, name, init_fn, deinit_fn, pm, data, \
172+
config, level, prio, api) \
169173
Z_DEVICE_STATE_DEFINE(dev_id); \
170-
Z_DEVICE_DEFINE(DT_INVALID_NODE, dev_id, name, init_fn, NULL, 0U, pm, \
171-
data, config, level, prio, api, \
174+
Z_DEVICE_DEFINE(DT_INVALID_NODE, dev_id, name, init_fn, deinit_fn, 0U, \
175+
pm, data, config, level, prio, api, \
172176
&Z_DEVICE_STATE_NAME(dev_id))
173177

178+
/**
179+
* @brief Create a device object and set it up for boot time initialization.
180+
*
181+
* @see DEVICE_DEINIT_DEFINE()
182+
*/
183+
#define DEVICE_DEFINE(dev_id, name, init_fn, pm, data, config, level, prio, \
184+
api) \
185+
DEVICE_DEINIT_DEFINE(dev_id, name, init_fn, NULL, pm, data, config, \
186+
level, prio, api)
187+
174188
/**
175189
* @brief Return a string name for a devicetree node.
176190
*
@@ -213,6 +227,9 @@ typedef int16_t device_handle_t;
213227
* @param node_id The devicetree node identifier.
214228
* @param init_fn Pointer to the device's initialization function, which will be
215229
* run by the kernel during system initialization. Can be `NULL`.
230+
* @param deinit_fn Pointer to the device's de-initialization function. Can be
231+
* `NULL`. It must release any acquired resources (e.g. pins, bus, clock...) and
232+
* leave the device in its reset state.
216233
* @param pm Pointer to the device's power management resources, a
217234
* @ref pm_device, which will be stored in @ref device.pm. Use `NULL` if the
218235
* device does not use PM.
@@ -226,16 +243,38 @@ typedef int16_t device_handle_t;
226243
* SYS_INIT() for details.
227244
* @param api Pointer to the device's API structure. Can be `NULL`.
228245
*/
229-
#define DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, prio, api, \
230-
...) \
246+
#define DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, deinit_fn, pm, data, config, \
247+
level, prio, api, ...) \
231248
Z_DEVICE_STATE_DEFINE(Z_DEVICE_DT_DEV_ID(node_id)); \
232249
Z_DEVICE_DEFINE(node_id, Z_DEVICE_DT_DEV_ID(node_id), \
233-
DEVICE_DT_NAME(node_id), init_fn, NULL, \
250+
DEVICE_DT_NAME(node_id), init_fn, deinit_fn, \
234251
Z_DEVICE_DT_FLAGS(node_id), pm, data, config, level, \
235252
prio, api, \
236253
&Z_DEVICE_STATE_NAME(Z_DEVICE_DT_DEV_ID(node_id)), \
237254
__VA_ARGS__)
238255

256+
/**
257+
* @brief Create a device object from a devicetree node identifier and set it up
258+
* for boot time initialization.
259+
*
260+
* @see DEVICE_DT_DEINIT_DEFINE()
261+
*/
262+
#define DEVICE_DT_DEFINE(node_id, init_fn, pm, data, config, level, prio, api, \
263+
...) \
264+
DEVICE_DT_DEINIT_DEFINE(node_id, init_fn, NULL, pm, data, config, \
265+
level, prio, api, __VA_ARGS__)
266+
267+
/**
268+
* @brief Like DEVICE_DT_DEINIT_DEFINE(), but uses an instance of a
269+
* `DT_DRV_COMPAT` compatible instead of a node identifier.
270+
*
271+
* @param inst Instance number. The `node_id` argument to DEVICE_DT_DEFINE() is
272+
* set to `DT_DRV_INST(inst)`.
273+
* @param ... Other parameters as expected by DEVICE_DT_DEFINE().
274+
*/
275+
#define DEVICE_DT_INST_DEINIT_DEFINE(inst, ...) \
276+
DEVICE_DT_DEINIT_DEFINE(DT_DRV_INST(inst), __VA_ARGS__)
277+
239278
/**
240279
* @brief Like DEVICE_DT_DEFINE(), but uses an instance of a `DT_DRV_COMPAT`
241280
* compatible instead of a node identifier.

0 commit comments

Comments
 (0)