Skip to content

Commit f22ffee

Browse files
bjarki-andreasendanieldegrasse
authored andcommitted
drivers: i2c: nrfx_twis: impl device deinit
Implement device deinit hooks for nRF TWIS device driver. Signed-off-by: Bjarki Arge Andreasen <[email protected]>
1 parent bd73c73 commit f22ffee

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

drivers/i2c/i2c_nrfx_twis.c

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ static bool shim_nrf_twis_is_resumed(const struct device *dev)
7373
(void)pm_device_state_get(dev, &state);
7474
return state == PM_DEVICE_STATE_ACTIVE;
7575
}
76+
77+
static bool shim_nrf_twis_is_suspended(const struct device *dev)
78+
{
79+
enum pm_device_state state;
80+
81+
(void)pm_device_state_get(dev, &state);
82+
return state == PM_DEVICE_STATE_SUSPENDED ||
83+
state == PM_DEVICE_STATE_OFF;
84+
}
7685
#else
7786
static bool shim_nrf_twis_is_resumed(const struct device *dev)
7887
{
@@ -199,7 +208,7 @@ static int shim_nrf_twis_pm_action_cb(const struct device *dev,
199208

200209
#if CONFIG_PM_DEVICE
201210
case PM_DEVICE_ACTION_SUSPEND:
202-
shim_nrf_twis_disable();
211+
shim_nrf_twis_disable(dev);
203212
break;
204213
#endif
205214

@@ -283,6 +292,35 @@ static int shim_nrf_twis_init(const struct device *dev)
283292
return pm_device_driver_init(dev, shim_nrf_twis_pm_action_cb);
284293
}
285294

295+
static int shim_nrf_twis_deinit(const struct device *dev)
296+
{
297+
const struct shim_nrf_twis_config *dev_config = dev->config;
298+
struct shim_nrf_twis_data *dev_data = dev->data;
299+
300+
if (dev_data->target_config != NULL) {
301+
LOG_ERR("target registered");
302+
return -EPERM;
303+
}
304+
305+
#if CONFIG_PM_DEVICE
306+
/*
307+
* PM must have suspended the device before driver can
308+
* be deinitialized
309+
*/
310+
if (!shim_nrf_twis_is_suspended(dev)) {
311+
LOG_ERR("device active");
312+
return -EBUSY;
313+
}
314+
#else
315+
/* Suspend device */
316+
shim_nrf_twis_disable(dev);
317+
#endif
318+
319+
/* Uninit device hardware */
320+
nrfx_twis_uninit(&dev_config->twis);
321+
return 0;
322+
}
323+
286324
#define SHIM_NRF_TWIS_NAME(id, name) \
287325
_CONCAT_4(shim_nrf_twis_, name, _, id)
288326

@@ -323,9 +361,10 @@ static int shim_nrf_twis_init(const struct device *dev)
323361
shim_nrf_twis_pm_action_cb, \
324362
); \
325363
\
326-
DEVICE_DT_DEFINE( \
364+
DEVICE_DT_DEINIT_DEFINE( \
327365
SHIM_NRF_TWIS_NODE(id), \
328366
shim_nrf_twis_init, \
367+
shim_nrf_twis_deinit, \
329368
PM_DEVICE_DT_GET(SHIM_NRF_TWIS_NODE(id)), \
330369
&SHIM_NRF_TWIS_NAME(id, data), \
331370
&SHIM_NRF_TWIS_NAME(id, config), \

0 commit comments

Comments
 (0)