Skip to content

Commit 0209268

Browse files
committed
[nrf fromtree] drivers: sensor: nordic: qdec: Add runtime PM
Add runtime PM to the driver. Signed-off-by: Krzysztof Chruściński <[email protected]> (cherry picked from commit b773306)
1 parent 3bedb2d commit 0209268

File tree

1 file changed

+35
-52
lines changed

1 file changed

+35
-52
lines changed

drivers/sensor/nordic/qdec_nrfx/qdec_nrfx.c

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -173,81 +173,64 @@ static const struct sensor_driver_api qdec_nrfx_driver_api = {
173173
.trigger_set = qdec_nrfx_trigger_set,
174174
};
175175

176-
#ifdef CONFIG_PM_DEVICE
177-
static int qdec_nrfx_pm_action(const struct device *dev,
178-
enum pm_device_action action)
176+
static void qdec_pm_suspend(const struct device *dev)
179177
{
180178
const struct qdec_nrfx_config *config = dev->config;
181-
int ret = 0;
182179

180+
nrfx_qdec_disable(&config->qdec);
181+
qdec_nrfx_gpio_ctrl(dev, false);
182+
183+
(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
184+
}
185+
186+
static void qdec_pm_resume(const struct device *dev)
187+
{
188+
const struct qdec_nrfx_config *config = dev->config;
189+
190+
(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);
191+
qdec_nrfx_gpio_ctrl(dev, true);
192+
nrfx_qdec_enable(&config->qdec);
193+
}
194+
195+
static int qdec_nrfx_pm_action(const struct device *dev, enum pm_device_action action)
196+
{
183197
switch (action) {
184198
case PM_DEVICE_ACTION_RESUME:
185-
ret = pinctrl_apply_state(config->pcfg,
186-
PINCTRL_STATE_DEFAULT);
187-
if (ret < 0) {
188-
return ret;
189-
}
190-
qdec_nrfx_gpio_ctrl(dev, true);
191-
nrfx_qdec_enable(&config->qdec);
192-
break;
193-
194-
case PM_DEVICE_ACTION_TURN_OFF:
195-
/* device must be uninitialized */
196-
nrfx_qdec_uninit(&config->qdec);
197-
ret = pinctrl_apply_state(config->pcfg,
198-
PINCTRL_STATE_SLEEP);
199-
if (ret < 0) {
200-
return ret;
201-
}
199+
qdec_pm_resume(dev);
202200
break;
203201

204202
case PM_DEVICE_ACTION_SUSPEND:
205-
/* device must be suspended */
206-
nrfx_qdec_disable(&config->qdec);
207-
qdec_nrfx_gpio_ctrl(dev, false);
208-
ret = pinctrl_apply_state(config->pcfg,
209-
PINCTRL_STATE_SLEEP);
210-
if (ret < 0) {
211-
return ret;
203+
if (IS_ENABLED(CONFIG_PM_DEVICE)) {
204+
qdec_pm_suspend(dev);
212205
}
213206
break;
214207
default:
215208
return -ENOTSUP;
209+
break;
216210
}
217211

218-
return ret;
212+
return 0;
219213
}
220-
#endif /* CONFIG_PM_DEVICE */
221214

222215
static int qdec_nrfx_init(const struct device *dev)
223216
{
224-
const struct qdec_nrfx_config *dev_config = dev->config;
225-
226-
dev_config->irq_connect();
217+
const struct qdec_nrfx_config *config = dev->config;
218+
nrfx_err_t nerr;
227219

228-
int err = pinctrl_apply_state(dev_config->pcfg, PINCTRL_STATE_DEFAULT);
220+
config->irq_connect();
229221

230-
if (err < 0) {
231-
return err;
222+
nerr = nrfx_qdec_init(&config->qdec, &config->config, qdec_nrfx_event_handler, (void *)dev);
223+
if (nerr != NRFX_SUCCESS) {
224+
return (nerr == NRFX_ERROR_INVALID_STATE) ? -EBUSY : -EFAULT;
232225
}
233226

234-
nrfx_err_t nerr = nrfx_qdec_init(&dev_config->qdec,
235-
&dev_config->config,
236-
qdec_nrfx_event_handler,
237-
(void *)dev);
238-
239-
if (nerr == NRFX_ERROR_INVALID_STATE) {
240-
LOG_ERR("qdec already in use");
241-
return -EBUSY;
242-
} else if (nerr != NRFX_SUCCESS) {
243-
LOG_ERR("failed to initialize qdec");
244-
return -EFAULT;
227+
/* End up in suspend state. */
228+
qdec_nrfx_gpio_ctrl(dev, false);
229+
if (IS_ENABLED(CONFIG_PM_DEVICE_RUNTIME)) {
230+
(void)pinctrl_apply_state(config->pcfg, PINCTRL_STATE_SLEEP);
245231
}
246232

247-
qdec_nrfx_gpio_ctrl(dev, true);
248-
nrfx_qdec_enable(&dev_config->qdec);
249-
250-
return 0;
233+
return pm_device_driver_init(dev, qdec_nrfx_pm_action);
251234
}
252235

253236
#define QDEC(idx) DT_NODELABEL(qdec##idx)
@@ -282,7 +265,7 @@ static int qdec_nrfx_init(const struct device *dev)
282265
.enable_pin = DT_PROP_OR(QDEC(idx), enable_pin, NRF_QDEC_PIN_NOT_CONNECTED), \
283266
.steps = QDEC_PROP(idx, steps), \
284267
}; \
285-
PM_DEVICE_DT_DEFINE(QDEC(idx), qdec_nrfx_pm_action); \
268+
PM_DEVICE_DT_DEFINE(QDEC(idx), qdec_nrfx_pm_action, PM_DEVICE_ISR_SAFE); \
286269
SENSOR_DEVICE_DT_DEFINE(QDEC(idx), \
287270
qdec_nrfx_init, \
288271
PM_DEVICE_DT_GET(QDEC(idx)), \

0 commit comments

Comments
 (0)