blob: 69635cde0e221048f416068244348600c151501d [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>
11#include <linux/err.h>
12#include <linux/interrupt.h>
13#include <linux/io.h>
14#include <linux/irq.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/of_device.h>
Han Xu944c01a2019-03-06 06:30:39 +000019#include <linux/pinctrl/consumer.h>
Gao Pan53149872016-11-22 21:52:17 +080020#include <linux/platform_device.h>
Han Xu944c01a2019-03-06 06:30:39 +000021#include <linux/pm_runtime.h>
Gao Pan53149872016-11-22 21:52:17 +080022#include <linux/slab.h>
23#include <linux/spi/spi.h>
24#include <linux/spi/spi_bitbang.h>
25#include <linux/types.h>
26
27#define DRIVER_NAME "fsl_lpspi"
28
Han Xu944c01a2019-03-06 06:30:39 +000029#define FSL_LPSPI_RPM_TIMEOUT 50 /* 50ms */
30
Gao Pan53149872016-11-22 21:52:17 +080031/* i.MX7ULP LPSPI registers */
32#define IMX7ULP_VERID 0x0
33#define IMX7ULP_PARAM 0x4
34#define IMX7ULP_CR 0x10
35#define IMX7ULP_SR 0x14
36#define IMX7ULP_IER 0x18
37#define IMX7ULP_DER 0x1c
38#define IMX7ULP_CFGR0 0x20
39#define IMX7ULP_CFGR1 0x24
40#define IMX7ULP_DMR0 0x30
41#define IMX7ULP_DMR1 0x34
42#define IMX7ULP_CCR 0x40
43#define IMX7ULP_FCR 0x58
44#define IMX7ULP_FSR 0x5c
45#define IMX7ULP_TCR 0x60
46#define IMX7ULP_TDR 0x64
47#define IMX7ULP_RSR 0x70
48#define IMX7ULP_RDR 0x74
49
50/* General control register field define */
51#define CR_RRF BIT(9)
52#define CR_RTF BIT(8)
53#define CR_RST BIT(1)
54#define CR_MEN BIT(0)
Clark Wang6a130442019-01-07 07:47:41 +000055#define SR_MBF BIT(24)
Gao Pan53149872016-11-22 21:52:17 +080056#define SR_TCF BIT(10)
Clark Wangc23fdef2019-01-07 07:47:38 +000057#define SR_FCF BIT(9)
Gao Pan53149872016-11-22 21:52:17 +080058#define SR_RDF BIT(1)
59#define SR_TDF BIT(0)
60#define IER_TCIE BIT(10)
Clark Wangc23fdef2019-01-07 07:47:38 +000061#define IER_FCIE BIT(9)
Gao Pan53149872016-11-22 21:52:17 +080062#define IER_RDIE BIT(1)
63#define IER_TDIE BIT(0)
64#define CFGR1_PCSCFG BIT(27)
Clark Wangbcd87312018-12-07 02:50:36 +000065#define CFGR1_PINCFG (BIT(24)|BIT(25))
Gao Pan53149872016-11-22 21:52:17 +080066#define CFGR1_PCSPOL BIT(8)
67#define CFGR1_NOSTALL BIT(3)
68#define CFGR1_MASTER BIT(0)
Clark Wang6a130442019-01-07 07:47:41 +000069#define FSR_RXCOUNT (BIT(16)|BIT(17)|BIT(18))
Gao Pan53149872016-11-22 21:52:17 +080070#define RSR_RXEMPTY BIT(1)
71#define TCR_CPOL BIT(31)
72#define TCR_CPHA BIT(30)
73#define TCR_CONT BIT(21)
74#define TCR_CONTC BIT(20)
75#define TCR_RXMSK BIT(19)
76#define TCR_TXMSK BIT(18)
77
78static int clkdivs[] = {1, 2, 4, 8, 16, 32, 64, 128};
79
80struct lpspi_config {
81 u8 bpw;
82 u8 chip_select;
83 u8 prescale;
84 u16 mode;
85 u32 speed_hz;
86};
87
88struct fsl_lpspi_data {
89 struct device *dev;
90 void __iomem *base;
Clark Wangf5e5afd2019-03-06 06:30:34 +000091 struct clk *clk_ipg;
92 struct clk *clk_per;
Clark Wangbcd87312018-12-07 02:50:36 +000093 bool is_slave;
Gao Pan53149872016-11-22 21:52:17 +080094
95 void *rx_buf;
96 const void *tx_buf;
97 void (*tx)(struct fsl_lpspi_data *);
98 void (*rx)(struct fsl_lpspi_data *);
99
100 u32 remain;
Clark Wangcf868742018-12-07 02:50:38 +0000101 u8 watermark;
Gao Pan53149872016-11-22 21:52:17 +0800102 u8 txfifosize;
103 u8 rxfifosize;
104
105 struct lpspi_config config;
106 struct completion xfer_done;
Clark Wangbcd87312018-12-07 02:50:36 +0000107
108 bool slave_aborted;
Gao Pan53149872016-11-22 21:52:17 +0800109};
110
111static const struct of_device_id fsl_lpspi_dt_ids[] = {
112 { .compatible = "fsl,imx7ulp-spi", },
113 { /* sentinel */ }
114};
115MODULE_DEVICE_TABLE(of, fsl_lpspi_dt_ids);
116
117#define LPSPI_BUF_RX(type) \
118static void fsl_lpspi_buf_rx_##type(struct fsl_lpspi_data *fsl_lpspi) \
119{ \
120 unsigned int val = readl(fsl_lpspi->base + IMX7ULP_RDR); \
121 \
122 if (fsl_lpspi->rx_buf) { \
123 *(type *)fsl_lpspi->rx_buf = val; \
124 fsl_lpspi->rx_buf += sizeof(type); \
125 } \
126}
127
128#define LPSPI_BUF_TX(type) \
129static void fsl_lpspi_buf_tx_##type(struct fsl_lpspi_data *fsl_lpspi) \
130{ \
131 type val = 0; \
132 \
133 if (fsl_lpspi->tx_buf) { \
134 val = *(type *)fsl_lpspi->tx_buf; \
135 fsl_lpspi->tx_buf += sizeof(type); \
136 } \
137 \
138 fsl_lpspi->remain -= sizeof(type); \
139 writel(val, fsl_lpspi->base + IMX7ULP_TDR); \
140}
141
142LPSPI_BUF_RX(u8)
143LPSPI_BUF_TX(u8)
144LPSPI_BUF_RX(u16)
145LPSPI_BUF_TX(u16)
146LPSPI_BUF_RX(u32)
147LPSPI_BUF_TX(u32)
148
149static void fsl_lpspi_intctrl(struct fsl_lpspi_data *fsl_lpspi,
150 unsigned int enable)
151{
152 writel(enable, fsl_lpspi->base + IMX7ULP_IER);
153}
154
Clark Wang07d71552018-12-07 02:50:34 +0000155static int lpspi_prepare_xfer_hardware(struct spi_controller *controller)
Gao Pan53149872016-11-22 21:52:17 +0800156{
Clark Wang07d71552018-12-07 02:50:34 +0000157 struct fsl_lpspi_data *fsl_lpspi =
158 spi_controller_get_devdata(controller);
Clark Wangf5e5afd2019-03-06 06:30:34 +0000159 int ret;
Gao Pan53149872016-11-22 21:52:17 +0800160
Han Xu944c01a2019-03-06 06:30:39 +0000161 ret = pm_runtime_get_sync(fsl_lpspi->dev);
162 if (ret < 0) {
163 dev_err(fsl_lpspi->dev, "failed to enable clock\n");
Clark Wangf5e5afd2019-03-06 06:30:34 +0000164 return ret;
165 }
166
167 return 0;
Gao Pan53149872016-11-22 21:52:17 +0800168}
169
Clark Wang07d71552018-12-07 02:50:34 +0000170static int lpspi_unprepare_xfer_hardware(struct spi_controller *controller)
Gao Pan53149872016-11-22 21:52:17 +0800171{
Clark Wang07d71552018-12-07 02:50:34 +0000172 struct fsl_lpspi_data *fsl_lpspi =
173 spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800174
Han Xu944c01a2019-03-06 06:30:39 +0000175 pm_runtime_mark_last_busy(fsl_lpspi->dev);
176 pm_runtime_put_autosuspend(fsl_lpspi->dev);
Gao Pan53149872016-11-22 21:52:17 +0800177
178 return 0;
179}
180
Gao Pan53149872016-11-22 21:52:17 +0800181static void fsl_lpspi_write_tx_fifo(struct fsl_lpspi_data *fsl_lpspi)
182{
183 u8 txfifo_cnt;
Clark Wangc23fdef2019-01-07 07:47:38 +0000184 u32 temp;
Gao Pan53149872016-11-22 21:52:17 +0800185
186 txfifo_cnt = readl(fsl_lpspi->base + IMX7ULP_FSR) & 0xff;
187
188 while (txfifo_cnt < fsl_lpspi->txfifosize) {
189 if (!fsl_lpspi->remain)
190 break;
191 fsl_lpspi->tx(fsl_lpspi);
192 txfifo_cnt++;
193 }
194
Clark Wangc23fdef2019-01-07 07:47:38 +0000195 if (txfifo_cnt < fsl_lpspi->txfifosize) {
196 if (!fsl_lpspi->is_slave) {
197 temp = readl(fsl_lpspi->base + IMX7ULP_TCR);
198 temp &= ~TCR_CONTC;
199 writel(temp, fsl_lpspi->base + IMX7ULP_TCR);
200 }
201
202 fsl_lpspi_intctrl(fsl_lpspi, IER_FCIE);
203 } else
Gao Pan53149872016-11-22 21:52:17 +0800204 fsl_lpspi_intctrl(fsl_lpspi, IER_TDIE);
205}
206
207static void fsl_lpspi_read_rx_fifo(struct fsl_lpspi_data *fsl_lpspi)
208{
209 while (!(readl(fsl_lpspi->base + IMX7ULP_RSR) & RSR_RXEMPTY))
210 fsl_lpspi->rx(fsl_lpspi);
211}
212
213static void fsl_lpspi_set_cmd(struct fsl_lpspi_data *fsl_lpspi,
214 bool is_first_xfer)
215{
216 u32 temp = 0;
217
218 temp |= fsl_lpspi->config.bpw - 1;
Gao Pane3a49392016-11-24 19:04:43 +0800219 temp |= (fsl_lpspi->config.mode & 0x3) << 30;
Clark Wangbcd87312018-12-07 02:50:36 +0000220 if (!fsl_lpspi->is_slave) {
221 temp |= fsl_lpspi->config.prescale << 27;
222 temp |= (fsl_lpspi->config.chip_select & 0x3) << 24;
Gao Pan53149872016-11-22 21:52:17 +0800223
Clark Wangbcd87312018-12-07 02:50:36 +0000224 /*
225 * Set TCR_CONT will keep SS asserted after current transfer.
226 * For the first transfer, clear TCR_CONTC to assert SS.
227 * For subsequent transfer, set TCR_CONTC to keep SS asserted.
228 */
229 temp |= TCR_CONT;
230 if (is_first_xfer)
231 temp &= ~TCR_CONTC;
232 else
233 temp |= TCR_CONTC;
234 }
Gao Pan53149872016-11-22 21:52:17 +0800235 writel(temp, fsl_lpspi->base + IMX7ULP_TCR);
236
237 dev_dbg(fsl_lpspi->dev, "TCR=0x%x\n", temp);
238}
239
240static void fsl_lpspi_set_watermark(struct fsl_lpspi_data *fsl_lpspi)
241{
Gao Pan53149872016-11-22 21:52:17 +0800242 u32 temp;
243
Clark Wangcf868742018-12-07 02:50:38 +0000244 temp = fsl_lpspi->watermark >> 1 | (fsl_lpspi->watermark >> 1) << 16;
Gao Pan53149872016-11-22 21:52:17 +0800245
246 writel(temp, fsl_lpspi->base + IMX7ULP_FCR);
247
248 dev_dbg(fsl_lpspi->dev, "FCR=0x%x\n", temp);
249}
250
251static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
252{
253 struct lpspi_config config = fsl_lpspi->config;
254 unsigned int perclk_rate, scldiv;
255 u8 prescale;
256
Clark Wangf5e5afd2019-03-06 06:30:34 +0000257 perclk_rate = clk_get_rate(fsl_lpspi->clk_per);
Clark Wang77736a92019-03-06 06:30:41 +0000258
259 if (config.speed_hz > perclk_rate / 2) {
260 dev_err(fsl_lpspi->dev,
261 "per-clk should be at least two times of transfer speed");
262 return -EINVAL;
263 }
264
Gao Pan53149872016-11-22 21:52:17 +0800265 for (prescale = 0; prescale < 8; prescale++) {
266 scldiv = perclk_rate /
267 (clkdivs[prescale] * config.speed_hz) - 2;
268 if (scldiv < 256) {
269 fsl_lpspi->config.prescale = prescale;
270 break;
271 }
272 }
273
274 if (prescale == 8 && scldiv >= 256)
275 return -EINVAL;
276
Clark Wangcf868742018-12-07 02:50:38 +0000277 writel(scldiv | (scldiv << 8) | ((scldiv >> 1) << 16),
278 fsl_lpspi->base + IMX7ULP_CCR);
Gao Pan53149872016-11-22 21:52:17 +0800279
280 dev_dbg(fsl_lpspi->dev, "perclk=%d, speed=%d, prescale =%d, scldiv=%d\n",
281 perclk_rate, config.speed_hz, prescale, scldiv);
282
283 return 0;
284}
285
286static int fsl_lpspi_config(struct fsl_lpspi_data *fsl_lpspi)
287{
288 u32 temp;
289 int ret;
290
Clark Wangbcd87312018-12-07 02:50:36 +0000291 if (!fsl_lpspi->is_slave) {
292 ret = fsl_lpspi_set_bitrate(fsl_lpspi);
293 if (ret)
294 return ret;
295 }
Gao Pan53149872016-11-22 21:52:17 +0800296
297 fsl_lpspi_set_watermark(fsl_lpspi);
298
Clark Wangbcd87312018-12-07 02:50:36 +0000299 if (!fsl_lpspi->is_slave)
300 temp = CFGR1_MASTER;
301 else
302 temp = CFGR1_PINCFG;
Gao Pan53149872016-11-22 21:52:17 +0800303 if (fsl_lpspi->config.mode & SPI_CS_HIGH)
304 temp |= CFGR1_PCSPOL;
305 writel(temp, fsl_lpspi->base + IMX7ULP_CFGR1);
306
307 temp = readl(fsl_lpspi->base + IMX7ULP_CR);
308 temp |= CR_RRF | CR_RTF | CR_MEN;
309 writel(temp, fsl_lpspi->base + IMX7ULP_CR);
310
311 return 0;
312}
313
Clark Wang77736a92019-03-06 06:30:41 +0000314static int fsl_lpspi_setup_transfer(struct spi_device *spi,
Gao Pan53149872016-11-22 21:52:17 +0800315 struct spi_transfer *t)
316{
Clark Wang07d71552018-12-07 02:50:34 +0000317 struct fsl_lpspi_data *fsl_lpspi =
318 spi_controller_get_devdata(spi->controller);
Gao Pan53149872016-11-22 21:52:17 +0800319
320 fsl_lpspi->config.mode = spi->mode;
321 fsl_lpspi->config.bpw = t ? t->bits_per_word : spi->bits_per_word;
322 fsl_lpspi->config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;
323 fsl_lpspi->config.chip_select = spi->chip_select;
324
325 if (!fsl_lpspi->config.speed_hz)
326 fsl_lpspi->config.speed_hz = spi->max_speed_hz;
327 if (!fsl_lpspi->config.bpw)
328 fsl_lpspi->config.bpw = spi->bits_per_word;
329
330 /* Initialize the functions for transfer */
331 if (fsl_lpspi->config.bpw <= 8) {
332 fsl_lpspi->rx = fsl_lpspi_buf_rx_u8;
333 fsl_lpspi->tx = fsl_lpspi_buf_tx_u8;
334 } else if (fsl_lpspi->config.bpw <= 16) {
335 fsl_lpspi->rx = fsl_lpspi_buf_rx_u16;
336 fsl_lpspi->tx = fsl_lpspi_buf_tx_u16;
337 } else {
338 fsl_lpspi->rx = fsl_lpspi_buf_rx_u32;
339 fsl_lpspi->tx = fsl_lpspi_buf_tx_u32;
340 }
341
Clark Wangcf868742018-12-07 02:50:38 +0000342 if (t->len <= fsl_lpspi->txfifosize)
343 fsl_lpspi->watermark = t->len;
344 else
345 fsl_lpspi->watermark = fsl_lpspi->txfifosize;
346
Clark Wang77736a92019-03-06 06:30:41 +0000347 return fsl_lpspi_config(fsl_lpspi);
Gao Pan53149872016-11-22 21:52:17 +0800348}
349
Clark Wangbcd87312018-12-07 02:50:36 +0000350static int fsl_lpspi_slave_abort(struct spi_controller *controller)
351{
352 struct fsl_lpspi_data *fsl_lpspi =
353 spi_controller_get_devdata(controller);
354
355 fsl_lpspi->slave_aborted = true;
356 complete(&fsl_lpspi->xfer_done);
357 return 0;
358}
359
360static int fsl_lpspi_wait_for_completion(struct spi_controller *controller)
361{
362 struct fsl_lpspi_data *fsl_lpspi =
363 spi_controller_get_devdata(controller);
364
365 if (fsl_lpspi->is_slave) {
366 if (wait_for_completion_interruptible(&fsl_lpspi->xfer_done) ||
367 fsl_lpspi->slave_aborted) {
368 dev_dbg(fsl_lpspi->dev, "interrupted\n");
369 return -EINTR;
370 }
371 } else {
372 if (!wait_for_completion_timeout(&fsl_lpspi->xfer_done, HZ)) {
373 dev_dbg(fsl_lpspi->dev, "wait for completion timeout\n");
374 return -ETIMEDOUT;
375 }
376 }
377
378 return 0;
379}
380
Clark Wanga15dc3d2019-01-07 07:47:43 +0000381static int fsl_lpspi_reset(struct fsl_lpspi_data *fsl_lpspi)
382{
383 u32 temp;
384
385 /* Disable all interrupt */
386 fsl_lpspi_intctrl(fsl_lpspi, 0);
387
388 /* W1C for all flags in SR */
389 temp = 0x3F << 8;
390 writel(temp, fsl_lpspi->base + IMX7ULP_SR);
391
392 /* Clear FIFO and disable module */
393 temp = CR_RRF | CR_RTF;
394 writel(temp, fsl_lpspi->base + IMX7ULP_CR);
395
396 return 0;
397}
398
Clark Wang07d71552018-12-07 02:50:34 +0000399static int fsl_lpspi_transfer_one(struct spi_controller *controller,
Gao Pan53149872016-11-22 21:52:17 +0800400 struct spi_device *spi,
401 struct spi_transfer *t)
402{
Clark Wang07d71552018-12-07 02:50:34 +0000403 struct fsl_lpspi_data *fsl_lpspi =
404 spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800405 int ret;
406
407 fsl_lpspi->tx_buf = t->tx_buf;
408 fsl_lpspi->rx_buf = t->rx_buf;
409 fsl_lpspi->remain = t->len;
410
411 reinit_completion(&fsl_lpspi->xfer_done);
Clark Wangbcd87312018-12-07 02:50:36 +0000412 fsl_lpspi->slave_aborted = false;
413
Gao Pan53149872016-11-22 21:52:17 +0800414 fsl_lpspi_write_tx_fifo(fsl_lpspi);
Gao Pand2ad0a622016-11-28 11:02:59 +0800415
Clark Wangbcd87312018-12-07 02:50:36 +0000416 ret = fsl_lpspi_wait_for_completion(controller);
417 if (ret)
418 return ret;
Gao Pan53149872016-11-22 21:52:17 +0800419
Clark Wanga15dc3d2019-01-07 07:47:43 +0000420 fsl_lpspi_reset(fsl_lpspi);
421
Gao Pand989eed2016-12-02 11:50:01 +0800422 return 0;
Gao Pan53149872016-11-22 21:52:17 +0800423}
424
Clark Wang07d71552018-12-07 02:50:34 +0000425static int fsl_lpspi_transfer_one_msg(struct spi_controller *controller,
Gao Pan53149872016-11-22 21:52:17 +0800426 struct spi_message *msg)
427{
Clark Wang07d71552018-12-07 02:50:34 +0000428 struct fsl_lpspi_data *fsl_lpspi =
429 spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800430 struct spi_device *spi = msg->spi;
431 struct spi_transfer *xfer;
432 bool is_first_xfer = true;
Geert Uytterhoevencc4a7ff2016-12-14 12:20:55 +0100433 int ret = 0;
Gao Pan53149872016-11-22 21:52:17 +0800434
435 msg->status = 0;
436 msg->actual_length = 0;
437
438 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Clark Wang77736a92019-03-06 06:30:41 +0000439 ret = fsl_lpspi_setup_transfer(spi, xfer);
440 if (ret < 0)
441 goto complete;
442
Gao Pan53149872016-11-22 21:52:17 +0800443 fsl_lpspi_set_cmd(fsl_lpspi, is_first_xfer);
444
445 is_first_xfer = false;
446
Clark Wang07d71552018-12-07 02:50:34 +0000447 ret = fsl_lpspi_transfer_one(controller, spi, xfer);
Gao Pan53149872016-11-22 21:52:17 +0800448 if (ret < 0)
449 goto complete;
450
451 msg->actual_length += xfer->len;
452 }
453
454complete:
Gao Pan53149872016-11-22 21:52:17 +0800455 msg->status = ret;
Clark Wang07d71552018-12-07 02:50:34 +0000456 spi_finalize_current_message(controller);
Gao Pan53149872016-11-22 21:52:17 +0800457
458 return ret;
459}
460
461static irqreturn_t fsl_lpspi_isr(int irq, void *dev_id)
462{
Clark Wangc23fdef2019-01-07 07:47:38 +0000463 u32 temp_SR, temp_IER;
Gao Pan53149872016-11-22 21:52:17 +0800464 struct fsl_lpspi_data *fsl_lpspi = dev_id;
Gao Pan53149872016-11-22 21:52:17 +0800465
Clark Wangc23fdef2019-01-07 07:47:38 +0000466 temp_IER = readl(fsl_lpspi->base + IMX7ULP_IER);
Gao Pan53149872016-11-22 21:52:17 +0800467 fsl_lpspi_intctrl(fsl_lpspi, 0);
Clark Wangc23fdef2019-01-07 07:47:38 +0000468 temp_SR = readl(fsl_lpspi->base + IMX7ULP_SR);
Gao Pan53149872016-11-22 21:52:17 +0800469
470 fsl_lpspi_read_rx_fifo(fsl_lpspi);
471
Clark Wangc23fdef2019-01-07 07:47:38 +0000472 if ((temp_SR & SR_TDF) && (temp_IER & IER_TDIE)) {
Gao Pan53149872016-11-22 21:52:17 +0800473 fsl_lpspi_write_tx_fifo(fsl_lpspi);
Clark Wangc23fdef2019-01-07 07:47:38 +0000474 return IRQ_HANDLED;
475 }
Gao Pan53149872016-11-22 21:52:17 +0800476
Clark Wang6a130442019-01-07 07:47:41 +0000477 if (temp_SR & SR_MBF ||
478 readl(fsl_lpspi->base + IMX7ULP_FSR) & FSR_RXCOUNT) {
479 writel(SR_FCF, fsl_lpspi->base + IMX7ULP_SR);
480 fsl_lpspi_intctrl(fsl_lpspi, IER_FCIE);
481 return IRQ_HANDLED;
482 }
483
Clark Wangc23fdef2019-01-07 07:47:38 +0000484 if (temp_SR & SR_FCF && (temp_IER & IER_FCIE)) {
485 writel(SR_FCF, fsl_lpspi->base + IMX7ULP_SR);
Gao Pan53149872016-11-22 21:52:17 +0800486 complete(&fsl_lpspi->xfer_done);
Gao Pan53149872016-11-22 21:52:17 +0800487 return IRQ_HANDLED;
488 }
489
490 return IRQ_NONE;
491}
492
Han Xu944c01a2019-03-06 06:30:39 +0000493int fsl_lpspi_runtime_resume(struct device *dev)
494{
495 struct fsl_lpspi_data *fsl_lpspi = dev_get_drvdata(dev);
496 int ret;
497
498 ret = clk_prepare_enable(fsl_lpspi->clk_per);
499 if (ret)
500 return ret;
501
502 ret = clk_prepare_enable(fsl_lpspi->clk_ipg);
503 if (ret) {
504 clk_disable_unprepare(fsl_lpspi->clk_per);
505 return ret;
506 }
507
508 return 0;
509}
510
511int fsl_lpspi_runtime_suspend(struct device *dev)
512{
513 struct fsl_lpspi_data *fsl_lpspi = dev_get_drvdata(dev);
514
515 clk_disable_unprepare(fsl_lpspi->clk_per);
516 clk_disable_unprepare(fsl_lpspi->clk_ipg);
517
518 return 0;
519}
520
521static int fsl_lpspi_init_rpm(struct fsl_lpspi_data *fsl_lpspi)
522{
523 struct device *dev = fsl_lpspi->dev;
524
525 pm_runtime_enable(dev);
526 pm_runtime_set_autosuspend_delay(dev, FSL_LPSPI_RPM_TIMEOUT);
527 pm_runtime_use_autosuspend(dev);
528
529 return 0;
530}
531
Gao Pan53149872016-11-22 21:52:17 +0800532static int fsl_lpspi_probe(struct platform_device *pdev)
533{
534 struct fsl_lpspi_data *fsl_lpspi;
Clark Wang07d71552018-12-07 02:50:34 +0000535 struct spi_controller *controller;
Gao Pan53149872016-11-22 21:52:17 +0800536 struct resource *res;
537 int ret, irq;
Gao Panb88a0de2016-11-28 11:03:00 +0800538 u32 temp;
Gao Pan53149872016-11-22 21:52:17 +0800539
Clark Wangbcd87312018-12-07 02:50:36 +0000540 if (of_property_read_bool((&pdev->dev)->of_node, "spi-slave"))
541 controller = spi_alloc_slave(&pdev->dev,
Clark Wang07d71552018-12-07 02:50:34 +0000542 sizeof(struct fsl_lpspi_data));
Clark Wangbcd87312018-12-07 02:50:36 +0000543 else
544 controller = spi_alloc_master(&pdev->dev,
545 sizeof(struct fsl_lpspi_data));
546
Clark Wang07d71552018-12-07 02:50:34 +0000547 if (!controller)
Gao Pan53149872016-11-22 21:52:17 +0800548 return -ENOMEM;
549
Clark Wang07d71552018-12-07 02:50:34 +0000550 platform_set_drvdata(pdev, controller);
Gao Pan53149872016-11-22 21:52:17 +0800551
Clark Wang07d71552018-12-07 02:50:34 +0000552 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32);
553 controller->bus_num = pdev->id;
Gao Pan53149872016-11-22 21:52:17 +0800554
Clark Wang07d71552018-12-07 02:50:34 +0000555 fsl_lpspi = spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800556 fsl_lpspi->dev = &pdev->dev;
Han Xu944c01a2019-03-06 06:30:39 +0000557 dev_set_drvdata(&pdev->dev, fsl_lpspi);
Clark Wangbcd87312018-12-07 02:50:36 +0000558 fsl_lpspi->is_slave = of_property_read_bool((&pdev->dev)->of_node,
559 "spi-slave");
Gao Pan53149872016-11-22 21:52:17 +0800560
Clark Wang07d71552018-12-07 02:50:34 +0000561 controller->transfer_one_message = fsl_lpspi_transfer_one_msg;
562 controller->prepare_transfer_hardware = lpspi_prepare_xfer_hardware;
563 controller->unprepare_transfer_hardware = lpspi_unprepare_xfer_hardware;
564 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
565 controller->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
566 controller->dev.of_node = pdev->dev.of_node;
567 controller->bus_num = pdev->id;
Clark Wangbcd87312018-12-07 02:50:36 +0000568 controller->slave_abort = fsl_lpspi_slave_abort;
Gao Pan53149872016-11-22 21:52:17 +0800569
570 init_completion(&fsl_lpspi->xfer_done);
571
572 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
573 fsl_lpspi->base = devm_ioremap_resource(&pdev->dev, res);
574 if (IS_ERR(fsl_lpspi->base)) {
575 ret = PTR_ERR(fsl_lpspi->base);
Clark Wang07d71552018-12-07 02:50:34 +0000576 goto out_controller_put;
Gao Pan53149872016-11-22 21:52:17 +0800577 }
578
579 irq = platform_get_irq(pdev, 0);
580 if (irq < 0) {
581 ret = irq;
Clark Wang07d71552018-12-07 02:50:34 +0000582 goto out_controller_put;
Gao Pan53149872016-11-22 21:52:17 +0800583 }
584
585 ret = devm_request_irq(&pdev->dev, irq, fsl_lpspi_isr, 0,
586 dev_name(&pdev->dev), fsl_lpspi);
587 if (ret) {
588 dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
Clark Wang07d71552018-12-07 02:50:34 +0000589 goto out_controller_put;
Gao Pan53149872016-11-22 21:52:17 +0800590 }
591
Clark Wangf5e5afd2019-03-06 06:30:34 +0000592 fsl_lpspi->clk_per = devm_clk_get(&pdev->dev, "per");
593 if (IS_ERR(fsl_lpspi->clk_per)) {
594 ret = PTR_ERR(fsl_lpspi->clk_per);
Clark Wang07d71552018-12-07 02:50:34 +0000595 goto out_controller_put;
Gao Pan53149872016-11-22 21:52:17 +0800596 }
597
Clark Wangf5e5afd2019-03-06 06:30:34 +0000598 fsl_lpspi->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
599 if (IS_ERR(fsl_lpspi->clk_ipg)) {
600 ret = PTR_ERR(fsl_lpspi->clk_ipg);
601 goto out_controller_put;
602 }
603
Han Xu944c01a2019-03-06 06:30:39 +0000604 /* enable the clock */
605 ret = fsl_lpspi_init_rpm(fsl_lpspi);
606 if (ret)
Clark Wangf5e5afd2019-03-06 06:30:34 +0000607 goto out_controller_put;
Clark Wangf5e5afd2019-03-06 06:30:34 +0000608
Han Xu944c01a2019-03-06 06:30:39 +0000609 ret = pm_runtime_get_sync(fsl_lpspi->dev);
610 if (ret < 0) {
611 dev_err(fsl_lpspi->dev, "failed to enable clock\n");
612 return ret;
Gao Panb88a0de2016-11-28 11:03:00 +0800613 }
614
615 temp = readl(fsl_lpspi->base + IMX7ULP_PARAM);
616 fsl_lpspi->txfifosize = 1 << (temp & 0x0f);
617 fsl_lpspi->rxfifosize = 1 << ((temp >> 8) & 0x0f);
618
Clark Wang07d71552018-12-07 02:50:34 +0000619 ret = devm_spi_register_controller(&pdev->dev, controller);
Gao Pan53149872016-11-22 21:52:17 +0800620 if (ret < 0) {
Clark Wang07d71552018-12-07 02:50:34 +0000621 dev_err(&pdev->dev, "spi_register_controller error.\n");
622 goto out_controller_put;
Gao Pan53149872016-11-22 21:52:17 +0800623 }
624
625 return 0;
626
Clark Wang07d71552018-12-07 02:50:34 +0000627out_controller_put:
628 spi_controller_put(controller);
Gao Pan53149872016-11-22 21:52:17 +0800629
630 return ret;
631}
632
633static int fsl_lpspi_remove(struct platform_device *pdev)
634{
Clark Wang07d71552018-12-07 02:50:34 +0000635 struct spi_controller *controller = platform_get_drvdata(pdev);
636 struct fsl_lpspi_data *fsl_lpspi =
637 spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800638
Han Xu944c01a2019-03-06 06:30:39 +0000639 pm_runtime_disable(fsl_lpspi->dev);
640
641 spi_master_put(controller);
Gao Pan53149872016-11-22 21:52:17 +0800642
643 return 0;
644}
645
Han Xu944c01a2019-03-06 06:30:39 +0000646#ifdef CONFIG_PM_SLEEP
647static int fsl_lpspi_suspend(struct device *dev)
648{
649 int ret;
650
651 pinctrl_pm_select_sleep_state(dev);
652 ret = pm_runtime_force_suspend(dev);
653 return ret;
654}
655
656static int fsl_lpspi_resume(struct device *dev)
657{
658 int ret;
659
660 ret = pm_runtime_force_resume(dev);
661 if (ret) {
662 dev_err(dev, "Error in resume: %d\n", ret);
663 return ret;
664 }
665
666 pinctrl_pm_select_default_state(dev);
667
668 return 0;
669}
670#endif /* CONFIG_PM_SLEEP */
671
672static const struct dev_pm_ops fsl_lpspi_pm_ops = {
673 SET_RUNTIME_PM_OPS(fsl_lpspi_runtime_suspend,
674 fsl_lpspi_runtime_resume, NULL)
675 SET_SYSTEM_SLEEP_PM_OPS(fsl_lpspi_suspend, fsl_lpspi_resume)
676};
677
Gao Pan53149872016-11-22 21:52:17 +0800678static struct platform_driver fsl_lpspi_driver = {
679 .driver = {
Gao Pan102ecc472017-01-04 17:38:16 +0800680 .name = DRIVER_NAME,
681 .of_match_table = fsl_lpspi_dt_ids,
Han Xu944c01a2019-03-06 06:30:39 +0000682 .pm = &fsl_lpspi_pm_ops,
Gao Pan102ecc472017-01-04 17:38:16 +0800683 },
Gao Pan53149872016-11-22 21:52:17 +0800684 .probe = fsl_lpspi_probe,
685 .remove = fsl_lpspi_remove,
686};
687module_platform_driver(fsl_lpspi_driver);
688
Clark Wang07d71552018-12-07 02:50:34 +0000689MODULE_DESCRIPTION("LPSPI Controller driver");
Gao Pan53149872016-11-22 21:52:17 +0800690MODULE_AUTHOR("Gao Pan <pandy.gao@nxp.com>");
Gao Panb6787b62016-12-02 11:50:00 +0800691MODULE_LICENSE("GPL");