developer | 399f805 | 2021-07-13 18:08:17 +0800 | [diff] [blame] | 1 | From 0c1e4af01506c913cc54e63f66bb5470f50790c7 Mon Sep 17 00:00:00 2001 |
| 2 | From: Leilk Liu <leilk.liu@mediatek.com> |
| 3 | Date: Tue, 13 Jul 2021 21:45:59 +0800 |
developer | cf63d49 | 2022-01-10 11:34:49 +0800 | [diff] [blame] | 4 | Subject: [PATCH] [Add spi runtime PM support] |
developer | 399f805 | 2021-07-13 18:08:17 +0800 | [diff] [blame] | 5 | |
| 6 | [Description] |
| 7 | Add ahb clk and enable runtime pm |
| 8 | |
| 9 | [Release-log] |
| 10 | N/A |
| 11 | |
| 12 | Change-Id: I0529f6e829f5fc4c5880508971c97b9434820340 |
| 13 | Signed-off-by: Leilk Liu <leilk.liu@mediatek.com> |
developer | 399f805 | 2021-07-13 18:08:17 +0800 | [diff] [blame] | 14 | --- |
| 15 | drivers/spi/spi-mt65xx.c | 77 ++++++++++++++++++++++++++++++++++------ |
| 16 | 1 file changed, 67 insertions(+), 10 deletions(-) |
| 17 | |
| 18 | diff --git a/drivers/spi/spi-mt65xx.c b/drivers/spi/spi-mt65xx.c |
| 19 | index 7e54984..ff2d825 100644 |
| 20 | --- a/drivers/spi/spi-mt65xx.c |
| 21 | +++ b/drivers/spi/spi-mt65xx.c |
| 22 | @@ -119,6 +119,8 @@ struct mtk_spi_compatible { |
| 23 | /* the IPM IP design improve some feature, and support dual/quad mode */ |
| 24 | bool ipm_design; |
| 25 | bool support_quad; |
| 26 | + /* some IC ahb & apb clk is different and also need to be enabled */ |
| 27 | + bool need_ahb_clk; |
| 28 | }; |
| 29 | |
| 30 | struct mtk_spi { |
| 31 | @@ -126,7 +128,7 @@ struct mtk_spi { |
| 32 | u32 state; |
| 33 | int pad_num; |
| 34 | u32 *pad_sel; |
| 35 | - struct clk *parent_clk, *sel_clk, *spi_clk; |
| 36 | + struct clk *parent_clk, *sel_clk, *spi_clk, *spi_hclk; |
| 37 | struct spi_transfer *cur_transfer; |
| 38 | u32 xfer_len; |
| 39 | u32 num_xfered; |
| 40 | @@ -147,12 +149,21 @@ static const struct mtk_spi_compatible mt2712_compat = { |
| 41 | .must_tx = true, |
| 42 | }; |
| 43 | |
| 44 | -static const struct mtk_spi_compatible ipm_compat = { |
| 45 | +static const struct mtk_spi_compatible ipm_compat_single = { |
| 46 | + .must_tx = true, |
| 47 | + .enhance_timing = true, |
| 48 | + .dma_ext = true, |
| 49 | + .ipm_design = true, |
| 50 | + .need_ahb_clk = true, |
| 51 | +}; |
| 52 | + |
| 53 | +static const struct mtk_spi_compatible ipm_compat_quad = { |
| 54 | .must_tx = true, |
| 55 | .enhance_timing = true, |
| 56 | .dma_ext = true, |
| 57 | .ipm_design = true, |
| 58 | .support_quad = true, |
| 59 | + .need_ahb_clk = true, |
| 60 | }; |
| 61 | |
| 62 | static const struct mtk_spi_compatible mt6765_compat = { |
| 63 | @@ -188,8 +199,11 @@ static const struct mtk_chip_config mtk_default_chip_info = { |
| 64 | }; |
| 65 | |
| 66 | static const struct of_device_id mtk_spi_of_match[] = { |
| 67 | - { .compatible = "mediatek,ipm-spi", |
| 68 | - .data = (void *)&ipm_compat, |
| 69 | + { .compatible = "mediatek,ipm-spi-single", |
| 70 | + .data = (void *)&ipm_compat_single, |
| 71 | + }, |
| 72 | + { .compatible = "mediatek,ipm-spi-quad", |
| 73 | + .data = (void *)&ipm_compat_quad, |
| 74 | }, |
| 75 | { .compatible = "mediatek,mt2701-spi", |
| 76 | .data = (void *)&mtk_common_compat, |
| 77 | @@ -992,7 +1006,7 @@ static int mtk_spi_probe(struct platform_device *pdev) |
| 78 | return -ENOMEM; |
| 79 | } |
| 80 | |
| 81 | -// master->auto_runtime_pm = true; |
| 82 | + master->auto_runtime_pm = true; |
| 83 | master->dev.of_node = pdev->dev.of_node; |
| 84 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST; |
| 85 | |
| 86 | @@ -1106,22 +1120,40 @@ static int mtk_spi_probe(struct platform_device *pdev) |
| 87 | goto err_put_master; |
| 88 | } |
| 89 | |
| 90 | + if (mdata->dev_comp->need_ahb_clk) { |
| 91 | + mdata->spi_hclk = devm_clk_get(&pdev->dev, "spi-hclk"); |
| 92 | + if (IS_ERR(mdata->spi_hclk)) { |
| 93 | + ret = PTR_ERR(mdata->spi_hclk); |
| 94 | + dev_err(&pdev->dev, "failed to get spi-hclk: %d\n", ret); |
| 95 | + goto err_put_master; |
| 96 | + } |
| 97 | + |
| 98 | + ret = clk_prepare_enable(mdata->spi_hclk); |
| 99 | + if (ret < 0) { |
| 100 | + dev_err(&pdev->dev, "failed to enable spi_hclk (%d)\n", ret); |
| 101 | + goto err_put_master; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | ret = clk_prepare_enable(mdata->spi_clk); |
| 106 | if (ret < 0) { |
| 107 | dev_err(&pdev->dev, "failed to enable spi_clk (%d)\n", ret); |
| 108 | goto err_put_master; |
| 109 | } |
| 110 | |
| 111 | - /*ret = clk_set_parent(mdata->sel_clk, mdata->parent_clk); |
| 112 | + ret = clk_set_parent(mdata->sel_clk, mdata->parent_clk); |
| 113 | if (ret < 0) { |
| 114 | dev_err(&pdev->dev, "failed to clk_set_parent (%d)\n", ret); |
| 115 | clk_disable_unprepare(mdata->spi_clk); |
| 116 | goto err_put_master; |
| 117 | } |
| 118 | |
| 119 | - clk_disable_unprepare(mdata->sel_clk);*/ |
| 120 | + clk_disable_unprepare(mdata->spi_clk); |
| 121 | + |
| 122 | + if (mdata->dev_comp->need_ahb_clk) |
| 123 | + clk_disable_unprepare(mdata->spi_hclk); |
| 124 | |
| 125 | - //pm_runtime_enable(&pdev->dev); |
| 126 | + pm_runtime_enable(&pdev->dev); |
| 127 | |
| 128 | ret = devm_spi_register_master(&pdev->dev, master); |
| 129 | if (ret) { |
| 130 | @@ -1201,8 +1233,11 @@ static int mtk_spi_suspend(struct device *dev) |
| 131 | if (ret) |
| 132 | return ret; |
| 133 | |
| 134 | - if (!pm_runtime_suspended(dev)) |
| 135 | + if (!pm_runtime_suspended(dev)) { |
| 136 | clk_disable_unprepare(mdata->spi_clk); |
| 137 | + if (mdata->dev_comp->need_ahb_clk) |
| 138 | + clk_disable_unprepare(mdata->spi_hclk); |
| 139 | + } |
| 140 | |
| 141 | return ret; |
| 142 | } |
| 143 | @@ -1214,6 +1249,14 @@ static int mtk_spi_resume(struct device *dev) |
| 144 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 145 | |
| 146 | if (!pm_runtime_suspended(dev)) { |
| 147 | + if (mdata->dev_comp->need_ahb_clk) { |
| 148 | + ret = clk_prepare_enable(mdata->spi_hclk); |
| 149 | + if (ret < 0) { |
| 150 | + dev_err(dev, "failed to enable spi_hclk (%d)\n", ret); |
| 151 | + return ret; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | ret = clk_prepare_enable(mdata->spi_clk); |
| 156 | if (ret < 0) { |
| 157 | dev_err(dev, "failed to enable spi_clk (%d)\n", ret); |
| 158 | @@ -1222,8 +1265,11 @@ static int mtk_spi_resume(struct device *dev) |
| 159 | } |
| 160 | |
| 161 | ret = spi_master_resume(master); |
| 162 | - if (ret < 0) |
| 163 | + if (ret < 0) { |
| 164 | clk_disable_unprepare(mdata->spi_clk); |
| 165 | + if (mdata->dev_comp->need_ahb_clk) |
| 166 | + clk_disable_unprepare(mdata->spi_hclk); |
| 167 | + } |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | @@ -1237,6 +1283,9 @@ static int mtk_spi_runtime_suspend(struct device *dev) |
| 172 | |
| 173 | clk_disable_unprepare(mdata->spi_clk); |
| 174 | |
| 175 | + if (mdata->dev_comp->need_ahb_clk) |
| 176 | + clk_disable_unprepare(mdata->spi_hclk); |
| 177 | + |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | @@ -1246,6 +1295,14 @@ static int mtk_spi_runtime_resume(struct device *dev) |
| 182 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 183 | int ret; |
| 184 | |
| 185 | + if (mdata->dev_comp->need_ahb_clk) { |
| 186 | + ret = clk_prepare_enable(mdata->spi_hclk); |
| 187 | + if (ret < 0) { |
| 188 | + dev_err(dev, "failed to enable spi_hclk (%d)\n", ret); |
| 189 | + return ret; |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | ret = clk_prepare_enable(mdata->spi_clk); |
| 194 | if (ret < 0) { |
| 195 | dev_err(dev, "failed to enable spi_clk (%d)\n", ret); |
| 196 | -- |
| 197 | 2.18.0 |
| 198 | |