Skip to content

Commit e5db259

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 f816c22 commit e5db259

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
@@ -136,13 +136,14 @@ typedef int16_t device_handle_t;
136136
#endif
137137

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

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

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

0 commit comments

Comments
 (0)