|
14 | 14 | #include <zephyr/sys/byteorder.h> |
15 | 15 | #include <zephyr/drivers/spi.h> |
16 | 16 | #include <zephyr/sys/crc.h> |
| 17 | +#include <zephyr/pm/device_runtime.h> |
17 | 18 |
|
18 | 19 | LOG_MODULE_REGISTER(sdhc_spi, CONFIG_SDHC_LOG_LEVEL); |
19 | 20 |
|
@@ -148,24 +149,30 @@ static int sdhc_spi_init_card(const struct device *dev) |
148 | 149 | const struct sdhc_spi_config *config = dev->config; |
149 | 150 | struct sdhc_spi_data *data = dev->data; |
150 | 151 | struct spi_config *spi_cfg = data->spi_cfg; |
151 | | - int ret; |
| 152 | + int ret, ret2; |
152 | 153 |
|
153 | 154 | if (spi_cfg->frequency == 0) { |
154 | 155 | /* Use default 400KHZ frequency */ |
155 | 156 | spi_cfg->frequency = SDMMC_CLOCK_400KHZ; |
156 | 157 | } |
| 158 | + |
| 159 | + /* Request SPI bus to be active */ |
| 160 | + if (pm_device_runtime_get(config->spi_dev) < 0) { |
| 161 | + return -EIO; |
| 162 | + } |
| 163 | + |
157 | 164 | /* the initial 74 clocks must be sent while CS is high */ |
158 | 165 | spi_cfg->operation |= SPI_CS_ACTIVE_HIGH; |
159 | 166 | ret = sdhc_spi_rx(config->spi_dev, spi_cfg, data->scratch, 10); |
160 | | - if (ret != 0) { |
161 | | - spi_release(config->spi_dev, spi_cfg); |
162 | | - spi_cfg->operation &= ~SPI_CS_ACTIVE_HIGH; |
163 | | - return ret; |
164 | | - } |
| 167 | + |
165 | 168 | /* Release lock on SPI bus */ |
166 | | - ret = spi_release(config->spi_dev, spi_cfg); |
| 169 | + ret2 = spi_release(config->spi_dev, spi_cfg); |
167 | 170 | spi_cfg->operation &= ~SPI_CS_ACTIVE_HIGH; |
168 | | - return ret; |
| 171 | + |
| 172 | + /* Release request for SPI bus to be active */ |
| 173 | + (void)pm_device_runtime_put(config->spi_dev); |
| 174 | + |
| 175 | + return ret ? ret : ret2; |
169 | 176 | } |
170 | 177 |
|
171 | 178 | /* Checks if SPI SD card is sending busy signal */ |
@@ -623,14 +630,20 @@ static int sdhc_spi_request(const struct device *dev, |
623 | 630 | { |
624 | 631 | const struct sdhc_spi_config *config = dev->config; |
625 | 632 | struct sdhc_spi_data *dev_data = dev->data; |
626 | | - int ret, stop_ret, retries = cmd->retries; |
| 633 | + int ret, ret2, stop_ret, retries = cmd->retries; |
627 | 634 | const struct sdhc_command stop_cmd = { |
628 | 635 | .opcode = SD_STOP_TRANSMISSION, |
629 | 636 | .arg = 0, |
630 | 637 | .response_type = SD_SPI_RSP_TYPE_R1b, |
631 | 638 | .timeout_ms = 1000, |
632 | 639 | .retries = 1, |
633 | 640 | }; |
| 641 | + |
| 642 | + /* Request SPI bus to be active */ |
| 643 | + if (pm_device_runtime_get(config->spi_dev) < 0) { |
| 644 | + return -EIO; |
| 645 | + } |
| 646 | + |
634 | 647 | if (data == NULL) { |
635 | 648 | do { |
636 | 649 | ret = sdhc_spi_send_cmd(dev, cmd, false); |
@@ -667,13 +680,14 @@ static int sdhc_spi_request(const struct device *dev, |
667 | 680 | } |
668 | 681 | } while ((ret != 0) && (retries > 0)); |
669 | 682 | } |
670 | | - if (ret) { |
671 | | - /* Release SPI bus */ |
672 | | - spi_release(config->spi_dev, dev_data->spi_cfg); |
673 | | - return ret; |
674 | | - } |
| 683 | + |
675 | 684 | /* Release SPI bus */ |
676 | | - return spi_release(config->spi_dev, dev_data->spi_cfg); |
| 685 | + ret2 = spi_release(config->spi_dev, dev_data->spi_cfg); |
| 686 | + |
| 687 | + /* Release request for SPI bus to be active */ |
| 688 | + (void)pm_device_runtime_put(config->spi_dev); |
| 689 | + |
| 690 | + return ret ? ret : ret2; |
677 | 691 | } |
678 | 692 |
|
679 | 693 | static int sdhc_spi_set_io(const struct device *dev, struct sdhc_io *ios) |
|
0 commit comments