Commit 9f0ebb64 authored by Gerard Marull-Paretas's avatar Gerard Marull-Paretas Committed by Fabio Baltieri
Browse files

drivers: i2c: nrfx_twim: simplify PM by using pm_device_driver_init



- Driver always initializes the device in the suspended state
- If CONFIG_PM_DEVICE_RUNTIME=n, device PM callback will be called with
  RESUME action, thus setting up pins to default state and enabling the
  peripheral

NOTE: when CONFIG_PM_DEVICE=n, the pinctrl sleep state will not be
available (-ENOENT) and so never applied, thus avoiding a pin
suspended->active transition.

Signed-off-by: default avatarGerard Marull-Paretas <gerard@teslabs.com>
parent 740eba13
Loading
Loading
Loading
Loading
+6 −33
Original line number Diff line number Diff line
@@ -297,40 +297,26 @@ static const struct i2c_driver_api i2c_nrfx_twim_driver_api = {
	.recover_bus = i2c_nrfx_twim_recover_bus,
};

#ifdef CONFIG_PM_DEVICE
static int twim_nrfx_pm_action(const struct device *dev,
			       enum pm_device_action action)
{
	const struct i2c_nrfx_twim_config *dev_config = dev->config;
	int ret = 0;

	switch (action) {
	case PM_DEVICE_ACTION_RESUME:
		ret = pinctrl_apply_state(dev_config->pcfg,
					  PINCTRL_STATE_DEFAULT);
		if (ret < 0) {
			return ret;
		}
		(void)pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_DEFAULT);
		nrfx_twim_enable(&dev_config->twim);
		break;

	case PM_DEVICE_ACTION_SUSPEND:
		nrfx_twim_disable(&dev_config->twim);

		ret = pinctrl_apply_state(dev_config->pcfg,
					  PINCTRL_STATE_SLEEP);
		if (ret < 0) {
			return ret;
		}
		(void)pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_SLEEP);
		break;

	default:
		ret = -ENOTSUP;
		return -ENOTSUP;
	}

	return ret;
	return 0;
}
#endif /* CONFIG_PM_DEVICE */

static int i2c_nrfx_twim_init(const struct device *dev)
{
@@ -342,13 +328,7 @@ static int i2c_nrfx_twim_init(const struct device *dev)
	k_sem_init(&dev_data->transfer_sync, 1, 1);
	k_sem_init(&dev_data->completion_sync, 0, 1);

	int err = pinctrl_apply_state(dev_config->pcfg,
				      COND_CODE_1(CONFIG_PM_DEVICE_RUNTIME,
						  (PINCTRL_STATE_SLEEP),
						  (PINCTRL_STATE_DEFAULT)));
	if (err < 0) {
		return err;
	}
	(void)pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_SLEEP);

	if (nrfx_twim_init(&dev_config->twim, &dev_config->twim_config,
			   event_handler, dev_data) != NRFX_SUCCESS) {
@@ -356,14 +336,7 @@ static int i2c_nrfx_twim_init(const struct device *dev)
		return -EIO;
	}

#ifdef CONFIG_PM_DEVICE_RUNTIME
	pm_device_init_suspended(dev);
	pm_device_runtime_enable(dev);
#else
	nrfx_twim_enable(&dev_config->twim);
#endif

	return 0;
	return pm_device_driver_init(dev, twim_nrfx_pm_action);
}

#define I2C_NRFX_TWIM_INVALID_FREQUENCY  ((nrf_twim_frequency_t)-1)
+1 −0
Original line number Diff line number Diff line
@@ -130,6 +130,7 @@
			interrupts = <3 NRF_DEFAULT_IRQ_PRIORITY>;
			easydma-maxcnt-bits = <14>;
			status = "disabled";
			zephyr,pm-device-runtime-auto;
		};

		spi0: spi@40004000 {
+1 −0
Original line number Diff line number Diff line
@@ -134,6 +134,7 @@
			interrupts = <3 NRF_DEFAULT_IRQ_PRIORITY>;
			easydma-maxcnt-bits = <10>;
			status = "disabled";
			zephyr,pm-device-runtime-auto;
		};

		spi0: spi@40004000 {
+1 −0
Original line number Diff line number Diff line
@@ -146,6 +146,7 @@
			interrupts = <3 NRF_DEFAULT_IRQ_PRIORITY>;
			easydma-maxcnt-bits = <14>;
			status = "disabled";
			zephyr,pm-device-runtime-auto;
		};

		spi1: spi@40003000 {
+2 −0
Original line number Diff line number Diff line
@@ -149,6 +149,7 @@
			interrupts = <3 NRF_DEFAULT_IRQ_PRIORITY>;
			easydma-maxcnt-bits = <15>;
			status = "disabled";
			zephyr,pm-device-runtime-auto;
		};

		spi0: spi@40003000 {
@@ -185,6 +186,7 @@
			interrupts = <4 NRF_DEFAULT_IRQ_PRIORITY>;
			easydma-maxcnt-bits = <15>;
			status = "disabled";
			zephyr,pm-device-runtime-auto;
		};

		spi1: spi@40004000 {
Loading