blob: b34fbc913fd62e053da1154841ada3cf1029a7b7 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Leilk Liua5682312015-08-07 15:19:50 +08002/*
3 * Copyright (c) 2015 MediaTek Inc.
4 * Author: Leilk Liu <leilk.liu@mediatek.com>
Leilk Liua5682312015-08-07 15:19:50 +08005 */
6
7#include <linux/clk.h>
8#include <linux/device.h>
9#include <linux/err.h>
10#include <linux/interrupt.h>
Leilk Liudd69a0a2015-08-24 11:45:15 +080011#include <linux/io.h>
Leilk Liua5682312015-08-07 15:19:50 +080012#include <linux/ioport.h>
13#include <linux/module.h>
14#include <linux/of.h>
Leilk Liu37457602015-10-26 16:09:44 +080015#include <linux/of_gpio.h>
Leilk Liua5682312015-08-07 15:19:50 +080016#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.xufdeae8f2019-09-11 05:55:31 -040020#include <linux/dma-mapping.h>
Leilk Liua5682312015-08-07 15:19:50 +080021
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 Liu058fe492017-06-12 09:24:39 +080031#define SPI_CFG2_REG 0x0028
luhua.xufdeae8f2019-09-11 05:55:31 -040032#define SPI_TX_SRC_REG_64 0x002c
33#define SPI_RX_DST_REG_64 0x0030
Leilk Liua5682312015-08-07 15:19:50 +080034
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 Liu058fe492017-06-12 09:24:39 +080039#define SPI_ADJUST_CFG0_CS_HOLD_OFFSET 0
40#define SPI_ADJUST_CFG0_CS_SETUP_OFFSET 16
Leilk Liua5682312015-08-07 15:19:50 +080041
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 Zhangf84d8662021-07-13 19:40:49 +080045#define SPI_CFG1_GET_TICK_DLY_OFFSET 29
Leilk Liua5682312015-08-07 15:19:50 +080046
Mason Zhangf84d8662021-07-13 19:40:49 +080047#define SPI_CFG1_GET_TICK_DLY_MASK 0xe0000000
Leilk Liua5682312015-08-07 15:19:50 +080048#define SPI_CFG1_CS_IDLE_MASK 0xff
49#define SPI_CFG1_PACKET_LOOP_MASK 0xff00
50#define SPI_CFG1_PACKET_LENGTH_MASK 0x3ff0000
leilk.liu44b37eb2020-07-01 17:00:20 +080051#define SPI_CFG2_SCK_HIGH_OFFSET 0
52#define SPI_CFG2_SCK_LOW_OFFSET 16
Leilk Liua5682312015-08-07 15:19:50 +080053
Leilk Liua71d6ea2015-08-20 17:19:08 +080054#define SPI_CMD_ACT BIT(0)
55#define SPI_CMD_RESUME BIT(1)
Leilk Liua5682312015-08-07 15:19:50 +080056#define SPI_CMD_RST BIT(2)
57#define SPI_CMD_PAUSE_EN BIT(4)
58#define SPI_CMD_DEASSERT BIT(5)
Leilk Liu058fe492017-06-12 09:24:39 +080059#define SPI_CMD_SAMPLE_SEL BIT(6)
60#define SPI_CMD_CS_POL BIT(7)
Leilk Liua5682312015-08-07 15:19:50 +080061#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 Liua5682312015-08-07 15:19:50 +080072#define MT8173_SPI_MAX_PAD_SEL 3
73
Leilk Liu50f8fec2015-08-24 11:45:16 +080074#define MTK_SPI_PAUSE_INT_STATUS 0x2
75
Leilk Liua5682312015-08-07 15:19:50 +080076#define MTK_SPI_IDLE 0
77#define MTK_SPI_PAUSED 1
78
Daniel Kurtz1ce24862017-01-27 00:21:54 +080079#define MTK_SPI_MAX_FIFO_SIZE 32U
Leilk Liua5682312015-08-07 15:19:50 +080080#define MTK_SPI_PACKET_SIZE 1024
luhua.xufdeae8f2019-09-11 05:55:31 -040081#define MTK_SPI_32BITS_MASK (0xffffffff)
82
83#define DMA_ADDR_EXT_BITS (36)
84#define DMA_ADDR_DEF_BITS (32)
Leilk Liua5682312015-08-07 15:19:50 +080085
86struct mtk_spi_compatible {
Leilk Liuaf579372015-08-20 17:19:07 +080087 bool need_pad_sel;
88 /* Must explicitly send dummy Tx bytes to do Rx only transfer */
89 bool must_tx;
Leilk Liu058fe492017-06-12 09:24:39 +080090 /* some IC design adjust cfg register to enhance time accuracy */
91 bool enhance_timing;
luhua.xufdeae8f2019-09-11 05:55:31 -040092 /* some IC support DMA addr extension */
93 bool dma_ext;
Mason Zhang162a31e2021-06-29 18:08:15 +080094 /* some IC no need unprepare SPI clk */
95 bool no_need_unprepare;
Leilk Liua5682312015-08-07 15:19:50 +080096};
97
98struct mtk_spi {
99 void __iomem *base;
100 u32 state;
Leilk Liu37457602015-10-26 16:09:44 +0800101 int pad_num;
102 u32 *pad_sel;
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800103 struct clk *parent_clk, *sel_clk, *spi_clk;
Leilk Liua5682312015-08-07 15:19:50 +0800104 struct spi_transfer *cur_transfer;
105 u32 xfer_len;
Peter Shih00bca732018-09-10 11:54:21 +0800106 u32 num_xfered;
Leilk Liua5682312015-08-07 15:19:50 +0800107 struct scatterlist *tx_sgl, *rx_sgl;
108 u32 tx_sgl_len, rx_sgl_len;
109 const struct mtk_spi_compatible *dev_comp;
Mason Zhang162a31e2021-06-29 18:08:15 +0800110 u32 spi_clk_hz;
Leilk Liua5682312015-08-07 15:19:50 +0800111};
112
Leilk Liu4eaf6f72015-12-31 10:59:00 +0800113static const struct mtk_spi_compatible mtk_common_compat;
Leilk Liufc4f2262017-06-12 09:24:40 +0800114
leilk.liu@mediatek.comb6b1f2d2017-06-20 16:21:07 +0800115static const struct mtk_spi_compatible mt2712_compat = {
116 .must_tx = true,
117};
118
luhua.xu2c231e02019-09-11 05:55:30 -0400119static const struct mtk_spi_compatible mt6765_compat = {
120 .need_pad_sel = true,
121 .must_tx = true,
122 .enhance_timing = true,
luhua.xufdeae8f2019-09-11 05:55:31 -0400123 .dma_ext = true,
luhua.xu2c231e02019-09-11 05:55:30 -0400124};
125
Leilk Liufc4f2262017-06-12 09:24:40 +0800126static const struct mtk_spi_compatible mt7622_compat = {
127 .must_tx = true,
128 .enhance_timing = true,
129};
130
Leilk Liua5682312015-08-07 15:19:50 +0800131static const struct mtk_spi_compatible mt8173_compat = {
Leilk Liuaf579372015-08-20 17:19:07 +0800132 .need_pad_sel = true,
133 .must_tx = true,
Leilk Liua5682312015-08-07 15:19:50 +0800134};
135
Leilk Liub654aa62018-11-01 14:02:19 +0800136static const struct mtk_spi_compatible mt8183_compat = {
137 .need_pad_sel = true,
138 .must_tx = true,
139 .enhance_timing = true,
140};
141
Mason Zhang162a31e2021-06-29 18:08:15 +0800142static 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 Liua5682312015-08-07 15:19:50 +0800150/*
151 * A piece of default chip info unless the platform
152 * supplies it.
153 */
154static const struct mtk_chip_config mtk_default_chip_info = {
Leilk Liu058fe492017-06-12 09:24:39 +0800155 .sample_sel = 0,
Mason Zhangf84d8662021-07-13 19:40:49 +0800156 .tick_delay = 0,
Leilk Liua5682312015-08-07 15:19:50 +0800157};
158
159static const struct of_device_id mtk_spi_of_match[] = {
Leilk Liu15bcdefd2015-12-31 10:59:01 +0800160 { .compatible = "mediatek,mt2701-spi",
161 .data = (void *)&mtk_common_compat,
162 },
leilk.liu@mediatek.comb6b1f2d2017-06-20 16:21:07 +0800163 { .compatible = "mediatek,mt2712-spi",
164 .data = (void *)&mt2712_compat,
165 },
Leilk Liu4eaf6f72015-12-31 10:59:00 +0800166 { .compatible = "mediatek,mt6589-spi",
167 .data = (void *)&mtk_common_compat,
168 },
luhua.xu2c231e02019-09-11 05:55:30 -0400169 { .compatible = "mediatek,mt6765-spi",
170 .data = (void *)&mt6765_compat,
171 },
Leilk Liufc4f2262017-06-12 09:24:40 +0800172 { .compatible = "mediatek,mt7622-spi",
173 .data = (void *)&mt7622_compat,
174 },
Leilk Liu942779c2018-11-20 16:41:08 +0800175 { .compatible = "mediatek,mt7629-spi",
176 .data = (void *)&mt7622_compat,
177 },
Leilk Liu4eaf6f72015-12-31 10:59:00 +0800178 { .compatible = "mediatek,mt8135-spi",
179 .data = (void *)&mtk_common_compat,
180 },
181 { .compatible = "mediatek,mt8173-spi",
182 .data = (void *)&mt8173_compat,
183 },
Leilk Liub654aa62018-11-01 14:02:19 +0800184 { .compatible = "mediatek,mt8183-spi",
185 .data = (void *)&mt8183_compat,
186 },
leilk.liu8cf125c2020-07-21 20:24:36 +0800187 { .compatible = "mediatek,mt8192-spi",
188 .data = (void *)&mt6765_compat,
189 },
Mason Zhang162a31e2021-06-29 18:08:15 +0800190 { .compatible = "mediatek,mt6893-spi",
191 .data = (void *)&mt6893_compat,
192 },
Leilk Liua5682312015-08-07 15:19:50 +0800193 {}
194};
195MODULE_DEVICE_TABLE(of, mtk_spi_of_match);
196
197static 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
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800211static int mtk_spi_prepare_message(struct spi_master *master,
212 struct spi_message *msg)
Leilk Liua5682312015-08-07 15:19:50 +0800213{
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800214 u16 cpha, cpol;
Leilk Liua5682312015-08-07 15:19:50 +0800215 u32 reg_val;
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800216 struct spi_device *spi = msg->spi;
Leilk Liu58a984c72015-10-26 16:09:43 +0800217 struct mtk_chip_config *chip_config = spi->controller_data;
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800218 struct mtk_spi *mdata = spi_master_get_devdata(master);
219
220 cpha = spi->mode & SPI_CPHA ? 1 : 0;
221 cpol = spi->mode & SPI_CPOL ? 1 : 0;
222
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800223 reg_val = readl(mdata->base + SPI_CMD_REG);
224 if (cpha)
225 reg_val |= SPI_CMD_CPHA;
226 else
227 reg_val &= ~SPI_CMD_CPHA;
228 if (cpol)
229 reg_val |= SPI_CMD_CPOL;
230 else
231 reg_val &= ~SPI_CMD_CPOL;
Leilk Liua5682312015-08-07 15:19:50 +0800232
233 /* set the mlsbx and mlsbtx */
Leilk Liu3e582c62019-06-05 11:07:04 +0800234 if (spi->mode & SPI_LSB_FIRST) {
Leilk Liua71d6ea2015-08-20 17:19:08 +0800235 reg_val &= ~SPI_CMD_TXMSBF;
Leilk Liua71d6ea2015-08-20 17:19:08 +0800236 reg_val &= ~SPI_CMD_RXMSBF;
Leilk Liu3e582c62019-06-05 11:07:04 +0800237 } else {
238 reg_val |= SPI_CMD_TXMSBF;
239 reg_val |= SPI_CMD_RXMSBF;
240 }
Leilk Liua5682312015-08-07 15:19:50 +0800241
242 /* set the tx/rx endian */
Leilk Liu44f636d2015-08-20 17:19:06 +0800243#ifdef __LITTLE_ENDIAN
244 reg_val &= ~SPI_CMD_TX_ENDIAN;
245 reg_val &= ~SPI_CMD_RX_ENDIAN;
246#else
247 reg_val |= SPI_CMD_TX_ENDIAN;
248 reg_val |= SPI_CMD_RX_ENDIAN;
249#endif
Leilk Liua5682312015-08-07 15:19:50 +0800250
Leilk Liu058fe492017-06-12 09:24:39 +0800251 if (mdata->dev_comp->enhance_timing) {
Luhua Xuae7c2d32019-11-18 12:57:16 +0800252 /* set CS polarity */
253 if (spi->mode & SPI_CS_HIGH)
Leilk Liu058fe492017-06-12 09:24:39 +0800254 reg_val |= SPI_CMD_CS_POL;
255 else
256 reg_val &= ~SPI_CMD_CS_POL;
Luhua Xuae7c2d32019-11-18 12:57:16 +0800257
Leilk Liu058fe492017-06-12 09:24:39 +0800258 if (chip_config->sample_sel)
259 reg_val |= SPI_CMD_SAMPLE_SEL;
260 else
261 reg_val &= ~SPI_CMD_SAMPLE_SEL;
262 }
263
Leilk Liua5682312015-08-07 15:19:50 +0800264 /* set finish and pause interrupt always enable */
Leilk Liu15293322015-08-27 21:09:04 +0800265 reg_val |= SPI_CMD_FINISH_IE | SPI_CMD_PAUSE_IE;
Leilk Liua5682312015-08-07 15:19:50 +0800266
267 /* disable dma mode */
268 reg_val &= ~(SPI_CMD_TX_DMA | SPI_CMD_RX_DMA);
269
270 /* disable deassert mode */
271 reg_val &= ~SPI_CMD_DEASSERT;
272
273 writel(reg_val, mdata->base + SPI_CMD_REG);
274
275 /* pad select */
276 if (mdata->dev_comp->need_pad_sel)
Leilk Liu37457602015-10-26 16:09:44 +0800277 writel(mdata->pad_sel[spi->chip_select],
278 mdata->base + SPI_PAD_SEL_REG);
Leilk Liua5682312015-08-07 15:19:50 +0800279
Mason Zhangf84d8662021-07-13 19:40:49 +0800280 /* tick delay */
281 reg_val = readl(mdata->base + SPI_CFG1_REG);
282 reg_val &= ~SPI_CFG1_GET_TICK_DLY_MASK;
283 reg_val |= ((chip_config->tick_delay & 0x7)
284 << SPI_CFG1_GET_TICK_DLY_OFFSET);
285 writel(reg_val, mdata->base + SPI_CFG1_REG);
286
Leilk Liua5682312015-08-07 15:19:50 +0800287 return 0;
288}
289
290static void mtk_spi_set_cs(struct spi_device *spi, bool enable)
291{
292 u32 reg_val;
293 struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
294
Luhua Xuae7c2d32019-11-18 12:57:16 +0800295 if (spi->mode & SPI_CS_HIGH)
296 enable = !enable;
297
Leilk Liua5682312015-08-07 15:19:50 +0800298 reg_val = readl(mdata->base + SPI_CMD_REG);
Leilk Liu6583d202015-09-07 19:37:57 +0800299 if (!enable) {
Leilk Liua5682312015-08-07 15:19:50 +0800300 reg_val |= SPI_CMD_PAUSE_EN;
Leilk Liu6583d202015-09-07 19:37:57 +0800301 writel(reg_val, mdata->base + SPI_CMD_REG);
302 } else {
Leilk Liua5682312015-08-07 15:19:50 +0800303 reg_val &= ~SPI_CMD_PAUSE_EN;
Leilk Liu6583d202015-09-07 19:37:57 +0800304 writel(reg_val, mdata->base + SPI_CMD_REG);
305 mdata->state = MTK_SPI_IDLE;
306 mtk_spi_reset(mdata);
307 }
Leilk Liua5682312015-08-07 15:19:50 +0800308}
309
310static void mtk_spi_prepare_transfer(struct spi_master *master,
311 struct spi_transfer *xfer)
312{
Mason Zhang162a31e2021-06-29 18:08:15 +0800313 u32 div, sck_time, reg_val;
Leilk Liua5682312015-08-07 15:19:50 +0800314 struct mtk_spi *mdata = spi_master_get_devdata(master);
315
Mason Zhang162a31e2021-06-29 18:08:15 +0800316 if (xfer->speed_hz < mdata->spi_clk_hz / 2)
317 div = DIV_ROUND_UP(mdata->spi_clk_hz, xfer->speed_hz);
Leilk Liua5682312015-08-07 15:19:50 +0800318 else
319 div = 1;
320
Leilk Liu2ce0acf2015-08-24 11:45:18 +0800321 sck_time = (div + 1) / 2;
Leilk Liua5682312015-08-07 15:19:50 +0800322
Leilk Liu058fe492017-06-12 09:24:39 +0800323 if (mdata->dev_comp->enhance_timing) {
leilk.liu9f6e7e82021-02-07 11:09:53 +0800324 reg_val = readl(mdata->base + SPI_CFG2_REG);
325 reg_val &= ~(0xffff << SPI_CFG2_SCK_HIGH_OFFSET);
326 reg_val |= (((sck_time - 1) & 0xffff)
leilk.liu44b37eb2020-07-01 17:00:20 +0800327 << SPI_CFG2_SCK_HIGH_OFFSET);
leilk.liu9f6e7e82021-02-07 11:09:53 +0800328 reg_val &= ~(0xffff << SPI_CFG2_SCK_LOW_OFFSET);
Leilk Liu058fe492017-06-12 09:24:39 +0800329 reg_val |= (((sck_time - 1) & 0xffff)
leilk.liu44b37eb2020-07-01 17:00:20 +0800330 << SPI_CFG2_SCK_LOW_OFFSET);
Leilk Liu058fe492017-06-12 09:24:39 +0800331 writel(reg_val, mdata->base + SPI_CFG2_REG);
Leilk Liu058fe492017-06-12 09:24:39 +0800332 } else {
leilk.liu9f6e7e82021-02-07 11:09:53 +0800333 reg_val = readl(mdata->base + SPI_CFG0_REG);
334 reg_val &= ~(0xff << SPI_CFG0_SCK_HIGH_OFFSET);
335 reg_val |= (((sck_time - 1) & 0xff)
Leilk Liu058fe492017-06-12 09:24:39 +0800336 << SPI_CFG0_SCK_HIGH_OFFSET);
leilk.liu9f6e7e82021-02-07 11:09:53 +0800337 reg_val &= ~(0xff << SPI_CFG0_SCK_LOW_OFFSET);
Leilk Liu058fe492017-06-12 09:24:39 +0800338 reg_val |= (((sck_time - 1) & 0xff) << SPI_CFG0_SCK_LOW_OFFSET);
Leilk Liu058fe492017-06-12 09:24:39 +0800339 writel(reg_val, mdata->base + SPI_CFG0_REG);
340 }
Leilk Liua5682312015-08-07 15:19:50 +0800341}
342
343static void mtk_spi_setup_packet(struct spi_master *master)
344{
345 u32 packet_size, packet_loop, reg_val;
346 struct mtk_spi *mdata = spi_master_get_devdata(master);
347
Leilk Liu50f8fec2015-08-24 11:45:16 +0800348 packet_size = min_t(u32, mdata->xfer_len, MTK_SPI_PACKET_SIZE);
Leilk Liua5682312015-08-07 15:19:50 +0800349 packet_loop = mdata->xfer_len / packet_size;
350
351 reg_val = readl(mdata->base + SPI_CFG1_REG);
Leilk Liu50f8fec2015-08-24 11:45:16 +0800352 reg_val &= ~(SPI_CFG1_PACKET_LENGTH_MASK | SPI_CFG1_PACKET_LOOP_MASK);
Leilk Liua5682312015-08-07 15:19:50 +0800353 reg_val |= (packet_size - 1) << SPI_CFG1_PACKET_LENGTH_OFFSET;
354 reg_val |= (packet_loop - 1) << SPI_CFG1_PACKET_LOOP_OFFSET;
355 writel(reg_val, mdata->base + SPI_CFG1_REG);
356}
357
358static void mtk_spi_enable_transfer(struct spi_master *master)
359{
Leilk Liu50f8fec2015-08-24 11:45:16 +0800360 u32 cmd;
Leilk Liua5682312015-08-07 15:19:50 +0800361 struct mtk_spi *mdata = spi_master_get_devdata(master);
362
363 cmd = readl(mdata->base + SPI_CMD_REG);
364 if (mdata->state == MTK_SPI_IDLE)
Leilk Liua71d6ea2015-08-20 17:19:08 +0800365 cmd |= SPI_CMD_ACT;
Leilk Liua5682312015-08-07 15:19:50 +0800366 else
Leilk Liua71d6ea2015-08-20 17:19:08 +0800367 cmd |= SPI_CMD_RESUME;
Leilk Liua5682312015-08-07 15:19:50 +0800368 writel(cmd, mdata->base + SPI_CMD_REG);
369}
370
Leilk Liu50f8fec2015-08-24 11:45:16 +0800371static int mtk_spi_get_mult_delta(u32 xfer_len)
Leilk Liua5682312015-08-07 15:19:50 +0800372{
Leilk Liu50f8fec2015-08-24 11:45:16 +0800373 u32 mult_delta;
Leilk Liua5682312015-08-07 15:19:50 +0800374
375 if (xfer_len > MTK_SPI_PACKET_SIZE)
376 mult_delta = xfer_len % MTK_SPI_PACKET_SIZE;
377 else
378 mult_delta = 0;
379
380 return mult_delta;
381}
382
383static void mtk_spi_update_mdata_len(struct spi_master *master)
384{
385 int mult_delta;
386 struct mtk_spi *mdata = spi_master_get_devdata(master);
387
388 if (mdata->tx_sgl_len && mdata->rx_sgl_len) {
389 if (mdata->tx_sgl_len > mdata->rx_sgl_len) {
390 mult_delta = mtk_spi_get_mult_delta(mdata->rx_sgl_len);
391 mdata->xfer_len = mdata->rx_sgl_len - mult_delta;
392 mdata->rx_sgl_len = mult_delta;
393 mdata->tx_sgl_len -= mdata->xfer_len;
394 } else {
395 mult_delta = mtk_spi_get_mult_delta(mdata->tx_sgl_len);
396 mdata->xfer_len = mdata->tx_sgl_len - mult_delta;
397 mdata->tx_sgl_len = mult_delta;
398 mdata->rx_sgl_len -= mdata->xfer_len;
399 }
400 } else if (mdata->tx_sgl_len) {
401 mult_delta = mtk_spi_get_mult_delta(mdata->tx_sgl_len);
402 mdata->xfer_len = mdata->tx_sgl_len - mult_delta;
403 mdata->tx_sgl_len = mult_delta;
404 } else if (mdata->rx_sgl_len) {
405 mult_delta = mtk_spi_get_mult_delta(mdata->rx_sgl_len);
406 mdata->xfer_len = mdata->rx_sgl_len - mult_delta;
407 mdata->rx_sgl_len = mult_delta;
408 }
409}
410
411static void mtk_spi_setup_dma_addr(struct spi_master *master,
412 struct spi_transfer *xfer)
413{
414 struct mtk_spi *mdata = spi_master_get_devdata(master);
415
luhua.xufdeae8f2019-09-11 05:55:31 -0400416 if (mdata->tx_sgl) {
417 writel((u32)(xfer->tx_dma & MTK_SPI_32BITS_MASK),
418 mdata->base + SPI_TX_SRC_REG);
419#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
420 if (mdata->dev_comp->dma_ext)
421 writel((u32)(xfer->tx_dma >> 32),
422 mdata->base + SPI_TX_SRC_REG_64);
423#endif
424 }
425
426 if (mdata->rx_sgl) {
427 writel((u32)(xfer->rx_dma & MTK_SPI_32BITS_MASK),
428 mdata->base + SPI_RX_DST_REG);
429#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
430 if (mdata->dev_comp->dma_ext)
431 writel((u32)(xfer->rx_dma >> 32),
432 mdata->base + SPI_RX_DST_REG_64);
433#endif
434 }
Leilk Liua5682312015-08-07 15:19:50 +0800435}
436
437static int mtk_spi_fifo_transfer(struct spi_master *master,
438 struct spi_device *spi,
439 struct spi_transfer *xfer)
440{
Nicolas Boichatde327e42015-12-27 18:17:06 +0800441 int cnt, remainder;
442 u32 reg_val;
Leilk Liua5682312015-08-07 15:19:50 +0800443 struct mtk_spi *mdata = spi_master_get_devdata(master);
444
445 mdata->cur_transfer = xfer;
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800446 mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, xfer->len);
Peter Shih00bca732018-09-10 11:54:21 +0800447 mdata->num_xfered = 0;
Leilk Liua5682312015-08-07 15:19:50 +0800448 mtk_spi_prepare_transfer(master, xfer);
449 mtk_spi_setup_packet(master);
450
Nicolas Boichatde327e42015-12-27 18:17:06 +0800451 cnt = xfer->len / 4;
Leilk Liu44f636d2015-08-20 17:19:06 +0800452 iowrite32_rep(mdata->base + SPI_TX_DATA_REG, xfer->tx_buf, cnt);
Leilk Liua5682312015-08-07 15:19:50 +0800453
Nicolas Boichatde327e42015-12-27 18:17:06 +0800454 remainder = xfer->len % 4;
455 if (remainder > 0) {
456 reg_val = 0;
457 memcpy(&reg_val, xfer->tx_buf + (cnt * 4), remainder);
458 writel(reg_val, mdata->base + SPI_TX_DATA_REG);
459 }
460
Leilk Liua5682312015-08-07 15:19:50 +0800461 mtk_spi_enable_transfer(master);
462
463 return 1;
464}
465
466static int mtk_spi_dma_transfer(struct spi_master *master,
467 struct spi_device *spi,
468 struct spi_transfer *xfer)
469{
470 int cmd;
471 struct mtk_spi *mdata = spi_master_get_devdata(master);
472
473 mdata->tx_sgl = NULL;
474 mdata->rx_sgl = NULL;
475 mdata->tx_sgl_len = 0;
476 mdata->rx_sgl_len = 0;
477 mdata->cur_transfer = xfer;
Peter Shih00bca732018-09-10 11:54:21 +0800478 mdata->num_xfered = 0;
Leilk Liua5682312015-08-07 15:19:50 +0800479
480 mtk_spi_prepare_transfer(master, xfer);
481
482 cmd = readl(mdata->base + SPI_CMD_REG);
483 if (xfer->tx_buf)
484 cmd |= SPI_CMD_TX_DMA;
485 if (xfer->rx_buf)
486 cmd |= SPI_CMD_RX_DMA;
487 writel(cmd, mdata->base + SPI_CMD_REG);
488
489 if (xfer->tx_buf)
490 mdata->tx_sgl = xfer->tx_sg.sgl;
491 if (xfer->rx_buf)
492 mdata->rx_sgl = xfer->rx_sg.sgl;
493
494 if (mdata->tx_sgl) {
495 xfer->tx_dma = sg_dma_address(mdata->tx_sgl);
496 mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl);
497 }
498 if (mdata->rx_sgl) {
499 xfer->rx_dma = sg_dma_address(mdata->rx_sgl);
500 mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl);
501 }
502
503 mtk_spi_update_mdata_len(master);
504 mtk_spi_setup_packet(master);
505 mtk_spi_setup_dma_addr(master, xfer);
506 mtk_spi_enable_transfer(master);
507
508 return 1;
509}
510
511static int mtk_spi_transfer_one(struct spi_master *master,
512 struct spi_device *spi,
513 struct spi_transfer *xfer)
514{
515 if (master->can_dma(master, spi, xfer))
516 return mtk_spi_dma_transfer(master, spi, xfer);
517 else
518 return mtk_spi_fifo_transfer(master, spi, xfer);
519}
520
521static bool mtk_spi_can_dma(struct spi_master *master,
522 struct spi_device *spi,
523 struct spi_transfer *xfer)
524{
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800525 /* Buffers for DMA transactions must be 4-byte aligned */
526 return (xfer->len > MTK_SPI_MAX_FIFO_SIZE &&
527 (unsigned long)xfer->tx_buf % 4 == 0 &&
528 (unsigned long)xfer->rx_buf % 4 == 0);
Leilk Liua5682312015-08-07 15:19:50 +0800529}
530
leilk.liu9f6e7e82021-02-07 11:09:53 +0800531static int mtk_spi_set_hw_cs_timing(struct spi_device *spi,
532 struct spi_delay *setup,
533 struct spi_delay *hold,
534 struct spi_delay *inactive)
535{
536 struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
537 u16 setup_dly, hold_dly, inactive_dly;
538 u32 reg_val;
539
540 if ((setup && setup->unit != SPI_DELAY_UNIT_SCK) ||
541 (hold && hold->unit != SPI_DELAY_UNIT_SCK) ||
542 (inactive && inactive->unit != SPI_DELAY_UNIT_SCK)) {
543 dev_err(&spi->dev,
544 "Invalid delay unit, should be SPI_DELAY_UNIT_SCK\n");
545 return -EINVAL;
546 }
547
548 setup_dly = setup ? setup->value : 1;
549 hold_dly = hold ? hold->value : 1;
550 inactive_dly = inactive ? inactive->value : 1;
551
552 reg_val = readl(mdata->base + SPI_CFG0_REG);
553 if (mdata->dev_comp->enhance_timing) {
554 reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_HOLD_OFFSET);
555 reg_val |= (((hold_dly - 1) & 0xffff)
556 << SPI_ADJUST_CFG0_CS_HOLD_OFFSET);
557 reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_SETUP_OFFSET);
558 reg_val |= (((setup_dly - 1) & 0xffff)
559 << SPI_ADJUST_CFG0_CS_SETUP_OFFSET);
560 } else {
561 reg_val &= ~(0xff << SPI_CFG0_CS_HOLD_OFFSET);
562 reg_val |= (((hold_dly - 1) & 0xff) << SPI_CFG0_CS_HOLD_OFFSET);
563 reg_val &= ~(0xff << SPI_CFG0_CS_SETUP_OFFSET);
564 reg_val |= (((setup_dly - 1) & 0xff)
565 << SPI_CFG0_CS_SETUP_OFFSET);
566 }
567 writel(reg_val, mdata->base + SPI_CFG0_REG);
568
569 reg_val = readl(mdata->base + SPI_CFG1_REG);
570 reg_val &= ~SPI_CFG1_CS_IDLE_MASK;
571 reg_val |= (((inactive_dly - 1) & 0xff) << SPI_CFG1_CS_IDLE_OFFSET);
572 writel(reg_val, mdata->base + SPI_CFG1_REG);
573
574 return 0;
575}
576
Leilk Liu58a984c72015-10-26 16:09:43 +0800577static int mtk_spi_setup(struct spi_device *spi)
578{
579 struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
580
581 if (!spi->controller_data)
582 spi->controller_data = (void *)&mtk_default_chip_info;
583
Nicolas Boichat98c8dcc2015-11-09 12:14:51 +0800584 if (mdata->dev_comp->need_pad_sel && gpio_is_valid(spi->cs_gpio))
Leilk Liu37457602015-10-26 16:09:44 +0800585 gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
586
Leilk Liu58a984c72015-10-26 16:09:43 +0800587 return 0;
588}
589
Leilk Liua5682312015-08-07 15:19:50 +0800590static irqreturn_t mtk_spi_interrupt(int irq, void *dev_id)
591{
Peter Shih00bca732018-09-10 11:54:21 +0800592 u32 cmd, reg_val, cnt, remainder, len;
Leilk Liua5682312015-08-07 15:19:50 +0800593 struct spi_master *master = dev_id;
594 struct mtk_spi *mdata = spi_master_get_devdata(master);
595 struct spi_transfer *trans = mdata->cur_transfer;
596
597 reg_val = readl(mdata->base + SPI_STATUS0_REG);
Leilk Liu50f8fec2015-08-24 11:45:16 +0800598 if (reg_val & MTK_SPI_PAUSE_INT_STATUS)
Leilk Liua5682312015-08-07 15:19:50 +0800599 mdata->state = MTK_SPI_PAUSED;
600 else
601 mdata->state = MTK_SPI_IDLE;
602
603 if (!master->can_dma(master, master->cur_msg->spi, trans)) {
Leilk Liua5682312015-08-07 15:19:50 +0800604 if (trans->rx_buf) {
Nicolas Boichatde327e42015-12-27 18:17:06 +0800605 cnt = mdata->xfer_len / 4;
Leilk Liu44f636d2015-08-20 17:19:06 +0800606 ioread32_rep(mdata->base + SPI_RX_DATA_REG,
Peter Shih00bca732018-09-10 11:54:21 +0800607 trans->rx_buf + mdata->num_xfered, cnt);
Nicolas Boichatde327e42015-12-27 18:17:06 +0800608 remainder = mdata->xfer_len % 4;
609 if (remainder > 0) {
610 reg_val = readl(mdata->base + SPI_RX_DATA_REG);
Peter Shih00bca732018-09-10 11:54:21 +0800611 memcpy(trans->rx_buf +
612 mdata->num_xfered +
613 (cnt * 4),
614 &reg_val,
615 remainder);
Nicolas Boichatde327e42015-12-27 18:17:06 +0800616 }
Leilk Liua5682312015-08-07 15:19:50 +0800617 }
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800618
Peter Shih00bca732018-09-10 11:54:21 +0800619 mdata->num_xfered += mdata->xfer_len;
620 if (mdata->num_xfered == trans->len) {
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800621 spi_finalize_current_transfer(master);
622 return IRQ_HANDLED;
623 }
624
Peter Shih00bca732018-09-10 11:54:21 +0800625 len = trans->len - mdata->num_xfered;
626 mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, len);
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800627 mtk_spi_setup_packet(master);
628
Leilk Liua4d8f642018-10-31 16:49:16 +0800629 cnt = mdata->xfer_len / 4;
Peter Shih00bca732018-09-10 11:54:21 +0800630 iowrite32_rep(mdata->base + SPI_TX_DATA_REG,
631 trans->tx_buf + mdata->num_xfered, cnt);
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800632
Leilk Liua4d8f642018-10-31 16:49:16 +0800633 remainder = mdata->xfer_len % 4;
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800634 if (remainder > 0) {
635 reg_val = 0;
Peter Shih00bca732018-09-10 11:54:21 +0800636 memcpy(&reg_val,
637 trans->tx_buf + (cnt * 4) + mdata->num_xfered,
638 remainder);
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800639 writel(reg_val, mdata->base + SPI_TX_DATA_REG);
640 }
641
642 mtk_spi_enable_transfer(master);
643
Leilk Liua5682312015-08-07 15:19:50 +0800644 return IRQ_HANDLED;
645 }
646
647 if (mdata->tx_sgl)
648 trans->tx_dma += mdata->xfer_len;
649 if (mdata->rx_sgl)
650 trans->rx_dma += mdata->xfer_len;
651
652 if (mdata->tx_sgl && (mdata->tx_sgl_len == 0)) {
653 mdata->tx_sgl = sg_next(mdata->tx_sgl);
654 if (mdata->tx_sgl) {
655 trans->tx_dma = sg_dma_address(mdata->tx_sgl);
656 mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl);
657 }
658 }
659 if (mdata->rx_sgl && (mdata->rx_sgl_len == 0)) {
660 mdata->rx_sgl = sg_next(mdata->rx_sgl);
661 if (mdata->rx_sgl) {
662 trans->rx_dma = sg_dma_address(mdata->rx_sgl);
663 mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl);
664 }
665 }
666
667 if (!mdata->tx_sgl && !mdata->rx_sgl) {
668 /* spi disable dma */
669 cmd = readl(mdata->base + SPI_CMD_REG);
670 cmd &= ~SPI_CMD_TX_DMA;
671 cmd &= ~SPI_CMD_RX_DMA;
672 writel(cmd, mdata->base + SPI_CMD_REG);
673
674 spi_finalize_current_transfer(master);
675 return IRQ_HANDLED;
676 }
677
678 mtk_spi_update_mdata_len(master);
679 mtk_spi_setup_packet(master);
680 mtk_spi_setup_dma_addr(master, trans);
681 mtk_spi_enable_transfer(master);
682
683 return IRQ_HANDLED;
684}
685
686static int mtk_spi_probe(struct platform_device *pdev)
687{
688 struct spi_master *master;
689 struct mtk_spi *mdata;
690 const struct of_device_id *of_id;
luhua.xufdeae8f2019-09-11 05:55:31 -0400691 int i, irq, ret, addr_bits;
Leilk Liua5682312015-08-07 15:19:50 +0800692
693 master = spi_alloc_master(&pdev->dev, sizeof(*mdata));
694 if (!master) {
695 dev_err(&pdev->dev, "failed to alloc spi master\n");
696 return -ENOMEM;
697 }
698
699 master->auto_runtime_pm = true;
700 master->dev.of_node = pdev->dev.of_node;
Leilk Liu3e582c62019-06-05 11:07:04 +0800701 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
Leilk Liua5682312015-08-07 15:19:50 +0800702
703 master->set_cs = mtk_spi_set_cs;
Leilk Liua5682312015-08-07 15:19:50 +0800704 master->prepare_message = mtk_spi_prepare_message;
705 master->transfer_one = mtk_spi_transfer_one;
706 master->can_dma = mtk_spi_can_dma;
Leilk Liu58a984c72015-10-26 16:09:43 +0800707 master->setup = mtk_spi_setup;
leilk.liu9f6e7e82021-02-07 11:09:53 +0800708 master->set_cs_timing = mtk_spi_set_hw_cs_timing;
Leilk Liua5682312015-08-07 15:19:50 +0800709
710 of_id = of_match_node(mtk_spi_of_match, pdev->dev.of_node);
711 if (!of_id) {
712 dev_err(&pdev->dev, "failed to probe of_node\n");
713 ret = -EINVAL;
714 goto err_put_master;
715 }
716
717 mdata = spi_master_get_devdata(master);
718 mdata->dev_comp = of_id->data;
Luhua Xuae7c2d32019-11-18 12:57:16 +0800719
720 if (mdata->dev_comp->enhance_timing)
721 master->mode_bits |= SPI_CS_HIGH;
722
Leilk Liua5682312015-08-07 15:19:50 +0800723 if (mdata->dev_comp->must_tx)
724 master->flags = SPI_MASTER_MUST_TX;
725
726 if (mdata->dev_comp->need_pad_sel) {
Leilk Liu37457602015-10-26 16:09:44 +0800727 mdata->pad_num = of_property_count_u32_elems(
728 pdev->dev.of_node,
729 "mediatek,pad-select");
730 if (mdata->pad_num < 0) {
731 dev_err(&pdev->dev,
732 "No 'mediatek,pad-select' property\n");
733 ret = -EINVAL;
Leilk Liua5682312015-08-07 15:19:50 +0800734 goto err_put_master;
735 }
736
Leilk Liu37457602015-10-26 16:09:44 +0800737 mdata->pad_sel = devm_kmalloc_array(&pdev->dev, mdata->pad_num,
738 sizeof(u32), GFP_KERNEL);
739 if (!mdata->pad_sel) {
740 ret = -ENOMEM;
Leilk Liua5682312015-08-07 15:19:50 +0800741 goto err_put_master;
742 }
Leilk Liu37457602015-10-26 16:09:44 +0800743
744 for (i = 0; i < mdata->pad_num; i++) {
745 of_property_read_u32_index(pdev->dev.of_node,
746 "mediatek,pad-select",
747 i, &mdata->pad_sel[i]);
748 if (mdata->pad_sel[i] > MT8173_SPI_MAX_PAD_SEL) {
749 dev_err(&pdev->dev, "wrong pad-sel[%d]: %u\n",
750 i, mdata->pad_sel[i]);
751 ret = -EINVAL;
752 goto err_put_master;
753 }
754 }
Leilk Liua5682312015-08-07 15:19:50 +0800755 }
756
757 platform_set_drvdata(pdev, master);
Markus Elfring5dd381e2019-09-21 14:45:40 +0200758 mdata->base = devm_platform_ioremap_resource(pdev, 0);
Leilk Liua5682312015-08-07 15:19:50 +0800759 if (IS_ERR(mdata->base)) {
760 ret = PTR_ERR(mdata->base);
761 goto err_put_master;
762 }
763
764 irq = platform_get_irq(pdev, 0);
765 if (irq < 0) {
Leilk Liua5682312015-08-07 15:19:50 +0800766 ret = irq;
767 goto err_put_master;
768 }
769
770 if (!pdev->dev.dma_mask)
771 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
772
773 ret = devm_request_irq(&pdev->dev, irq, mtk_spi_interrupt,
774 IRQF_TRIGGER_NONE, dev_name(&pdev->dev), master);
775 if (ret) {
776 dev_err(&pdev->dev, "failed to register irq (%d)\n", ret);
777 goto err_put_master;
778 }
779
Leilk Liua5682312015-08-07 15:19:50 +0800780 mdata->parent_clk = devm_clk_get(&pdev->dev, "parent-clk");
781 if (IS_ERR(mdata->parent_clk)) {
782 ret = PTR_ERR(mdata->parent_clk);
783 dev_err(&pdev->dev, "failed to get parent-clk: %d\n", ret);
784 goto err_put_master;
785 }
786
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800787 mdata->sel_clk = devm_clk_get(&pdev->dev, "sel-clk");
788 if (IS_ERR(mdata->sel_clk)) {
Javier Martinez Canillase26d15f2015-09-15 14:46:45 +0200789 ret = PTR_ERR(mdata->sel_clk);
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800790 dev_err(&pdev->dev, "failed to get sel-clk: %d\n", ret);
791 goto err_put_master;
792 }
793
794 mdata->spi_clk = devm_clk_get(&pdev->dev, "spi-clk");
795 if (IS_ERR(mdata->spi_clk)) {
Javier Martinez Canillase26d15f2015-09-15 14:46:45 +0200796 ret = PTR_ERR(mdata->spi_clk);
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800797 dev_err(&pdev->dev, "failed to get spi-clk: %d\n", ret);
798 goto err_put_master;
799 }
800
Leilk Liua5682312015-08-07 15:19:50 +0800801 ret = clk_prepare_enable(mdata->spi_clk);
802 if (ret < 0) {
803 dev_err(&pdev->dev, "failed to enable spi_clk (%d)\n", ret);
804 goto err_put_master;
805 }
806
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800807 ret = clk_set_parent(mdata->sel_clk, mdata->parent_clk);
Leilk Liua5682312015-08-07 15:19:50 +0800808 if (ret < 0) {
809 dev_err(&pdev->dev, "failed to clk_set_parent (%d)\n", ret);
Leilk Liue38da372015-11-25 17:50:38 +0800810 clk_disable_unprepare(mdata->spi_clk);
811 goto err_put_master;
Leilk Liua5682312015-08-07 15:19:50 +0800812 }
813
Mason Zhang162a31e2021-06-29 18:08:15 +0800814 mdata->spi_clk_hz = clk_get_rate(mdata->spi_clk);
815
816 if (mdata->dev_comp->no_need_unprepare)
817 clk_disable(mdata->spi_clk);
818 else
819 clk_disable_unprepare(mdata->spi_clk);
Leilk Liua5682312015-08-07 15:19:50 +0800820
821 pm_runtime_enable(&pdev->dev);
822
823 ret = devm_spi_register_master(&pdev->dev, master);
824 if (ret) {
825 dev_err(&pdev->dev, "failed to register master (%d)\n", ret);
Leilk Liue38da372015-11-25 17:50:38 +0800826 goto err_disable_runtime_pm;
Leilk Liua5682312015-08-07 15:19:50 +0800827 }
828
Leilk Liu37457602015-10-26 16:09:44 +0800829 if (mdata->dev_comp->need_pad_sel) {
830 if (mdata->pad_num != master->num_chipselect) {
831 dev_err(&pdev->dev,
832 "pad_num does not match num_chipselect(%d != %d)\n",
833 mdata->pad_num, master->num_chipselect);
834 ret = -EINVAL;
Leilk Liue38da372015-11-25 17:50:38 +0800835 goto err_disable_runtime_pm;
Leilk Liu37457602015-10-26 16:09:44 +0800836 }
837
Nicolas Boichat98c8dcc2015-11-09 12:14:51 +0800838 if (!master->cs_gpios && master->num_chipselect > 1) {
839 dev_err(&pdev->dev,
840 "cs_gpios not specified and num_chipselect > 1\n");
841 ret = -EINVAL;
Leilk Liue38da372015-11-25 17:50:38 +0800842 goto err_disable_runtime_pm;
Nicolas Boichat98c8dcc2015-11-09 12:14:51 +0800843 }
844
845 if (master->cs_gpios) {
846 for (i = 0; i < master->num_chipselect; i++) {
847 ret = devm_gpio_request(&pdev->dev,
848 master->cs_gpios[i],
849 dev_name(&pdev->dev));
850 if (ret) {
851 dev_err(&pdev->dev,
852 "can't get CS GPIO %i\n", i);
Leilk Liue38da372015-11-25 17:50:38 +0800853 goto err_disable_runtime_pm;
Nicolas Boichat98c8dcc2015-11-09 12:14:51 +0800854 }
Leilk Liu37457602015-10-26 16:09:44 +0800855 }
856 }
857 }
858
luhua.xufdeae8f2019-09-11 05:55:31 -0400859 if (mdata->dev_comp->dma_ext)
860 addr_bits = DMA_ADDR_EXT_BITS;
861 else
862 addr_bits = DMA_ADDR_DEF_BITS;
863 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(addr_bits));
864 if (ret)
865 dev_notice(&pdev->dev, "SPI dma_set_mask(%d) failed, ret:%d\n",
866 addr_bits, ret);
867
Leilk Liua5682312015-08-07 15:19:50 +0800868 return 0;
869
Leilk Liue38da372015-11-25 17:50:38 +0800870err_disable_runtime_pm:
871 pm_runtime_disable(&pdev->dev);
Leilk Liua5682312015-08-07 15:19:50 +0800872err_put_master:
873 spi_master_put(master);
874
875 return ret;
876}
877
878static int mtk_spi_remove(struct platform_device *pdev)
879{
880 struct spi_master *master = platform_get_drvdata(pdev);
881 struct mtk_spi *mdata = spi_master_get_devdata(master);
882
883 pm_runtime_disable(&pdev->dev);
884
885 mtk_spi_reset(mdata);
Leilk Liua5682312015-08-07 15:19:50 +0800886
Mason Zhang162a31e2021-06-29 18:08:15 +0800887 if (mdata->dev_comp->no_need_unprepare)
888 clk_unprepare(mdata->spi_clk);
889
Leilk Liua5682312015-08-07 15:19:50 +0800890 return 0;
891}
892
893#ifdef CONFIG_PM_SLEEP
894static int mtk_spi_suspend(struct device *dev)
895{
896 int ret;
897 struct spi_master *master = dev_get_drvdata(dev);
898 struct mtk_spi *mdata = spi_master_get_devdata(master);
899
900 ret = spi_master_suspend(master);
901 if (ret)
902 return ret;
903
904 if (!pm_runtime_suspended(dev))
905 clk_disable_unprepare(mdata->spi_clk);
906
907 return ret;
908}
909
910static int mtk_spi_resume(struct device *dev)
911{
912 int ret;
913 struct spi_master *master = dev_get_drvdata(dev);
914 struct mtk_spi *mdata = spi_master_get_devdata(master);
915
916 if (!pm_runtime_suspended(dev)) {
917 ret = clk_prepare_enable(mdata->spi_clk);
Leilk Liu13da5a02015-08-24 11:45:17 +0800918 if (ret < 0) {
919 dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
Leilk Liua5682312015-08-07 15:19:50 +0800920 return ret;
Leilk Liu13da5a02015-08-24 11:45:17 +0800921 }
Leilk Liua5682312015-08-07 15:19:50 +0800922 }
923
924 ret = spi_master_resume(master);
925 if (ret < 0)
926 clk_disable_unprepare(mdata->spi_clk);
927
928 return ret;
929}
930#endif /* CONFIG_PM_SLEEP */
931
932#ifdef CONFIG_PM
933static int mtk_spi_runtime_suspend(struct device *dev)
934{
935 struct spi_master *master = dev_get_drvdata(dev);
936 struct mtk_spi *mdata = spi_master_get_devdata(master);
937
Mason Zhang162a31e2021-06-29 18:08:15 +0800938 if (mdata->dev_comp->no_need_unprepare)
939 clk_disable(mdata->spi_clk);
940 else
941 clk_disable_unprepare(mdata->spi_clk);
Leilk Liua5682312015-08-07 15:19:50 +0800942
943 return 0;
944}
945
946static int mtk_spi_runtime_resume(struct device *dev)
947{
948 struct spi_master *master = dev_get_drvdata(dev);
949 struct mtk_spi *mdata = spi_master_get_devdata(master);
Leilk Liu13da5a02015-08-24 11:45:17 +0800950 int ret;
Leilk Liua5682312015-08-07 15:19:50 +0800951
Mason Zhang162a31e2021-06-29 18:08:15 +0800952 if (mdata->dev_comp->no_need_unprepare)
953 ret = clk_enable(mdata->spi_clk);
954 else
955 ret = clk_prepare_enable(mdata->spi_clk);
Leilk Liu13da5a02015-08-24 11:45:17 +0800956 if (ret < 0) {
957 dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
958 return ret;
959 }
960
961 return 0;
Leilk Liua5682312015-08-07 15:19:50 +0800962}
963#endif /* CONFIG_PM */
964
965static const struct dev_pm_ops mtk_spi_pm = {
966 SET_SYSTEM_SLEEP_PM_OPS(mtk_spi_suspend, mtk_spi_resume)
967 SET_RUNTIME_PM_OPS(mtk_spi_runtime_suspend,
968 mtk_spi_runtime_resume, NULL)
969};
970
kbuild test robot4299aaa2015-08-07 22:33:11 +0800971static struct platform_driver mtk_spi_driver = {
Leilk Liua5682312015-08-07 15:19:50 +0800972 .driver = {
973 .name = "mtk-spi",
974 .pm = &mtk_spi_pm,
975 .of_match_table = mtk_spi_of_match,
976 },
977 .probe = mtk_spi_probe,
978 .remove = mtk_spi_remove,
979};
980
981module_platform_driver(mtk_spi_driver);
982
983MODULE_DESCRIPTION("MTK SPI Controller driver");
984MODULE_AUTHOR("Leilk Liu <leilk.liu@mediatek.com>");
985MODULE_LICENSE("GPL v2");
Axel Line4001882015-08-11 09:15:30 +0800986MODULE_ALIAS("platform:mtk-spi");