blob: 8d5fa7f1e5069a7e0c7182dd12c2767a050e52f2 [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
45#define SPI_CFG1_GET_TICK_DLY_OFFSET 30
46
47#define SPI_CFG1_CS_IDLE_MASK 0xff
48#define SPI_CFG1_PACKET_LOOP_MASK 0xff00
49#define SPI_CFG1_PACKET_LENGTH_MASK 0x3ff0000
leilk.liu44b37eb2020-07-01 17:00:20 +080050#define SPI_CFG2_SCK_HIGH_OFFSET 0
51#define SPI_CFG2_SCK_LOW_OFFSET 16
Leilk Liua5682312015-08-07 15:19:50 +080052
Leilk Liua71d6ea2015-08-20 17:19:08 +080053#define SPI_CMD_ACT BIT(0)
54#define SPI_CMD_RESUME BIT(1)
Leilk Liua5682312015-08-07 15:19:50 +080055#define SPI_CMD_RST BIT(2)
56#define SPI_CMD_PAUSE_EN BIT(4)
57#define SPI_CMD_DEASSERT BIT(5)
Leilk Liu058fe492017-06-12 09:24:39 +080058#define SPI_CMD_SAMPLE_SEL BIT(6)
59#define SPI_CMD_CS_POL BIT(7)
Leilk Liua5682312015-08-07 15:19:50 +080060#define SPI_CMD_CPHA BIT(8)
61#define SPI_CMD_CPOL BIT(9)
62#define SPI_CMD_RX_DMA BIT(10)
63#define SPI_CMD_TX_DMA BIT(11)
64#define SPI_CMD_TXMSBF BIT(12)
65#define SPI_CMD_RXMSBF BIT(13)
66#define SPI_CMD_RX_ENDIAN BIT(14)
67#define SPI_CMD_TX_ENDIAN BIT(15)
68#define SPI_CMD_FINISH_IE BIT(16)
69#define SPI_CMD_PAUSE_IE BIT(17)
70
Leilk Liua5682312015-08-07 15:19:50 +080071#define MT8173_SPI_MAX_PAD_SEL 3
72
Leilk Liu50f8fec2015-08-24 11:45:16 +080073#define MTK_SPI_PAUSE_INT_STATUS 0x2
74
Leilk Liua5682312015-08-07 15:19:50 +080075#define MTK_SPI_IDLE 0
76#define MTK_SPI_PAUSED 1
77
Daniel Kurtz1ce24862017-01-27 00:21:54 +080078#define MTK_SPI_MAX_FIFO_SIZE 32U
Leilk Liua5682312015-08-07 15:19:50 +080079#define MTK_SPI_PACKET_SIZE 1024
luhua.xufdeae8f2019-09-11 05:55:31 -040080#define MTK_SPI_32BITS_MASK (0xffffffff)
81
82#define DMA_ADDR_EXT_BITS (36)
83#define DMA_ADDR_DEF_BITS (32)
Leilk Liua5682312015-08-07 15:19:50 +080084
85struct mtk_spi_compatible {
Leilk Liuaf579372015-08-20 17:19:07 +080086 bool need_pad_sel;
87 /* Must explicitly send dummy Tx bytes to do Rx only transfer */
88 bool must_tx;
Leilk Liu058fe492017-06-12 09:24:39 +080089 /* some IC design adjust cfg register to enhance time accuracy */
90 bool enhance_timing;
luhua.xufdeae8f2019-09-11 05:55:31 -040091 /* some IC support DMA addr extension */
92 bool dma_ext;
Leilk Liua5682312015-08-07 15:19:50 +080093};
94
95struct mtk_spi {
96 void __iomem *base;
97 u32 state;
Leilk Liu37457602015-10-26 16:09:44 +080098 int pad_num;
99 u32 *pad_sel;
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800100 struct clk *parent_clk, *sel_clk, *spi_clk;
Leilk Liua5682312015-08-07 15:19:50 +0800101 struct spi_transfer *cur_transfer;
102 u32 xfer_len;
Peter Shih00bca732018-09-10 11:54:21 +0800103 u32 num_xfered;
Leilk Liua5682312015-08-07 15:19:50 +0800104 struct scatterlist *tx_sgl, *rx_sgl;
105 u32 tx_sgl_len, rx_sgl_len;
106 const struct mtk_spi_compatible *dev_comp;
107};
108
Leilk Liu4eaf6f72015-12-31 10:59:00 +0800109static const struct mtk_spi_compatible mtk_common_compat;
Leilk Liufc4f2262017-06-12 09:24:40 +0800110
leilk.liu@mediatek.comb6b1f2d2017-06-20 16:21:07 +0800111static const struct mtk_spi_compatible mt2712_compat = {
112 .must_tx = true,
113};
114
luhua.xu2c231e02019-09-11 05:55:30 -0400115static const struct mtk_spi_compatible mt6765_compat = {
116 .need_pad_sel = true,
117 .must_tx = true,
118 .enhance_timing = true,
luhua.xufdeae8f2019-09-11 05:55:31 -0400119 .dma_ext = true,
luhua.xu2c231e02019-09-11 05:55:30 -0400120};
121
Leilk Liufc4f2262017-06-12 09:24:40 +0800122static const struct mtk_spi_compatible mt7622_compat = {
123 .must_tx = true,
124 .enhance_timing = true,
125};
126
Leilk Liua5682312015-08-07 15:19:50 +0800127static const struct mtk_spi_compatible mt8173_compat = {
Leilk Liuaf579372015-08-20 17:19:07 +0800128 .need_pad_sel = true,
129 .must_tx = true,
Leilk Liua5682312015-08-07 15:19:50 +0800130};
131
Leilk Liub654aa62018-11-01 14:02:19 +0800132static const struct mtk_spi_compatible mt8183_compat = {
133 .need_pad_sel = true,
134 .must_tx = true,
135 .enhance_timing = true,
136};
137
Leilk Liua5682312015-08-07 15:19:50 +0800138/*
139 * A piece of default chip info unless the platform
140 * supplies it.
141 */
142static const struct mtk_chip_config mtk_default_chip_info = {
Leilk Liu058fe492017-06-12 09:24:39 +0800143 .sample_sel = 0,
Leilk Liua5682312015-08-07 15:19:50 +0800144};
145
146static const struct of_device_id mtk_spi_of_match[] = {
Leilk Liu15bcdefd2015-12-31 10:59:01 +0800147 { .compatible = "mediatek,mt2701-spi",
148 .data = (void *)&mtk_common_compat,
149 },
leilk.liu@mediatek.comb6b1f2d2017-06-20 16:21:07 +0800150 { .compatible = "mediatek,mt2712-spi",
151 .data = (void *)&mt2712_compat,
152 },
Leilk Liu4eaf6f72015-12-31 10:59:00 +0800153 { .compatible = "mediatek,mt6589-spi",
154 .data = (void *)&mtk_common_compat,
155 },
luhua.xu2c231e02019-09-11 05:55:30 -0400156 { .compatible = "mediatek,mt6765-spi",
157 .data = (void *)&mt6765_compat,
158 },
Leilk Liufc4f2262017-06-12 09:24:40 +0800159 { .compatible = "mediatek,mt7622-spi",
160 .data = (void *)&mt7622_compat,
161 },
Leilk Liu942779c2018-11-20 16:41:08 +0800162 { .compatible = "mediatek,mt7629-spi",
163 .data = (void *)&mt7622_compat,
164 },
Leilk Liu4eaf6f72015-12-31 10:59:00 +0800165 { .compatible = "mediatek,mt8135-spi",
166 .data = (void *)&mtk_common_compat,
167 },
168 { .compatible = "mediatek,mt8173-spi",
169 .data = (void *)&mt8173_compat,
170 },
Leilk Liub654aa62018-11-01 14:02:19 +0800171 { .compatible = "mediatek,mt8183-spi",
172 .data = (void *)&mt8183_compat,
173 },
leilk.liu8cf125c2020-07-21 20:24:36 +0800174 { .compatible = "mediatek,mt8192-spi",
175 .data = (void *)&mt6765_compat,
176 },
Leilk Liua5682312015-08-07 15:19:50 +0800177 {}
178};
179MODULE_DEVICE_TABLE(of, mtk_spi_of_match);
180
181static void mtk_spi_reset(struct mtk_spi *mdata)
182{
183 u32 reg_val;
184
185 /* set the software reset bit in SPI_CMD_REG. */
186 reg_val = readl(mdata->base + SPI_CMD_REG);
187 reg_val |= SPI_CMD_RST;
188 writel(reg_val, mdata->base + SPI_CMD_REG);
189
190 reg_val = readl(mdata->base + SPI_CMD_REG);
191 reg_val &= ~SPI_CMD_RST;
192 writel(reg_val, mdata->base + SPI_CMD_REG);
193}
194
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800195static int mtk_spi_prepare_message(struct spi_master *master,
196 struct spi_message *msg)
Leilk Liua5682312015-08-07 15:19:50 +0800197{
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800198 u16 cpha, cpol;
Leilk Liua5682312015-08-07 15:19:50 +0800199 u32 reg_val;
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800200 struct spi_device *spi = msg->spi;
Leilk Liu58a984c72015-10-26 16:09:43 +0800201 struct mtk_chip_config *chip_config = spi->controller_data;
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800202 struct mtk_spi *mdata = spi_master_get_devdata(master);
203
204 cpha = spi->mode & SPI_CPHA ? 1 : 0;
205 cpol = spi->mode & SPI_CPOL ? 1 : 0;
206
Leilk Liu79b5d3f2015-10-26 16:09:41 +0800207 reg_val = readl(mdata->base + SPI_CMD_REG);
208 if (cpha)
209 reg_val |= SPI_CMD_CPHA;
210 else
211 reg_val &= ~SPI_CMD_CPHA;
212 if (cpol)
213 reg_val |= SPI_CMD_CPOL;
214 else
215 reg_val &= ~SPI_CMD_CPOL;
Leilk Liua5682312015-08-07 15:19:50 +0800216
217 /* set the mlsbx and mlsbtx */
Leilk Liu3e582c62019-06-05 11:07:04 +0800218 if (spi->mode & SPI_LSB_FIRST) {
Leilk Liua71d6ea2015-08-20 17:19:08 +0800219 reg_val &= ~SPI_CMD_TXMSBF;
Leilk Liua71d6ea2015-08-20 17:19:08 +0800220 reg_val &= ~SPI_CMD_RXMSBF;
Leilk Liu3e582c62019-06-05 11:07:04 +0800221 } else {
222 reg_val |= SPI_CMD_TXMSBF;
223 reg_val |= SPI_CMD_RXMSBF;
224 }
Leilk Liua5682312015-08-07 15:19:50 +0800225
226 /* set the tx/rx endian */
Leilk Liu44f636d2015-08-20 17:19:06 +0800227#ifdef __LITTLE_ENDIAN
228 reg_val &= ~SPI_CMD_TX_ENDIAN;
229 reg_val &= ~SPI_CMD_RX_ENDIAN;
230#else
231 reg_val |= SPI_CMD_TX_ENDIAN;
232 reg_val |= SPI_CMD_RX_ENDIAN;
233#endif
Leilk Liua5682312015-08-07 15:19:50 +0800234
Leilk Liu058fe492017-06-12 09:24:39 +0800235 if (mdata->dev_comp->enhance_timing) {
Luhua Xuae7c2d32019-11-18 12:57:16 +0800236 /* set CS polarity */
237 if (spi->mode & SPI_CS_HIGH)
Leilk Liu058fe492017-06-12 09:24:39 +0800238 reg_val |= SPI_CMD_CS_POL;
239 else
240 reg_val &= ~SPI_CMD_CS_POL;
Luhua Xuae7c2d32019-11-18 12:57:16 +0800241
Leilk Liu058fe492017-06-12 09:24:39 +0800242 if (chip_config->sample_sel)
243 reg_val |= SPI_CMD_SAMPLE_SEL;
244 else
245 reg_val &= ~SPI_CMD_SAMPLE_SEL;
246 }
247
Leilk Liua5682312015-08-07 15:19:50 +0800248 /* set finish and pause interrupt always enable */
Leilk Liu15293322015-08-27 21:09:04 +0800249 reg_val |= SPI_CMD_FINISH_IE | SPI_CMD_PAUSE_IE;
Leilk Liua5682312015-08-07 15:19:50 +0800250
251 /* disable dma mode */
252 reg_val &= ~(SPI_CMD_TX_DMA | SPI_CMD_RX_DMA);
253
254 /* disable deassert mode */
255 reg_val &= ~SPI_CMD_DEASSERT;
256
257 writel(reg_val, mdata->base + SPI_CMD_REG);
258
259 /* pad select */
260 if (mdata->dev_comp->need_pad_sel)
Leilk Liu37457602015-10-26 16:09:44 +0800261 writel(mdata->pad_sel[spi->chip_select],
262 mdata->base + SPI_PAD_SEL_REG);
Leilk Liua5682312015-08-07 15:19:50 +0800263
264 return 0;
265}
266
267static void mtk_spi_set_cs(struct spi_device *spi, bool enable)
268{
269 u32 reg_val;
270 struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
271
Luhua Xuae7c2d32019-11-18 12:57:16 +0800272 if (spi->mode & SPI_CS_HIGH)
273 enable = !enable;
274
Leilk Liua5682312015-08-07 15:19:50 +0800275 reg_val = readl(mdata->base + SPI_CMD_REG);
Leilk Liu6583d202015-09-07 19:37:57 +0800276 if (!enable) {
Leilk Liua5682312015-08-07 15:19:50 +0800277 reg_val |= SPI_CMD_PAUSE_EN;
Leilk Liu6583d202015-09-07 19:37:57 +0800278 writel(reg_val, mdata->base + SPI_CMD_REG);
279 } else {
Leilk Liua5682312015-08-07 15:19:50 +0800280 reg_val &= ~SPI_CMD_PAUSE_EN;
Leilk Liu6583d202015-09-07 19:37:57 +0800281 writel(reg_val, mdata->base + SPI_CMD_REG);
282 mdata->state = MTK_SPI_IDLE;
283 mtk_spi_reset(mdata);
284 }
Leilk Liua5682312015-08-07 15:19:50 +0800285}
286
287static void mtk_spi_prepare_transfer(struct spi_master *master,
288 struct spi_transfer *xfer)
289{
leilk.liu9f6e7e82021-02-07 11:09:53 +0800290 u32 spi_clk_hz, div, sck_time, reg_val;
Leilk Liua5682312015-08-07 15:19:50 +0800291 struct mtk_spi *mdata = spi_master_get_devdata(master);
292
293 spi_clk_hz = clk_get_rate(mdata->spi_clk);
294 if (xfer->speed_hz < spi_clk_hz / 2)
295 div = DIV_ROUND_UP(spi_clk_hz, xfer->speed_hz);
296 else
297 div = 1;
298
Leilk Liu2ce0acf2015-08-24 11:45:18 +0800299 sck_time = (div + 1) / 2;
Leilk Liua5682312015-08-07 15:19:50 +0800300
Leilk Liu058fe492017-06-12 09:24:39 +0800301 if (mdata->dev_comp->enhance_timing) {
leilk.liu9f6e7e82021-02-07 11:09:53 +0800302 reg_val = readl(mdata->base + SPI_CFG2_REG);
303 reg_val &= ~(0xffff << SPI_CFG2_SCK_HIGH_OFFSET);
304 reg_val |= (((sck_time - 1) & 0xffff)
leilk.liu44b37eb2020-07-01 17:00:20 +0800305 << SPI_CFG2_SCK_HIGH_OFFSET);
leilk.liu9f6e7e82021-02-07 11:09:53 +0800306 reg_val &= ~(0xffff << SPI_CFG2_SCK_LOW_OFFSET);
Leilk Liu058fe492017-06-12 09:24:39 +0800307 reg_val |= (((sck_time - 1) & 0xffff)
leilk.liu44b37eb2020-07-01 17:00:20 +0800308 << SPI_CFG2_SCK_LOW_OFFSET);
Leilk Liu058fe492017-06-12 09:24:39 +0800309 writel(reg_val, mdata->base + SPI_CFG2_REG);
Leilk Liu058fe492017-06-12 09:24:39 +0800310 } else {
leilk.liu9f6e7e82021-02-07 11:09:53 +0800311 reg_val = readl(mdata->base + SPI_CFG0_REG);
312 reg_val &= ~(0xff << SPI_CFG0_SCK_HIGH_OFFSET);
313 reg_val |= (((sck_time - 1) & 0xff)
Leilk Liu058fe492017-06-12 09:24:39 +0800314 << SPI_CFG0_SCK_HIGH_OFFSET);
leilk.liu9f6e7e82021-02-07 11:09:53 +0800315 reg_val &= ~(0xff << SPI_CFG0_SCK_LOW_OFFSET);
Leilk Liu058fe492017-06-12 09:24:39 +0800316 reg_val |= (((sck_time - 1) & 0xff) << SPI_CFG0_SCK_LOW_OFFSET);
Leilk Liu058fe492017-06-12 09:24:39 +0800317 writel(reg_val, mdata->base + SPI_CFG0_REG);
318 }
Leilk Liua5682312015-08-07 15:19:50 +0800319}
320
321static void mtk_spi_setup_packet(struct spi_master *master)
322{
323 u32 packet_size, packet_loop, reg_val;
324 struct mtk_spi *mdata = spi_master_get_devdata(master);
325
Leilk Liu50f8fec2015-08-24 11:45:16 +0800326 packet_size = min_t(u32, mdata->xfer_len, MTK_SPI_PACKET_SIZE);
Leilk Liua5682312015-08-07 15:19:50 +0800327 packet_loop = mdata->xfer_len / packet_size;
328
329 reg_val = readl(mdata->base + SPI_CFG1_REG);
Leilk Liu50f8fec2015-08-24 11:45:16 +0800330 reg_val &= ~(SPI_CFG1_PACKET_LENGTH_MASK | SPI_CFG1_PACKET_LOOP_MASK);
Leilk Liua5682312015-08-07 15:19:50 +0800331 reg_val |= (packet_size - 1) << SPI_CFG1_PACKET_LENGTH_OFFSET;
332 reg_val |= (packet_loop - 1) << SPI_CFG1_PACKET_LOOP_OFFSET;
333 writel(reg_val, mdata->base + SPI_CFG1_REG);
334}
335
336static void mtk_spi_enable_transfer(struct spi_master *master)
337{
Leilk Liu50f8fec2015-08-24 11:45:16 +0800338 u32 cmd;
Leilk Liua5682312015-08-07 15:19:50 +0800339 struct mtk_spi *mdata = spi_master_get_devdata(master);
340
341 cmd = readl(mdata->base + SPI_CMD_REG);
342 if (mdata->state == MTK_SPI_IDLE)
Leilk Liua71d6ea2015-08-20 17:19:08 +0800343 cmd |= SPI_CMD_ACT;
Leilk Liua5682312015-08-07 15:19:50 +0800344 else
Leilk Liua71d6ea2015-08-20 17:19:08 +0800345 cmd |= SPI_CMD_RESUME;
Leilk Liua5682312015-08-07 15:19:50 +0800346 writel(cmd, mdata->base + SPI_CMD_REG);
347}
348
Leilk Liu50f8fec2015-08-24 11:45:16 +0800349static int mtk_spi_get_mult_delta(u32 xfer_len)
Leilk Liua5682312015-08-07 15:19:50 +0800350{
Leilk Liu50f8fec2015-08-24 11:45:16 +0800351 u32 mult_delta;
Leilk Liua5682312015-08-07 15:19:50 +0800352
353 if (xfer_len > MTK_SPI_PACKET_SIZE)
354 mult_delta = xfer_len % MTK_SPI_PACKET_SIZE;
355 else
356 mult_delta = 0;
357
358 return mult_delta;
359}
360
361static void mtk_spi_update_mdata_len(struct spi_master *master)
362{
363 int mult_delta;
364 struct mtk_spi *mdata = spi_master_get_devdata(master);
365
366 if (mdata->tx_sgl_len && mdata->rx_sgl_len) {
367 if (mdata->tx_sgl_len > mdata->rx_sgl_len) {
368 mult_delta = mtk_spi_get_mult_delta(mdata->rx_sgl_len);
369 mdata->xfer_len = mdata->rx_sgl_len - mult_delta;
370 mdata->rx_sgl_len = mult_delta;
371 mdata->tx_sgl_len -= mdata->xfer_len;
372 } else {
373 mult_delta = mtk_spi_get_mult_delta(mdata->tx_sgl_len);
374 mdata->xfer_len = mdata->tx_sgl_len - mult_delta;
375 mdata->tx_sgl_len = mult_delta;
376 mdata->rx_sgl_len -= mdata->xfer_len;
377 }
378 } else if (mdata->tx_sgl_len) {
379 mult_delta = mtk_spi_get_mult_delta(mdata->tx_sgl_len);
380 mdata->xfer_len = mdata->tx_sgl_len - mult_delta;
381 mdata->tx_sgl_len = mult_delta;
382 } else if (mdata->rx_sgl_len) {
383 mult_delta = mtk_spi_get_mult_delta(mdata->rx_sgl_len);
384 mdata->xfer_len = mdata->rx_sgl_len - mult_delta;
385 mdata->rx_sgl_len = mult_delta;
386 }
387}
388
389static void mtk_spi_setup_dma_addr(struct spi_master *master,
390 struct spi_transfer *xfer)
391{
392 struct mtk_spi *mdata = spi_master_get_devdata(master);
393
luhua.xufdeae8f2019-09-11 05:55:31 -0400394 if (mdata->tx_sgl) {
395 writel((u32)(xfer->tx_dma & MTK_SPI_32BITS_MASK),
396 mdata->base + SPI_TX_SRC_REG);
397#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
398 if (mdata->dev_comp->dma_ext)
399 writel((u32)(xfer->tx_dma >> 32),
400 mdata->base + SPI_TX_SRC_REG_64);
401#endif
402 }
403
404 if (mdata->rx_sgl) {
405 writel((u32)(xfer->rx_dma & MTK_SPI_32BITS_MASK),
406 mdata->base + SPI_RX_DST_REG);
407#ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
408 if (mdata->dev_comp->dma_ext)
409 writel((u32)(xfer->rx_dma >> 32),
410 mdata->base + SPI_RX_DST_REG_64);
411#endif
412 }
Leilk Liua5682312015-08-07 15:19:50 +0800413}
414
415static int mtk_spi_fifo_transfer(struct spi_master *master,
416 struct spi_device *spi,
417 struct spi_transfer *xfer)
418{
Nicolas Boichatde327e42015-12-27 18:17:06 +0800419 int cnt, remainder;
420 u32 reg_val;
Leilk Liua5682312015-08-07 15:19:50 +0800421 struct mtk_spi *mdata = spi_master_get_devdata(master);
422
423 mdata->cur_transfer = xfer;
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800424 mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, xfer->len);
Peter Shih00bca732018-09-10 11:54:21 +0800425 mdata->num_xfered = 0;
Leilk Liua5682312015-08-07 15:19:50 +0800426 mtk_spi_prepare_transfer(master, xfer);
427 mtk_spi_setup_packet(master);
428
Nicolas Boichatde327e42015-12-27 18:17:06 +0800429 cnt = xfer->len / 4;
Peter Hess3a70dd22021-07-06 14:16:09 +0200430 if (xfer->tx_buf)
431 iowrite32_rep(mdata->base + SPI_TX_DATA_REG, xfer->tx_buf, cnt);
432
433 if (xfer->rx_buf)
434 ioread32_rep(mdata->base + SPI_RX_DATA_REG, xfer->rx_buf, cnt);
Leilk Liua5682312015-08-07 15:19:50 +0800435
Nicolas Boichatde327e42015-12-27 18:17:06 +0800436 remainder = xfer->len % 4;
437 if (remainder > 0) {
438 reg_val = 0;
Peter Hess3a70dd22021-07-06 14:16:09 +0200439 if (xfer->tx_buf) {
440 memcpy(&reg_val, xfer->tx_buf + (cnt * 4), remainder);
441 writel(reg_val, mdata->base + SPI_TX_DATA_REG);
442 }
443 if (xfer->rx_buf) {
444 reg_val = readl(mdata->base + SPI_RX_DATA_REG);
445 memcpy(xfer->rx_buf + (cnt * 4), &reg_val, remainder);
446 }
Nicolas Boichatde327e42015-12-27 18:17:06 +0800447 }
448
Leilk Liua5682312015-08-07 15:19:50 +0800449 mtk_spi_enable_transfer(master);
450
451 return 1;
452}
453
454static int mtk_spi_dma_transfer(struct spi_master *master,
455 struct spi_device *spi,
456 struct spi_transfer *xfer)
457{
458 int cmd;
459 struct mtk_spi *mdata = spi_master_get_devdata(master);
460
461 mdata->tx_sgl = NULL;
462 mdata->rx_sgl = NULL;
463 mdata->tx_sgl_len = 0;
464 mdata->rx_sgl_len = 0;
465 mdata->cur_transfer = xfer;
Peter Shih00bca732018-09-10 11:54:21 +0800466 mdata->num_xfered = 0;
Leilk Liua5682312015-08-07 15:19:50 +0800467
468 mtk_spi_prepare_transfer(master, xfer);
469
470 cmd = readl(mdata->base + SPI_CMD_REG);
471 if (xfer->tx_buf)
472 cmd |= SPI_CMD_TX_DMA;
473 if (xfer->rx_buf)
474 cmd |= SPI_CMD_RX_DMA;
475 writel(cmd, mdata->base + SPI_CMD_REG);
476
477 if (xfer->tx_buf)
478 mdata->tx_sgl = xfer->tx_sg.sgl;
479 if (xfer->rx_buf)
480 mdata->rx_sgl = xfer->rx_sg.sgl;
481
482 if (mdata->tx_sgl) {
483 xfer->tx_dma = sg_dma_address(mdata->tx_sgl);
484 mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl);
485 }
486 if (mdata->rx_sgl) {
487 xfer->rx_dma = sg_dma_address(mdata->rx_sgl);
488 mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl);
489 }
490
491 mtk_spi_update_mdata_len(master);
492 mtk_spi_setup_packet(master);
493 mtk_spi_setup_dma_addr(master, xfer);
494 mtk_spi_enable_transfer(master);
495
496 return 1;
497}
498
499static int mtk_spi_transfer_one(struct spi_master *master,
500 struct spi_device *spi,
501 struct spi_transfer *xfer)
502{
503 if (master->can_dma(master, spi, xfer))
504 return mtk_spi_dma_transfer(master, spi, xfer);
505 else
506 return mtk_spi_fifo_transfer(master, spi, xfer);
507}
508
509static bool mtk_spi_can_dma(struct spi_master *master,
510 struct spi_device *spi,
511 struct spi_transfer *xfer)
512{
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800513 /* Buffers for DMA transactions must be 4-byte aligned */
514 return (xfer->len > MTK_SPI_MAX_FIFO_SIZE &&
515 (unsigned long)xfer->tx_buf % 4 == 0 &&
516 (unsigned long)xfer->rx_buf % 4 == 0);
Leilk Liua5682312015-08-07 15:19:50 +0800517}
518
leilk.liu9f6e7e82021-02-07 11:09:53 +0800519static int mtk_spi_set_hw_cs_timing(struct spi_device *spi,
520 struct spi_delay *setup,
521 struct spi_delay *hold,
522 struct spi_delay *inactive)
523{
524 struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
525 u16 setup_dly, hold_dly, inactive_dly;
526 u32 reg_val;
527
528 if ((setup && setup->unit != SPI_DELAY_UNIT_SCK) ||
529 (hold && hold->unit != SPI_DELAY_UNIT_SCK) ||
530 (inactive && inactive->unit != SPI_DELAY_UNIT_SCK)) {
531 dev_err(&spi->dev,
532 "Invalid delay unit, should be SPI_DELAY_UNIT_SCK\n");
533 return -EINVAL;
534 }
535
536 setup_dly = setup ? setup->value : 1;
537 hold_dly = hold ? hold->value : 1;
538 inactive_dly = inactive ? inactive->value : 1;
539
540 reg_val = readl(mdata->base + SPI_CFG0_REG);
541 if (mdata->dev_comp->enhance_timing) {
542 reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_HOLD_OFFSET);
543 reg_val |= (((hold_dly - 1) & 0xffff)
544 << SPI_ADJUST_CFG0_CS_HOLD_OFFSET);
545 reg_val &= ~(0xffff << SPI_ADJUST_CFG0_CS_SETUP_OFFSET);
546 reg_val |= (((setup_dly - 1) & 0xffff)
547 << SPI_ADJUST_CFG0_CS_SETUP_OFFSET);
548 } else {
549 reg_val &= ~(0xff << SPI_CFG0_CS_HOLD_OFFSET);
550 reg_val |= (((hold_dly - 1) & 0xff) << SPI_CFG0_CS_HOLD_OFFSET);
551 reg_val &= ~(0xff << SPI_CFG0_CS_SETUP_OFFSET);
552 reg_val |= (((setup_dly - 1) & 0xff)
553 << SPI_CFG0_CS_SETUP_OFFSET);
554 }
555 writel(reg_val, mdata->base + SPI_CFG0_REG);
556
557 reg_val = readl(mdata->base + SPI_CFG1_REG);
558 reg_val &= ~SPI_CFG1_CS_IDLE_MASK;
559 reg_val |= (((inactive_dly - 1) & 0xff) << SPI_CFG1_CS_IDLE_OFFSET);
560 writel(reg_val, mdata->base + SPI_CFG1_REG);
561
562 return 0;
563}
564
Leilk Liu58a984c72015-10-26 16:09:43 +0800565static int mtk_spi_setup(struct spi_device *spi)
566{
567 struct mtk_spi *mdata = spi_master_get_devdata(spi->master);
568
569 if (!spi->controller_data)
570 spi->controller_data = (void *)&mtk_default_chip_info;
571
Nicolas Boichat98c8dcc2015-11-09 12:14:51 +0800572 if (mdata->dev_comp->need_pad_sel && gpio_is_valid(spi->cs_gpio))
Leilk Liu37457602015-10-26 16:09:44 +0800573 gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
574
Leilk Liu58a984c72015-10-26 16:09:43 +0800575 return 0;
576}
577
Leilk Liua5682312015-08-07 15:19:50 +0800578static irqreturn_t mtk_spi_interrupt(int irq, void *dev_id)
579{
Peter Shih00bca732018-09-10 11:54:21 +0800580 u32 cmd, reg_val, cnt, remainder, len;
Leilk Liua5682312015-08-07 15:19:50 +0800581 struct spi_master *master = dev_id;
582 struct mtk_spi *mdata = spi_master_get_devdata(master);
583 struct spi_transfer *trans = mdata->cur_transfer;
584
585 reg_val = readl(mdata->base + SPI_STATUS0_REG);
Leilk Liu50f8fec2015-08-24 11:45:16 +0800586 if (reg_val & MTK_SPI_PAUSE_INT_STATUS)
Leilk Liua5682312015-08-07 15:19:50 +0800587 mdata->state = MTK_SPI_PAUSED;
588 else
589 mdata->state = MTK_SPI_IDLE;
590
591 if (!master->can_dma(master, master->cur_msg->spi, trans)) {
Leilk Liua5682312015-08-07 15:19:50 +0800592 if (trans->rx_buf) {
Nicolas Boichatde327e42015-12-27 18:17:06 +0800593 cnt = mdata->xfer_len / 4;
Leilk Liu44f636d2015-08-20 17:19:06 +0800594 ioread32_rep(mdata->base + SPI_RX_DATA_REG,
Peter Shih00bca732018-09-10 11:54:21 +0800595 trans->rx_buf + mdata->num_xfered, cnt);
Nicolas Boichatde327e42015-12-27 18:17:06 +0800596 remainder = mdata->xfer_len % 4;
597 if (remainder > 0) {
598 reg_val = readl(mdata->base + SPI_RX_DATA_REG);
Peter Shih00bca732018-09-10 11:54:21 +0800599 memcpy(trans->rx_buf +
600 mdata->num_xfered +
601 (cnt * 4),
602 &reg_val,
603 remainder);
Nicolas Boichatde327e42015-12-27 18:17:06 +0800604 }
Leilk Liua5682312015-08-07 15:19:50 +0800605 }
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800606
Peter Shih00bca732018-09-10 11:54:21 +0800607 mdata->num_xfered += mdata->xfer_len;
608 if (mdata->num_xfered == trans->len) {
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800609 spi_finalize_current_transfer(master);
610 return IRQ_HANDLED;
611 }
612
Peter Shih00bca732018-09-10 11:54:21 +0800613 len = trans->len - mdata->num_xfered;
614 mdata->xfer_len = min(MTK_SPI_MAX_FIFO_SIZE, len);
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800615 mtk_spi_setup_packet(master);
616
Leilk Liua4d8f642018-10-31 16:49:16 +0800617 cnt = mdata->xfer_len / 4;
Peter Shih00bca732018-09-10 11:54:21 +0800618 iowrite32_rep(mdata->base + SPI_TX_DATA_REG,
619 trans->tx_buf + mdata->num_xfered, cnt);
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800620
Leilk Liua4d8f642018-10-31 16:49:16 +0800621 remainder = mdata->xfer_len % 4;
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800622 if (remainder > 0) {
623 reg_val = 0;
Peter Shih00bca732018-09-10 11:54:21 +0800624 memcpy(&reg_val,
625 trans->tx_buf + (cnt * 4) + mdata->num_xfered,
626 remainder);
Daniel Kurtz1ce24862017-01-27 00:21:54 +0800627 writel(reg_val, mdata->base + SPI_TX_DATA_REG);
628 }
629
630 mtk_spi_enable_transfer(master);
631
Leilk Liua5682312015-08-07 15:19:50 +0800632 return IRQ_HANDLED;
633 }
634
635 if (mdata->tx_sgl)
636 trans->tx_dma += mdata->xfer_len;
637 if (mdata->rx_sgl)
638 trans->rx_dma += mdata->xfer_len;
639
640 if (mdata->tx_sgl && (mdata->tx_sgl_len == 0)) {
641 mdata->tx_sgl = sg_next(mdata->tx_sgl);
642 if (mdata->tx_sgl) {
643 trans->tx_dma = sg_dma_address(mdata->tx_sgl);
644 mdata->tx_sgl_len = sg_dma_len(mdata->tx_sgl);
645 }
646 }
647 if (mdata->rx_sgl && (mdata->rx_sgl_len == 0)) {
648 mdata->rx_sgl = sg_next(mdata->rx_sgl);
649 if (mdata->rx_sgl) {
650 trans->rx_dma = sg_dma_address(mdata->rx_sgl);
651 mdata->rx_sgl_len = sg_dma_len(mdata->rx_sgl);
652 }
653 }
654
655 if (!mdata->tx_sgl && !mdata->rx_sgl) {
656 /* spi disable dma */
657 cmd = readl(mdata->base + SPI_CMD_REG);
658 cmd &= ~SPI_CMD_TX_DMA;
659 cmd &= ~SPI_CMD_RX_DMA;
660 writel(cmd, mdata->base + SPI_CMD_REG);
661
662 spi_finalize_current_transfer(master);
663 return IRQ_HANDLED;
664 }
665
666 mtk_spi_update_mdata_len(master);
667 mtk_spi_setup_packet(master);
668 mtk_spi_setup_dma_addr(master, trans);
669 mtk_spi_enable_transfer(master);
670
671 return IRQ_HANDLED;
672}
673
674static int mtk_spi_probe(struct platform_device *pdev)
675{
676 struct spi_master *master;
677 struct mtk_spi *mdata;
678 const struct of_device_id *of_id;
luhua.xufdeae8f2019-09-11 05:55:31 -0400679 int i, irq, ret, addr_bits;
Leilk Liua5682312015-08-07 15:19:50 +0800680
681 master = spi_alloc_master(&pdev->dev, sizeof(*mdata));
682 if (!master) {
683 dev_err(&pdev->dev, "failed to alloc spi master\n");
684 return -ENOMEM;
685 }
686
687 master->auto_runtime_pm = true;
688 master->dev.of_node = pdev->dev.of_node;
Leilk Liu3e582c62019-06-05 11:07:04 +0800689 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
Leilk Liua5682312015-08-07 15:19:50 +0800690
691 master->set_cs = mtk_spi_set_cs;
Leilk Liua5682312015-08-07 15:19:50 +0800692 master->prepare_message = mtk_spi_prepare_message;
693 master->transfer_one = mtk_spi_transfer_one;
694 master->can_dma = mtk_spi_can_dma;
Leilk Liu58a984c72015-10-26 16:09:43 +0800695 master->setup = mtk_spi_setup;
leilk.liu9f6e7e82021-02-07 11:09:53 +0800696 master->set_cs_timing = mtk_spi_set_hw_cs_timing;
Leilk Liua5682312015-08-07 15:19:50 +0800697
698 of_id = of_match_node(mtk_spi_of_match, pdev->dev.of_node);
699 if (!of_id) {
700 dev_err(&pdev->dev, "failed to probe of_node\n");
701 ret = -EINVAL;
702 goto err_put_master;
703 }
704
705 mdata = spi_master_get_devdata(master);
706 mdata->dev_comp = of_id->data;
Luhua Xuae7c2d32019-11-18 12:57:16 +0800707
708 if (mdata->dev_comp->enhance_timing)
709 master->mode_bits |= SPI_CS_HIGH;
710
Leilk Liua5682312015-08-07 15:19:50 +0800711 if (mdata->dev_comp->must_tx)
712 master->flags = SPI_MASTER_MUST_TX;
713
714 if (mdata->dev_comp->need_pad_sel) {
Leilk Liu37457602015-10-26 16:09:44 +0800715 mdata->pad_num = of_property_count_u32_elems(
716 pdev->dev.of_node,
717 "mediatek,pad-select");
718 if (mdata->pad_num < 0) {
719 dev_err(&pdev->dev,
720 "No 'mediatek,pad-select' property\n");
721 ret = -EINVAL;
Leilk Liua5682312015-08-07 15:19:50 +0800722 goto err_put_master;
723 }
724
Leilk Liu37457602015-10-26 16:09:44 +0800725 mdata->pad_sel = devm_kmalloc_array(&pdev->dev, mdata->pad_num,
726 sizeof(u32), GFP_KERNEL);
727 if (!mdata->pad_sel) {
728 ret = -ENOMEM;
Leilk Liua5682312015-08-07 15:19:50 +0800729 goto err_put_master;
730 }
Leilk Liu37457602015-10-26 16:09:44 +0800731
732 for (i = 0; i < mdata->pad_num; i++) {
733 of_property_read_u32_index(pdev->dev.of_node,
734 "mediatek,pad-select",
735 i, &mdata->pad_sel[i]);
736 if (mdata->pad_sel[i] > MT8173_SPI_MAX_PAD_SEL) {
737 dev_err(&pdev->dev, "wrong pad-sel[%d]: %u\n",
738 i, mdata->pad_sel[i]);
739 ret = -EINVAL;
740 goto err_put_master;
741 }
742 }
Leilk Liua5682312015-08-07 15:19:50 +0800743 }
744
745 platform_set_drvdata(pdev, master);
Markus Elfring5dd381e2019-09-21 14:45:40 +0200746 mdata->base = devm_platform_ioremap_resource(pdev, 0);
Leilk Liua5682312015-08-07 15:19:50 +0800747 if (IS_ERR(mdata->base)) {
748 ret = PTR_ERR(mdata->base);
749 goto err_put_master;
750 }
751
752 irq = platform_get_irq(pdev, 0);
753 if (irq < 0) {
Leilk Liua5682312015-08-07 15:19:50 +0800754 ret = irq;
755 goto err_put_master;
756 }
757
758 if (!pdev->dev.dma_mask)
759 pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
760
761 ret = devm_request_irq(&pdev->dev, irq, mtk_spi_interrupt,
762 IRQF_TRIGGER_NONE, dev_name(&pdev->dev), master);
763 if (ret) {
764 dev_err(&pdev->dev, "failed to register irq (%d)\n", ret);
765 goto err_put_master;
766 }
767
Leilk Liua5682312015-08-07 15:19:50 +0800768 mdata->parent_clk = devm_clk_get(&pdev->dev, "parent-clk");
769 if (IS_ERR(mdata->parent_clk)) {
770 ret = PTR_ERR(mdata->parent_clk);
771 dev_err(&pdev->dev, "failed to get parent-clk: %d\n", ret);
772 goto err_put_master;
773 }
774
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800775 mdata->sel_clk = devm_clk_get(&pdev->dev, "sel-clk");
776 if (IS_ERR(mdata->sel_clk)) {
Javier Martinez Canillase26d15f2015-09-15 14:46:45 +0200777 ret = PTR_ERR(mdata->sel_clk);
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800778 dev_err(&pdev->dev, "failed to get sel-clk: %d\n", ret);
779 goto err_put_master;
780 }
781
782 mdata->spi_clk = devm_clk_get(&pdev->dev, "spi-clk");
783 if (IS_ERR(mdata->spi_clk)) {
Javier Martinez Canillase26d15f2015-09-15 14:46:45 +0200784 ret = PTR_ERR(mdata->spi_clk);
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800785 dev_err(&pdev->dev, "failed to get spi-clk: %d\n", ret);
786 goto err_put_master;
787 }
788
Leilk Liua5682312015-08-07 15:19:50 +0800789 ret = clk_prepare_enable(mdata->spi_clk);
790 if (ret < 0) {
791 dev_err(&pdev->dev, "failed to enable spi_clk (%d)\n", ret);
792 goto err_put_master;
793 }
794
Leilk Liuadcbcfe2015-08-31 21:18:57 +0800795 ret = clk_set_parent(mdata->sel_clk, mdata->parent_clk);
Leilk Liua5682312015-08-07 15:19:50 +0800796 if (ret < 0) {
797 dev_err(&pdev->dev, "failed to clk_set_parent (%d)\n", ret);
Leilk Liue38da372015-11-25 17:50:38 +0800798 clk_disable_unprepare(mdata->spi_clk);
799 goto err_put_master;
Leilk Liua5682312015-08-07 15:19:50 +0800800 }
801
802 clk_disable_unprepare(mdata->spi_clk);
803
804 pm_runtime_enable(&pdev->dev);
805
806 ret = devm_spi_register_master(&pdev->dev, master);
807 if (ret) {
808 dev_err(&pdev->dev, "failed to register master (%d)\n", ret);
Leilk Liue38da372015-11-25 17:50:38 +0800809 goto err_disable_runtime_pm;
Leilk Liua5682312015-08-07 15:19:50 +0800810 }
811
Leilk Liu37457602015-10-26 16:09:44 +0800812 if (mdata->dev_comp->need_pad_sel) {
813 if (mdata->pad_num != master->num_chipselect) {
814 dev_err(&pdev->dev,
815 "pad_num does not match num_chipselect(%d != %d)\n",
816 mdata->pad_num, master->num_chipselect);
817 ret = -EINVAL;
Leilk Liue38da372015-11-25 17:50:38 +0800818 goto err_disable_runtime_pm;
Leilk Liu37457602015-10-26 16:09:44 +0800819 }
820
Nicolas Boichat98c8dcc2015-11-09 12:14:51 +0800821 if (!master->cs_gpios && master->num_chipselect > 1) {
822 dev_err(&pdev->dev,
823 "cs_gpios not specified and num_chipselect > 1\n");
824 ret = -EINVAL;
Leilk Liue38da372015-11-25 17:50:38 +0800825 goto err_disable_runtime_pm;
Nicolas Boichat98c8dcc2015-11-09 12:14:51 +0800826 }
827
828 if (master->cs_gpios) {
829 for (i = 0; i < master->num_chipselect; i++) {
830 ret = devm_gpio_request(&pdev->dev,
831 master->cs_gpios[i],
832 dev_name(&pdev->dev));
833 if (ret) {
834 dev_err(&pdev->dev,
835 "can't get CS GPIO %i\n", i);
Leilk Liue38da372015-11-25 17:50:38 +0800836 goto err_disable_runtime_pm;
Nicolas Boichat98c8dcc2015-11-09 12:14:51 +0800837 }
Leilk Liu37457602015-10-26 16:09:44 +0800838 }
839 }
840 }
841
luhua.xufdeae8f2019-09-11 05:55:31 -0400842 if (mdata->dev_comp->dma_ext)
843 addr_bits = DMA_ADDR_EXT_BITS;
844 else
845 addr_bits = DMA_ADDR_DEF_BITS;
846 ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(addr_bits));
847 if (ret)
848 dev_notice(&pdev->dev, "SPI dma_set_mask(%d) failed, ret:%d\n",
849 addr_bits, ret);
850
Leilk Liua5682312015-08-07 15:19:50 +0800851 return 0;
852
Leilk Liue38da372015-11-25 17:50:38 +0800853err_disable_runtime_pm:
854 pm_runtime_disable(&pdev->dev);
Leilk Liua5682312015-08-07 15:19:50 +0800855err_put_master:
856 spi_master_put(master);
857
858 return ret;
859}
860
861static int mtk_spi_remove(struct platform_device *pdev)
862{
863 struct spi_master *master = platform_get_drvdata(pdev);
864 struct mtk_spi *mdata = spi_master_get_devdata(master);
865
866 pm_runtime_disable(&pdev->dev);
867
868 mtk_spi_reset(mdata);
Leilk Liua5682312015-08-07 15:19:50 +0800869
870 return 0;
871}
872
873#ifdef CONFIG_PM_SLEEP
874static int mtk_spi_suspend(struct device *dev)
875{
876 int ret;
877 struct spi_master *master = dev_get_drvdata(dev);
878 struct mtk_spi *mdata = spi_master_get_devdata(master);
879
880 ret = spi_master_suspend(master);
881 if (ret)
882 return ret;
883
884 if (!pm_runtime_suspended(dev))
885 clk_disable_unprepare(mdata->spi_clk);
886
887 return ret;
888}
889
890static int mtk_spi_resume(struct device *dev)
891{
892 int ret;
893 struct spi_master *master = dev_get_drvdata(dev);
894 struct mtk_spi *mdata = spi_master_get_devdata(master);
895
896 if (!pm_runtime_suspended(dev)) {
897 ret = clk_prepare_enable(mdata->spi_clk);
Leilk Liu13da5a02015-08-24 11:45:17 +0800898 if (ret < 0) {
899 dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
Leilk Liua5682312015-08-07 15:19:50 +0800900 return ret;
Leilk Liu13da5a02015-08-24 11:45:17 +0800901 }
Leilk Liua5682312015-08-07 15:19:50 +0800902 }
903
904 ret = spi_master_resume(master);
905 if (ret < 0)
906 clk_disable_unprepare(mdata->spi_clk);
907
908 return ret;
909}
910#endif /* CONFIG_PM_SLEEP */
911
912#ifdef CONFIG_PM
913static int mtk_spi_runtime_suspend(struct device *dev)
914{
915 struct spi_master *master = dev_get_drvdata(dev);
916 struct mtk_spi *mdata = spi_master_get_devdata(master);
917
918 clk_disable_unprepare(mdata->spi_clk);
919
920 return 0;
921}
922
923static int mtk_spi_runtime_resume(struct device *dev)
924{
925 struct spi_master *master = dev_get_drvdata(dev);
926 struct mtk_spi *mdata = spi_master_get_devdata(master);
Leilk Liu13da5a02015-08-24 11:45:17 +0800927 int ret;
Leilk Liua5682312015-08-07 15:19:50 +0800928
Leilk Liu13da5a02015-08-24 11:45:17 +0800929 ret = clk_prepare_enable(mdata->spi_clk);
930 if (ret < 0) {
931 dev_err(dev, "failed to enable spi_clk (%d)\n", ret);
932 return ret;
933 }
934
935 return 0;
Leilk Liua5682312015-08-07 15:19:50 +0800936}
937#endif /* CONFIG_PM */
938
939static const struct dev_pm_ops mtk_spi_pm = {
940 SET_SYSTEM_SLEEP_PM_OPS(mtk_spi_suspend, mtk_spi_resume)
941 SET_RUNTIME_PM_OPS(mtk_spi_runtime_suspend,
942 mtk_spi_runtime_resume, NULL)
943};
944
kbuild test robot4299aaa2015-08-07 22:33:11 +0800945static struct platform_driver mtk_spi_driver = {
Leilk Liua5682312015-08-07 15:19:50 +0800946 .driver = {
947 .name = "mtk-spi",
948 .pm = &mtk_spi_pm,
949 .of_match_table = mtk_spi_of_match,
950 },
951 .probe = mtk_spi_probe,
952 .remove = mtk_spi_remove,
953};
954
955module_platform_driver(mtk_spi_driver);
956
957MODULE_DESCRIPTION("MTK SPI Controller driver");
958MODULE_AUTHOR("Leilk Liu <leilk.liu@mediatek.com>");
959MODULE_LICENSE("GPL v2");
Axel Line4001882015-08-11 09:15:30 +0800960MODULE_ALIAS("platform:mtk-spi");