blob: 977e8b55c82b7ddc1dc1b05c41cfc838c93b40e0 [file] [log] [blame]
Fabio Estevam6126fd82018-05-02 16:18:29 -03001// SPDX-License-Identifier: GPL-2.0+
2//
3// Freescale i.MX7ULP LPSPI driver
4//
5// Copyright 2016 Freescale Semiconductor, Inc.
Clark Wang07d71552018-12-07 02:50:34 +00006// Copyright 2018 NXP Semiconductors
Gao Pan53149872016-11-22 21:52:17 +08007
8#include <linux/clk.h>
9#include <linux/completion.h>
10#include <linux/delay.h>
Clark Wang09c04462019-03-06 06:30:45 +000011#include <linux/dmaengine.h>
12#include <linux/dma-mapping.h>
Gao Pan53149872016-11-22 21:52:17 +080013#include <linux/err.h>
14#include <linux/interrupt.h>
15#include <linux/io.h>
16#include <linux/irq.h>
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/of.h>
Han Xu944c01a2019-03-06 06:30:39 +000020#include <linux/pinctrl/consumer.h>
Gao Pan53149872016-11-22 21:52:17 +080021#include <linux/platform_device.h>
Sascha Hauerc6547c22022-04-14 18:22:37 +020022#include <linux/dma/imx-dma.h>
Han Xu944c01a2019-03-06 06:30:39 +000023#include <linux/pm_runtime.h>
Gao Pan53149872016-11-22 21:52:17 +080024#include <linux/slab.h>
25#include <linux/spi/spi.h>
26#include <linux/spi/spi_bitbang.h>
27#include <linux/types.h>
28
29#define DRIVER_NAME "fsl_lpspi"
30
Han Xu944c01a2019-03-06 06:30:39 +000031#define FSL_LPSPI_RPM_TIMEOUT 50 /* 50ms */
32
Clark Wang09c04462019-03-06 06:30:45 +000033/* The maximum bytes that edma can transfer once.*/
34#define FSL_LPSPI_MAX_EDMA_BYTES ((1 << 15) - 1)
35
Gao Pan53149872016-11-22 21:52:17 +080036/* i.MX7ULP LPSPI registers */
37#define IMX7ULP_VERID 0x0
38#define IMX7ULP_PARAM 0x4
39#define IMX7ULP_CR 0x10
40#define IMX7ULP_SR 0x14
41#define IMX7ULP_IER 0x18
42#define IMX7ULP_DER 0x1c
43#define IMX7ULP_CFGR0 0x20
44#define IMX7ULP_CFGR1 0x24
45#define IMX7ULP_DMR0 0x30
46#define IMX7ULP_DMR1 0x34
47#define IMX7ULP_CCR 0x40
48#define IMX7ULP_FCR 0x58
49#define IMX7ULP_FSR 0x5c
50#define IMX7ULP_TCR 0x60
51#define IMX7ULP_TDR 0x64
52#define IMX7ULP_RSR 0x70
53#define IMX7ULP_RDR 0x74
54
55/* General control register field define */
56#define CR_RRF BIT(9)
57#define CR_RTF BIT(8)
58#define CR_RST BIT(1)
59#define CR_MEN BIT(0)
Clark Wang6a130442019-01-07 07:47:41 +000060#define SR_MBF BIT(24)
Gao Pan53149872016-11-22 21:52:17 +080061#define SR_TCF BIT(10)
Clark Wangc23fdef2019-01-07 07:47:38 +000062#define SR_FCF BIT(9)
Gao Pan53149872016-11-22 21:52:17 +080063#define SR_RDF BIT(1)
64#define SR_TDF BIT(0)
65#define IER_TCIE BIT(10)
Clark Wangc23fdef2019-01-07 07:47:38 +000066#define IER_FCIE BIT(9)
Gao Pan53149872016-11-22 21:52:17 +080067#define IER_RDIE BIT(1)
68#define IER_TDIE BIT(0)
Clark Wang09c04462019-03-06 06:30:45 +000069#define DER_RDDE BIT(1)
70#define DER_TDDE BIT(0)
Gao Pan53149872016-11-22 21:52:17 +080071#define CFGR1_PCSCFG BIT(27)
Clark Wangbcd87312018-12-07 02:50:36 +000072#define CFGR1_PINCFG (BIT(24)|BIT(25))
Gao Pan53149872016-11-22 21:52:17 +080073#define CFGR1_PCSPOL BIT(8)
74#define CFGR1_NOSTALL BIT(3)
Yang Yingliang2e2af402023-08-07 20:40:53 +080075#define CFGR1_HOST BIT(0)
Clark Wang69c8a9b2019-03-21 09:57:12 +000076#define FSR_TXCOUNT (0xFF)
Gao Pan53149872016-11-22 21:52:17 +080077#define RSR_RXEMPTY BIT(1)
78#define TCR_CPOL BIT(31)
79#define TCR_CPHA BIT(30)
80#define TCR_CONT BIT(21)
81#define TCR_CONTC BIT(20)
82#define TCR_RXMSK BIT(19)
83#define TCR_TXMSK BIT(18)
84
Carlos Song783bf5d2024-08-20 15:06:58 +080085struct fsl_lpspi_devtype_data {
86 u8 prescale_max;
87};
88
Gao Pan53149872016-11-22 21:52:17 +080089struct lpspi_config {
90 u8 bpw;
91 u8 chip_select;
92 u8 prescale;
93 u16 mode;
94 u32 speed_hz;
95};
96
97struct fsl_lpspi_data {
98 struct device *dev;
99 void __iomem *base;
Clark Wang09c04462019-03-06 06:30:45 +0000100 unsigned long base_phys;
Clark Wangf5e5afd2019-03-06 06:30:34 +0000101 struct clk *clk_ipg;
102 struct clk *clk_per;
Yang Yingliang2e2af402023-08-07 20:40:53 +0800103 bool is_target;
Clark Wang2a052592020-07-27 11:14:48 +0800104 bool is_only_cs1;
Clark Wangc7a40252019-03-06 06:30:43 +0000105 bool is_first_byte;
Gao Pan53149872016-11-22 21:52:17 +0800106
107 void *rx_buf;
108 const void *tx_buf;
109 void (*tx)(struct fsl_lpspi_data *);
110 void (*rx)(struct fsl_lpspi_data *);
111
112 u32 remain;
Clark Wangcf868742018-12-07 02:50:38 +0000113 u8 watermark;
Gao Pan53149872016-11-22 21:52:17 +0800114 u8 txfifosize;
115 u8 rxfifosize;
116
117 struct lpspi_config config;
118 struct completion xfer_done;
Clark Wangbcd87312018-12-07 02:50:36 +0000119
Yang Yingliang2e2af402023-08-07 20:40:53 +0800120 bool target_aborted;
Clark Wangc7a40252019-03-06 06:30:43 +0000121
Clark Wang09c04462019-03-06 06:30:45 +0000122 /* DMA */
123 bool usedma;
124 struct completion dma_rx_completion;
125 struct completion dma_tx_completion;
Carlos Song783bf5d2024-08-20 15:06:58 +0800126
127 const struct fsl_lpspi_devtype_data *devtype_data;
128};
129
130/*
131 * ERR051608 fixed or not:
132 * https://www.nxp.com/docs/en/errata/i.MX93_1P87f.pdf
133 */
134static struct fsl_lpspi_devtype_data imx93_lpspi_devtype_data = {
135 .prescale_max = 1,
136};
137
138static struct fsl_lpspi_devtype_data imx7ulp_lpspi_devtype_data = {
Stefan Wahrenff949d92024-09-05 13:15:37 +0200139 .prescale_max = 7,
Gao Pan53149872016-11-22 21:52:17 +0800140};
141
142static const struct of_device_id fsl_lpspi_dt_ids[] = {
Carlos Song783bf5d2024-08-20 15:06:58 +0800143 { .compatible = "fsl,imx7ulp-spi", .data = &imx7ulp_lpspi_devtype_data,},
144 { .compatible = "fsl,imx93-spi", .data = &imx93_lpspi_devtype_data,},
Gao Pan53149872016-11-22 21:52:17 +0800145 { /* sentinel */ }
146};
147MODULE_DEVICE_TABLE(of, fsl_lpspi_dt_ids);
148
149#define LPSPI_BUF_RX(type) \
150static void fsl_lpspi_buf_rx_##type(struct fsl_lpspi_data *fsl_lpspi) \
151{ \
152 unsigned int val = readl(fsl_lpspi->base + IMX7ULP_RDR); \
153 \
154 if (fsl_lpspi->rx_buf) { \
155 *(type *)fsl_lpspi->rx_buf = val; \
156 fsl_lpspi->rx_buf += sizeof(type); \
157 } \
158}
159
160#define LPSPI_BUF_TX(type) \
161static void fsl_lpspi_buf_tx_##type(struct fsl_lpspi_data *fsl_lpspi) \
162{ \
163 type val = 0; \
164 \
165 if (fsl_lpspi->tx_buf) { \
166 val = *(type *)fsl_lpspi->tx_buf; \
167 fsl_lpspi->tx_buf += sizeof(type); \
168 } \
169 \
170 fsl_lpspi->remain -= sizeof(type); \
171 writel(val, fsl_lpspi->base + IMX7ULP_TDR); \
172}
173
174LPSPI_BUF_RX(u8)
175LPSPI_BUF_TX(u8)
176LPSPI_BUF_RX(u16)
177LPSPI_BUF_TX(u16)
178LPSPI_BUF_RX(u32)
179LPSPI_BUF_TX(u32)
180
181static void fsl_lpspi_intctrl(struct fsl_lpspi_data *fsl_lpspi,
182 unsigned int enable)
183{
184 writel(enable, fsl_lpspi->base + IMX7ULP_IER);
185}
186
Clark Wang09c04462019-03-06 06:30:45 +0000187static int fsl_lpspi_bytes_per_word(const int bpw)
188{
189 return DIV_ROUND_UP(bpw, BITS_PER_BYTE);
190}
191
192static bool fsl_lpspi_can_dma(struct spi_controller *controller,
193 struct spi_device *spi,
194 struct spi_transfer *transfer)
195{
196 unsigned int bytes_per_word;
197
198 if (!controller->dma_rx)
199 return false;
200
201 bytes_per_word = fsl_lpspi_bytes_per_word(transfer->bits_per_word);
202
Aishwarya Rcb75b0c2020-04-07 18:25:57 +0530203 switch (bytes_per_word) {
204 case 1:
205 case 2:
206 case 4:
207 break;
208 default:
209 return false;
Clark Wang09c04462019-03-06 06:30:45 +0000210 }
211
212 return true;
213}
214
Clark Wang07d71552018-12-07 02:50:34 +0000215static int lpspi_prepare_xfer_hardware(struct spi_controller *controller)
Gao Pan53149872016-11-22 21:52:17 +0800216{
Clark Wang07d71552018-12-07 02:50:34 +0000217 struct fsl_lpspi_data *fsl_lpspi =
218 spi_controller_get_devdata(controller);
Clark Wangf5e5afd2019-03-06 06:30:34 +0000219 int ret;
Gao Pan53149872016-11-22 21:52:17 +0800220
Wang Lia0367542021-04-09 09:54:30 +0000221 ret = pm_runtime_resume_and_get(fsl_lpspi->dev);
Han Xu944c01a2019-03-06 06:30:39 +0000222 if (ret < 0) {
223 dev_err(fsl_lpspi->dev, "failed to enable clock\n");
Clark Wangf5e5afd2019-03-06 06:30:34 +0000224 return ret;
225 }
226
227 return 0;
Gao Pan53149872016-11-22 21:52:17 +0800228}
229
Clark Wang07d71552018-12-07 02:50:34 +0000230static int lpspi_unprepare_xfer_hardware(struct spi_controller *controller)
Gao Pan53149872016-11-22 21:52:17 +0800231{
Clark Wang07d71552018-12-07 02:50:34 +0000232 struct fsl_lpspi_data *fsl_lpspi =
233 spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800234
Han Xu944c01a2019-03-06 06:30:39 +0000235 pm_runtime_mark_last_busy(fsl_lpspi->dev);
236 pm_runtime_put_autosuspend(fsl_lpspi->dev);
Gao Pan53149872016-11-22 21:52:17 +0800237
238 return 0;
239}
240
Gao Pan53149872016-11-22 21:52:17 +0800241static void fsl_lpspi_write_tx_fifo(struct fsl_lpspi_data *fsl_lpspi)
242{
243 u8 txfifo_cnt;
Clark Wangc23fdef2019-01-07 07:47:38 +0000244 u32 temp;
Gao Pan53149872016-11-22 21:52:17 +0800245
246 txfifo_cnt = readl(fsl_lpspi->base + IMX7ULP_FSR) & 0xff;
247
248 while (txfifo_cnt < fsl_lpspi->txfifosize) {
249 if (!fsl_lpspi->remain)
250 break;
251 fsl_lpspi->tx(fsl_lpspi);
252 txfifo_cnt++;
253 }
254
Clark Wangc23fdef2019-01-07 07:47:38 +0000255 if (txfifo_cnt < fsl_lpspi->txfifosize) {
Yang Yingliang2e2af402023-08-07 20:40:53 +0800256 if (!fsl_lpspi->is_target) {
Clark Wangc23fdef2019-01-07 07:47:38 +0000257 temp = readl(fsl_lpspi->base + IMX7ULP_TCR);
258 temp &= ~TCR_CONTC;
259 writel(temp, fsl_lpspi->base + IMX7ULP_TCR);
260 }
261
262 fsl_lpspi_intctrl(fsl_lpspi, IER_FCIE);
263 } else
Gao Pan53149872016-11-22 21:52:17 +0800264 fsl_lpspi_intctrl(fsl_lpspi, IER_TDIE);
265}
266
267static void fsl_lpspi_read_rx_fifo(struct fsl_lpspi_data *fsl_lpspi)
268{
269 while (!(readl(fsl_lpspi->base + IMX7ULP_RSR) & RSR_RXEMPTY))
270 fsl_lpspi->rx(fsl_lpspi);
271}
272
Clark Wangc7a40252019-03-06 06:30:43 +0000273static void fsl_lpspi_set_cmd(struct fsl_lpspi_data *fsl_lpspi)
Gao Pan53149872016-11-22 21:52:17 +0800274{
275 u32 temp = 0;
276
277 temp |= fsl_lpspi->config.bpw - 1;
Gao Pane3a49392016-11-24 19:04:43 +0800278 temp |= (fsl_lpspi->config.mode & 0x3) << 30;
Clark Wang2a052592020-07-27 11:14:48 +0800279 temp |= (fsl_lpspi->config.chip_select & 0x3) << 24;
Yang Yingliang2e2af402023-08-07 20:40:53 +0800280 if (!fsl_lpspi->is_target) {
Clark Wangbcd87312018-12-07 02:50:36 +0000281 temp |= fsl_lpspi->config.prescale << 27;
Clark Wangbcd87312018-12-07 02:50:36 +0000282 /*
283 * Set TCR_CONT will keep SS asserted after current transfer.
284 * For the first transfer, clear TCR_CONTC to assert SS.
285 * For subsequent transfer, set TCR_CONTC to keep SS asserted.
286 */
Clark Wang09c04462019-03-06 06:30:45 +0000287 if (!fsl_lpspi->usedma) {
288 temp |= TCR_CONT;
289 if (fsl_lpspi->is_first_byte)
290 temp &= ~TCR_CONTC;
291 else
292 temp |= TCR_CONTC;
293 }
Clark Wangbcd87312018-12-07 02:50:36 +0000294 }
Gao Pan53149872016-11-22 21:52:17 +0800295 writel(temp, fsl_lpspi->base + IMX7ULP_TCR);
296
297 dev_dbg(fsl_lpspi->dev, "TCR=0x%x\n", temp);
298}
299
300static void fsl_lpspi_set_watermark(struct fsl_lpspi_data *fsl_lpspi)
301{
Gao Pan53149872016-11-22 21:52:17 +0800302 u32 temp;
303
Clark Wang09c04462019-03-06 06:30:45 +0000304 if (!fsl_lpspi->usedma)
305 temp = fsl_lpspi->watermark >> 1 |
306 (fsl_lpspi->watermark >> 1) << 16;
307 else
308 temp = fsl_lpspi->watermark >> 1;
Gao Pan53149872016-11-22 21:52:17 +0800309
310 writel(temp, fsl_lpspi->base + IMX7ULP_FCR);
311
312 dev_dbg(fsl_lpspi->dev, "FCR=0x%x\n", temp);
313}
314
315static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
316{
317 struct lpspi_config config = fsl_lpspi->config;
Stefan Wahren730bbfa2024-08-04 13:36:11 +0200318 unsigned int perclk_rate, scldiv, div;
Carlos Song783bf5d2024-08-20 15:06:58 +0800319 u8 prescale_max;
Gao Pan53149872016-11-22 21:52:17 +0800320 u8 prescale;
321
Clark Wangf5e5afd2019-03-06 06:30:34 +0000322 perclk_rate = clk_get_rate(fsl_lpspi->clk_per);
Carlos Song783bf5d2024-08-20 15:06:58 +0800323 prescale_max = fsl_lpspi->devtype_data->prescale_max;
Clark Wang77736a92019-03-06 06:30:41 +0000324
Clark Wangf571d912023-05-05 14:41:59 +0800325 if (!config.speed_hz) {
326 dev_err(fsl_lpspi->dev,
327 "error: the transmission speed provided is 0!\n");
328 return -EINVAL;
329 }
330
Clark Wang77736a92019-03-06 06:30:41 +0000331 if (config.speed_hz > perclk_rate / 2) {
332 dev_err(fsl_lpspi->dev,
333 "per-clk should be at least two times of transfer speed");
334 return -EINVAL;
335 }
336
Stefan Wahren730bbfa2024-08-04 13:36:11 +0200337 div = DIV_ROUND_UP(perclk_rate, config.speed_hz);
338
Stefan Wahrenff949d92024-09-05 13:15:37 +0200339 for (prescale = 0; prescale <= prescale_max; prescale++) {
Stefan Wahren730bbfa2024-08-04 13:36:11 +0200340 scldiv = div / (1 << prescale) - 2;
Gao Pan53149872016-11-22 21:52:17 +0800341 if (scldiv < 256) {
342 fsl_lpspi->config.prescale = prescale;
343 break;
344 }
345 }
346
Oleksandr Suvorov2fa98702020-02-20 14:11:48 +0000347 if (scldiv >= 256)
Gao Pan53149872016-11-22 21:52:17 +0800348 return -EINVAL;
349
Clark Wangcf868742018-12-07 02:50:38 +0000350 writel(scldiv | (scldiv << 8) | ((scldiv >> 1) << 16),
351 fsl_lpspi->base + IMX7ULP_CCR);
Gao Pan53149872016-11-22 21:52:17 +0800352
Clark Wang4e3891a2019-03-06 06:30:49 +0000353 dev_dbg(fsl_lpspi->dev, "perclk=%d, speed=%d, prescale=%d, scldiv=%d\n",
Gao Pan53149872016-11-22 21:52:17 +0800354 perclk_rate, config.speed_hz, prescale, scldiv);
355
356 return 0;
357}
358
Clark Wang09c04462019-03-06 06:30:45 +0000359static int fsl_lpspi_dma_configure(struct spi_controller *controller)
360{
361 int ret;
362 enum dma_slave_buswidth buswidth;
363 struct dma_slave_config rx = {}, tx = {};
364 struct fsl_lpspi_data *fsl_lpspi =
365 spi_controller_get_devdata(controller);
366
367 switch (fsl_lpspi_bytes_per_word(fsl_lpspi->config.bpw)) {
368 case 4:
369 buswidth = DMA_SLAVE_BUSWIDTH_4_BYTES;
370 break;
371 case 2:
372 buswidth = DMA_SLAVE_BUSWIDTH_2_BYTES;
373 break;
374 case 1:
375 buswidth = DMA_SLAVE_BUSWIDTH_1_BYTE;
376 break;
377 default:
378 return -EINVAL;
379 }
380
381 tx.direction = DMA_MEM_TO_DEV;
382 tx.dst_addr = fsl_lpspi->base_phys + IMX7ULP_TDR;
383 tx.dst_addr_width = buswidth;
384 tx.dst_maxburst = 1;
385 ret = dmaengine_slave_config(controller->dma_tx, &tx);
386 if (ret) {
387 dev_err(fsl_lpspi->dev, "TX dma configuration failed with %d\n",
388 ret);
389 return ret;
390 }
391
392 rx.direction = DMA_DEV_TO_MEM;
393 rx.src_addr = fsl_lpspi->base_phys + IMX7ULP_RDR;
394 rx.src_addr_width = buswidth;
395 rx.src_maxburst = 1;
396 ret = dmaengine_slave_config(controller->dma_rx, &rx);
397 if (ret) {
398 dev_err(fsl_lpspi->dev, "RX dma configuration failed with %d\n",
399 ret);
400 return ret;
401 }
402
403 return 0;
404}
405
Gao Pan53149872016-11-22 21:52:17 +0800406static int fsl_lpspi_config(struct fsl_lpspi_data *fsl_lpspi)
407{
408 u32 temp;
409 int ret;
410
Yang Yingliang2e2af402023-08-07 20:40:53 +0800411 if (!fsl_lpspi->is_target) {
Clark Wangbcd87312018-12-07 02:50:36 +0000412 ret = fsl_lpspi_set_bitrate(fsl_lpspi);
413 if (ret)
414 return ret;
415 }
Gao Pan53149872016-11-22 21:52:17 +0800416
417 fsl_lpspi_set_watermark(fsl_lpspi);
418
Yang Yingliang2e2af402023-08-07 20:40:53 +0800419 if (!fsl_lpspi->is_target)
420 temp = CFGR1_HOST;
Clark Wangbcd87312018-12-07 02:50:36 +0000421 else
422 temp = CFGR1_PINCFG;
Gao Pan53149872016-11-22 21:52:17 +0800423 if (fsl_lpspi->config.mode & SPI_CS_HIGH)
424 temp |= CFGR1_PCSPOL;
425 writel(temp, fsl_lpspi->base + IMX7ULP_CFGR1);
426
427 temp = readl(fsl_lpspi->base + IMX7ULP_CR);
428 temp |= CR_RRF | CR_RTF | CR_MEN;
429 writel(temp, fsl_lpspi->base + IMX7ULP_CR);
430
Clark Wang09c04462019-03-06 06:30:45 +0000431 temp = 0;
432 if (fsl_lpspi->usedma)
433 temp = DER_TDDE | DER_RDDE;
434 writel(temp, fsl_lpspi->base + IMX7ULP_DER);
435
Gao Pan53149872016-11-22 21:52:17 +0800436 return 0;
437}
438
Clark Wang09c04462019-03-06 06:30:45 +0000439static int fsl_lpspi_setup_transfer(struct spi_controller *controller,
440 struct spi_device *spi,
Gao Pan53149872016-11-22 21:52:17 +0800441 struct spi_transfer *t)
442{
Clark Wang07d71552018-12-07 02:50:34 +0000443 struct fsl_lpspi_data *fsl_lpspi =
444 spi_controller_get_devdata(spi->controller);
Gao Pan53149872016-11-22 21:52:17 +0800445
Clark Wang578465ea2019-03-06 06:30:47 +0000446 if (t == NULL)
447 return -EINVAL;
448
Gao Pan53149872016-11-22 21:52:17 +0800449 fsl_lpspi->config.mode = spi->mode;
Clark Wang578465ea2019-03-06 06:30:47 +0000450 fsl_lpspi->config.bpw = t->bits_per_word;
451 fsl_lpspi->config.speed_hz = t->speed_hz;
Clark Wang2a052592020-07-27 11:14:48 +0800452 if (fsl_lpspi->is_only_cs1)
453 fsl_lpspi->config.chip_select = 1;
454 else
Amit Kumar Mahapatra via Alsa-devel9e264f3f2023-03-10 23:02:03 +0530455 fsl_lpspi->config.chip_select = spi_get_chipselect(spi, 0);
Gao Pan53149872016-11-22 21:52:17 +0800456
457 if (!fsl_lpspi->config.speed_hz)
458 fsl_lpspi->config.speed_hz = spi->max_speed_hz;
459 if (!fsl_lpspi->config.bpw)
460 fsl_lpspi->config.bpw = spi->bits_per_word;
461
462 /* Initialize the functions for transfer */
463 if (fsl_lpspi->config.bpw <= 8) {
464 fsl_lpspi->rx = fsl_lpspi_buf_rx_u8;
465 fsl_lpspi->tx = fsl_lpspi_buf_tx_u8;
466 } else if (fsl_lpspi->config.bpw <= 16) {
467 fsl_lpspi->rx = fsl_lpspi_buf_rx_u16;
468 fsl_lpspi->tx = fsl_lpspi_buf_tx_u16;
469 } else {
470 fsl_lpspi->rx = fsl_lpspi_buf_rx_u32;
471 fsl_lpspi->tx = fsl_lpspi_buf_tx_u32;
472 }
473
Clark Wangcf868742018-12-07 02:50:38 +0000474 if (t->len <= fsl_lpspi->txfifosize)
475 fsl_lpspi->watermark = t->len;
476 else
477 fsl_lpspi->watermark = fsl_lpspi->txfifosize;
478
Clark Wang09c04462019-03-06 06:30:45 +0000479 if (fsl_lpspi_can_dma(controller, spi, t))
zhengbina68735d2019-12-24 11:52:04 +0800480 fsl_lpspi->usedma = true;
Clark Wang09c04462019-03-06 06:30:45 +0000481 else
zhengbina68735d2019-12-24 11:52:04 +0800482 fsl_lpspi->usedma = false;
Clark Wang09c04462019-03-06 06:30:45 +0000483
Clark Wang77736a92019-03-06 06:30:41 +0000484 return fsl_lpspi_config(fsl_lpspi);
Gao Pan53149872016-11-22 21:52:17 +0800485}
486
Yang Yingliang2e2af402023-08-07 20:40:53 +0800487static int fsl_lpspi_target_abort(struct spi_controller *controller)
Clark Wangbcd87312018-12-07 02:50:36 +0000488{
489 struct fsl_lpspi_data *fsl_lpspi =
490 spi_controller_get_devdata(controller);
491
Yang Yingliang2e2af402023-08-07 20:40:53 +0800492 fsl_lpspi->target_aborted = true;
Clark Wang8863eca2019-04-02 12:45:53 +0000493 if (!fsl_lpspi->usedma)
494 complete(&fsl_lpspi->xfer_done);
495 else {
496 complete(&fsl_lpspi->dma_tx_completion);
497 complete(&fsl_lpspi->dma_rx_completion);
498 }
499
Clark Wangbcd87312018-12-07 02:50:36 +0000500 return 0;
501}
502
503static int fsl_lpspi_wait_for_completion(struct spi_controller *controller)
504{
505 struct fsl_lpspi_data *fsl_lpspi =
506 spi_controller_get_devdata(controller);
507
Yang Yingliang2e2af402023-08-07 20:40:53 +0800508 if (fsl_lpspi->is_target) {
Clark Wangbcd87312018-12-07 02:50:36 +0000509 if (wait_for_completion_interruptible(&fsl_lpspi->xfer_done) ||
Yang Yingliang2e2af402023-08-07 20:40:53 +0800510 fsl_lpspi->target_aborted) {
Clark Wangbcd87312018-12-07 02:50:36 +0000511 dev_dbg(fsl_lpspi->dev, "interrupted\n");
512 return -EINTR;
513 }
514 } else {
515 if (!wait_for_completion_timeout(&fsl_lpspi->xfer_done, HZ)) {
516 dev_dbg(fsl_lpspi->dev, "wait for completion timeout\n");
517 return -ETIMEDOUT;
518 }
519 }
520
521 return 0;
522}
523
Clark Wanga15dc3d2019-01-07 07:47:43 +0000524static int fsl_lpspi_reset(struct fsl_lpspi_data *fsl_lpspi)
525{
526 u32 temp;
527
Clark Wang09c04462019-03-06 06:30:45 +0000528 if (!fsl_lpspi->usedma) {
529 /* Disable all interrupt */
530 fsl_lpspi_intctrl(fsl_lpspi, 0);
531 }
Clark Wanga15dc3d2019-01-07 07:47:43 +0000532
533 /* W1C for all flags in SR */
534 temp = 0x3F << 8;
535 writel(temp, fsl_lpspi->base + IMX7ULP_SR);
536
537 /* Clear FIFO and disable module */
538 temp = CR_RRF | CR_RTF;
539 writel(temp, fsl_lpspi->base + IMX7ULP_CR);
540
541 return 0;
542}
543
Clark Wang09c04462019-03-06 06:30:45 +0000544static void fsl_lpspi_dma_rx_callback(void *cookie)
545{
546 struct fsl_lpspi_data *fsl_lpspi = (struct fsl_lpspi_data *)cookie;
547
548 complete(&fsl_lpspi->dma_rx_completion);
549}
550
551static void fsl_lpspi_dma_tx_callback(void *cookie)
552{
553 struct fsl_lpspi_data *fsl_lpspi = (struct fsl_lpspi_data *)cookie;
554
555 complete(&fsl_lpspi->dma_tx_completion);
556}
557
558static int fsl_lpspi_calculate_timeout(struct fsl_lpspi_data *fsl_lpspi,
559 int size)
560{
561 unsigned long timeout = 0;
562
563 /* Time with actual data transfer and CS change delay related to HW */
564 timeout = (8 + 4) * size / fsl_lpspi->config.speed_hz;
565
566 /* Add extra second for scheduler related activities */
567 timeout += 1;
568
569 /* Double calculated timeout */
570 return msecs_to_jiffies(2 * timeout * MSEC_PER_SEC);
571}
572
573static int fsl_lpspi_dma_transfer(struct spi_controller *controller,
574 struct fsl_lpspi_data *fsl_lpspi,
575 struct spi_transfer *transfer)
576{
577 struct dma_async_tx_descriptor *desc_tx, *desc_rx;
578 unsigned long transfer_timeout;
Wolfram Sangeef51e92024-04-30 13:41:34 +0200579 unsigned long time_left;
Clark Wang09c04462019-03-06 06:30:45 +0000580 struct sg_table *tx = &transfer->tx_sg, *rx = &transfer->rx_sg;
581 int ret;
582
583 ret = fsl_lpspi_dma_configure(controller);
584 if (ret)
585 return ret;
586
587 desc_rx = dmaengine_prep_slave_sg(controller->dma_rx,
588 rx->sgl, rx->nents, DMA_DEV_TO_MEM,
589 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
590 if (!desc_rx)
591 return -EINVAL;
592
593 desc_rx->callback = fsl_lpspi_dma_rx_callback;
594 desc_rx->callback_param = (void *)fsl_lpspi;
595 dmaengine_submit(desc_rx);
596 reinit_completion(&fsl_lpspi->dma_rx_completion);
597 dma_async_issue_pending(controller->dma_rx);
598
599 desc_tx = dmaengine_prep_slave_sg(controller->dma_tx,
600 tx->sgl, tx->nents, DMA_MEM_TO_DEV,
601 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
602 if (!desc_tx) {
603 dmaengine_terminate_all(controller->dma_tx);
604 return -EINVAL;
605 }
606
607 desc_tx->callback = fsl_lpspi_dma_tx_callback;
608 desc_tx->callback_param = (void *)fsl_lpspi;
609 dmaengine_submit(desc_tx);
610 reinit_completion(&fsl_lpspi->dma_tx_completion);
611 dma_async_issue_pending(controller->dma_tx);
612
Yang Yingliang2e2af402023-08-07 20:40:53 +0800613 fsl_lpspi->target_aborted = false;
Clark Wang09c04462019-03-06 06:30:45 +0000614
Yang Yingliang2e2af402023-08-07 20:40:53 +0800615 if (!fsl_lpspi->is_target) {
Clark Wang09c04462019-03-06 06:30:45 +0000616 transfer_timeout = fsl_lpspi_calculate_timeout(fsl_lpspi,
617 transfer->len);
618
619 /* Wait eDMA to finish the data transfer.*/
Wolfram Sangeef51e92024-04-30 13:41:34 +0200620 time_left = wait_for_completion_timeout(&fsl_lpspi->dma_tx_completion,
621 transfer_timeout);
622 if (!time_left) {
Clark Wang09c04462019-03-06 06:30:45 +0000623 dev_err(fsl_lpspi->dev, "I/O Error in DMA TX\n");
624 dmaengine_terminate_all(controller->dma_tx);
625 dmaengine_terminate_all(controller->dma_rx);
626 fsl_lpspi_reset(fsl_lpspi);
627 return -ETIMEDOUT;
628 }
629
Wolfram Sangeef51e92024-04-30 13:41:34 +0200630 time_left = wait_for_completion_timeout(&fsl_lpspi->dma_rx_completion,
631 transfer_timeout);
632 if (!time_left) {
Clark Wang09c04462019-03-06 06:30:45 +0000633 dev_err(fsl_lpspi->dev, "I/O Error in DMA RX\n");
634 dmaengine_terminate_all(controller->dma_tx);
635 dmaengine_terminate_all(controller->dma_rx);
636 fsl_lpspi_reset(fsl_lpspi);
637 return -ETIMEDOUT;
638 }
639 } else {
640 if (wait_for_completion_interruptible(&fsl_lpspi->dma_tx_completion) ||
Yang Yingliang2e2af402023-08-07 20:40:53 +0800641 fsl_lpspi->target_aborted) {
Clark Wang09c04462019-03-06 06:30:45 +0000642 dev_dbg(fsl_lpspi->dev,
643 "I/O Error in DMA TX interrupted\n");
644 dmaengine_terminate_all(controller->dma_tx);
645 dmaengine_terminate_all(controller->dma_rx);
646 fsl_lpspi_reset(fsl_lpspi);
647 return -EINTR;
648 }
649
650 if (wait_for_completion_interruptible(&fsl_lpspi->dma_rx_completion) ||
Yang Yingliang2e2af402023-08-07 20:40:53 +0800651 fsl_lpspi->target_aborted) {
Clark Wang09c04462019-03-06 06:30:45 +0000652 dev_dbg(fsl_lpspi->dev,
653 "I/O Error in DMA RX interrupted\n");
654 dmaengine_terminate_all(controller->dma_tx);
655 dmaengine_terminate_all(controller->dma_rx);
656 fsl_lpspi_reset(fsl_lpspi);
657 return -EINTR;
658 }
659 }
660
661 fsl_lpspi_reset(fsl_lpspi);
662
663 return 0;
664}
665
666static void fsl_lpspi_dma_exit(struct spi_controller *controller)
667{
668 if (controller->dma_rx) {
669 dma_release_channel(controller->dma_rx);
670 controller->dma_rx = NULL;
671 }
672
673 if (controller->dma_tx) {
674 dma_release_channel(controller->dma_tx);
675 controller->dma_tx = NULL;
676 }
677}
678
679static int fsl_lpspi_dma_init(struct device *dev,
680 struct fsl_lpspi_data *fsl_lpspi,
681 struct spi_controller *controller)
682{
683 int ret;
684
685 /* Prepare for TX DMA: */
Peter Ujfalusi2e33f312019-11-13 11:42:50 +0200686 controller->dma_tx = dma_request_chan(dev, "tx");
Clark Wang09c04462019-03-06 06:30:45 +0000687 if (IS_ERR(controller->dma_tx)) {
688 ret = PTR_ERR(controller->dma_tx);
689 dev_dbg(dev, "can't get the TX DMA channel, error %d!\n", ret);
690 controller->dma_tx = NULL;
691 goto err;
692 }
693
694 /* Prepare for RX DMA: */
Peter Ujfalusi2e33f312019-11-13 11:42:50 +0200695 controller->dma_rx = dma_request_chan(dev, "rx");
Clark Wang09c04462019-03-06 06:30:45 +0000696 if (IS_ERR(controller->dma_rx)) {
697 ret = PTR_ERR(controller->dma_rx);
698 dev_dbg(dev, "can't get the RX DMA channel, error %d\n", ret);
699 controller->dma_rx = NULL;
700 goto err;
701 }
702
703 init_completion(&fsl_lpspi->dma_rx_completion);
704 init_completion(&fsl_lpspi->dma_tx_completion);
705 controller->can_dma = fsl_lpspi_can_dma;
706 controller->max_dma_len = FSL_LPSPI_MAX_EDMA_BYTES;
707
708 return 0;
709err:
710 fsl_lpspi_dma_exit(controller);
711 return ret;
712}
713
Clark Wangc7a40252019-03-06 06:30:43 +0000714static int fsl_lpspi_pio_transfer(struct spi_controller *controller,
Gao Pan53149872016-11-22 21:52:17 +0800715 struct spi_transfer *t)
716{
Clark Wang07d71552018-12-07 02:50:34 +0000717 struct fsl_lpspi_data *fsl_lpspi =
718 spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800719 int ret;
720
721 fsl_lpspi->tx_buf = t->tx_buf;
722 fsl_lpspi->rx_buf = t->rx_buf;
723 fsl_lpspi->remain = t->len;
724
725 reinit_completion(&fsl_lpspi->xfer_done);
Yang Yingliang2e2af402023-08-07 20:40:53 +0800726 fsl_lpspi->target_aborted = false;
Clark Wangbcd87312018-12-07 02:50:36 +0000727
Gao Pan53149872016-11-22 21:52:17 +0800728 fsl_lpspi_write_tx_fifo(fsl_lpspi);
Gao Pand2ad0a622016-11-28 11:02:59 +0800729
Clark Wangbcd87312018-12-07 02:50:36 +0000730 ret = fsl_lpspi_wait_for_completion(controller);
731 if (ret)
732 return ret;
Gao Pan53149872016-11-22 21:52:17 +0800733
Clark Wanga15dc3d2019-01-07 07:47:43 +0000734 fsl_lpspi_reset(fsl_lpspi);
735
Gao Pand989eed2016-12-02 11:50:01 +0800736 return 0;
Gao Pan53149872016-11-22 21:52:17 +0800737}
738
Clark Wangc7a40252019-03-06 06:30:43 +0000739static int fsl_lpspi_transfer_one(struct spi_controller *controller,
740 struct spi_device *spi,
741 struct spi_transfer *t)
Gao Pan53149872016-11-22 21:52:17 +0800742{
Clark Wang07d71552018-12-07 02:50:34 +0000743 struct fsl_lpspi_data *fsl_lpspi =
Clark Wangc7a40252019-03-06 06:30:43 +0000744 spi_controller_get_devdata(controller);
745 int ret;
Gao Pan53149872016-11-22 21:52:17 +0800746
Clark Wangc7a40252019-03-06 06:30:43 +0000747 fsl_lpspi->is_first_byte = true;
Clark Wang09c04462019-03-06 06:30:45 +0000748 ret = fsl_lpspi_setup_transfer(controller, spi, t);
Clark Wangc7a40252019-03-06 06:30:43 +0000749 if (ret < 0)
750 return ret;
Gao Pan53149872016-11-22 21:52:17 +0800751
Clark Wangc7a40252019-03-06 06:30:43 +0000752 fsl_lpspi_set_cmd(fsl_lpspi);
753 fsl_lpspi->is_first_byte = false;
Clark Wang77736a92019-03-06 06:30:41 +0000754
Clark Wang09c04462019-03-06 06:30:45 +0000755 if (fsl_lpspi->usedma)
756 ret = fsl_lpspi_dma_transfer(controller, fsl_lpspi, t);
757 else
758 ret = fsl_lpspi_pio_transfer(controller, t);
Clark Wangc7a40252019-03-06 06:30:43 +0000759 if (ret < 0)
760 return ret;
Gao Pan53149872016-11-22 21:52:17 +0800761
Clark Wangc7a40252019-03-06 06:30:43 +0000762 return 0;
Gao Pan53149872016-11-22 21:52:17 +0800763}
764
765static irqreturn_t fsl_lpspi_isr(int irq, void *dev_id)
766{
Clark Wangc23fdef2019-01-07 07:47:38 +0000767 u32 temp_SR, temp_IER;
Gao Pan53149872016-11-22 21:52:17 +0800768 struct fsl_lpspi_data *fsl_lpspi = dev_id;
Gao Pan53149872016-11-22 21:52:17 +0800769
Clark Wangc23fdef2019-01-07 07:47:38 +0000770 temp_IER = readl(fsl_lpspi->base + IMX7ULP_IER);
Gao Pan53149872016-11-22 21:52:17 +0800771 fsl_lpspi_intctrl(fsl_lpspi, 0);
Clark Wangc23fdef2019-01-07 07:47:38 +0000772 temp_SR = readl(fsl_lpspi->base + IMX7ULP_SR);
Gao Pan53149872016-11-22 21:52:17 +0800773
774 fsl_lpspi_read_rx_fifo(fsl_lpspi);
775
Clark Wangc23fdef2019-01-07 07:47:38 +0000776 if ((temp_SR & SR_TDF) && (temp_IER & IER_TDIE)) {
Gao Pan53149872016-11-22 21:52:17 +0800777 fsl_lpspi_write_tx_fifo(fsl_lpspi);
Clark Wangc23fdef2019-01-07 07:47:38 +0000778 return IRQ_HANDLED;
779 }
Gao Pan53149872016-11-22 21:52:17 +0800780
Clark Wang6a130442019-01-07 07:47:41 +0000781 if (temp_SR & SR_MBF ||
Clark Wang69c8a9b2019-03-21 09:57:12 +0000782 readl(fsl_lpspi->base + IMX7ULP_FSR) & FSR_TXCOUNT) {
Clark Wang6a130442019-01-07 07:47:41 +0000783 writel(SR_FCF, fsl_lpspi->base + IMX7ULP_SR);
784 fsl_lpspi_intctrl(fsl_lpspi, IER_FCIE);
785 return IRQ_HANDLED;
786 }
787
Clark Wangc23fdef2019-01-07 07:47:38 +0000788 if (temp_SR & SR_FCF && (temp_IER & IER_FCIE)) {
789 writel(SR_FCF, fsl_lpspi->base + IMX7ULP_SR);
Colin Ian King1b0a2b22019-09-26 12:37:01 +0100790 complete(&fsl_lpspi->xfer_done);
Gao Pan53149872016-11-22 21:52:17 +0800791 return IRQ_HANDLED;
792 }
793
794 return IRQ_NONE;
795}
796
Axel Lina18656e2019-04-07 22:58:16 +0800797#ifdef CONFIG_PM
kbuild test robot809b169a2019-03-19 09:46:33 +0800798static int fsl_lpspi_runtime_resume(struct device *dev)
Han Xu944c01a2019-03-06 06:30:39 +0000799{
Axel Lin6599be32019-04-07 22:58:15 +0800800 struct spi_controller *controller = dev_get_drvdata(dev);
801 struct fsl_lpspi_data *fsl_lpspi;
Han Xu944c01a2019-03-06 06:30:39 +0000802 int ret;
803
Axel Lin6599be32019-04-07 22:58:15 +0800804 fsl_lpspi = spi_controller_get_devdata(controller);
805
Han Xu944c01a2019-03-06 06:30:39 +0000806 ret = clk_prepare_enable(fsl_lpspi->clk_per);
807 if (ret)
808 return ret;
809
810 ret = clk_prepare_enable(fsl_lpspi->clk_ipg);
811 if (ret) {
812 clk_disable_unprepare(fsl_lpspi->clk_per);
813 return ret;
814 }
815
816 return 0;
817}
818
kbuild test robot809b169a2019-03-19 09:46:33 +0800819static int fsl_lpspi_runtime_suspend(struct device *dev)
Han Xu944c01a2019-03-06 06:30:39 +0000820{
Axel Lin6599be32019-04-07 22:58:15 +0800821 struct spi_controller *controller = dev_get_drvdata(dev);
822 struct fsl_lpspi_data *fsl_lpspi;
823
824 fsl_lpspi = spi_controller_get_devdata(controller);
Han Xu944c01a2019-03-06 06:30:39 +0000825
826 clk_disable_unprepare(fsl_lpspi->clk_per);
827 clk_disable_unprepare(fsl_lpspi->clk_ipg);
828
829 return 0;
830}
Axel Lina18656e2019-04-07 22:58:16 +0800831#endif
Han Xu944c01a2019-03-06 06:30:39 +0000832
833static int fsl_lpspi_init_rpm(struct fsl_lpspi_data *fsl_lpspi)
834{
835 struct device *dev = fsl_lpspi->dev;
836
837 pm_runtime_enable(dev);
838 pm_runtime_set_autosuspend_delay(dev, FSL_LPSPI_RPM_TIMEOUT);
839 pm_runtime_use_autosuspend(dev);
840
841 return 0;
842}
843
Gao Pan53149872016-11-22 21:52:17 +0800844static int fsl_lpspi_probe(struct platform_device *pdev)
845{
Carlos Song783bf5d2024-08-20 15:06:58 +0800846 const struct fsl_lpspi_devtype_data *devtype_data;
Gao Pan53149872016-11-22 21:52:17 +0800847 struct fsl_lpspi_data *fsl_lpspi;
Clark Wang07d71552018-12-07 02:50:34 +0000848 struct spi_controller *controller;
Gao Pan53149872016-11-22 21:52:17 +0800849 struct resource *res;
Linus Walleij8cdcd8a2020-06-25 22:02:52 +0200850 int ret, irq;
Alexander Steina55265e2023-07-05 11:01:43 +0200851 u32 num_cs;
Gao Panb88a0de2016-11-28 11:03:00 +0800852 u32 temp;
Yang Yingliang2e2af402023-08-07 20:40:53 +0800853 bool is_target;
Gao Pan53149872016-11-22 21:52:17 +0800854
Carlos Song783bf5d2024-08-20 15:06:58 +0800855 devtype_data = of_device_get_match_data(&pdev->dev);
856 if (!devtype_data)
857 return -ENODEV;
858
Yang Yingliang2e2af402023-08-07 20:40:53 +0800859 is_target = of_property_read_bool((&pdev->dev)->of_node, "spi-slave");
860 if (is_target)
Alexander Sverdlin2ae0ab02024-03-12 12:20:48 +0100861 controller = devm_spi_alloc_target(&pdev->dev,
862 sizeof(struct fsl_lpspi_data));
Clark Wangbcd87312018-12-07 02:50:36 +0000863 else
Alexander Sverdlin2ae0ab02024-03-12 12:20:48 +0100864 controller = devm_spi_alloc_host(&pdev->dev,
865 sizeof(struct fsl_lpspi_data));
Clark Wangbcd87312018-12-07 02:50:36 +0000866
Clark Wang07d71552018-12-07 02:50:34 +0000867 if (!controller)
Gao Pan53149872016-11-22 21:52:17 +0800868 return -ENOMEM;
869
Clark Wang07d71552018-12-07 02:50:34 +0000870 platform_set_drvdata(pdev, controller);
Gao Pan53149872016-11-22 21:52:17 +0800871
Clark Wang07d71552018-12-07 02:50:34 +0000872 fsl_lpspi = spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800873 fsl_lpspi->dev = &pdev->dev;
Yang Yingliang2e2af402023-08-07 20:40:53 +0800874 fsl_lpspi->is_target = is_target;
Clark Wang2a052592020-07-27 11:14:48 +0800875 fsl_lpspi->is_only_cs1 = of_property_read_bool((&pdev->dev)->of_node,
876 "fsl,spi-only-use-cs1-sel");
Carlos Song783bf5d2024-08-20 15:06:58 +0800877 fsl_lpspi->devtype_data = devtype_data;
Philippe Schenkerbc3a8b22019-12-04 14:13:33 +0000878
Gao Pan53149872016-11-22 21:52:17 +0800879 init_completion(&fsl_lpspi->xfer_done);
880
Yang Yingliangc9e1bb722022-09-24 21:18:53 +0800881 fsl_lpspi->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
Gao Pan53149872016-11-22 21:52:17 +0800882 if (IS_ERR(fsl_lpspi->base)) {
883 ret = PTR_ERR(fsl_lpspi->base);
Carlos Songbff892a2024-04-03 16:40:29 +0800884 return ret;
Gao Pan53149872016-11-22 21:52:17 +0800885 }
Clark Wang09c04462019-03-06 06:30:45 +0000886 fsl_lpspi->base_phys = res->start;
Gao Pan53149872016-11-22 21:52:17 +0800887
888 irq = platform_get_irq(pdev, 0);
889 if (irq < 0) {
890 ret = irq;
Carlos Songbff892a2024-04-03 16:40:29 +0800891 return ret;
Gao Pan53149872016-11-22 21:52:17 +0800892 }
893
894 ret = devm_request_irq(&pdev->dev, irq, fsl_lpspi_isr, 0,
895 dev_name(&pdev->dev), fsl_lpspi);
896 if (ret) {
897 dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
Carlos Songbff892a2024-04-03 16:40:29 +0800898 return ret;
Gao Pan53149872016-11-22 21:52:17 +0800899 }
900
Clark Wangf5e5afd2019-03-06 06:30:34 +0000901 fsl_lpspi->clk_per = devm_clk_get(&pdev->dev, "per");
902 if (IS_ERR(fsl_lpspi->clk_per)) {
903 ret = PTR_ERR(fsl_lpspi->clk_per);
Carlos Songbff892a2024-04-03 16:40:29 +0800904 return ret;
Gao Pan53149872016-11-22 21:52:17 +0800905 }
906
Clark Wangf5e5afd2019-03-06 06:30:34 +0000907 fsl_lpspi->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
908 if (IS_ERR(fsl_lpspi->clk_ipg)) {
909 ret = PTR_ERR(fsl_lpspi->clk_ipg);
Carlos Songbff892a2024-04-03 16:40:29 +0800910 return ret;
Clark Wangf5e5afd2019-03-06 06:30:34 +0000911 }
912
Han Xu944c01a2019-03-06 06:30:39 +0000913 /* enable the clock */
914 ret = fsl_lpspi_init_rpm(fsl_lpspi);
915 if (ret)
Carlos Songbff892a2024-04-03 16:40:29 +0800916 return ret;
Clark Wangf5e5afd2019-03-06 06:30:34 +0000917
Han Xu944c01a2019-03-06 06:30:39 +0000918 ret = pm_runtime_get_sync(fsl_lpspi->dev);
919 if (ret < 0) {
920 dev_err(fsl_lpspi->dev, "failed to enable clock\n");
Dinghao Liu8d728802020-05-23 21:38:59 +0800921 goto out_pm_get;
Gao Panb88a0de2016-11-28 11:03:00 +0800922 }
923
924 temp = readl(fsl_lpspi->base + IMX7ULP_PARAM);
925 fsl_lpspi->txfifosize = 1 << (temp & 0x0f);
926 fsl_lpspi->rxfifosize = 1 << ((temp >> 8) & 0x0f);
Alexander Steindfc07ee2023-07-17 10:59:33 +0200927 if (of_property_read_u32((&pdev->dev)->of_node, "num-cs",
Alexander Steinf46b06e2023-07-17 10:59:34 +0200928 &num_cs)) {
929 if (of_device_is_compatible(pdev->dev.of_node, "fsl,imx93-spi"))
930 num_cs = ((temp >> 16) & 0xf);
931 else
932 num_cs = 1;
933 }
Alexander Steindfc07ee2023-07-17 10:59:33 +0200934
935 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32);
936 controller->transfer_one = fsl_lpspi_transfer_one;
937 controller->prepare_transfer_hardware = lpspi_prepare_xfer_hardware;
938 controller->unprepare_transfer_hardware = lpspi_unprepare_xfer_hardware;
939 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
940 controller->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
941 controller->dev.of_node = pdev->dev.of_node;
942 controller->bus_num = pdev->id;
943 controller->num_chipselect = num_cs;
Yang Yingliang2e2af402023-08-07 20:40:53 +0800944 controller->target_abort = fsl_lpspi_target_abort;
945 if (!fsl_lpspi->is_target)
Alexander Steindfc07ee2023-07-17 10:59:33 +0200946 controller->use_gpio_descriptors = true;
Gao Panb88a0de2016-11-28 11:03:00 +0800947
Clark Wang09c04462019-03-06 06:30:45 +0000948 ret = fsl_lpspi_dma_init(&pdev->dev, fsl_lpspi, controller);
949 if (ret == -EPROBE_DEFER)
Dinghao Liu8d728802020-05-23 21:38:59 +0800950 goto out_pm_get;
Clark Wang09c04462019-03-06 06:30:45 +0000951 if (ret < 0)
Alexander Steind5786c82023-05-31 09:28:49 +0200952 dev_warn(&pdev->dev, "dma setup error %d, use pio\n", ret);
Clark Wang9728fb32023-05-05 14:35:57 +0800953 else
954 /*
955 * disable LPSPI module IRQ when enable DMA mode successfully,
956 * to prevent the unexpected LPSPI module IRQ events.
957 */
958 disable_irq(irq);
Clark Wang09c04462019-03-06 06:30:45 +0000959
Clark Wang16d79182020-07-27 11:14:46 +0800960 ret = devm_spi_register_controller(&pdev->dev, controller);
961 if (ret < 0) {
Christophe JAILLET0df874c2022-08-05 23:39:19 +0200962 dev_err_probe(&pdev->dev, ret, "spi_register_controller error\n");
Alexander Steinf02bff32021-11-09 11:31:34 +0100963 goto free_dma;
Clark Wang16d79182020-07-27 11:14:46 +0800964 }
965
Clark Wang2abbae52020-07-14 15:52:47 +0800966 pm_runtime_mark_last_busy(fsl_lpspi->dev);
967 pm_runtime_put_autosuspend(fsl_lpspi->dev);
968
Gao Pan53149872016-11-22 21:52:17 +0800969 return 0;
970
Alexander Steinf02bff32021-11-09 11:31:34 +0100971free_dma:
972 fsl_lpspi_dma_exit(controller);
Dinghao Liu8d728802020-05-23 21:38:59 +0800973out_pm_get:
Clark Wang2abbae52020-07-14 15:52:47 +0800974 pm_runtime_dont_use_autosuspend(fsl_lpspi->dev);
975 pm_runtime_put_sync(fsl_lpspi->dev);
976 pm_runtime_disable(fsl_lpspi->dev);
Gao Pan53149872016-11-22 21:52:17 +0800977
978 return ret;
979}
980
Uwe Kleine-Königedd49c82023-03-03 18:19:40 +0100981static void fsl_lpspi_remove(struct platform_device *pdev)
Gao Pan53149872016-11-22 21:52:17 +0800982{
Clark Wang07d71552018-12-07 02:50:34 +0000983 struct spi_controller *controller = platform_get_drvdata(pdev);
984 struct fsl_lpspi_data *fsl_lpspi =
985 spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800986
Alexander Steinf02bff32021-11-09 11:31:34 +0100987 fsl_lpspi_dma_exit(controller);
988
Jinjie Ruan3b577de2024-09-06 10:12:51 +0800989 pm_runtime_dont_use_autosuspend(fsl_lpspi->dev);
Han Xu944c01a2019-03-06 06:30:39 +0000990 pm_runtime_disable(fsl_lpspi->dev);
Gao Pan53149872016-11-22 21:52:17 +0800991}
992
Fabio Estevam6765e852024-06-24 21:20:23 -0300993static int fsl_lpspi_suspend(struct device *dev)
Han Xu944c01a2019-03-06 06:30:39 +0000994{
Han Xu944c01a2019-03-06 06:30:39 +0000995 pinctrl_pm_select_sleep_state(dev);
ye xingchen014eac32022-09-08 01:04:29 +0000996 return pm_runtime_force_suspend(dev);
Han Xu944c01a2019-03-06 06:30:39 +0000997}
998
Fabio Estevam6765e852024-06-24 21:20:23 -0300999static int fsl_lpspi_resume(struct device *dev)
Han Xu944c01a2019-03-06 06:30:39 +00001000{
1001 int ret;
1002
1003 ret = pm_runtime_force_resume(dev);
1004 if (ret) {
1005 dev_err(dev, "Error in resume: %d\n", ret);
1006 return ret;
1007 }
1008
1009 pinctrl_pm_select_default_state(dev);
1010
1011 return 0;
1012}
Han Xu944c01a2019-03-06 06:30:39 +00001013
1014static const struct dev_pm_ops fsl_lpspi_pm_ops = {
1015 SET_RUNTIME_PM_OPS(fsl_lpspi_runtime_suspend,
1016 fsl_lpspi_runtime_resume, NULL)
Fabio Estevam6765e852024-06-24 21:20:23 -03001017 SYSTEM_SLEEP_PM_OPS(fsl_lpspi_suspend, fsl_lpspi_resume)
Han Xu944c01a2019-03-06 06:30:39 +00001018};
1019
Gao Pan53149872016-11-22 21:52:17 +08001020static struct platform_driver fsl_lpspi_driver = {
1021 .driver = {
Gao Pan102ecc472017-01-04 17:38:16 +08001022 .name = DRIVER_NAME,
1023 .of_match_table = fsl_lpspi_dt_ids,
Fabio Estevam14201392024-06-25 15:39:19 -03001024 .pm = pm_ptr(&fsl_lpspi_pm_ops),
Gao Pan102ecc472017-01-04 17:38:16 +08001025 },
Gao Pan53149872016-11-22 21:52:17 +08001026 .probe = fsl_lpspi_probe,
Uwe Kleine-Königedd49c82023-03-03 18:19:40 +01001027 .remove_new = fsl_lpspi_remove,
Gao Pan53149872016-11-22 21:52:17 +08001028};
1029module_platform_driver(fsl_lpspi_driver);
1030
Clark Wang07d71552018-12-07 02:50:34 +00001031MODULE_DESCRIPTION("LPSPI Controller driver");
Gao Pan53149872016-11-22 21:52:17 +08001032MODULE_AUTHOR("Gao Pan <pandy.gao@nxp.com>");
Gao Panb6787b62016-12-02 11:50:00 +08001033MODULE_LICENSE("GPL");