blob: 06578412b04da1b2cd3b011f08baa3d4ef18159e [file] [log] [blame]
Lanqing Liue7d973a2018-08-16 20:54:51 +08001// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2018 Spreadtrum Communications Inc.
3
4#include <linux/clk.h>
5#include <linux/interrupt.h>
6#include <linux/io.h>
7#include <linux/iopoll.h>
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/of.h>
11#include <linux/of_device.h>
12#include <linux/platform_device.h>
13#include <linux/pm_runtime.h>
14#include <linux/spi/spi.h>
15
16#define SPRD_SPI_TXD 0x0
17#define SPRD_SPI_CLKD 0x4
18#define SPRD_SPI_CTL0 0x8
19#define SPRD_SPI_CTL1 0xc
20#define SPRD_SPI_CTL2 0x10
21#define SPRD_SPI_CTL3 0x14
22#define SPRD_SPI_CTL4 0x18
23#define SPRD_SPI_CTL5 0x1c
24#define SPRD_SPI_INT_EN 0x20
25#define SPRD_SPI_INT_CLR 0x24
26#define SPRD_SPI_INT_RAW_STS 0x28
27#define SPRD_SPI_INT_MASK_STS 0x2c
28#define SPRD_SPI_STS1 0x30
29#define SPRD_SPI_STS2 0x34
30#define SPRD_SPI_DSP_WAIT 0x38
31#define SPRD_SPI_STS3 0x3c
32#define SPRD_SPI_CTL6 0x40
33#define SPRD_SPI_STS4 0x44
34#define SPRD_SPI_FIFO_RST 0x48
35#define SPRD_SPI_CTL7 0x4c
36#define SPRD_SPI_STS5 0x50
37#define SPRD_SPI_CTL8 0x54
38#define SPRD_SPI_CTL9 0x58
39#define SPRD_SPI_CTL10 0x5c
40#define SPRD_SPI_CTL11 0x60
41#define SPRD_SPI_CTL12 0x64
42#define SPRD_SPI_STS6 0x68
43#define SPRD_SPI_STS7 0x6c
44#define SPRD_SPI_STS8 0x70
45#define SPRD_SPI_STS9 0x74
46
47/* Bits & mask definition for register CTL0 */
48#define SPRD_SPI_SCK_REV BIT(13)
49#define SPRD_SPI_NG_TX BIT(1)
50#define SPRD_SPI_NG_RX BIT(0)
51#define SPRD_SPI_CHNL_LEN_MASK GENMASK(4, 0)
52#define SPRD_SPI_CSN_MASK GENMASK(11, 8)
53#define SPRD_SPI_CS0_VALID BIT(8)
54
55/* Bits & mask definition for register SPI_INT_EN */
56#define SPRD_SPI_TX_END_INT_EN BIT(8)
57#define SPRD_SPI_RX_END_INT_EN BIT(9)
58
59/* Bits & mask definition for register SPI_INT_RAW_STS */
60#define SPRD_SPI_TX_END_RAW BIT(8)
61#define SPRD_SPI_RX_END_RAW BIT(9)
62
63/* Bits & mask definition for register SPI_INT_CLR */
64#define SPRD_SPI_TX_END_CLR BIT(8)
65#define SPRD_SPI_RX_END_CLR BIT(9)
66
67/* Bits & mask definition for register INT_MASK_STS */
68#define SPRD_SPI_MASK_RX_END BIT(9)
69#define SPRD_SPI_MASK_TX_END BIT(8)
70
71/* Bits & mask definition for register STS2 */
72#define SPRD_SPI_TX_BUSY BIT(8)
73
74/* Bits & mask definition for register CTL1 */
75#define SPRD_SPI_RX_MODE BIT(12)
76#define SPRD_SPI_TX_MODE BIT(13)
77#define SPRD_SPI_RTX_MD_MASK GENMASK(13, 12)
78
79/* Bits & mask definition for register CTL2 */
80#define SPRD_SPI_DMA_EN BIT(6)
81
82/* Bits & mask definition for register CTL4 */
83#define SPRD_SPI_START_RX BIT(9)
84#define SPRD_SPI_ONLY_RECV_MASK GENMASK(8, 0)
85
86/* Bits & mask definition for register SPI_INT_CLR */
87#define SPRD_SPI_RX_END_INT_CLR BIT(9)
88#define SPRD_SPI_TX_END_INT_CLR BIT(8)
89
90/* Bits & mask definition for register SPI_INT_RAW */
91#define SPRD_SPI_RX_END_IRQ BIT(9)
92#define SPRD_SPI_TX_END_IRQ BIT(8)
93
94/* Bits & mask definition for register CTL12 */
95#define SPRD_SPI_SW_RX_REQ BIT(0)
96#define SPRD_SPI_SW_TX_REQ BIT(1)
97
98/* Bits & mask definition for register CTL7 */
99#define SPRD_SPI_DATA_LINE2_EN BIT(15)
100#define SPRD_SPI_MODE_MASK GENMASK(5, 3)
101#define SPRD_SPI_MODE_OFFSET 3
102#define SPRD_SPI_3WIRE_MODE 4
103#define SPRD_SPI_4WIRE_MODE 0
104
105/* Bits & mask definition for register CTL8 */
106#define SPRD_SPI_TX_MAX_LEN_MASK GENMASK(19, 0)
107#define SPRD_SPI_TX_LEN_H_MASK GENMASK(3, 0)
108#define SPRD_SPI_TX_LEN_H_OFFSET 16
109
110/* Bits & mask definition for register CTL9 */
111#define SPRD_SPI_TX_LEN_L_MASK GENMASK(15, 0)
112
113/* Bits & mask definition for register CTL10 */
114#define SPRD_SPI_RX_MAX_LEN_MASK GENMASK(19, 0)
115#define SPRD_SPI_RX_LEN_H_MASK GENMASK(3, 0)
116#define SPRD_SPI_RX_LEN_H_OFFSET 16
117
118/* Bits & mask definition for register CTL11 */
119#define SPRD_SPI_RX_LEN_L_MASK GENMASK(15, 0)
120
121/* Default & maximum word delay cycles */
122#define SPRD_SPI_MIN_DELAY_CYCLE 14
123#define SPRD_SPI_MAX_DELAY_CYCLE 130
124
125#define SPRD_SPI_FIFO_SIZE 32
126#define SPRD_SPI_CHIP_CS_NUM 0x4
127#define SPRD_SPI_CHNL_LEN 2
128#define SPRD_SPI_DEFAULT_SOURCE 26000000
129#define SPRD_SPI_MAX_SPEED_HZ 48000000
130#define SPRD_SPI_AUTOSUSPEND_DELAY 100
131
132struct sprd_spi {
133 void __iomem *base;
134 struct device *dev;
135 struct clk *clk;
Lanqing Liude082d82019-02-13 15:36:09 +0800136 int irq;
Lanqing Liue7d973a2018-08-16 20:54:51 +0800137 u32 src_clk;
138 u32 hw_mode;
139 u32 trans_len;
140 u32 trans_mode;
141 u32 word_delay;
142 u32 hw_speed_hz;
143 u32 len;
144 int status;
Lanqing Liude082d82019-02-13 15:36:09 +0800145 struct completion xfer_completion;
Lanqing Liue7d973a2018-08-16 20:54:51 +0800146 const void *tx_buf;
147 void *rx_buf;
148 int (*read_bufs)(struct sprd_spi *ss, u32 len);
149 int (*write_bufs)(struct sprd_spi *ss, u32 len);
150};
151
152static u32 sprd_spi_transfer_max_timeout(struct sprd_spi *ss,
153 struct spi_transfer *t)
154{
155 /*
156 * The time spent on transmission of the full FIFO data is the maximum
157 * SPI transmission time.
158 */
159 u32 size = t->bits_per_word * SPRD_SPI_FIFO_SIZE;
160 u32 bit_time_us = DIV_ROUND_UP(USEC_PER_SEC, ss->hw_speed_hz);
161 u32 total_time_us = size * bit_time_us;
162 /*
163 * There is an interval between data and the data in our SPI hardware,
164 * so the total transmission time need add the interval time.
165 */
166 u32 interval_cycle = SPRD_SPI_FIFO_SIZE * ss->word_delay;
167 u32 interval_time_us = DIV_ROUND_UP(interval_cycle * USEC_PER_SEC,
168 ss->src_clk);
169
170 return total_time_us + interval_time_us;
171}
172
173static int sprd_spi_wait_for_tx_end(struct sprd_spi *ss, struct spi_transfer *t)
174{
175 u32 val, us;
176 int ret;
177
178 us = sprd_spi_transfer_max_timeout(ss, t);
179 ret = readl_relaxed_poll_timeout(ss->base + SPRD_SPI_INT_RAW_STS, val,
180 val & SPRD_SPI_TX_END_IRQ, 0, us);
181 if (ret) {
182 dev_err(ss->dev, "SPI error, spi send timeout!\n");
183 return ret;
184 }
185
186 ret = readl_relaxed_poll_timeout(ss->base + SPRD_SPI_STS2, val,
187 !(val & SPRD_SPI_TX_BUSY), 0, us);
188 if (ret) {
189 dev_err(ss->dev, "SPI error, spi busy timeout!\n");
190 return ret;
191 }
192
193 writel_relaxed(SPRD_SPI_TX_END_INT_CLR, ss->base + SPRD_SPI_INT_CLR);
194
195 return 0;
196}
197
198static int sprd_spi_wait_for_rx_end(struct sprd_spi *ss, struct spi_transfer *t)
199{
200 u32 val, us;
201 int ret;
202
203 us = sprd_spi_transfer_max_timeout(ss, t);
204 ret = readl_relaxed_poll_timeout(ss->base + SPRD_SPI_INT_RAW_STS, val,
205 val & SPRD_SPI_RX_END_IRQ, 0, us);
206 if (ret) {
207 dev_err(ss->dev, "SPI error, spi rx timeout!\n");
208 return ret;
209 }
210
211 writel_relaxed(SPRD_SPI_RX_END_INT_CLR, ss->base + SPRD_SPI_INT_CLR);
212
213 return 0;
214}
215
216static void sprd_spi_tx_req(struct sprd_spi *ss)
217{
218 writel_relaxed(SPRD_SPI_SW_TX_REQ, ss->base + SPRD_SPI_CTL12);
219}
220
221static void sprd_spi_rx_req(struct sprd_spi *ss)
222{
223 writel_relaxed(SPRD_SPI_SW_RX_REQ, ss->base + SPRD_SPI_CTL12);
224}
225
226static void sprd_spi_enter_idle(struct sprd_spi *ss)
227{
228 u32 val = readl_relaxed(ss->base + SPRD_SPI_CTL1);
229
230 val &= ~SPRD_SPI_RTX_MD_MASK;
231 writel_relaxed(val, ss->base + SPRD_SPI_CTL1);
232}
233
234static void sprd_spi_set_transfer_bits(struct sprd_spi *ss, u32 bits)
235{
236 u32 val = readl_relaxed(ss->base + SPRD_SPI_CTL0);
237
238 /* Set the valid bits for every transaction */
239 val &= ~(SPRD_SPI_CHNL_LEN_MASK << SPRD_SPI_CHNL_LEN);
240 val |= bits << SPRD_SPI_CHNL_LEN;
241 writel_relaxed(val, ss->base + SPRD_SPI_CTL0);
242}
243
244static void sprd_spi_set_tx_length(struct sprd_spi *ss, u32 length)
245{
246 u32 val = readl_relaxed(ss->base + SPRD_SPI_CTL8);
247
248 length &= SPRD_SPI_TX_MAX_LEN_MASK;
249 val &= ~SPRD_SPI_TX_LEN_H_MASK;
250 val |= length >> SPRD_SPI_TX_LEN_H_OFFSET;
251 writel_relaxed(val, ss->base + SPRD_SPI_CTL8);
252
253 val = length & SPRD_SPI_TX_LEN_L_MASK;
254 writel_relaxed(val, ss->base + SPRD_SPI_CTL9);
255}
256
257static void sprd_spi_set_rx_length(struct sprd_spi *ss, u32 length)
258{
259 u32 val = readl_relaxed(ss->base + SPRD_SPI_CTL10);
260
261 length &= SPRD_SPI_RX_MAX_LEN_MASK;
262 val &= ~SPRD_SPI_RX_LEN_H_MASK;
263 val |= length >> SPRD_SPI_RX_LEN_H_OFFSET;
264 writel_relaxed(val, ss->base + SPRD_SPI_CTL10);
265
266 val = length & SPRD_SPI_RX_LEN_L_MASK;
267 writel_relaxed(val, ss->base + SPRD_SPI_CTL11);
268}
269
270static void sprd_spi_chipselect(struct spi_device *sdev, bool cs)
271{
272 struct spi_controller *sctlr = sdev->controller;
273 struct sprd_spi *ss = spi_controller_get_devdata(sctlr);
274 u32 val;
275
276 val = readl_relaxed(ss->base + SPRD_SPI_CTL0);
277 /* The SPI controller will pull down CS pin if cs is 0 */
278 if (!cs) {
279 val &= ~SPRD_SPI_CS0_VALID;
280 writel_relaxed(val, ss->base + SPRD_SPI_CTL0);
281 } else {
282 val |= SPRD_SPI_CSN_MASK;
283 writel_relaxed(val, ss->base + SPRD_SPI_CTL0);
284 }
285}
286
287static int sprd_spi_write_only_receive(struct sprd_spi *ss, u32 len)
288{
289 u32 val;
290
291 /* Clear the start receive bit and reset receive data number */
292 val = readl_relaxed(ss->base + SPRD_SPI_CTL4);
293 val &= ~(SPRD_SPI_START_RX | SPRD_SPI_ONLY_RECV_MASK);
294 writel_relaxed(val, ss->base + SPRD_SPI_CTL4);
295
296 /* Set the receive data length */
297 val = readl_relaxed(ss->base + SPRD_SPI_CTL4);
298 val |= len & SPRD_SPI_ONLY_RECV_MASK;
299 writel_relaxed(val, ss->base + SPRD_SPI_CTL4);
300
301 /* Trigger to receive data */
302 val = readl_relaxed(ss->base + SPRD_SPI_CTL4);
303 val |= SPRD_SPI_START_RX;
304 writel_relaxed(val, ss->base + SPRD_SPI_CTL4);
305
306 return len;
307}
308
309static int sprd_spi_write_bufs_u8(struct sprd_spi *ss, u32 len)
310{
311 u8 *tx_p = (u8 *)ss->tx_buf;
312 int i;
313
314 for (i = 0; i < len; i++)
315 writeb_relaxed(tx_p[i], ss->base + SPRD_SPI_TXD);
316
317 ss->tx_buf += i;
318 return i;
319}
320
321static int sprd_spi_write_bufs_u16(struct sprd_spi *ss, u32 len)
322{
323 u16 *tx_p = (u16 *)ss->tx_buf;
324 int i;
325
326 for (i = 0; i < len; i++)
327 writew_relaxed(tx_p[i], ss->base + SPRD_SPI_TXD);
328
329 ss->tx_buf += i << 1;
330 return i << 1;
331}
332
333static int sprd_spi_write_bufs_u32(struct sprd_spi *ss, u32 len)
334{
335 u32 *tx_p = (u32 *)ss->tx_buf;
336 int i;
337
338 for (i = 0; i < len; i++)
339 writel_relaxed(tx_p[i], ss->base + SPRD_SPI_TXD);
340
341 ss->tx_buf += i << 2;
342 return i << 2;
343}
344
345static int sprd_spi_read_bufs_u8(struct sprd_spi *ss, u32 len)
346{
347 u8 *rx_p = (u8 *)ss->rx_buf;
348 int i;
349
350 for (i = 0; i < len; i++)
351 rx_p[i] = readb_relaxed(ss->base + SPRD_SPI_TXD);
352
353 ss->rx_buf += i;
354 return i;
355}
356
357static int sprd_spi_read_bufs_u16(struct sprd_spi *ss, u32 len)
358{
359 u16 *rx_p = (u16 *)ss->rx_buf;
360 int i;
361
362 for (i = 0; i < len; i++)
363 rx_p[i] = readw_relaxed(ss->base + SPRD_SPI_TXD);
364
365 ss->rx_buf += i << 1;
366 return i << 1;
367}
368
369static int sprd_spi_read_bufs_u32(struct sprd_spi *ss, u32 len)
370{
371 u32 *rx_p = (u32 *)ss->rx_buf;
372 int i;
373
374 for (i = 0; i < len; i++)
375 rx_p[i] = readl_relaxed(ss->base + SPRD_SPI_TXD);
376
377 ss->rx_buf += i << 2;
378 return i << 2;
379}
380
381static int sprd_spi_txrx_bufs(struct spi_device *sdev, struct spi_transfer *t)
382{
383 struct sprd_spi *ss = spi_controller_get_devdata(sdev->controller);
384 u32 trans_len = ss->trans_len, len;
385 int ret, write_size = 0;
386
387 while (trans_len) {
388 len = trans_len > SPRD_SPI_FIFO_SIZE ? SPRD_SPI_FIFO_SIZE :
389 trans_len;
390 if (ss->trans_mode & SPRD_SPI_TX_MODE) {
391 sprd_spi_set_tx_length(ss, len);
392 write_size += ss->write_bufs(ss, len);
393
394 /*
395 * For our 3 wires mode or dual TX line mode, we need
396 * to request the controller to transfer.
397 */
398 if (ss->hw_mode & SPI_3WIRE || ss->hw_mode & SPI_TX_DUAL)
399 sprd_spi_tx_req(ss);
400
401 ret = sprd_spi_wait_for_tx_end(ss, t);
402 } else {
403 sprd_spi_set_rx_length(ss, len);
404
405 /*
406 * For our 3 wires mode or dual TX line mode, we need
407 * to request the controller to read.
408 */
409 if (ss->hw_mode & SPI_3WIRE || ss->hw_mode & SPI_TX_DUAL)
410 sprd_spi_rx_req(ss);
411 else
412 write_size += ss->write_bufs(ss, len);
413
414 ret = sprd_spi_wait_for_rx_end(ss, t);
415 }
416
417 if (ret)
418 goto complete;
419
420 if (ss->trans_mode & SPRD_SPI_RX_MODE)
421 ss->read_bufs(ss, len);
422
423 trans_len -= len;
424 }
425
426 ret = write_size;
427
428complete:
429 sprd_spi_enter_idle(ss);
430
431 return ret;
432}
433
434static void sprd_spi_set_speed(struct sprd_spi *ss, u32 speed_hz)
435{
436 /*
437 * From SPI datasheet, the prescale calculation formula:
438 * prescale = SPI source clock / (2 * SPI_freq) - 1;
439 */
440 u32 clk_div = DIV_ROUND_UP(ss->src_clk, speed_hz << 1) - 1;
441
442 /* Save the real hardware speed */
443 ss->hw_speed_hz = (ss->src_clk >> 1) / (clk_div + 1);
444 writel_relaxed(clk_div, ss->base + SPRD_SPI_CLKD);
445}
446
447static void sprd_spi_init_hw(struct sprd_spi *ss, struct spi_transfer *t)
448{
449 u16 word_delay, interval;
450 u32 val;
451
452 val = readl_relaxed(ss->base + SPRD_SPI_CTL7);
453 val &= ~(SPRD_SPI_SCK_REV | SPRD_SPI_NG_TX | SPRD_SPI_NG_RX);
454 /* Set default chip selection, clock phase and clock polarity */
455 val |= ss->hw_mode & SPI_CPHA ? SPRD_SPI_NG_RX : SPRD_SPI_NG_TX;
456 val |= ss->hw_mode & SPI_CPOL ? SPRD_SPI_SCK_REV : 0;
457 writel_relaxed(val, ss->base + SPRD_SPI_CTL0);
458
459 /*
460 * Set the intervals of two SPI frames, and the inteval calculation
461 * formula as below per datasheet:
462 * interval time (source clock cycles) = interval * 4 + 10.
463 */
464 word_delay = clamp_t(u16, t->word_delay, SPRD_SPI_MIN_DELAY_CYCLE,
465 SPRD_SPI_MAX_DELAY_CYCLE);
466 interval = DIV_ROUND_UP(word_delay - 10, 4);
467 ss->word_delay = interval * 4 + 10;
468 writel_relaxed(interval, ss->base + SPRD_SPI_CTL5);
469
470 /* Reset SPI fifo */
471 writel_relaxed(1, ss->base + SPRD_SPI_FIFO_RST);
472 writel_relaxed(0, ss->base + SPRD_SPI_FIFO_RST);
473
474 /* Set SPI work mode */
475 val = readl_relaxed(ss->base + SPRD_SPI_CTL7);
476 val &= ~SPRD_SPI_MODE_MASK;
477
478 if (ss->hw_mode & SPI_3WIRE)
479 val |= SPRD_SPI_3WIRE_MODE << SPRD_SPI_MODE_OFFSET;
480 else
481 val |= SPRD_SPI_4WIRE_MODE << SPRD_SPI_MODE_OFFSET;
482
483 if (ss->hw_mode & SPI_TX_DUAL)
484 val |= SPRD_SPI_DATA_LINE2_EN;
485 else
486 val &= ~SPRD_SPI_DATA_LINE2_EN;
487
488 writel_relaxed(val, ss->base + SPRD_SPI_CTL7);
489}
490
491static int sprd_spi_setup_transfer(struct spi_device *sdev,
492 struct spi_transfer *t)
493{
494 struct sprd_spi *ss = spi_controller_get_devdata(sdev->controller);
495 u8 bits_per_word = t->bits_per_word;
496 u32 val, mode = 0;
497
498 ss->len = t->len;
499 ss->tx_buf = t->tx_buf;
500 ss->rx_buf = t->rx_buf;
501
502 ss->hw_mode = sdev->mode;
503 sprd_spi_init_hw(ss, t);
504
505 /* Set tansfer speed and valid bits */
506 sprd_spi_set_speed(ss, t->speed_hz);
507 sprd_spi_set_transfer_bits(ss, bits_per_word);
508
509 if (bits_per_word > 16)
510 bits_per_word = round_up(bits_per_word, 16);
511 else
512 bits_per_word = round_up(bits_per_word, 8);
513
514 switch (bits_per_word) {
515 case 8:
516 ss->trans_len = t->len;
517 ss->read_bufs = sprd_spi_read_bufs_u8;
518 ss->write_bufs = sprd_spi_write_bufs_u8;
519 break;
520 case 16:
521 ss->trans_len = t->len >> 1;
522 ss->read_bufs = sprd_spi_read_bufs_u16;
523 ss->write_bufs = sprd_spi_write_bufs_u16;
524 break;
525 case 32:
526 ss->trans_len = t->len >> 2;
527 ss->read_bufs = sprd_spi_read_bufs_u32;
528 ss->write_bufs = sprd_spi_write_bufs_u32;
529 break;
530 default:
531 return -EINVAL;
532 }
533
534 /* Set transfer read or write mode */
535 val = readl_relaxed(ss->base + SPRD_SPI_CTL1);
536 val &= ~SPRD_SPI_RTX_MD_MASK;
537 if (t->tx_buf)
538 mode |= SPRD_SPI_TX_MODE;
539 if (t->rx_buf)
540 mode |= SPRD_SPI_RX_MODE;
541
542 writel_relaxed(val | mode, ss->base + SPRD_SPI_CTL1);
543
544 ss->trans_mode = mode;
545
546 /*
547 * If in only receive mode, we need to trigger the SPI controller to
548 * receive data automatically.
549 */
550 if (ss->trans_mode == SPRD_SPI_RX_MODE)
551 ss->write_bufs = sprd_spi_write_only_receive;
552
553 return 0;
554}
555
556static int sprd_spi_transfer_one(struct spi_controller *sctlr,
557 struct spi_device *sdev,
558 struct spi_transfer *t)
559{
560 int ret;
561
562 ret = sprd_spi_setup_transfer(sdev, t);
563 if (ret)
564 goto setup_err;
565
566 ret = sprd_spi_txrx_bufs(sdev, t);
567 if (ret == t->len)
568 ret = 0;
569 else if (ret >= 0)
570 ret = -EREMOTEIO;
571
572setup_err:
573 spi_finalize_current_transfer(sctlr);
574
575 return ret;
576}
577
Lanqing Liude082d82019-02-13 15:36:09 +0800578static irqreturn_t sprd_spi_handle_irq(int irq, void *data)
579{
580 struct sprd_spi *ss = (struct sprd_spi *)data;
581 u32 val = readl_relaxed(ss->base + SPRD_SPI_INT_MASK_STS);
582
583 if (val & SPRD_SPI_MASK_TX_END) {
584 writel_relaxed(SPRD_SPI_TX_END_CLR, ss->base + SPRD_SPI_INT_CLR);
585 if (!(ss->trans_mode & SPRD_SPI_RX_MODE))
586 complete(&ss->xfer_completion);
587
588 return IRQ_HANDLED;
589 }
590
591 if (val & SPRD_SPI_MASK_RX_END) {
592 writel_relaxed(SPRD_SPI_RX_END_CLR, ss->base + SPRD_SPI_INT_CLR);
593 complete(&ss->xfer_completion);
594
595 return IRQ_HANDLED;
596 }
597
598 return IRQ_NONE;
599}
600
601static int sprd_spi_irq_init(struct platform_device *pdev, struct sprd_spi *ss)
602{
603 int ret;
604
605 ss->irq = platform_get_irq(pdev, 0);
606 if (ss->irq < 0) {
607 dev_err(&pdev->dev, "failed to get irq resource\n");
608 return ss->irq;
609 }
610
611 ret = devm_request_irq(&pdev->dev, ss->irq, sprd_spi_handle_irq,
612 0, pdev->name, ss);
613 if (ret)
614 dev_err(&pdev->dev, "failed to request spi irq %d, ret = %d\n",
615 ss->irq, ret);
616
617 return ret;
618}
619
Lanqing Liue7d973a2018-08-16 20:54:51 +0800620static int sprd_spi_clk_init(struct platform_device *pdev, struct sprd_spi *ss)
621{
622 struct clk *clk_spi, *clk_parent;
623
624 clk_spi = devm_clk_get(&pdev->dev, "spi");
625 if (IS_ERR(clk_spi)) {
626 dev_warn(&pdev->dev, "can't get the spi clock\n");
627 clk_spi = NULL;
628 }
629
630 clk_parent = devm_clk_get(&pdev->dev, "source");
631 if (IS_ERR(clk_parent)) {
632 dev_warn(&pdev->dev, "can't get the source clock\n");
633 clk_parent = NULL;
634 }
635
636 ss->clk = devm_clk_get(&pdev->dev, "enable");
637 if (IS_ERR(ss->clk)) {
638 dev_err(&pdev->dev, "can't get the enable clock\n");
639 return PTR_ERR(ss->clk);
640 }
641
642 if (!clk_set_parent(clk_spi, clk_parent))
643 ss->src_clk = clk_get_rate(clk_spi);
644 else
645 ss->src_clk = SPRD_SPI_DEFAULT_SOURCE;
646
647 return 0;
648}
649
650static int sprd_spi_probe(struct platform_device *pdev)
651{
652 struct spi_controller *sctlr;
653 struct resource *res;
654 struct sprd_spi *ss;
655 int ret;
656
657 pdev->id = of_alias_get_id(pdev->dev.of_node, "spi");
658 sctlr = spi_alloc_master(&pdev->dev, sizeof(*ss));
659 if (!sctlr)
660 return -ENOMEM;
661
662 ss = spi_controller_get_devdata(sctlr);
663 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
664 ss->base = devm_ioremap_resource(&pdev->dev, res);
665 if (IS_ERR(ss->base)) {
666 ret = PTR_ERR(ss->base);
667 goto free_controller;
668 }
669
670 ss->dev = &pdev->dev;
671 sctlr->dev.of_node = pdev->dev.of_node;
672 sctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_3WIRE | SPI_TX_DUAL;
673 sctlr->bus_num = pdev->id;
674 sctlr->set_cs = sprd_spi_chipselect;
675 sctlr->transfer_one = sprd_spi_transfer_one;
676 sctlr->auto_runtime_pm = true;
677 sctlr->max_speed_hz = min_t(u32, ss->src_clk >> 1,
678 SPRD_SPI_MAX_SPEED_HZ);
679
Lanqing Liude082d82019-02-13 15:36:09 +0800680 init_completion(&ss->xfer_completion);
Lanqing Liue7d973a2018-08-16 20:54:51 +0800681 platform_set_drvdata(pdev, sctlr);
682 ret = sprd_spi_clk_init(pdev, ss);
683 if (ret)
684 goto free_controller;
685
Lanqing Liude082d82019-02-13 15:36:09 +0800686 ret = sprd_spi_irq_init(pdev, ss);
687 if (ret)
688 goto free_controller;
689
Lanqing Liue7d973a2018-08-16 20:54:51 +0800690 ret = clk_prepare_enable(ss->clk);
691 if (ret)
692 goto free_controller;
693
694 ret = pm_runtime_set_active(&pdev->dev);
695 if (ret < 0)
696 goto disable_clk;
697
698 pm_runtime_set_autosuspend_delay(&pdev->dev,
699 SPRD_SPI_AUTOSUSPEND_DELAY);
700 pm_runtime_use_autosuspend(&pdev->dev);
701 pm_runtime_enable(&pdev->dev);
702 ret = pm_runtime_get_sync(&pdev->dev);
703 if (ret < 0) {
704 dev_err(&pdev->dev, "failed to resume SPI controller\n");
705 goto err_rpm_put;
706 }
707
708 ret = devm_spi_register_controller(&pdev->dev, sctlr);
709 if (ret)
710 goto err_rpm_put;
711
712 pm_runtime_mark_last_busy(&pdev->dev);
713 pm_runtime_put_autosuspend(&pdev->dev);
714
715 return 0;
716
717err_rpm_put:
718 pm_runtime_put_noidle(&pdev->dev);
719 pm_runtime_disable(&pdev->dev);
720disable_clk:
721 clk_disable_unprepare(ss->clk);
722free_controller:
723 spi_controller_put(sctlr);
724
725 return ret;
726}
727
Arnd Bergmann71d9a842018-09-26 14:58:21 +0200728static int sprd_spi_remove(struct platform_device *pdev)
Lanqing Liue7d973a2018-08-16 20:54:51 +0800729{
730 struct spi_controller *sctlr = platform_get_drvdata(pdev);
731 struct sprd_spi *ss = spi_controller_get_devdata(sctlr);
732 int ret;
733
734 ret = pm_runtime_get_sync(ss->dev);
735 if (ret < 0) {
736 dev_err(ss->dev, "failed to resume SPI controller\n");
737 return ret;
738 }
739
Lanqing Liude082d82019-02-13 15:36:09 +0800740 spi_controller_suspend(sctlr);
741
Lanqing Liue7d973a2018-08-16 20:54:51 +0800742 clk_disable_unprepare(ss->clk);
743 pm_runtime_put_noidle(&pdev->dev);
744 pm_runtime_disable(&pdev->dev);
745
746 return 0;
747}
748
749static int __maybe_unused sprd_spi_runtime_suspend(struct device *dev)
750{
751 struct spi_controller *sctlr = dev_get_drvdata(dev);
752 struct sprd_spi *ss = spi_controller_get_devdata(sctlr);
753
754 clk_disable_unprepare(ss->clk);
755
756 return 0;
757}
758
759static int __maybe_unused sprd_spi_runtime_resume(struct device *dev)
760{
761 struct spi_controller *sctlr = dev_get_drvdata(dev);
762 struct sprd_spi *ss = spi_controller_get_devdata(sctlr);
763 int ret;
764
765 ret = clk_prepare_enable(ss->clk);
766 if (ret)
767 return ret;
768
769 return 0;
770}
771
772static const struct dev_pm_ops sprd_spi_pm_ops = {
773 SET_RUNTIME_PM_OPS(sprd_spi_runtime_suspend,
774 sprd_spi_runtime_resume, NULL)
775};
776
777static const struct of_device_id sprd_spi_of_match[] = {
778 { .compatible = "sprd,sc9860-spi", },
779 { /* sentinel */ }
780};
781
782static struct platform_driver sprd_spi_driver = {
783 .driver = {
784 .name = "sprd-spi",
785 .of_match_table = sprd_spi_of_match,
786 .pm = &sprd_spi_pm_ops,
787 },
788 .probe = sprd_spi_probe,
789 .remove = sprd_spi_remove,
790};
791
792module_platform_driver(sprd_spi_driver);
793
794MODULE_DESCRIPTION("Spreadtrum SPI Controller driver");
795MODULE_AUTHOR("Lanqing Liu <lanqing.liu@spreadtrum.com>");
796MODULE_LICENSE("GPL v2");