Skip to content

Commit 373ab28

Browse files
AlessandroLuokartben
authored andcommitted
drivers: spi: ambiq: optimize ambiq spi device pm
This commit optimizes the device pm for ambiq spi driver by adding pinctrl sleep/resume. Signed-off-by: Hao Luo <hluo@ambiq.com>
1 parent 37134f5 commit 373ab28

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

drivers/spi/spi_ambiq_spic.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,25 +465,44 @@ static int spi_ambiq_init(const struct device *dev)
465465
#ifdef CONFIG_PM_DEVICE
466466
static int spi_ambiq_pm_action(const struct device *dev, enum pm_device_action action)
467467
{
468+
const struct spi_ambiq_config *cfg = dev->config;
468469
struct spi_ambiq_data *data = dev->data;
469-
uint32_t ret;
470+
int err;
470471
am_hal_sysctrl_power_state_e status;
471472

472473
switch (action) {
473474
case PM_DEVICE_ACTION_RESUME:
475+
/* Set pins to active state */
476+
err = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT);
477+
if (err < 0) {
478+
return err;
479+
}
474480
status = AM_HAL_SYSCTRL_WAKE;
475481
break;
476482
case PM_DEVICE_ACTION_SUSPEND:
483+
/* Move pins to sleep state */
484+
err = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_SLEEP);
485+
if ((err < 0) && (err != -ENOENT)) {
486+
/*
487+
* If returning -ENOENT, no pins where defined for sleep mode :
488+
* Do not output on console (might sleep already) when going to
489+
* sleep,
490+
* "SPI pinctrl sleep state not available"
491+
* and don't block PM suspend.
492+
* Else return the error.
493+
*/
494+
return err;
495+
}
477496
status = AM_HAL_SYSCTRL_DEEPSLEEP;
478497
break;
479498
default:
480499
return -ENOTSUP;
481500
}
482501

483-
ret = am_hal_iom_power_ctrl(data->iom_handler, status, true);
502+
err = am_hal_iom_power_ctrl(data->iom_handler, status, true);
484503

485-
if (ret != AM_HAL_STATUS_SUCCESS) {
486-
LOG_ERR("am_hal_iom_power_ctrl failed: %d", ret);
504+
if (err != AM_HAL_STATUS_SUCCESS) {
505+
LOG_ERR("am_hal_iom_power_ctrl failed: %d", err);
487506
return -EPERM;
488507
} else {
489508
return 0;

drivers/spi/spi_ambiq_spid.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,24 +364,43 @@ static int spi_ambiq_init(const struct device *dev)
364364
#ifdef CONFIG_PM_DEVICE
365365
static int spi_ambiq_pm_action(const struct device *dev, enum pm_device_action action)
366366
{
367+
const struct spi_ambiq_config *cfg = dev->config;
367368
struct spi_ambiq_data *data = dev->data;
368-
uint32_t ret;
369+
int err;
369370
am_hal_sysctrl_power_state_e status;
370371

371372
switch (action) {
372373
case PM_DEVICE_ACTION_RESUME:
374+
/* Set pins to active state */
375+
err = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_DEFAULT);
376+
if (err < 0) {
377+
return err;
378+
}
373379
status = AM_HAL_SYSCTRL_WAKE;
374380
break;
375381
case PM_DEVICE_ACTION_SUSPEND:
382+
/* Move pins to sleep state */
383+
err = pinctrl_apply_state(cfg->pcfg, PINCTRL_STATE_SLEEP);
384+
if ((err < 0) && (err != -ENOENT)) {
385+
/*
386+
* If returning -ENOENT, no pins where defined for sleep mode :
387+
* Do not output on console (might sleep already) when going to
388+
* sleep,
389+
* "SPI pinctrl sleep state not available"
390+
* and don't block PM suspend.
391+
* Else return the error.
392+
*/
393+
return err;
394+
}
376395
status = AM_HAL_SYSCTRL_DEEPSLEEP;
377396
break;
378397
default:
379398
return -ENOTSUP;
380399
}
381400

382-
ret = am_hal_ios_power_ctrl(data->ios_handler, status, true);
383-
if (ret != AM_HAL_STATUS_SUCCESS) {
384-
LOG_ERR("am_hal_ios_power_ctrl failed: %d", ret);
401+
err = am_hal_ios_power_ctrl(data->ios_handler, status, true);
402+
if (err != AM_HAL_STATUS_SUCCESS) {
403+
LOG_ERR("am_hal_ios_power_ctrl failed: %d", err);
385404
return -EPERM;
386405
} else {
387406
return 0;

0 commit comments

Comments
 (0)