Thomas Gleixner | 1802d0b | 2019-05-27 08:55:21 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 MediaTek Inc. |
| 4 | * Author: Leilk Liu <leilk.liu@mediatek.com> |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/clk.h> |
| 8 | #include <linux/device.h> |
| 9 | #include <linux/err.h> |
| 10 | #include <linux/interrupt.h> |
Leilk Liu | dd69a0a | 2015-08-24 11:45:15 +0800 | [diff] [blame] | 11 | #include <linux/io.h> |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 12 | #include <linux/ioport.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/of.h> |
Leilk Liu | 3745760 | 2015-10-26 16:09:44 +0800 | [diff] [blame] | 15 | #include <linux/of_gpio.h> |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 16 | #include <linux/platform_device.h> |
| 17 | #include <linux/platform_data/spi-mt65xx.h> |
| 18 | #include <linux/pm_runtime.h> |
| 19 | #include <linux/spi/spi.h> |
luhua.xu | fdeae8f | 2019-09-11 05:55:31 -0400 | [diff] [blame] | 20 | #include <linux/dma-mapping.h> |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 21 | |
| 22 | #define SPI_CFG0_REG 0x0000 |
| 23 | #define SPI_CFG1_REG 0x0004 |
| 24 | #define SPI_TX_SRC_REG 0x0008 |
| 25 | #define SPI_RX_DST_REG 0x000c |
| 26 | #define SPI_TX_DATA_REG 0x0010 |
| 27 | #define SPI_RX_DATA_REG 0x0014 |
| 28 | #define SPI_CMD_REG 0x0018 |
| 29 | #define SPI_STATUS0_REG 0x001c |
| 30 | #define SPI_PAD_SEL_REG 0x0024 |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 31 | #define SPI_CFG2_REG 0x0028 |
luhua.xu | fdeae8f | 2019-09-11 05:55:31 -0400 | [diff] [blame] | 32 | #define SPI_TX_SRC_REG_64 0x002c |
| 33 | #define SPI_RX_DST_REG_64 0x0030 |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 34 | |
| 35 | #define SPI_CFG0_SCK_HIGH_OFFSET 0 |
| 36 | #define SPI_CFG0_SCK_LOW_OFFSET 8 |
| 37 | #define SPI_CFG0_CS_HOLD_OFFSET 16 |
| 38 | #define SPI_CFG0_CS_SETUP_OFFSET 24 |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 39 | #define SPI_ADJUST_CFG0_CS_HOLD_OFFSET 0 |
| 40 | #define SPI_ADJUST_CFG0_CS_SETUP_OFFSET 16 |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 41 | |
| 42 | #define SPI_CFG1_CS_IDLE_OFFSET 0 |
| 43 | #define SPI_CFG1_PACKET_LOOP_OFFSET 8 |
| 44 | #define SPI_CFG1_PACKET_LENGTH_OFFSET 16 |
Mason Zhang | f84d866 | 2021-07-13 19:40:49 +0800 | [diff] [blame] | 45 | #define SPI_CFG1_GET_TICK_DLY_OFFSET 29 |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 46 | |
Mason Zhang | f84d866 | 2021-07-13 19:40:49 +0800 | [diff] [blame] | 47 | #define SPI_CFG1_GET_TICK_DLY_MASK 0xe0000000 |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 48 | #define SPI_CFG1_CS_IDLE_MASK 0xff |
| 49 | #define SPI_CFG1_PACKET_LOOP_MASK 0xff00 |
| 50 | #define SPI_CFG1_PACKET_LENGTH_MASK 0x3ff0000 |
leilk.liu | 44b37eb | 2020-07-01 17:00:20 +0800 | [diff] [blame] | 51 | #define SPI_CFG2_SCK_HIGH_OFFSET 0 |
| 52 | #define SPI_CFG2_SCK_LOW_OFFSET 16 |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 53 | |
Leilk Liu | a71d6ea | 2015-08-20 17:19:08 +0800 | [diff] [blame] | 54 | #define SPI_CMD_ACT BIT(0) |
| 55 | #define SPI_CMD_RESUME BIT(1) |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 56 | #define SPI_CMD_RST BIT(2) |
| 57 | #define SPI_CMD_PAUSE_EN BIT(4) |
| 58 | #define SPI_CMD_DEASSERT BIT(5) |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 59 | #define SPI_CMD_SAMPLE_SEL BIT(6) |
| 60 | #define SPI_CMD_CS_POL BIT(7) |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 61 | #define SPI_CMD_CPHA BIT(8) |
| 62 | #define SPI_CMD_CPOL BIT(9) |
| 63 | #define SPI_CMD_RX_DMA BIT(10) |
| 64 | #define SPI_CMD_TX_DMA BIT(11) |
| 65 | #define SPI_CMD_TXMSBF BIT(12) |
| 66 | #define SPI_CMD_RXMSBF BIT(13) |
| 67 | #define SPI_CMD_RX_ENDIAN BIT(14) |
| 68 | #define SPI_CMD_TX_ENDIAN BIT(15) |
| 69 | #define SPI_CMD_FINISH_IE BIT(16) |
| 70 | #define SPI_CMD_PAUSE_IE BIT(17) |
| 71 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 72 | #define MT8173_SPI_MAX_PAD_SEL 3 |
| 73 | |
Leilk Liu | 50f8fec | 2015-08-24 11:45:16 +0800 | [diff] [blame] | 74 | #define MTK_SPI_PAUSE_INT_STATUS 0x2 |
| 75 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 76 | #define MTK_SPI_IDLE 0 |
| 77 | #define MTK_SPI_PAUSED 1 |
| 78 | |
Daniel Kurtz | 1ce2486 | 2017-01-27 00:21:54 +0800 | [diff] [blame] | 79 | #define MTK_SPI_MAX_FIFO_SIZE 32U |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 80 | #define MTK_SPI_PACKET_SIZE 1024 |
luhua.xu | fdeae8f | 2019-09-11 05:55:31 -0400 | [diff] [blame] | 81 | #define MTK_SPI_32BITS_MASK (0xffffffff) |
| 82 | |
| 83 | #define DMA_ADDR_EXT_BITS (36) |
| 84 | #define DMA_ADDR_DEF_BITS (32) |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 85 | |
| 86 | struct mtk_spi_compatible { |
Leilk Liu | af57937 | 2015-08-20 17:19:07 +0800 | [diff] [blame] | 87 | bool need_pad_sel; |
| 88 | /* Must explicitly send dummy Tx bytes to do Rx only transfer */ |
| 89 | bool must_tx; |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 90 | /* some IC design adjust cfg register to enhance time accuracy */ |
| 91 | bool enhance_timing; |
luhua.xu | fdeae8f | 2019-09-11 05:55:31 -0400 | [diff] [blame] | 92 | /* some IC support DMA addr extension */ |
| 93 | bool dma_ext; |
Mason Zhang | 162a31e | 2021-06-29 18:08:15 +0800 | [diff] [blame] | 94 | /* some IC no need unprepare SPI clk */ |
| 95 | bool no_need_unprepare; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | struct mtk_spi { |
| 99 | void __iomem *base; |
| 100 | u32 state; |
Leilk Liu | 3745760 | 2015-10-26 16:09:44 +0800 | [diff] [blame] | 101 | int pad_num; |
| 102 | u32 *pad_sel; |
Leilk Liu | adcbcfe | 2015-08-31 21:18:57 +0800 | [diff] [blame] | 103 | struct clk *parent_clk, *sel_clk, *spi_clk; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 104 | struct spi_transfer *cur_transfer; |
| 105 | u32 xfer_len; |
Peter Shih | 00bca73 | 2018-09-10 11:54:21 +0800 | [diff] [blame] | 106 | u32 num_xfered; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 107 | struct scatterlist *tx_sgl, *rx_sgl; |
| 108 | u32 tx_sgl_len, rx_sgl_len; |
| 109 | const struct mtk_spi_compatible *dev_comp; |
Mason Zhang | 162a31e | 2021-06-29 18:08:15 +0800 | [diff] [blame] | 110 | u32 spi_clk_hz; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 111 | }; |
| 112 | |
Leilk Liu | 4eaf6f7 | 2015-12-31 10:59:00 +0800 | [diff] [blame] | 113 | static const struct mtk_spi_compatible mtk_common_compat; |
Leilk Liu | fc4f226 | 2017-06-12 09:24:40 +0800 | [diff] [blame] | 114 | |
leilk.liu@mediatek.com | b6b1f2d | 2017-06-20 16:21:07 +0800 | [diff] [blame] | 115 | static const struct mtk_spi_compatible mt2712_compat = { |
| 116 | .must_tx = true, |
| 117 | }; |
| 118 | |
luhua.xu | 2c231e0 | 2019-09-11 05:55:30 -0400 | [diff] [blame] | 119 | static const struct mtk_spi_compatible mt6765_compat = { |
| 120 | .need_pad_sel = true, |
| 121 | .must_tx = true, |
| 122 | .enhance_timing = true, |
luhua.xu | fdeae8f | 2019-09-11 05:55:31 -0400 | [diff] [blame] | 123 | .dma_ext = true, |
luhua.xu | 2c231e0 | 2019-09-11 05:55:30 -0400 | [diff] [blame] | 124 | }; |
| 125 | |
Leilk Liu | fc4f226 | 2017-06-12 09:24:40 +0800 | [diff] [blame] | 126 | static const struct mtk_spi_compatible mt7622_compat = { |
| 127 | .must_tx = true, |
| 128 | .enhance_timing = true, |
| 129 | }; |
| 130 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 131 | static const struct mtk_spi_compatible mt8173_compat = { |
Leilk Liu | af57937 | 2015-08-20 17:19:07 +0800 | [diff] [blame] | 132 | .need_pad_sel = true, |
| 133 | .must_tx = true, |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 134 | }; |
| 135 | |
Leilk Liu | b654aa6 | 2018-11-01 14:02:19 +0800 | [diff] [blame] | 136 | static const struct mtk_spi_compatible mt8183_compat = { |
| 137 | .need_pad_sel = true, |
| 138 | .must_tx = true, |
| 139 | .enhance_timing = true, |
| 140 | }; |
| 141 | |
Mason Zhang | 162a31e | 2021-06-29 18:08:15 +0800 | [diff] [blame] | 142 | static const struct mtk_spi_compatible mt6893_compat = { |
| 143 | .need_pad_sel = true, |
| 144 | .must_tx = true, |
| 145 | .enhance_timing = true, |
| 146 | .dma_ext = true, |
| 147 | .no_need_unprepare = true, |
| 148 | }; |
| 149 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 150 | /* |
| 151 | * A piece of default chip info unless the platform |
| 152 | * supplies it. |
| 153 | */ |
| 154 | static const struct mtk_chip_config mtk_default_chip_info = { |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 155 | .sample_sel = 0, |
Mason Zhang | f84d866 | 2021-07-13 19:40:49 +0800 | [diff] [blame] | 156 | .tick_delay = 0, |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | static const struct of_device_id mtk_spi_of_match[] = { |
Leilk Liu | 15bcdefd | 2015-12-31 10:59:01 +0800 | [diff] [blame] | 160 | { .compatible = "mediatek,mt2701-spi", |
| 161 | .data = (void *)&mtk_common_compat, |
| 162 | }, |
leilk.liu@mediatek.com | b6b1f2d | 2017-06-20 16:21:07 +0800 | [diff] [blame] | 163 | { .compatible = "mediatek,mt2712-spi", |
| 164 | .data = (void *)&mt2712_compat, |
| 165 | }, |
Leilk Liu | 4eaf6f7 | 2015-12-31 10:59:00 +0800 | [diff] [blame] | 166 | { .compatible = "mediatek,mt6589-spi", |
| 167 | .data = (void *)&mtk_common_compat, |
| 168 | }, |
luhua.xu | 2c231e0 | 2019-09-11 05:55:30 -0400 | [diff] [blame] | 169 | { .compatible = "mediatek,mt6765-spi", |
| 170 | .data = (void *)&mt6765_compat, |
| 171 | }, |
Leilk Liu | fc4f226 | 2017-06-12 09:24:40 +0800 | [diff] [blame] | 172 | { .compatible = "mediatek,mt7622-spi", |
| 173 | .data = (void *)&mt7622_compat, |
| 174 | }, |
Leilk Liu | 942779c | 2018-11-20 16:41:08 +0800 | [diff] [blame] | 175 | { .compatible = "mediatek,mt7629-spi", |
| 176 | .data = (void *)&mt7622_compat, |
| 177 | }, |
Leilk Liu | 4eaf6f7 | 2015-12-31 10:59:00 +0800 | [diff] [blame] | 178 | { .compatible = "mediatek,mt8135-spi", |
| 179 | .data = (void *)&mtk_common_compat, |
| 180 | }, |
| 181 | { .compatible = "mediatek,mt8173-spi", |
| 182 | .data = (void *)&mt8173_compat, |
| 183 | }, |
Leilk Liu | b654aa6 | 2018-11-01 14:02:19 +0800 | [diff] [blame] | 184 | { .compatible = "mediatek,mt8183-spi", |
| 185 | .data = (void *)&mt8183_compat, |
| 186 | }, |
leilk.liu | 8cf125c | 2020-07-21 20:24:36 +0800 | [diff] [blame] | 187 | { .compatible = "mediatek,mt8192-spi", |
| 188 | .data = (void *)&mt6765_compat, |
| 189 | }, |
Mason Zhang | 162a31e | 2021-06-29 18:08:15 +0800 | [diff] [blame] | 190 | { .compatible = "mediatek,mt6893-spi", |
| 191 | .data = (void *)&mt6893_compat, |
| 192 | }, |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 193 | {} |
| 194 | }; |
| 195 | MODULE_DEVICE_TABLE(of, mtk_spi_of_match); |
| 196 | |
| 197 | static void mtk_spi_reset(struct mtk_spi *mdata) |
| 198 | { |
| 199 | u32 reg_val; |
| 200 | |
| 201 | /* set the software reset bit in SPI_CMD_REG. */ |
| 202 | reg_val = readl(mdata->base + SPI_CMD_REG); |
| 203 | reg_val |= SPI_CMD_RST; |
| 204 | writel(reg_val, mdata->base + SPI_CMD_REG); |
| 205 | |
| 206 | reg_val = readl(mdata->base + SPI_CMD_REG); |
| 207 | reg_val &= ~SPI_CMD_RST; |
| 208 | writel(reg_val, mdata->base + SPI_CMD_REG); |
| 209 | } |
| 210 | |
Mason Zhang | 04e6bb0 | 2021-08-04 21:37:47 +0800 | [diff] [blame] | 211 | static int mtk_spi_set_hw_cs_timing(struct spi_device *spi) |
| 212 | { |
| 213 | struct mtk_spi *mdata = spi_master_get_devdata(spi->master); |
| 214 | struct spi_delay *cs_setup = &spi->cs_setup; |
| 215 | struct spi_delay *cs_hold = &spi->cs_hold; |
| 216 | struct spi_delay *cs_inactive = &spi->cs_inactive; |
Mason Zhang | 5c842e5 | 2021-08-09 13:59:12 +0800 | [diff] [blame] | 217 | u32 setup, hold, inactive; |
Mason Zhang | 04e6bb0 | 2021-08-04 21:37:47 +0800 | [diff] [blame] | 218 | u32 reg_val; |
| 219 | int delay; |
| 220 | |
| 221 | delay = spi_delay_to_ns(cs_setup, NULL); |
| 222 | if (delay < 0) |
| 223 | return delay; |
| 224 | setup = (delay * DIV_ROUND_UP(mdata->spi_clk_hz, 1000000)) / 1000; |
| 225 | |
| 226 | delay = spi_delay_to_ns(cs_hold, NULL); |
| 227 | if (delay < 0) |
| 228 | return delay; |
| 229 | hold = (delay * DIV_ROUND_UP(mdata->spi_clk_hz, 1000000)) / 1000; |
| 230 | |
| 231 | delay = spi_delay_to_ns(cs_inactive, NULL); |
| 232 | if (delay < 0) |
| 233 | return delay; |
| 234 | inactive = (delay * DIV_ROUND_UP(mdata->spi_clk_hz, 1000000)) / 1000; |
| 235 | |
Dafna Hirschfeld | 3672bb8 | 2021-10-01 17:21:53 +0200 | [diff] [blame^] | 236 | if (hold || setup) { |
| 237 | reg_val = readl(mdata->base + SPI_CFG0_REG); |
| 238 | if (mdata->dev_comp->enhance_timing) { |
| 239 | if (hold) { |
| 240 | hold = min_t(u32, hold, 0x10000); |
| 241 | reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_HOLD_OFFSET); |
| 242 | reg_val |= (((hold - 1) & 0xffff) |
| 243 | << SPI_ADJUST_CFG0_CS_HOLD_OFFSET); |
| 244 | } |
| 245 | if (setup) { |
| 246 | setup = min_t(u32, setup, 0x10000); |
| 247 | reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_SETUP_OFFSET); |
| 248 | reg_val |= (((setup - 1) & 0xffff) |
| 249 | << SPI_ADJUST_CFG0_CS_SETUP_OFFSET); |
| 250 | } |
| 251 | } else { |
| 252 | if (hold) { |
| 253 | hold = min_t(u32, hold, 0x100); |
| 254 | reg_val &= ~(0xff << SPI_CFG0_CS_HOLD_OFFSET); |
| 255 | reg_val |= (((hold - 1) & 0xff) << SPI_CFG0_CS_HOLD_OFFSET); |
| 256 | } |
| 257 | if (setup) { |
| 258 | setup = min_t(u32, setup, 0x100); |
| 259 | reg_val &= ~(0xff << SPI_CFG0_CS_SETUP_OFFSET); |
| 260 | reg_val |= (((setup - 1) & 0xff) |
| 261 | << SPI_CFG0_CS_SETUP_OFFSET); |
| 262 | } |
| 263 | } |
| 264 | writel(reg_val, mdata->base + SPI_CFG0_REG); |
Mason Zhang | 04e6bb0 | 2021-08-04 21:37:47 +0800 | [diff] [blame] | 265 | } |
Mason Zhang | 04e6bb0 | 2021-08-04 21:37:47 +0800 | [diff] [blame] | 266 | |
Dafna Hirschfeld | 3672bb8 | 2021-10-01 17:21:53 +0200 | [diff] [blame^] | 267 | if (inactive) { |
| 268 | inactive = min_t(u32, inactive, 0x100); |
| 269 | reg_val = readl(mdata->base + SPI_CFG1_REG); |
| 270 | reg_val &= ~SPI_CFG1_CS_IDLE_MASK; |
| 271 | reg_val |= (((inactive - 1) & 0xff) << SPI_CFG1_CS_IDLE_OFFSET); |
| 272 | writel(reg_val, mdata->base + SPI_CFG1_REG); |
| 273 | } |
Mason Zhang | 04e6bb0 | 2021-08-04 21:37:47 +0800 | [diff] [blame] | 274 | |
| 275 | return 0; |
| 276 | } |
| 277 | |
Leilk Liu | 79b5d3f | 2015-10-26 16:09:41 +0800 | [diff] [blame] | 278 | static int mtk_spi_prepare_message(struct spi_master *master, |
| 279 | struct spi_message *msg) |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 280 | { |
Leilk Liu | 79b5d3f | 2015-10-26 16:09:41 +0800 | [diff] [blame] | 281 | u16 cpha, cpol; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 282 | u32 reg_val; |
Leilk Liu | 79b5d3f | 2015-10-26 16:09:41 +0800 | [diff] [blame] | 283 | struct spi_device *spi = msg->spi; |
Leilk Liu | 58a984c7 | 2015-10-26 16:09:43 +0800 | [diff] [blame] | 284 | struct mtk_chip_config *chip_config = spi->controller_data; |
Leilk Liu | 79b5d3f | 2015-10-26 16:09:41 +0800 | [diff] [blame] | 285 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 286 | |
| 287 | cpha = spi->mode & SPI_CPHA ? 1 : 0; |
| 288 | cpol = spi->mode & SPI_CPOL ? 1 : 0; |
| 289 | |
Leilk Liu | 79b5d3f | 2015-10-26 16:09:41 +0800 | [diff] [blame] | 290 | reg_val = readl(mdata->base + SPI_CMD_REG); |
| 291 | if (cpha) |
| 292 | reg_val |= SPI_CMD_CPHA; |
| 293 | else |
| 294 | reg_val &= ~SPI_CMD_CPHA; |
| 295 | if (cpol) |
| 296 | reg_val |= SPI_CMD_CPOL; |
| 297 | else |
| 298 | reg_val &= ~SPI_CMD_CPOL; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 299 | |
| 300 | /* set the mlsbx and mlsbtx */ |
Leilk Liu | 3e582c6 | 2019-06-05 11:07:04 +0800 | [diff] [blame] | 301 | if (spi->mode & SPI_LSB_FIRST) { |
Leilk Liu | a71d6ea | 2015-08-20 17:19:08 +0800 | [diff] [blame] | 302 | reg_val &= ~SPI_CMD_TXMSBF; |
Leilk Liu | a71d6ea | 2015-08-20 17:19:08 +0800 | [diff] [blame] | 303 | reg_val &= ~SPI_CMD_RXMSBF; |
Leilk Liu | 3e582c6 | 2019-06-05 11:07:04 +0800 | [diff] [blame] | 304 | } else { |
| 305 | reg_val |= SPI_CMD_TXMSBF; |
| 306 | reg_val |= SPI_CMD_RXMSBF; |
| 307 | } |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 308 | |
| 309 | /* set the tx/rx endian */ |
Leilk Liu | 44f636d | 2015-08-20 17:19:06 +0800 | [diff] [blame] | 310 | #ifdef __LITTLE_ENDIAN |
| 311 | reg_val &= ~SPI_CMD_TX_ENDIAN; |
| 312 | reg_val &= ~SPI_CMD_RX_ENDIAN; |
| 313 | #else |
| 314 | reg_val |= SPI_CMD_TX_ENDIAN; |
| 315 | reg_val |= SPI_CMD_RX_ENDIAN; |
| 316 | #endif |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 317 | |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 318 | if (mdata->dev_comp->enhance_timing) { |
Luhua Xu | ae7c2d3 | 2019-11-18 12:57:16 +0800 | [diff] [blame] | 319 | /* set CS polarity */ |
| 320 | if (spi->mode & SPI_CS_HIGH) |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 321 | reg_val |= SPI_CMD_CS_POL; |
| 322 | else |
| 323 | reg_val &= ~SPI_CMD_CS_POL; |
Luhua Xu | ae7c2d3 | 2019-11-18 12:57:16 +0800 | [diff] [blame] | 324 | |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 325 | if (chip_config->sample_sel) |
| 326 | reg_val |= SPI_CMD_SAMPLE_SEL; |
| 327 | else |
| 328 | reg_val &= ~SPI_CMD_SAMPLE_SEL; |
| 329 | } |
| 330 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 331 | /* set finish and pause interrupt always enable */ |
Leilk Liu | 1529332 | 2015-08-27 21:09:04 +0800 | [diff] [blame] | 332 | reg_val |= SPI_CMD_FINISH_IE | SPI_CMD_PAUSE_IE; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 333 | |
| 334 | /* disable dma mode */ |
| 335 | reg_val &= ~(SPI_CMD_TX_DMA | SPI_CMD_RX_DMA); |
| 336 | |
| 337 | /* disable deassert mode */ |
| 338 | reg_val &= ~SPI_CMD_DEASSERT; |
| 339 | |
| 340 | writel(reg_val, mdata->base + SPI_CMD_REG); |
| 341 | |
| 342 | /* pad select */ |
| 343 | if (mdata->dev_comp->need_pad_sel) |
Leilk Liu | 3745760 | 2015-10-26 16:09:44 +0800 | [diff] [blame] | 344 | writel(mdata->pad_sel[spi->chip_select], |
| 345 | mdata->base + SPI_PAD_SEL_REG); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 346 | |
Mason Zhang | f84d866 | 2021-07-13 19:40:49 +0800 | [diff] [blame] | 347 | /* tick delay */ |
| 348 | reg_val = readl(mdata->base + SPI_CFG1_REG); |
| 349 | reg_val &= ~SPI_CFG1_GET_TICK_DLY_MASK; |
| 350 | reg_val |= ((chip_config->tick_delay & 0x7) |
| 351 | << SPI_CFG1_GET_TICK_DLY_OFFSET); |
| 352 | writel(reg_val, mdata->base + SPI_CFG1_REG); |
| 353 | |
Mason Zhang | 04e6bb0 | 2021-08-04 21:37:47 +0800 | [diff] [blame] | 354 | /* set hw cs timing */ |
| 355 | mtk_spi_set_hw_cs_timing(spi); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | static void mtk_spi_set_cs(struct spi_device *spi, bool enable) |
| 360 | { |
| 361 | u32 reg_val; |
| 362 | struct mtk_spi *mdata = spi_master_get_devdata(spi->master); |
| 363 | |
Luhua Xu | ae7c2d3 | 2019-11-18 12:57:16 +0800 | [diff] [blame] | 364 | if (spi->mode & SPI_CS_HIGH) |
| 365 | enable = !enable; |
| 366 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 367 | reg_val = readl(mdata->base + SPI_CMD_REG); |
Leilk Liu | 6583d20 | 2015-09-07 19:37:57 +0800 | [diff] [blame] | 368 | if (!enable) { |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 369 | reg_val |= SPI_CMD_PAUSE_EN; |
Leilk Liu | 6583d20 | 2015-09-07 19:37:57 +0800 | [diff] [blame] | 370 | writel(reg_val, mdata->base + SPI_CMD_REG); |
| 371 | } else { |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 372 | reg_val &= ~SPI_CMD_PAUSE_EN; |
Leilk Liu | 6583d20 | 2015-09-07 19:37:57 +0800 | [diff] [blame] | 373 | writel(reg_val, mdata->base + SPI_CMD_REG); |
| 374 | mdata->state = MTK_SPI_IDLE; |
| 375 | mtk_spi_reset(mdata); |
| 376 | } |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static void mtk_spi_prepare_transfer(struct spi_master *master, |
| 380 | struct spi_transfer *xfer) |
| 381 | { |
Mason Zhang | 162a31e | 2021-06-29 18:08:15 +0800 | [diff] [blame] | 382 | u32 div, sck_time, reg_val; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 383 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 384 | |
Mason Zhang | 162a31e | 2021-06-29 18:08:15 +0800 | [diff] [blame] | 385 | if (xfer->speed_hz < mdata->spi_clk_hz / 2) |
| 386 | div = DIV_ROUND_UP(mdata->spi_clk_hz, xfer->speed_hz); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 387 | else |
| 388 | div = 1; |
| 389 | |
Leilk Liu | 2ce0acf | 2015-08-24 11:45:18 +0800 | [diff] [blame] | 390 | sck_time = (div + 1) / 2; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 391 | |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 392 | if (mdata->dev_comp->enhance_timing) { |
leilk.liu | 9f6e7e8 | 2021-02-07 11:09:53 +0800 | [diff] [blame] | 393 | reg_val = readl(mdata->base + SPI_CFG2_REG); |
| 394 | reg_val &= ~(0xffff << SPI_CFG2_SCK_HIGH_OFFSET); |
| 395 | reg_val |= (((sck_time - 1) & 0xffff) |
leilk.liu | 44b37eb | 2020-07-01 17:00:20 +0800 | [diff] [blame] | 396 | << SPI_CFG2_SCK_HIGH_OFFSET); |
leilk.liu | 9f6e7e8 | 2021-02-07 11:09:53 +0800 | [diff] [blame] | 397 | reg_val &= ~(0xffff << SPI_CFG2_SCK_LOW_OFFSET); |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 398 | reg_val |= (((sck_time - 1) & 0xffff) |
leilk.liu | 44b37eb | 2020-07-01 17:00:20 +0800 | [diff] [blame] | 399 | << SPI_CFG2_SCK_LOW_OFFSET); |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 400 | writel(reg_val, mdata->base + SPI_CFG2_REG); |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 401 | } else { |
leilk.liu | 9f6e7e8 | 2021-02-07 11:09:53 +0800 | [diff] [blame] | 402 | reg_val = readl(mdata->base + SPI_CFG0_REG); |
| 403 | reg_val &= ~(0xff << SPI_CFG0_SCK_HIGH_OFFSET); |
| 404 | reg_val |= (((sck_time - 1) & 0xff) |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 405 | << SPI_CFG0_SCK_HIGH_OFFSET); |
leilk.liu | 9f6e7e8 | 2021-02-07 11:09:53 +0800 | [diff] [blame] | 406 | reg_val &= ~(0xff << SPI_CFG0_SCK_LOW_OFFSET); |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 407 | reg_val |= (((sck_time - 1) & 0xff) << SPI_CFG0_SCK_LOW_OFFSET); |
Leilk Liu | 058fe49 | 2017-06-12 09:24:39 +0800 | [diff] [blame] | 408 | writel(reg_val, mdata->base + SPI_CFG0_REG); |
| 409 | } |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static void mtk_spi_setup_packet(struct spi_master *master) |
| 413 | { |
| 414 | u32 packet_size, packet_loop, reg_val; |
| 415 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 416 | |
Leilk Liu | 50f8fec | 2015-08-24 11:45:16 +0800 | [diff] [blame] | 417 | packet_size = min_t(u32, mdata->xfer_len, MTK_SPI_PACKET_SIZE); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 418 | packet_loop = mdata->xfer_len / packet_size; |
| 419 | |
| 420 | reg_val = readl(mdata->base + SPI_CFG1_REG); |
Leilk Liu | 50f8fec | 2015-08-24 11:45:16 +0800 | [diff] [blame] | 421 | reg_val &= ~(SPI_CFG1_PACKET_LENGTH_MASK | SPI_CFG1_PACKET_LOOP_MASK); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 422 | reg_val |= (packet_size - 1) << SPI_CFG1_PACKET_LENGTH_OFFSET; |
| 423 | reg_val |= (packet_loop - 1) << SPI_CFG1_PACKET_LOOP_OFFSET; |
| 424 | writel(reg_val, mdata->base + SPI_CFG1_REG); |
| 425 | } |
| 426 | |
| 427 | static void mtk_spi_enable_transfer(struct spi_master *master) |
| 428 | { |
Leilk Liu | 50f8fec | 2015-08-24 11:45:16 +0800 | [diff] [blame] | 429 | u32 cmd; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 430 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 431 | |
| 432 | cmd = readl(mdata->base + SPI_CMD_REG); |
| 433 | if (mdata->state == MTK_SPI_IDLE) |
Leilk Liu | a71d6ea | 2015-08-20 17:19:08 +0800 | [diff] [blame] | 434 | cmd |= SPI_CMD_ACT; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 435 | else |
Leilk Liu | a71d6ea | 2015-08-20 17:19:08 +0800 | [diff] [blame] | 436 | cmd |= SPI_CMD_RESUME; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 437 | writel(cmd, mdata->base + SPI_CMD_REG); |
| 438 | } |
| 439 | |
Leilk Liu | 50f8fec | 2015-08-24 11:45:16 +0800 | [diff] [blame] | 440 | static int mtk_spi_get_mult_delta(u32 xfer_len) |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 441 | { |
Leilk Liu | 50f8fec | 2015-08-24 11:45:16 +0800 | [diff] [blame] | 442 | u32 mult_delta; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 443 | |
| 444 | if (xfer_len > MTK_SPI_PACKET_SIZE) |
| 445 | mult_delta = xfer_len % MTK_SPI_PACKET_SIZE; |
| 446 | else |
| 447 | mult_delta = 0; |
| 448 | |
| 449 | return mult_delta; |
| 450 | } |
| 451 | |
| 452 | static void mtk_spi_update_mdata_len(struct spi_master *master) |
| 453 | { |
| 454 | int mult_delta; |
| 455 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 456 | |
| 457 | if (mdata->tx_sgl_len && mdata->rx_sgl_len) { |
| 458 | if (mdata->tx_sgl_len > mdata->rx_sgl_len) { |
| 459 | mult_delta = mtk_spi_get_mult_delta(mdata->rx_sgl_len); |
| 460 | mdata->xfer_len = mdata->rx_sgl_len - mult_delta; |
| 461 | mdata->rx_sgl_len = mult_delta; |
| 462 | mdata->tx_sgl_len -= mdata->xfer_len; |
| 463 | } else { |
| 464 | mult_delta = mtk_spi_get_mult_delta(mdata->tx_sgl_len); |
| 465 | mdata->xfer_len = mdata->tx_sgl_len - mult_delta; |
| 466 | mdata->tx_sgl_len = mult_delta; |
| 467 | mdata->rx_sgl_len -= mdata->xfer_len; |
| 468 | } |
| 469 | } else if (mdata->tx_sgl_len) { |
| 470 | mult_delta = mtk_spi_get_mult_delta(mdata->tx_sgl_len); |
| 471 | mdata->xfer_len = mdata->tx_sgl_len - mult_delta; |
| 472 | mdata->tx_sgl_len = mult_delta; |
| 473 | } else if (mdata->rx_sgl_len) { |
| 474 | mult_delta = mtk_spi_get_mult_delta(mdata->rx_sgl_len); |
| 475 | mdata->xfer_len = mdata->rx_sgl_len - mult_delta; |
| 476 | mdata->rx_sgl_len = mult_delta; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | static void mtk_spi_setup_dma_addr(struct spi_master *master, |
| 481 | struct spi_transfer *xfer) |
| 482 | { |
| 483 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 484 | |
luhua.xu | fdeae8f | 2019-09-11 05:55:31 -0400 | [diff] [blame] | 485 | if (mdata->tx_sgl) { |
| 486 | writel((u32)(xfer->tx_dma & MTK_SPI_32BITS_MASK), |
| 487 | mdata->base + SPI_TX_SRC_REG); |
| 488 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
| 489 | if (mdata->dev_comp->dma_ext) |
| 490 | writel((u32)(xfer->tx_dma >> 32), |
| 491 | mdata->base + SPI_TX_SRC_REG_64); |
| 492 | #endif |
| 493 | } |
| 494 | |
| 495 | if (mdata->rx_sgl) { |
| 496 | writel((u32)(xfer->rx_dma & MTK_SPI_32BITS_MASK), |
| 497 | mdata->base + SPI_RX_DST_REG); |
| 498 | #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT |
| 499 | if (mdata->dev_comp->dma_ext) |
| 500 | writel((u32)(xfer->rx_dma >> 32), |
| 501 | mdata->base + SPI_RX_DST_REG_64); |
| 502 | #endif |
| 503 | } |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static int mtk_spi_fifo_transfer(struct spi_master *master, |
| 507 | struct spi_device *spi, |
| 508 | struct spi_transfer *xfer) |
| 509 | { |
Nicolas Boichat | de327e4 | 2015-12-27 18:17:06 +0800 | [diff] [blame] | 510 | int cnt, remainder; |
| 511 | u32 reg_val; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 512 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 513 | |
| 514 | mdata->cur_transfer = xfer; |
Daniel Kurtz | 1ce2486 | 2017-01-27 00:21:54 +0800 | [diff] [blame] | 515 | mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, xfer->len); |
Peter Shih | 00bca73 | 2018-09-10 11:54:21 +0800 | [diff] [blame] | 516 | mdata->num_xfered = 0; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 517 | mtk_spi_prepare_transfer(master, xfer); |
| 518 | mtk_spi_setup_packet(master); |
| 519 | |
Guenter Roeck | 0d5c395 | 2021-08-01 20:00:23 -0700 | [diff] [blame] | 520 | if (xfer->tx_buf) { |
| 521 | cnt = xfer->len / 4; |
Peter Hess | 3a70dd2 | 2021-07-06 14:16:09 +0200 | [diff] [blame] | 522 | iowrite32_rep(mdata->base + SPI_TX_DATA_REG, xfer->tx_buf, cnt); |
Guenter Roeck | 0d5c395 | 2021-08-01 20:00:23 -0700 | [diff] [blame] | 523 | remainder = xfer->len % 4; |
| 524 | if (remainder > 0) { |
| 525 | reg_val = 0; |
Peter Hess | 3a70dd2 | 2021-07-06 14:16:09 +0200 | [diff] [blame] | 526 | memcpy(®_val, xfer->tx_buf + (cnt * 4), remainder); |
| 527 | writel(reg_val, mdata->base + SPI_TX_DATA_REG); |
| 528 | } |
Nicolas Boichat | de327e4 | 2015-12-27 18:17:06 +0800 | [diff] [blame] | 529 | } |
| 530 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 531 | mtk_spi_enable_transfer(master); |
| 532 | |
| 533 | return 1; |
| 534 | } |
| 535 | |
| 536 | static int mtk_spi_dma_transfer(struct spi_master *master, |
| 537 | struct spi_device *spi, |
| 538 | struct spi_transfer *xfer) |
| 539 | { |
| 540 | int cmd; |
| 541 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 542 | |
| 543 | mdata->tx_sgl = NULL; |
| 544 | mdata->rx_sgl = NULL; |
| 545 | mdata->tx_sgl_len = 0; |
| 546 | mdata->rx_sgl_len = 0; |
| 547 | mdata->cur_transfer = xfer; |
Peter Shih | 00bca73 | 2018-09-10 11:54:21 +0800 | [diff] [blame] | 548 | mdata->num_xfered = 0; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 549 | |
| 550 | mtk_spi_prepare_transfer(master, xfer); |
| 551 | |
| 552 | cmd = readl(mdata->base + SPI_CMD_REG); |
| 553 | if (xfer->tx_buf) |
| 554 | cmd |= SPI_CMD_TX_DMA; |
| 555 | if (xfer->rx_buf) |
| 556 | cmd |= SPI_CMD_RX_DMA; |
| 557 | writel(cmd, mdata->base + SPI_CMD_REG); |
| 558 | |
| 559 | if (xfer->tx_buf) |
| 560 | mdata->tx_sgl = xfer->tx_sg.sgl; |
| 561 | if (xfer->rx_buf) |
| 562 | mdata->rx_sgl = xfer->rx_sg.sgl; |
| 563 | |
| 564 | if (mdata->tx_sgl) { |
| 565 | xfer->tx_dma = sg_dma_address(mdata->tx_sgl); |
| 566 | mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl); |
| 567 | } |
| 568 | if (mdata->rx_sgl) { |
| 569 | xfer->rx_dma = sg_dma_address(mdata->rx_sgl); |
| 570 | mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl); |
| 571 | } |
| 572 | |
| 573 | mtk_spi_update_mdata_len(master); |
| 574 | mtk_spi_setup_packet(master); |
| 575 | mtk_spi_setup_dma_addr(master, xfer); |
| 576 | mtk_spi_enable_transfer(master); |
| 577 | |
| 578 | return 1; |
| 579 | } |
| 580 | |
| 581 | static int mtk_spi_transfer_one(struct spi_master *master, |
| 582 | struct spi_device *spi, |
| 583 | struct spi_transfer *xfer) |
| 584 | { |
| 585 | if (master->can_dma(master, spi, xfer)) |
| 586 | return mtk_spi_dma_transfer(master, spi, xfer); |
| 587 | else |
| 588 | return mtk_spi_fifo_transfer(master, spi, xfer); |
| 589 | } |
| 590 | |
| 591 | static bool mtk_spi_can_dma(struct spi_master *master, |
| 592 | struct spi_device *spi, |
| 593 | struct spi_transfer *xfer) |
| 594 | { |
Daniel Kurtz | 1ce2486 | 2017-01-27 00:21:54 +0800 | [diff] [blame] | 595 | /* Buffers for DMA transactions must be 4-byte aligned */ |
| 596 | return (xfer->len > MTK_SPI_MAX_FIFO_SIZE && |
| 597 | (unsigned long)xfer->tx_buf % 4 == 0 && |
| 598 | (unsigned long)xfer->rx_buf % 4 == 0); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 599 | } |
| 600 | |
Leilk Liu | 58a984c7 | 2015-10-26 16:09:43 +0800 | [diff] [blame] | 601 | static int mtk_spi_setup(struct spi_device *spi) |
| 602 | { |
| 603 | struct mtk_spi *mdata = spi_master_get_devdata(spi->master); |
| 604 | |
| 605 | if (!spi->controller_data) |
| 606 | spi->controller_data = (void *)&mtk_default_chip_info; |
| 607 | |
Nicolas Boichat | 98c8dcc | 2015-11-09 12:14:51 +0800 | [diff] [blame] | 608 | if (mdata->dev_comp->need_pad_sel && gpio_is_valid(spi->cs_gpio)) |
Leilk Liu | 3745760 | 2015-10-26 16:09:44 +0800 | [diff] [blame] | 609 | gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH)); |
| 610 | |
Leilk Liu | 58a984c7 | 2015-10-26 16:09:43 +0800 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 614 | static irqreturn_t mtk_spi_interrupt(int irq, void *dev_id) |
| 615 | { |
Peter Shih | 00bca73 | 2018-09-10 11:54:21 +0800 | [diff] [blame] | 616 | u32 cmd, reg_val, cnt, remainder, len; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 617 | struct spi_master *master = dev_id; |
| 618 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 619 | struct spi_transfer *trans = mdata->cur_transfer; |
| 620 | |
| 621 | reg_val = readl(mdata->base + SPI_STATUS0_REG); |
Leilk Liu | 50f8fec | 2015-08-24 11:45:16 +0800 | [diff] [blame] | 622 | if (reg_val & MTK_SPI_PAUSE_INT_STATUS) |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 623 | mdata->state = MTK_SPI_PAUSED; |
| 624 | else |
| 625 | mdata->state = MTK_SPI_IDLE; |
| 626 | |
| 627 | if (!master->can_dma(master, master->cur_msg->spi, trans)) { |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 628 | if (trans->rx_buf) { |
Nicolas Boichat | de327e4 | 2015-12-27 18:17:06 +0800 | [diff] [blame] | 629 | cnt = mdata->xfer_len / 4; |
Leilk Liu | 44f636d | 2015-08-20 17:19:06 +0800 | [diff] [blame] | 630 | ioread32_rep(mdata->base + SPI_RX_DATA_REG, |
Peter Shih | 00bca73 | 2018-09-10 11:54:21 +0800 | [diff] [blame] | 631 | trans->rx_buf + mdata->num_xfered, cnt); |
Nicolas Boichat | de327e4 | 2015-12-27 18:17:06 +0800 | [diff] [blame] | 632 | remainder = mdata->xfer_len % 4; |
| 633 | if (remainder > 0) { |
| 634 | reg_val = readl(mdata->base + SPI_RX_DATA_REG); |
Peter Shih | 00bca73 | 2018-09-10 11:54:21 +0800 | [diff] [blame] | 635 | memcpy(trans->rx_buf + |
| 636 | mdata->num_xfered + |
| 637 | (cnt * 4), |
| 638 | ®_val, |
| 639 | remainder); |
Nicolas Boichat | de327e4 | 2015-12-27 18:17:06 +0800 | [diff] [blame] | 640 | } |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 641 | } |
Daniel Kurtz | 1ce2486 | 2017-01-27 00:21:54 +0800 | [diff] [blame] | 642 | |
Peter Shih | 00bca73 | 2018-09-10 11:54:21 +0800 | [diff] [blame] | 643 | mdata->num_xfered += mdata->xfer_len; |
| 644 | if (mdata->num_xfered == trans->len) { |
Daniel Kurtz | 1ce2486 | 2017-01-27 00:21:54 +0800 | [diff] [blame] | 645 | spi_finalize_current_transfer(master); |
| 646 | return IRQ_HANDLED; |
| 647 | } |
| 648 | |
Peter Shih | 00bca73 | 2018-09-10 11:54:21 +0800 | [diff] [blame] | 649 | len = trans->len - mdata->num_xfered; |
| 650 | mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, len); |
Daniel Kurtz | 1ce2486 | 2017-01-27 00:21:54 +0800 | [diff] [blame] | 651 | mtk_spi_setup_packet(master); |
| 652 | |
Leilk Liu | a4d8f64 | 2018-10-31 16:49:16 +0800 | [diff] [blame] | 653 | cnt = mdata->xfer_len / 4; |
Peter Shih | 00bca73 | 2018-09-10 11:54:21 +0800 | [diff] [blame] | 654 | iowrite32_rep(mdata->base + SPI_TX_DATA_REG, |
| 655 | trans->tx_buf + mdata->num_xfered, cnt); |
Daniel Kurtz | 1ce2486 | 2017-01-27 00:21:54 +0800 | [diff] [blame] | 656 | |
Leilk Liu | a4d8f64 | 2018-10-31 16:49:16 +0800 | [diff] [blame] | 657 | remainder = mdata->xfer_len % 4; |
Daniel Kurtz | 1ce2486 | 2017-01-27 00:21:54 +0800 | [diff] [blame] | 658 | if (remainder > 0) { |
| 659 | reg_val = 0; |
Peter Shih | 00bca73 | 2018-09-10 11:54:21 +0800 | [diff] [blame] | 660 | memcpy(®_val, |
| 661 | trans->tx_buf + (cnt * 4) + mdata->num_xfered, |
| 662 | remainder); |
Daniel Kurtz | 1ce2486 | 2017-01-27 00:21:54 +0800 | [diff] [blame] | 663 | writel(reg_val, mdata->base + SPI_TX_DATA_REG); |
| 664 | } |
| 665 | |
| 666 | mtk_spi_enable_transfer(master); |
| 667 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 668 | return IRQ_HANDLED; |
| 669 | } |
| 670 | |
| 671 | if (mdata->tx_sgl) |
| 672 | trans->tx_dma += mdata->xfer_len; |
| 673 | if (mdata->rx_sgl) |
| 674 | trans->rx_dma += mdata->xfer_len; |
| 675 | |
| 676 | if (mdata->tx_sgl && (mdata->tx_sgl_len == 0)) { |
| 677 | mdata->tx_sgl = sg_next(mdata->tx_sgl); |
| 678 | if (mdata->tx_sgl) { |
| 679 | trans->tx_dma = sg_dma_address(mdata->tx_sgl); |
| 680 | mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl); |
| 681 | } |
| 682 | } |
| 683 | if (mdata->rx_sgl && (mdata->rx_sgl_len == 0)) { |
| 684 | mdata->rx_sgl = sg_next(mdata->rx_sgl); |
| 685 | if (mdata->rx_sgl) { |
| 686 | trans->rx_dma = sg_dma_address(mdata->rx_sgl); |
| 687 | mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | if (!mdata->tx_sgl && !mdata->rx_sgl) { |
| 692 | /* spi disable dma */ |
| 693 | cmd = readl(mdata->base + SPI_CMD_REG); |
| 694 | cmd &= ~SPI_CMD_TX_DMA; |
| 695 | cmd &= ~SPI_CMD_RX_DMA; |
| 696 | writel(cmd, mdata->base + SPI_CMD_REG); |
| 697 | |
| 698 | spi_finalize_current_transfer(master); |
| 699 | return IRQ_HANDLED; |
| 700 | } |
| 701 | |
| 702 | mtk_spi_update_mdata_len(master); |
| 703 | mtk_spi_setup_packet(master); |
| 704 | mtk_spi_setup_dma_addr(master, trans); |
| 705 | mtk_spi_enable_transfer(master); |
| 706 | |
| 707 | return IRQ_HANDLED; |
| 708 | } |
| 709 | |
| 710 | static int mtk_spi_probe(struct platform_device *pdev) |
| 711 | { |
| 712 | struct spi_master *master; |
| 713 | struct mtk_spi *mdata; |
| 714 | const struct of_device_id *of_id; |
luhua.xu | fdeae8f | 2019-09-11 05:55:31 -0400 | [diff] [blame] | 715 | int i, irq, ret, addr_bits; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 716 | |
| 717 | master = spi_alloc_master(&pdev->dev, sizeof(*mdata)); |
| 718 | if (!master) { |
| 719 | dev_err(&pdev->dev, "failed to alloc spi master\n"); |
| 720 | return -ENOMEM; |
| 721 | } |
| 722 | |
| 723 | master->auto_runtime_pm = true; |
| 724 | master->dev.of_node = pdev->dev.of_node; |
Leilk Liu | 3e582c6 | 2019-06-05 11:07:04 +0800 | [diff] [blame] | 725 | master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 726 | |
| 727 | master->set_cs = mtk_spi_set_cs; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 728 | master->prepare_message = mtk_spi_prepare_message; |
| 729 | master->transfer_one = mtk_spi_transfer_one; |
| 730 | master->can_dma = mtk_spi_can_dma; |
Leilk Liu | 58a984c7 | 2015-10-26 16:09:43 +0800 | [diff] [blame] | 731 | master->setup = mtk_spi_setup; |
leilk.liu | 9f6e7e8 | 2021-02-07 11:09:53 +0800 | [diff] [blame] | 732 | master->set_cs_timing = mtk_spi_set_hw_cs_timing; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 733 | |
| 734 | of_id = of_match_node(mtk_spi_of_match, pdev->dev.of_node); |
| 735 | if (!of_id) { |
| 736 | dev_err(&pdev->dev, "failed to probe of_node\n"); |
| 737 | ret = -EINVAL; |
| 738 | goto err_put_master; |
| 739 | } |
| 740 | |
| 741 | mdata = spi_master_get_devdata(master); |
| 742 | mdata->dev_comp = of_id->data; |
Luhua Xu | ae7c2d3 | 2019-11-18 12:57:16 +0800 | [diff] [blame] | 743 | |
| 744 | if (mdata->dev_comp->enhance_timing) |
| 745 | master->mode_bits |= SPI_CS_HIGH; |
| 746 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 747 | if (mdata->dev_comp->must_tx) |
| 748 | master->flags = SPI_MASTER_MUST_TX; |
| 749 | |
| 750 | if (mdata->dev_comp->need_pad_sel) { |
Leilk Liu | 3745760 | 2015-10-26 16:09:44 +0800 | [diff] [blame] | 751 | mdata->pad_num = of_property_count_u32_elems( |
| 752 | pdev->dev.of_node, |
| 753 | "mediatek,pad-select"); |
| 754 | if (mdata->pad_num < 0) { |
| 755 | dev_err(&pdev->dev, |
| 756 | "No 'mediatek,pad-select' property\n"); |
| 757 | ret = -EINVAL; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 758 | goto err_put_master; |
| 759 | } |
| 760 | |
Leilk Liu | 3745760 | 2015-10-26 16:09:44 +0800 | [diff] [blame] | 761 | mdata->pad_sel = devm_kmalloc_array(&pdev->dev, mdata->pad_num, |
| 762 | sizeof(u32), GFP_KERNEL); |
| 763 | if (!mdata->pad_sel) { |
| 764 | ret = -ENOMEM; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 765 | goto err_put_master; |
| 766 | } |
Leilk Liu | 3745760 | 2015-10-26 16:09:44 +0800 | [diff] [blame] | 767 | |
| 768 | for (i = 0; i < mdata->pad_num; i++) { |
| 769 | of_property_read_u32_index(pdev->dev.of_node, |
| 770 | "mediatek,pad-select", |
| 771 | i, &mdata->pad_sel[i]); |
| 772 | if (mdata->pad_sel[i] > MT8173_SPI_MAX_PAD_SEL) { |
| 773 | dev_err(&pdev->dev, "wrong pad-sel[%d]: %u\n", |
| 774 | i, mdata->pad_sel[i]); |
| 775 | ret = -EINVAL; |
| 776 | goto err_put_master; |
| 777 | } |
| 778 | } |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | platform_set_drvdata(pdev, master); |
Markus Elfring | 5dd381e | 2019-09-21 14:45:40 +0200 | [diff] [blame] | 782 | mdata->base = devm_platform_ioremap_resource(pdev, 0); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 783 | if (IS_ERR(mdata->base)) { |
| 784 | ret = PTR_ERR(mdata->base); |
| 785 | goto err_put_master; |
| 786 | } |
| 787 | |
| 788 | irq = platform_get_irq(pdev, 0); |
| 789 | if (irq < 0) { |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 790 | ret = irq; |
| 791 | goto err_put_master; |
| 792 | } |
| 793 | |
| 794 | if (!pdev->dev.dma_mask) |
| 795 | pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; |
| 796 | |
| 797 | ret = devm_request_irq(&pdev->dev, irq, mtk_spi_interrupt, |
| 798 | IRQF_TRIGGER_NONE, dev_name(&pdev->dev), master); |
| 799 | if (ret) { |
| 800 | dev_err(&pdev->dev, "failed to register irq (%d)\n", ret); |
| 801 | goto err_put_master; |
| 802 | } |
| 803 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 804 | mdata->parent_clk = devm_clk_get(&pdev->dev, "parent-clk"); |
| 805 | if (IS_ERR(mdata->parent_clk)) { |
| 806 | ret = PTR_ERR(mdata->parent_clk); |
| 807 | dev_err(&pdev->dev, "failed to get parent-clk: %d\n", ret); |
| 808 | goto err_put_master; |
| 809 | } |
| 810 | |
Leilk Liu | adcbcfe | 2015-08-31 21:18:57 +0800 | [diff] [blame] | 811 | mdata->sel_clk = devm_clk_get(&pdev->dev, "sel-clk"); |
| 812 | if (IS_ERR(mdata->sel_clk)) { |
Javier Martinez Canillas | e26d15f | 2015-09-15 14:46:45 +0200 | [diff] [blame] | 813 | ret = PTR_ERR(mdata->sel_clk); |
Leilk Liu | adcbcfe | 2015-08-31 21:18:57 +0800 | [diff] [blame] | 814 | dev_err(&pdev->dev, "failed to get sel-clk: %d\n", ret); |
| 815 | goto err_put_master; |
| 816 | } |
| 817 | |
| 818 | mdata->spi_clk = devm_clk_get(&pdev->dev, "spi-clk"); |
| 819 | if (IS_ERR(mdata->spi_clk)) { |
Javier Martinez Canillas | e26d15f | 2015-09-15 14:46:45 +0200 | [diff] [blame] | 820 | ret = PTR_ERR(mdata->spi_clk); |
Leilk Liu | adcbcfe | 2015-08-31 21:18:57 +0800 | [diff] [blame] | 821 | dev_err(&pdev->dev, "failed to get spi-clk: %d\n", ret); |
| 822 | goto err_put_master; |
| 823 | } |
| 824 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 825 | ret = clk_prepare_enable(mdata->spi_clk); |
| 826 | if (ret < 0) { |
| 827 | dev_err(&pdev->dev, "failed to enable spi_clk (%d)\n", ret); |
| 828 | goto err_put_master; |
| 829 | } |
| 830 | |
Leilk Liu | adcbcfe | 2015-08-31 21:18:57 +0800 | [diff] [blame] | 831 | ret = clk_set_parent(mdata->sel_clk, mdata->parent_clk); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 832 | if (ret < 0) { |
| 833 | dev_err(&pdev->dev, "failed to clk_set_parent (%d)\n", ret); |
Leilk Liu | e38da37 | 2015-11-25 17:50:38 +0800 | [diff] [blame] | 834 | clk_disable_unprepare(mdata->spi_clk); |
| 835 | goto err_put_master; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 836 | } |
| 837 | |
Mason Zhang | 162a31e | 2021-06-29 18:08:15 +0800 | [diff] [blame] | 838 | mdata->spi_clk_hz = clk_get_rate(mdata->spi_clk); |
| 839 | |
| 840 | if (mdata->dev_comp->no_need_unprepare) |
| 841 | clk_disable(mdata->spi_clk); |
| 842 | else |
| 843 | clk_disable_unprepare(mdata->spi_clk); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 844 | |
| 845 | pm_runtime_enable(&pdev->dev); |
| 846 | |
Leilk Liu | 3745760 | 2015-10-26 16:09:44 +0800 | [diff] [blame] | 847 | if (mdata->dev_comp->need_pad_sel) { |
| 848 | if (mdata->pad_num != master->num_chipselect) { |
| 849 | dev_err(&pdev->dev, |
| 850 | "pad_num does not match num_chipselect(%d != %d)\n", |
| 851 | mdata->pad_num, master->num_chipselect); |
| 852 | ret = -EINVAL; |
Leilk Liu | e38da37 | 2015-11-25 17:50:38 +0800 | [diff] [blame] | 853 | goto err_disable_runtime_pm; |
Leilk Liu | 3745760 | 2015-10-26 16:09:44 +0800 | [diff] [blame] | 854 | } |
| 855 | |
Nicolas Boichat | 98c8dcc | 2015-11-09 12:14:51 +0800 | [diff] [blame] | 856 | if (!master->cs_gpios && master->num_chipselect > 1) { |
| 857 | dev_err(&pdev->dev, |
| 858 | "cs_gpios not specified and num_chipselect > 1\n"); |
| 859 | ret = -EINVAL; |
Leilk Liu | e38da37 | 2015-11-25 17:50:38 +0800 | [diff] [blame] | 860 | goto err_disable_runtime_pm; |
Nicolas Boichat | 98c8dcc | 2015-11-09 12:14:51 +0800 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | if (master->cs_gpios) { |
| 864 | for (i = 0; i < master->num_chipselect; i++) { |
| 865 | ret = devm_gpio_request(&pdev->dev, |
| 866 | master->cs_gpios[i], |
| 867 | dev_name(&pdev->dev)); |
| 868 | if (ret) { |
| 869 | dev_err(&pdev->dev, |
| 870 | "can't get CS GPIO %i\n", i); |
Leilk Liu | e38da37 | 2015-11-25 17:50:38 +0800 | [diff] [blame] | 871 | goto err_disable_runtime_pm; |
Nicolas Boichat | 98c8dcc | 2015-11-09 12:14:51 +0800 | [diff] [blame] | 872 | } |
Leilk Liu | 3745760 | 2015-10-26 16:09:44 +0800 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
luhua.xu | fdeae8f | 2019-09-11 05:55:31 -0400 | [diff] [blame] | 877 | if (mdata->dev_comp->dma_ext) |
| 878 | addr_bits = DMA_ADDR_EXT_BITS; |
| 879 | else |
| 880 | addr_bits = DMA_ADDR_DEF_BITS; |
| 881 | ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(addr_bits)); |
| 882 | if (ret) |
| 883 | dev_notice(&pdev->dev, "SPI dma_set_mask(%d) failed, ret:%d\n", |
| 884 | addr_bits, ret); |
| 885 | |
Mason Zhang | c934fec | 2021-07-13 19:42:48 +0800 | [diff] [blame] | 886 | ret = devm_spi_register_master(&pdev->dev, master); |
| 887 | if (ret) { |
| 888 | dev_err(&pdev->dev, "failed to register master (%d)\n", ret); |
| 889 | goto err_disable_runtime_pm; |
| 890 | } |
| 891 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 892 | return 0; |
| 893 | |
Leilk Liu | e38da37 | 2015-11-25 17:50:38 +0800 | [diff] [blame] | 894 | err_disable_runtime_pm: |
| 895 | pm_runtime_disable(&pdev->dev); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 896 | err_put_master: |
| 897 | spi_master_put(master); |
| 898 | |
| 899 | return ret; |
| 900 | } |
| 901 | |
| 902 | static int mtk_spi_remove(struct platform_device *pdev) |
| 903 | { |
| 904 | struct spi_master *master = platform_get_drvdata(pdev); |
| 905 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 906 | |
| 907 | pm_runtime_disable(&pdev->dev); |
| 908 | |
| 909 | mtk_spi_reset(mdata); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 910 | |
Mason Zhang | 162a31e | 2021-06-29 18:08:15 +0800 | [diff] [blame] | 911 | if (mdata->dev_comp->no_need_unprepare) |
| 912 | clk_unprepare(mdata->spi_clk); |
| 913 | |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 914 | return 0; |
| 915 | } |
| 916 | |
| 917 | #ifdef CONFIG_PM_SLEEP |
| 918 | static int mtk_spi_suspend(struct device *dev) |
| 919 | { |
| 920 | int ret; |
| 921 | struct spi_master *master = dev_get_drvdata(dev); |
| 922 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 923 | |
| 924 | ret = spi_master_suspend(master); |
| 925 | if (ret) |
| 926 | return ret; |
| 927 | |
| 928 | if (!pm_runtime_suspended(dev)) |
| 929 | clk_disable_unprepare(mdata->spi_clk); |
| 930 | |
| 931 | return ret; |
| 932 | } |
| 933 | |
| 934 | static int mtk_spi_resume(struct device *dev) |
| 935 | { |
| 936 | int ret; |
| 937 | struct spi_master *master = dev_get_drvdata(dev); |
| 938 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 939 | |
| 940 | if (!pm_runtime_suspended(dev)) { |
| 941 | ret = clk_prepare_enable(mdata->spi_clk); |
Leilk Liu | 13da5a0 | 2015-08-24 11:45:17 +0800 | [diff] [blame] | 942 | if (ret < 0) { |
| 943 | dev_err(dev, "failed to enable spi_clk (%d)\n", ret); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 944 | return ret; |
Leilk Liu | 13da5a0 | 2015-08-24 11:45:17 +0800 | [diff] [blame] | 945 | } |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 946 | } |
| 947 | |
| 948 | ret = spi_master_resume(master); |
| 949 | if (ret < 0) |
| 950 | clk_disable_unprepare(mdata->spi_clk); |
| 951 | |
| 952 | return ret; |
| 953 | } |
| 954 | #endif /* CONFIG_PM_SLEEP */ |
| 955 | |
| 956 | #ifdef CONFIG_PM |
| 957 | static int mtk_spi_runtime_suspend(struct device *dev) |
| 958 | { |
| 959 | struct spi_master *master = dev_get_drvdata(dev); |
| 960 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
| 961 | |
Mason Zhang | 162a31e | 2021-06-29 18:08:15 +0800 | [diff] [blame] | 962 | if (mdata->dev_comp->no_need_unprepare) |
| 963 | clk_disable(mdata->spi_clk); |
| 964 | else |
| 965 | clk_disable_unprepare(mdata->spi_clk); |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 966 | |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | static int mtk_spi_runtime_resume(struct device *dev) |
| 971 | { |
| 972 | struct spi_master *master = dev_get_drvdata(dev); |
| 973 | struct mtk_spi *mdata = spi_master_get_devdata(master); |
Leilk Liu | 13da5a0 | 2015-08-24 11:45:17 +0800 | [diff] [blame] | 974 | int ret; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 975 | |
Mason Zhang | 162a31e | 2021-06-29 18:08:15 +0800 | [diff] [blame] | 976 | if (mdata->dev_comp->no_need_unprepare) |
| 977 | ret = clk_enable(mdata->spi_clk); |
| 978 | else |
| 979 | ret = clk_prepare_enable(mdata->spi_clk); |
Leilk Liu | 13da5a0 | 2015-08-24 11:45:17 +0800 | [diff] [blame] | 980 | if (ret < 0) { |
| 981 | dev_err(dev, "failed to enable spi_clk (%d)\n", ret); |
| 982 | return ret; |
| 983 | } |
| 984 | |
| 985 | return 0; |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 986 | } |
| 987 | #endif /* CONFIG_PM */ |
| 988 | |
| 989 | static const struct dev_pm_ops mtk_spi_pm = { |
| 990 | SET_SYSTEM_SLEEP_PM_OPS(mtk_spi_suspend, mtk_spi_resume) |
| 991 | SET_RUNTIME_PM_OPS(mtk_spi_runtime_suspend, |
| 992 | mtk_spi_runtime_resume, NULL) |
| 993 | }; |
| 994 | |
kbuild test robot | 4299aaa | 2015-08-07 22:33:11 +0800 | [diff] [blame] | 995 | static struct platform_driver mtk_spi_driver = { |
Leilk Liu | a568231 | 2015-08-07 15:19:50 +0800 | [diff] [blame] | 996 | .driver = { |
| 997 | .name = "mtk-spi", |
| 998 | .pm = &mtk_spi_pm, |
| 999 | .of_match_table = mtk_spi_of_match, |
| 1000 | }, |
| 1001 | .probe = mtk_spi_probe, |
| 1002 | .remove = mtk_spi_remove, |
| 1003 | }; |
| 1004 | |
| 1005 | module_platform_driver(mtk_spi_driver); |
| 1006 | |
| 1007 | MODULE_DESCRIPTION("MTK SPI Controller driver"); |
| 1008 | MODULE_AUTHOR("Leilk Liu <leilk.liu@mediatek.com>"); |
| 1009 | MODULE_LICENSE("GPL v2"); |
Axel Lin | e400188 | 2015-08-11 09:15:30 +0800 | [diff] [blame] | 1010 | MODULE_ALIAS("platform:mtk-spi"); |