blob: a25e0e03f0583a15de5770f6391406557b0d2e4f [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>
Clark Wangc7a40252019-03-06 06:30:43 +000012#include <linux/gpio.h>
Gao Pan53149872016-11-22 21:52:17 +080013#include <linux/interrupt.h>
14#include <linux/io.h>
15#include <linux/irq.h>
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/of_device.h>
Clark Wangc7a40252019-03-06 06:30:43 +000020#include <linux/of_gpio.h>
Han Xu944c01a2019-03-06 06:30:39 +000021#include <linux/pinctrl/consumer.h>
Gao Pan53149872016-11-22 21:52:17 +080022#include <linux/platform_device.h>
Clark Wangc7a40252019-03-06 06:30:43 +000023#include <linux/platform_data/spi-imx.h>
Han Xu944c01a2019-03-06 06:30:39 +000024#include <linux/pm_runtime.h>
Gao Pan53149872016-11-22 21:52:17 +080025#include <linux/slab.h>
26#include <linux/spi/spi.h>
27#include <linux/spi/spi_bitbang.h>
28#include <linux/types.h>
29
30#define DRIVER_NAME "fsl_lpspi"
31
Han Xu944c01a2019-03-06 06:30:39 +000032#define FSL_LPSPI_RPM_TIMEOUT 50 /* 50ms */
33
Gao Pan53149872016-11-22 21:52:17 +080034/* i.MX7ULP LPSPI registers */
35#define IMX7ULP_VERID 0x0
36#define IMX7ULP_PARAM 0x4
37#define IMX7ULP_CR 0x10
38#define IMX7ULP_SR 0x14
39#define IMX7ULP_IER 0x18
40#define IMX7ULP_DER 0x1c
41#define IMX7ULP_CFGR0 0x20
42#define IMX7ULP_CFGR1 0x24
43#define IMX7ULP_DMR0 0x30
44#define IMX7ULP_DMR1 0x34
45#define IMX7ULP_CCR 0x40
46#define IMX7ULP_FCR 0x58
47#define IMX7ULP_FSR 0x5c
48#define IMX7ULP_TCR 0x60
49#define IMX7ULP_TDR 0x64
50#define IMX7ULP_RSR 0x70
51#define IMX7ULP_RDR 0x74
52
53/* General control register field define */
54#define CR_RRF BIT(9)
55#define CR_RTF BIT(8)
56#define CR_RST BIT(1)
57#define CR_MEN BIT(0)
Clark Wang6a130442019-01-07 07:47:41 +000058#define SR_MBF BIT(24)
Gao Pan53149872016-11-22 21:52:17 +080059#define SR_TCF BIT(10)
Clark Wangc23fdef2019-01-07 07:47:38 +000060#define SR_FCF BIT(9)
Gao Pan53149872016-11-22 21:52:17 +080061#define SR_RDF BIT(1)
62#define SR_TDF BIT(0)
63#define IER_TCIE BIT(10)
Clark Wangc23fdef2019-01-07 07:47:38 +000064#define IER_FCIE BIT(9)
Gao Pan53149872016-11-22 21:52:17 +080065#define IER_RDIE BIT(1)
66#define IER_TDIE BIT(0)
67#define CFGR1_PCSCFG BIT(27)
Clark Wangbcd87312018-12-07 02:50:36 +000068#define CFGR1_PINCFG (BIT(24)|BIT(25))
Gao Pan53149872016-11-22 21:52:17 +080069#define CFGR1_PCSPOL BIT(8)
70#define CFGR1_NOSTALL BIT(3)
71#define CFGR1_MASTER BIT(0)
Clark Wang6a130442019-01-07 07:47:41 +000072#define FSR_RXCOUNT (BIT(16)|BIT(17)|BIT(18))
Gao Pan53149872016-11-22 21:52:17 +080073#define RSR_RXEMPTY BIT(1)
74#define TCR_CPOL BIT(31)
75#define TCR_CPHA BIT(30)
76#define TCR_CONT BIT(21)
77#define TCR_CONTC BIT(20)
78#define TCR_RXMSK BIT(19)
79#define TCR_TXMSK BIT(18)
80
81static int clkdivs[] = {1, 2, 4, 8, 16, 32, 64, 128};
82
83struct lpspi_config {
84 u8 bpw;
85 u8 chip_select;
86 u8 prescale;
87 u16 mode;
88 u32 speed_hz;
89};
90
91struct fsl_lpspi_data {
92 struct device *dev;
93 void __iomem *base;
Clark Wangf5e5afd2019-03-06 06:30:34 +000094 struct clk *clk_ipg;
95 struct clk *clk_per;
Clark Wangbcd87312018-12-07 02:50:36 +000096 bool is_slave;
Clark Wangc7a40252019-03-06 06:30:43 +000097 bool is_first_byte;
Gao Pan53149872016-11-22 21:52:17 +080098
99 void *rx_buf;
100 const void *tx_buf;
101 void (*tx)(struct fsl_lpspi_data *);
102 void (*rx)(struct fsl_lpspi_data *);
103
104 u32 remain;
Clark Wangcf868742018-12-07 02:50:38 +0000105 u8 watermark;
Gao Pan53149872016-11-22 21:52:17 +0800106 u8 txfifosize;
107 u8 rxfifosize;
108
109 struct lpspi_config config;
110 struct completion xfer_done;
Clark Wangbcd87312018-12-07 02:50:36 +0000111
112 bool slave_aborted;
Clark Wangc7a40252019-03-06 06:30:43 +0000113
114 int chipselect[0];
Gao Pan53149872016-11-22 21:52:17 +0800115};
116
117static const struct of_device_id fsl_lpspi_dt_ids[] = {
118 { .compatible = "fsl,imx7ulp-spi", },
119 { /* sentinel */ }
120};
121MODULE_DEVICE_TABLE(of, fsl_lpspi_dt_ids);
122
123#define LPSPI_BUF_RX(type) \
124static void fsl_lpspi_buf_rx_##type(struct fsl_lpspi_data *fsl_lpspi) \
125{ \
126 unsigned int val = readl(fsl_lpspi->base + IMX7ULP_RDR); \
127 \
128 if (fsl_lpspi->rx_buf) { \
129 *(type *)fsl_lpspi->rx_buf = val; \
130 fsl_lpspi->rx_buf += sizeof(type); \
131 } \
132}
133
134#define LPSPI_BUF_TX(type) \
135static void fsl_lpspi_buf_tx_##type(struct fsl_lpspi_data *fsl_lpspi) \
136{ \
137 type val = 0; \
138 \
139 if (fsl_lpspi->tx_buf) { \
140 val = *(type *)fsl_lpspi->tx_buf; \
141 fsl_lpspi->tx_buf += sizeof(type); \
142 } \
143 \
144 fsl_lpspi->remain -= sizeof(type); \
145 writel(val, fsl_lpspi->base + IMX7ULP_TDR); \
146}
147
148LPSPI_BUF_RX(u8)
149LPSPI_BUF_TX(u8)
150LPSPI_BUF_RX(u16)
151LPSPI_BUF_TX(u16)
152LPSPI_BUF_RX(u32)
153LPSPI_BUF_TX(u32)
154
155static void fsl_lpspi_intctrl(struct fsl_lpspi_data *fsl_lpspi,
156 unsigned int enable)
157{
158 writel(enable, fsl_lpspi->base + IMX7ULP_IER);
159}
160
Clark Wang07d71552018-12-07 02:50:34 +0000161static int lpspi_prepare_xfer_hardware(struct spi_controller *controller)
Gao Pan53149872016-11-22 21:52:17 +0800162{
Clark Wang07d71552018-12-07 02:50:34 +0000163 struct fsl_lpspi_data *fsl_lpspi =
164 spi_controller_get_devdata(controller);
Clark Wangf5e5afd2019-03-06 06:30:34 +0000165 int ret;
Gao Pan53149872016-11-22 21:52:17 +0800166
Han Xu944c01a2019-03-06 06:30:39 +0000167 ret = pm_runtime_get_sync(fsl_lpspi->dev);
168 if (ret < 0) {
169 dev_err(fsl_lpspi->dev, "failed to enable clock\n");
Clark Wangf5e5afd2019-03-06 06:30:34 +0000170 return ret;
171 }
172
173 return 0;
Gao Pan53149872016-11-22 21:52:17 +0800174}
175
Clark Wang07d71552018-12-07 02:50:34 +0000176static int lpspi_unprepare_xfer_hardware(struct spi_controller *controller)
Gao Pan53149872016-11-22 21:52:17 +0800177{
Clark Wang07d71552018-12-07 02:50:34 +0000178 struct fsl_lpspi_data *fsl_lpspi =
179 spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800180
Han Xu944c01a2019-03-06 06:30:39 +0000181 pm_runtime_mark_last_busy(fsl_lpspi->dev);
182 pm_runtime_put_autosuspend(fsl_lpspi->dev);
Gao Pan53149872016-11-22 21:52:17 +0800183
184 return 0;
185}
186
Clark Wangc7a40252019-03-06 06:30:43 +0000187static int fsl_lpspi_prepare_message(struct spi_controller *controller,
188 struct spi_message *msg)
189{
190 struct fsl_lpspi_data *fsl_lpspi =
191 spi_controller_get_devdata(controller);
192 struct spi_device *spi = msg->spi;
193 int gpio = fsl_lpspi->chipselect[spi->chip_select];
194
195 if (gpio_is_valid(gpio))
196 gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH ? 0 : 1);
197
198 return 0;
199}
200
Gao Pan53149872016-11-22 21:52:17 +0800201static void fsl_lpspi_write_tx_fifo(struct fsl_lpspi_data *fsl_lpspi)
202{
203 u8 txfifo_cnt;
Clark Wangc23fdef2019-01-07 07:47:38 +0000204 u32 temp;
Gao Pan53149872016-11-22 21:52:17 +0800205
206 txfifo_cnt = readl(fsl_lpspi->base + IMX7ULP_FSR) & 0xff;
207
208 while (txfifo_cnt < fsl_lpspi->txfifosize) {
209 if (!fsl_lpspi->remain)
210 break;
211 fsl_lpspi->tx(fsl_lpspi);
212 txfifo_cnt++;
213 }
214
Clark Wangc23fdef2019-01-07 07:47:38 +0000215 if (txfifo_cnt < fsl_lpspi->txfifosize) {
216 if (!fsl_lpspi->is_slave) {
217 temp = readl(fsl_lpspi->base + IMX7ULP_TCR);
218 temp &= ~TCR_CONTC;
219 writel(temp, fsl_lpspi->base + IMX7ULP_TCR);
220 }
221
222 fsl_lpspi_intctrl(fsl_lpspi, IER_FCIE);
223 } else
Gao Pan53149872016-11-22 21:52:17 +0800224 fsl_lpspi_intctrl(fsl_lpspi, IER_TDIE);
225}
226
227static void fsl_lpspi_read_rx_fifo(struct fsl_lpspi_data *fsl_lpspi)
228{
229 while (!(readl(fsl_lpspi->base + IMX7ULP_RSR) & RSR_RXEMPTY))
230 fsl_lpspi->rx(fsl_lpspi);
231}
232
Clark Wangc7a40252019-03-06 06:30:43 +0000233static void fsl_lpspi_set_cmd(struct fsl_lpspi_data *fsl_lpspi)
Gao Pan53149872016-11-22 21:52:17 +0800234{
235 u32 temp = 0;
236
237 temp |= fsl_lpspi->config.bpw - 1;
Gao Pane3a49392016-11-24 19:04:43 +0800238 temp |= (fsl_lpspi->config.mode & 0x3) << 30;
Clark Wangbcd87312018-12-07 02:50:36 +0000239 if (!fsl_lpspi->is_slave) {
240 temp |= fsl_lpspi->config.prescale << 27;
241 temp |= (fsl_lpspi->config.chip_select & 0x3) << 24;
Gao Pan53149872016-11-22 21:52:17 +0800242
Clark Wangbcd87312018-12-07 02:50:36 +0000243 /*
244 * Set TCR_CONT will keep SS asserted after current transfer.
245 * For the first transfer, clear TCR_CONTC to assert SS.
246 * For subsequent transfer, set TCR_CONTC to keep SS asserted.
247 */
248 temp |= TCR_CONT;
Clark Wangc7a40252019-03-06 06:30:43 +0000249 if (fsl_lpspi->is_first_byte)
Clark Wangbcd87312018-12-07 02:50:36 +0000250 temp &= ~TCR_CONTC;
251 else
252 temp |= TCR_CONTC;
253 }
Gao Pan53149872016-11-22 21:52:17 +0800254 writel(temp, fsl_lpspi->base + IMX7ULP_TCR);
255
256 dev_dbg(fsl_lpspi->dev, "TCR=0x%x\n", temp);
257}
258
259static void fsl_lpspi_set_watermark(struct fsl_lpspi_data *fsl_lpspi)
260{
Gao Pan53149872016-11-22 21:52:17 +0800261 u32 temp;
262
Clark Wangcf868742018-12-07 02:50:38 +0000263 temp = fsl_lpspi->watermark >> 1 | (fsl_lpspi->watermark >> 1) << 16;
Gao Pan53149872016-11-22 21:52:17 +0800264
265 writel(temp, fsl_lpspi->base + IMX7ULP_FCR);
266
267 dev_dbg(fsl_lpspi->dev, "FCR=0x%x\n", temp);
268}
269
270static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
271{
272 struct lpspi_config config = fsl_lpspi->config;
273 unsigned int perclk_rate, scldiv;
274 u8 prescale;
275
Clark Wangf5e5afd2019-03-06 06:30:34 +0000276 perclk_rate = clk_get_rate(fsl_lpspi->clk_per);
Clark Wang77736a92019-03-06 06:30:41 +0000277
278 if (config.speed_hz > perclk_rate / 2) {
279 dev_err(fsl_lpspi->dev,
280 "per-clk should be at least two times of transfer speed");
281 return -EINVAL;
282 }
283
Gao Pan53149872016-11-22 21:52:17 +0800284 for (prescale = 0; prescale < 8; prescale++) {
285 scldiv = perclk_rate /
286 (clkdivs[prescale] * config.speed_hz) - 2;
287 if (scldiv < 256) {
288 fsl_lpspi->config.prescale = prescale;
289 break;
290 }
291 }
292
293 if (prescale == 8 && scldiv >= 256)
294 return -EINVAL;
295
Clark Wangcf868742018-12-07 02:50:38 +0000296 writel(scldiv | (scldiv << 8) | ((scldiv >> 1) << 16),
297 fsl_lpspi->base + IMX7ULP_CCR);
Gao Pan53149872016-11-22 21:52:17 +0800298
299 dev_dbg(fsl_lpspi->dev, "perclk=%d, speed=%d, prescale =%d, scldiv=%d\n",
300 perclk_rate, config.speed_hz, prescale, scldiv);
301
302 return 0;
303}
304
305static int fsl_lpspi_config(struct fsl_lpspi_data *fsl_lpspi)
306{
307 u32 temp;
308 int ret;
309
Clark Wangbcd87312018-12-07 02:50:36 +0000310 if (!fsl_lpspi->is_slave) {
311 ret = fsl_lpspi_set_bitrate(fsl_lpspi);
312 if (ret)
313 return ret;
314 }
Gao Pan53149872016-11-22 21:52:17 +0800315
316 fsl_lpspi_set_watermark(fsl_lpspi);
317
Clark Wangbcd87312018-12-07 02:50:36 +0000318 if (!fsl_lpspi->is_slave)
319 temp = CFGR1_MASTER;
320 else
321 temp = CFGR1_PINCFG;
Gao Pan53149872016-11-22 21:52:17 +0800322 if (fsl_lpspi->config.mode & SPI_CS_HIGH)
323 temp |= CFGR1_PCSPOL;
324 writel(temp, fsl_lpspi->base + IMX7ULP_CFGR1);
325
326 temp = readl(fsl_lpspi->base + IMX7ULP_CR);
327 temp |= CR_RRF | CR_RTF | CR_MEN;
328 writel(temp, fsl_lpspi->base + IMX7ULP_CR);
329
330 return 0;
331}
332
Clark Wang77736a92019-03-06 06:30:41 +0000333static int fsl_lpspi_setup_transfer(struct spi_device *spi,
Gao Pan53149872016-11-22 21:52:17 +0800334 struct spi_transfer *t)
335{
Clark Wang07d71552018-12-07 02:50:34 +0000336 struct fsl_lpspi_data *fsl_lpspi =
337 spi_controller_get_devdata(spi->controller);
Gao Pan53149872016-11-22 21:52:17 +0800338
339 fsl_lpspi->config.mode = spi->mode;
340 fsl_lpspi->config.bpw = t ? t->bits_per_word : spi->bits_per_word;
341 fsl_lpspi->config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;
342 fsl_lpspi->config.chip_select = spi->chip_select;
343
344 if (!fsl_lpspi->config.speed_hz)
345 fsl_lpspi->config.speed_hz = spi->max_speed_hz;
346 if (!fsl_lpspi->config.bpw)
347 fsl_lpspi->config.bpw = spi->bits_per_word;
348
349 /* Initialize the functions for transfer */
350 if (fsl_lpspi->config.bpw <= 8) {
351 fsl_lpspi->rx = fsl_lpspi_buf_rx_u8;
352 fsl_lpspi->tx = fsl_lpspi_buf_tx_u8;
353 } else if (fsl_lpspi->config.bpw <= 16) {
354 fsl_lpspi->rx = fsl_lpspi_buf_rx_u16;
355 fsl_lpspi->tx = fsl_lpspi_buf_tx_u16;
356 } else {
357 fsl_lpspi->rx = fsl_lpspi_buf_rx_u32;
358 fsl_lpspi->tx = fsl_lpspi_buf_tx_u32;
359 }
360
Clark Wangcf868742018-12-07 02:50:38 +0000361 if (t->len <= fsl_lpspi->txfifosize)
362 fsl_lpspi->watermark = t->len;
363 else
364 fsl_lpspi->watermark = fsl_lpspi->txfifosize;
365
Clark Wang77736a92019-03-06 06:30:41 +0000366 return fsl_lpspi_config(fsl_lpspi);
Gao Pan53149872016-11-22 21:52:17 +0800367}
368
Clark Wangbcd87312018-12-07 02:50:36 +0000369static int fsl_lpspi_slave_abort(struct spi_controller *controller)
370{
371 struct fsl_lpspi_data *fsl_lpspi =
372 spi_controller_get_devdata(controller);
373
374 fsl_lpspi->slave_aborted = true;
375 complete(&fsl_lpspi->xfer_done);
376 return 0;
377}
378
379static int fsl_lpspi_wait_for_completion(struct spi_controller *controller)
380{
381 struct fsl_lpspi_data *fsl_lpspi =
382 spi_controller_get_devdata(controller);
383
384 if (fsl_lpspi->is_slave) {
385 if (wait_for_completion_interruptible(&fsl_lpspi->xfer_done) ||
386 fsl_lpspi->slave_aborted) {
387 dev_dbg(fsl_lpspi->dev, "interrupted\n");
388 return -EINTR;
389 }
390 } else {
391 if (!wait_for_completion_timeout(&fsl_lpspi->xfer_done, HZ)) {
392 dev_dbg(fsl_lpspi->dev, "wait for completion timeout\n");
393 return -ETIMEDOUT;
394 }
395 }
396
397 return 0;
398}
399
Clark Wanga15dc3d2019-01-07 07:47:43 +0000400static int fsl_lpspi_reset(struct fsl_lpspi_data *fsl_lpspi)
401{
402 u32 temp;
403
404 /* Disable all interrupt */
405 fsl_lpspi_intctrl(fsl_lpspi, 0);
406
407 /* W1C for all flags in SR */
408 temp = 0x3F << 8;
409 writel(temp, fsl_lpspi->base + IMX7ULP_SR);
410
411 /* Clear FIFO and disable module */
412 temp = CR_RRF | CR_RTF;
413 writel(temp, fsl_lpspi->base + IMX7ULP_CR);
414
415 return 0;
416}
417
Clark Wangc7a40252019-03-06 06:30:43 +0000418static int fsl_lpspi_pio_transfer(struct spi_controller *controller,
Gao Pan53149872016-11-22 21:52:17 +0800419 struct spi_transfer *t)
420{
Clark Wang07d71552018-12-07 02:50:34 +0000421 struct fsl_lpspi_data *fsl_lpspi =
422 spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800423 int ret;
424
425 fsl_lpspi->tx_buf = t->tx_buf;
426 fsl_lpspi->rx_buf = t->rx_buf;
427 fsl_lpspi->remain = t->len;
428
429 reinit_completion(&fsl_lpspi->xfer_done);
Clark Wangbcd87312018-12-07 02:50:36 +0000430 fsl_lpspi->slave_aborted = false;
431
Gao Pan53149872016-11-22 21:52:17 +0800432 fsl_lpspi_write_tx_fifo(fsl_lpspi);
Gao Pand2ad0a622016-11-28 11:02:59 +0800433
Clark Wangbcd87312018-12-07 02:50:36 +0000434 ret = fsl_lpspi_wait_for_completion(controller);
435 if (ret)
436 return ret;
Gao Pan53149872016-11-22 21:52:17 +0800437
Clark Wanga15dc3d2019-01-07 07:47:43 +0000438 fsl_lpspi_reset(fsl_lpspi);
439
Gao Pand989eed2016-12-02 11:50:01 +0800440 return 0;
Gao Pan53149872016-11-22 21:52:17 +0800441}
442
Clark Wangc7a40252019-03-06 06:30:43 +0000443static int fsl_lpspi_transfer_one(struct spi_controller *controller,
444 struct spi_device *spi,
445 struct spi_transfer *t)
Gao Pan53149872016-11-22 21:52:17 +0800446{
Clark Wang07d71552018-12-07 02:50:34 +0000447 struct fsl_lpspi_data *fsl_lpspi =
Clark Wangc7a40252019-03-06 06:30:43 +0000448 spi_controller_get_devdata(controller);
449 int ret;
Gao Pan53149872016-11-22 21:52:17 +0800450
Clark Wangc7a40252019-03-06 06:30:43 +0000451 fsl_lpspi->is_first_byte = true;
452 ret = fsl_lpspi_setup_transfer(spi, t);
453 if (ret < 0)
454 return ret;
Gao Pan53149872016-11-22 21:52:17 +0800455
Clark Wangc7a40252019-03-06 06:30:43 +0000456 fsl_lpspi_set_cmd(fsl_lpspi);
457 fsl_lpspi->is_first_byte = false;
Clark Wang77736a92019-03-06 06:30:41 +0000458
Clark Wangc7a40252019-03-06 06:30:43 +0000459 ret = fsl_lpspi_pio_transfer(controller, t);
460 if (ret < 0)
461 return ret;
Gao Pan53149872016-11-22 21:52:17 +0800462
Clark Wangc7a40252019-03-06 06:30:43 +0000463 return 0;
Gao Pan53149872016-11-22 21:52:17 +0800464}
465
466static irqreturn_t fsl_lpspi_isr(int irq, void *dev_id)
467{
Clark Wangc23fdef2019-01-07 07:47:38 +0000468 u32 temp_SR, temp_IER;
Gao Pan53149872016-11-22 21:52:17 +0800469 struct fsl_lpspi_data *fsl_lpspi = dev_id;
Gao Pan53149872016-11-22 21:52:17 +0800470
Clark Wangc23fdef2019-01-07 07:47:38 +0000471 temp_IER = readl(fsl_lpspi->base + IMX7ULP_IER);
Gao Pan53149872016-11-22 21:52:17 +0800472 fsl_lpspi_intctrl(fsl_lpspi, 0);
Clark Wangc23fdef2019-01-07 07:47:38 +0000473 temp_SR = readl(fsl_lpspi->base + IMX7ULP_SR);
Gao Pan53149872016-11-22 21:52:17 +0800474
475 fsl_lpspi_read_rx_fifo(fsl_lpspi);
476
Clark Wangc23fdef2019-01-07 07:47:38 +0000477 if ((temp_SR & SR_TDF) && (temp_IER & IER_TDIE)) {
Gao Pan53149872016-11-22 21:52:17 +0800478 fsl_lpspi_write_tx_fifo(fsl_lpspi);
Clark Wangc23fdef2019-01-07 07:47:38 +0000479 return IRQ_HANDLED;
480 }
Gao Pan53149872016-11-22 21:52:17 +0800481
Clark Wang6a130442019-01-07 07:47:41 +0000482 if (temp_SR & SR_MBF ||
483 readl(fsl_lpspi->base + IMX7ULP_FSR) & FSR_RXCOUNT) {
484 writel(SR_FCF, fsl_lpspi->base + IMX7ULP_SR);
485 fsl_lpspi_intctrl(fsl_lpspi, IER_FCIE);
486 return IRQ_HANDLED;
487 }
488
Clark Wangc23fdef2019-01-07 07:47:38 +0000489 if (temp_SR & SR_FCF && (temp_IER & IER_FCIE)) {
490 writel(SR_FCF, fsl_lpspi->base + IMX7ULP_SR);
Gao Pan53149872016-11-22 21:52:17 +0800491 complete(&fsl_lpspi->xfer_done);
Gao Pan53149872016-11-22 21:52:17 +0800492 return IRQ_HANDLED;
493 }
494
495 return IRQ_NONE;
496}
497
Han Xu944c01a2019-03-06 06:30:39 +0000498int fsl_lpspi_runtime_resume(struct device *dev)
499{
500 struct fsl_lpspi_data *fsl_lpspi = dev_get_drvdata(dev);
501 int ret;
502
503 ret = clk_prepare_enable(fsl_lpspi->clk_per);
504 if (ret)
505 return ret;
506
507 ret = clk_prepare_enable(fsl_lpspi->clk_ipg);
508 if (ret) {
509 clk_disable_unprepare(fsl_lpspi->clk_per);
510 return ret;
511 }
512
513 return 0;
514}
515
516int fsl_lpspi_runtime_suspend(struct device *dev)
517{
518 struct fsl_lpspi_data *fsl_lpspi = dev_get_drvdata(dev);
519
520 clk_disable_unprepare(fsl_lpspi->clk_per);
521 clk_disable_unprepare(fsl_lpspi->clk_ipg);
522
523 return 0;
524}
525
526static int fsl_lpspi_init_rpm(struct fsl_lpspi_data *fsl_lpspi)
527{
528 struct device *dev = fsl_lpspi->dev;
529
530 pm_runtime_enable(dev);
531 pm_runtime_set_autosuspend_delay(dev, FSL_LPSPI_RPM_TIMEOUT);
532 pm_runtime_use_autosuspend(dev);
533
534 return 0;
535}
536
Gao Pan53149872016-11-22 21:52:17 +0800537static int fsl_lpspi_probe(struct platform_device *pdev)
538{
Clark Wangc7a40252019-03-06 06:30:43 +0000539 struct device_node *np = pdev->dev.of_node;
Gao Pan53149872016-11-22 21:52:17 +0800540 struct fsl_lpspi_data *fsl_lpspi;
Clark Wang07d71552018-12-07 02:50:34 +0000541 struct spi_controller *controller;
Clark Wangc7a40252019-03-06 06:30:43 +0000542 struct spi_imx_master *lpspi_platform_info =
543 dev_get_platdata(&pdev->dev);
Gao Pan53149872016-11-22 21:52:17 +0800544 struct resource *res;
Clark Wangc7a40252019-03-06 06:30:43 +0000545 int i, ret, irq;
Gao Panb88a0de2016-11-28 11:03:00 +0800546 u32 temp;
Gao Pan53149872016-11-22 21:52:17 +0800547
Clark Wangbcd87312018-12-07 02:50:36 +0000548 if (of_property_read_bool((&pdev->dev)->of_node, "spi-slave"))
549 controller = spi_alloc_slave(&pdev->dev,
Clark Wang07d71552018-12-07 02:50:34 +0000550 sizeof(struct fsl_lpspi_data));
Clark Wangbcd87312018-12-07 02:50:36 +0000551 else
552 controller = spi_alloc_master(&pdev->dev,
553 sizeof(struct fsl_lpspi_data));
554
Clark Wang07d71552018-12-07 02:50:34 +0000555 if (!controller)
Gao Pan53149872016-11-22 21:52:17 +0800556 return -ENOMEM;
557
Clark Wang07d71552018-12-07 02:50:34 +0000558 platform_set_drvdata(pdev, controller);
Gao Pan53149872016-11-22 21:52:17 +0800559
Clark Wang07d71552018-12-07 02:50:34 +0000560 controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32);
561 controller->bus_num = pdev->id;
Gao Pan53149872016-11-22 21:52:17 +0800562
Clark Wang07d71552018-12-07 02:50:34 +0000563 fsl_lpspi = spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800564 fsl_lpspi->dev = &pdev->dev;
Han Xu944c01a2019-03-06 06:30:39 +0000565 dev_set_drvdata(&pdev->dev, fsl_lpspi);
Clark Wangbcd87312018-12-07 02:50:36 +0000566 fsl_lpspi->is_slave = of_property_read_bool((&pdev->dev)->of_node,
567 "spi-slave");
Gao Pan53149872016-11-22 21:52:17 +0800568
Clark Wangc7a40252019-03-06 06:30:43 +0000569 if (!fsl_lpspi->is_slave) {
570 for (i = 0; i < controller->num_chipselect; i++) {
571 int cs_gpio = of_get_named_gpio(np, "cs-gpios", i);
572
573 if (!gpio_is_valid(cs_gpio) && lpspi_platform_info)
574 cs_gpio = lpspi_platform_info->chipselect[i];
575
576 fsl_lpspi->chipselect[i] = cs_gpio;
577 if (!gpio_is_valid(cs_gpio))
578 continue;
579
580 ret = devm_gpio_request(&pdev->dev,
581 fsl_lpspi->chipselect[i],
582 DRIVER_NAME);
583 if (ret) {
584 dev_err(&pdev->dev, "can't get cs gpios\n");
585 goto out_controller_put;
586 }
587 }
588 controller->cs_gpios = fsl_lpspi->chipselect;
589 controller->prepare_message = fsl_lpspi_prepare_message;
590 }
591
592 controller->transfer_one = fsl_lpspi_transfer_one;
Clark Wang07d71552018-12-07 02:50:34 +0000593 controller->prepare_transfer_hardware = lpspi_prepare_xfer_hardware;
594 controller->unprepare_transfer_hardware = lpspi_unprepare_xfer_hardware;
595 controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
596 controller->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
597 controller->dev.of_node = pdev->dev.of_node;
598 controller->bus_num = pdev->id;
Clark Wangbcd87312018-12-07 02:50:36 +0000599 controller->slave_abort = fsl_lpspi_slave_abort;
Gao Pan53149872016-11-22 21:52:17 +0800600
601 init_completion(&fsl_lpspi->xfer_done);
602
603 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
604 fsl_lpspi->base = devm_ioremap_resource(&pdev->dev, res);
605 if (IS_ERR(fsl_lpspi->base)) {
606 ret = PTR_ERR(fsl_lpspi->base);
Clark Wang07d71552018-12-07 02:50:34 +0000607 goto out_controller_put;
Gao Pan53149872016-11-22 21:52:17 +0800608 }
609
610 irq = platform_get_irq(pdev, 0);
611 if (irq < 0) {
612 ret = irq;
Clark Wang07d71552018-12-07 02:50:34 +0000613 goto out_controller_put;
Gao Pan53149872016-11-22 21:52:17 +0800614 }
615
616 ret = devm_request_irq(&pdev->dev, irq, fsl_lpspi_isr, 0,
617 dev_name(&pdev->dev), fsl_lpspi);
618 if (ret) {
619 dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
Clark Wang07d71552018-12-07 02:50:34 +0000620 goto out_controller_put;
Gao Pan53149872016-11-22 21:52:17 +0800621 }
622
Clark Wangf5e5afd2019-03-06 06:30:34 +0000623 fsl_lpspi->clk_per = devm_clk_get(&pdev->dev, "per");
624 if (IS_ERR(fsl_lpspi->clk_per)) {
625 ret = PTR_ERR(fsl_lpspi->clk_per);
Clark Wang07d71552018-12-07 02:50:34 +0000626 goto out_controller_put;
Gao Pan53149872016-11-22 21:52:17 +0800627 }
628
Clark Wangf5e5afd2019-03-06 06:30:34 +0000629 fsl_lpspi->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
630 if (IS_ERR(fsl_lpspi->clk_ipg)) {
631 ret = PTR_ERR(fsl_lpspi->clk_ipg);
632 goto out_controller_put;
633 }
634
Han Xu944c01a2019-03-06 06:30:39 +0000635 /* enable the clock */
636 ret = fsl_lpspi_init_rpm(fsl_lpspi);
637 if (ret)
Clark Wangf5e5afd2019-03-06 06:30:34 +0000638 goto out_controller_put;
Clark Wangf5e5afd2019-03-06 06:30:34 +0000639
Han Xu944c01a2019-03-06 06:30:39 +0000640 ret = pm_runtime_get_sync(fsl_lpspi->dev);
641 if (ret < 0) {
642 dev_err(fsl_lpspi->dev, "failed to enable clock\n");
643 return ret;
Gao Panb88a0de2016-11-28 11:03:00 +0800644 }
645
646 temp = readl(fsl_lpspi->base + IMX7ULP_PARAM);
647 fsl_lpspi->txfifosize = 1 << (temp & 0x0f);
648 fsl_lpspi->rxfifosize = 1 << ((temp >> 8) & 0x0f);
649
Clark Wang07d71552018-12-07 02:50:34 +0000650 ret = devm_spi_register_controller(&pdev->dev, controller);
Gao Pan53149872016-11-22 21:52:17 +0800651 if (ret < 0) {
Clark Wang07d71552018-12-07 02:50:34 +0000652 dev_err(&pdev->dev, "spi_register_controller error.\n");
653 goto out_controller_put;
Gao Pan53149872016-11-22 21:52:17 +0800654 }
655
656 return 0;
657
Clark Wang07d71552018-12-07 02:50:34 +0000658out_controller_put:
659 spi_controller_put(controller);
Gao Pan53149872016-11-22 21:52:17 +0800660
661 return ret;
662}
663
664static int fsl_lpspi_remove(struct platform_device *pdev)
665{
Clark Wang07d71552018-12-07 02:50:34 +0000666 struct spi_controller *controller = platform_get_drvdata(pdev);
667 struct fsl_lpspi_data *fsl_lpspi =
668 spi_controller_get_devdata(controller);
Gao Pan53149872016-11-22 21:52:17 +0800669
Han Xu944c01a2019-03-06 06:30:39 +0000670 pm_runtime_disable(fsl_lpspi->dev);
671
672 spi_master_put(controller);
Gao Pan53149872016-11-22 21:52:17 +0800673
674 return 0;
675}
676
Han Xu944c01a2019-03-06 06:30:39 +0000677#ifdef CONFIG_PM_SLEEP
678static int fsl_lpspi_suspend(struct device *dev)
679{
680 int ret;
681
682 pinctrl_pm_select_sleep_state(dev);
683 ret = pm_runtime_force_suspend(dev);
684 return ret;
685}
686
687static int fsl_lpspi_resume(struct device *dev)
688{
689 int ret;
690
691 ret = pm_runtime_force_resume(dev);
692 if (ret) {
693 dev_err(dev, "Error in resume: %d\n", ret);
694 return ret;
695 }
696
697 pinctrl_pm_select_default_state(dev);
698
699 return 0;
700}
701#endif /* CONFIG_PM_SLEEP */
702
703static const struct dev_pm_ops fsl_lpspi_pm_ops = {
704 SET_RUNTIME_PM_OPS(fsl_lpspi_runtime_suspend,
705 fsl_lpspi_runtime_resume, NULL)
706 SET_SYSTEM_SLEEP_PM_OPS(fsl_lpspi_suspend, fsl_lpspi_resume)
707};
708
Gao Pan53149872016-11-22 21:52:17 +0800709static struct platform_driver fsl_lpspi_driver = {
710 .driver = {
Gao Pan102ecc472017-01-04 17:38:16 +0800711 .name = DRIVER_NAME,
712 .of_match_table = fsl_lpspi_dt_ids,
Han Xu944c01a2019-03-06 06:30:39 +0000713 .pm = &fsl_lpspi_pm_ops,
Gao Pan102ecc472017-01-04 17:38:16 +0800714 },
Gao Pan53149872016-11-22 21:52:17 +0800715 .probe = fsl_lpspi_probe,
716 .remove = fsl_lpspi_remove,
717};
718module_platform_driver(fsl_lpspi_driver);
719
Clark Wang07d71552018-12-07 02:50:34 +0000720MODULE_DESCRIPTION("LPSPI Controller driver");
Gao Pan53149872016-11-22 21:52:17 +0800721MODULE_AUTHOR("Gao Pan <pandy.gao@nxp.com>");
Gao Panb6787b62016-12-02 11:50:00 +0800722MODULE_LICENSE("GPL");