blob: 82133584c0ea319aa1474a91a8bd7b638c72029f [file] [log] [blame]
Chao Fu349ad662013-08-16 11:08:55 +08001/*
2 * drivers/spi/spi-fsl-dspi.c
3 *
4 * Copyright 2013 Freescale Semiconductor, Inc.
5 *
6 * Freescale DSPI driver
7 * This file contains a driver for the Freescale DSPI
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 */
15
Xiubo Lia3108362014-09-29 10:57:06 +080016#include <linux/clk.h>
17#include <linux/delay.h>
18#include <linux/err.h>
19#include <linux/errno.h>
20#include <linux/interrupt.h>
21#include <linux/io.h>
Chao Fu349ad662013-08-16 11:08:55 +080022#include <linux/kernel.h>
23#include <linux/module.h>
Chao Fu349ad662013-08-16 11:08:55 +080024#include <linux/of.h>
25#include <linux/of_device.h>
Xiubo Lia3108362014-09-29 10:57:06 +080026#include <linux/platform_device.h>
27#include <linux/pm_runtime.h>
28#include <linux/regmap.h>
29#include <linux/sched.h>
30#include <linux/spi/spi.h>
31#include <linux/spi/spi_bitbang.h>
Chao Fu349ad662013-08-16 11:08:55 +080032
33#define DRIVER_NAME "fsl-dspi"
34
35#define TRAN_STATE_RX_VOID 0x01
36#define TRAN_STATE_TX_VOID 0x02
37#define TRAN_STATE_WORD_ODD_NUM 0x04
38
39#define DSPI_FIFO_SIZE 4
40
41#define SPI_MCR 0x00
42#define SPI_MCR_MASTER (1 << 31)
43#define SPI_MCR_PCSIS (0x3F << 16)
44#define SPI_MCR_CLR_TXF (1 << 11)
45#define SPI_MCR_CLR_RXF (1 << 10)
46
47#define SPI_TCR 0x08
48
Alexander Stein5cc7b042014-11-04 09:20:18 +010049#define SPI_CTAR(x) (0x0c + (((x) & 0x3) * 4))
Chao Fu349ad662013-08-16 11:08:55 +080050#define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27)
51#define SPI_CTAR_CPOL(x) ((x) << 26)
52#define SPI_CTAR_CPHA(x) ((x) << 25)
53#define SPI_CTAR_LSBFE(x) ((x) << 24)
54#define SPI_CTAR_PCSSCR(x) (((x) & 0x00000003) << 22)
55#define SPI_CTAR_PASC(x) (((x) & 0x00000003) << 20)
56#define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
57#define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
58#define SPI_CTAR_CSSCK(x) (((x) & 0x0000000f) << 12)
59#define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
60#define SPI_CTAR_DT(x) (((x) & 0x0000000f) << 4)
61#define SPI_CTAR_BR(x) ((x) & 0x0000000f)
62
63#define SPI_CTAR0_SLAVE 0x0c
64
65#define SPI_SR 0x2c
66#define SPI_SR_EOQF 0x10000000
67
68#define SPI_RSER 0x30
69#define SPI_RSER_EOQFE 0x10000000
70
71#define SPI_PUSHR 0x34
72#define SPI_PUSHR_CONT (1 << 31)
Alexander Stein5cc7b042014-11-04 09:20:18 +010073#define SPI_PUSHR_CTAS(x) (((x) & 0x00000003) << 28)
Chao Fu349ad662013-08-16 11:08:55 +080074#define SPI_PUSHR_EOQ (1 << 27)
75#define SPI_PUSHR_CTCNT (1 << 26)
76#define SPI_PUSHR_PCS(x) (((1 << x) & 0x0000003f) << 16)
77#define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
78
79#define SPI_PUSHR_SLAVE 0x34
80
81#define SPI_POPR 0x38
82#define SPI_POPR_RXDATA(x) ((x) & 0x0000ffff)
83
84#define SPI_TXFR0 0x3c
85#define SPI_TXFR1 0x40
86#define SPI_TXFR2 0x44
87#define SPI_TXFR3 0x48
88#define SPI_RXFR0 0x7c
89#define SPI_RXFR1 0x80
90#define SPI_RXFR2 0x84
91#define SPI_RXFR3 0x88
92
93#define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
94#define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
95#define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
96#define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
97
98#define SPI_CS_INIT 0x01
99#define SPI_CS_ASSERT 0x02
100#define SPI_CS_DROP 0x04
101
102struct chip_data {
103 u32 mcr_val;
104 u32 ctar_val;
105 u16 void_write_data;
106};
107
108struct fsl_dspi {
Chao Fu9298bc72015-01-27 16:27:22 +0530109 struct spi_master *master;
Chao Fu349ad662013-08-16 11:08:55 +0800110 struct platform_device *pdev;
111
Chao Fu1acbdeb2014-02-12 15:29:05 +0800112 struct regmap *regmap;
Chao Fu349ad662013-08-16 11:08:55 +0800113 int irq;
Chao Fu88386e82014-02-12 15:29:06 +0800114 struct clk *clk;
Chao Fu349ad662013-08-16 11:08:55 +0800115
Chao Fu88386e82014-02-12 15:29:06 +0800116 struct spi_transfer *cur_transfer;
Chao Fu9298bc72015-01-27 16:27:22 +0530117 struct spi_message *cur_msg;
Chao Fu349ad662013-08-16 11:08:55 +0800118 struct chip_data *cur_chip;
119 size_t len;
120 void *tx;
121 void *tx_end;
122 void *rx;
123 void *rx_end;
124 char dataflags;
125 u8 cs;
126 u16 void_write_data;
Chao Fu9298bc72015-01-27 16:27:22 +0530127 u32 cs_change;
Chao Fu349ad662013-08-16 11:08:55 +0800128
Chao Fu88386e82014-02-12 15:29:06 +0800129 wait_queue_head_t waitq;
130 u32 waitflags;
Chao Fu349ad662013-08-16 11:08:55 +0800131};
132
133static inline int is_double_byte_mode(struct fsl_dspi *dspi)
134{
Chao Fu1acbdeb2014-02-12 15:29:05 +0800135 unsigned int val;
Chao Fu349ad662013-08-16 11:08:55 +0800136
Chao Fu1acbdeb2014-02-12 15:29:05 +0800137 regmap_read(dspi->regmap, SPI_CTAR(dspi->cs), &val);
Chao Fu349ad662013-08-16 11:08:55 +0800138
Chao Fu1acbdeb2014-02-12 15:29:05 +0800139 return ((val & SPI_FRAME_BITS_MASK) == SPI_FRAME_BITS(8)) ? 0 : 1;
Chao Fu349ad662013-08-16 11:08:55 +0800140}
141
142static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
143 unsigned long clkrate)
144{
145 /* Valid baud rate pre-scaler values */
146 int pbr_tbl[4] = {2, 3, 5, 7};
147 int brs[16] = { 2, 4, 6, 8,
148 16, 32, 64, 128,
149 256, 512, 1024, 2048,
150 4096, 8192, 16384, 32768 };
Aaron Bricee689d6d2015-04-03 13:39:29 -0700151 int scale_needed, scale, minscale = INT_MAX;
152 int i, j;
Chao Fu349ad662013-08-16 11:08:55 +0800153
Aaron Bricee689d6d2015-04-03 13:39:29 -0700154 scale_needed = clkrate / speed_hz;
155 if (clkrate % speed_hz)
156 scale_needed++;
Chao Fu349ad662013-08-16 11:08:55 +0800157
Aaron Bricee689d6d2015-04-03 13:39:29 -0700158 for (i = 0; i < ARRAY_SIZE(brs); i++)
159 for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
160 scale = brs[i] * pbr_tbl[j];
161 if (scale >= scale_needed) {
162 if (scale < minscale) {
163 minscale = scale;
164 *br = i;
165 *pbr = j;
166 }
167 break;
Chao Fu349ad662013-08-16 11:08:55 +0800168 }
169 }
170
Aaron Bricee689d6d2015-04-03 13:39:29 -0700171 if (minscale == INT_MAX) {
172 pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
173 speed_hz, clkrate);
174 *pbr = ARRAY_SIZE(pbr_tbl) - 1;
175 *br = ARRAY_SIZE(brs) - 1;
176 }
Chao Fu349ad662013-08-16 11:08:55 +0800177}
178
179static int dspi_transfer_write(struct fsl_dspi *dspi)
180{
181 int tx_count = 0;
182 int tx_word;
183 u16 d16;
184 u8 d8;
185 u32 dspi_pushr = 0;
186 int first = 1;
187
188 tx_word = is_double_byte_mode(dspi);
189
190 /* If we are in word mode, but only have a single byte to transfer
191 * then switch to byte mode temporarily. Will switch back at the
192 * end of the transfer.
193 */
194 if (tx_word && (dspi->len == 1)) {
195 dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
Chao Fu1acbdeb2014-02-12 15:29:05 +0800196 regmap_update_bits(dspi->regmap, SPI_CTAR(dspi->cs),
197 SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
Chao Fu349ad662013-08-16 11:08:55 +0800198 tx_word = 0;
199 }
200
201 while (dspi->len && (tx_count < DSPI_FIFO_SIZE)) {
202 if (tx_word) {
203 if (dspi->len == 1)
204 break;
205
206 if (!(dspi->dataflags & TRAN_STATE_TX_VOID)) {
207 d16 = *(u16 *)dspi->tx;
208 dspi->tx += 2;
209 } else {
210 d16 = dspi->void_write_data;
211 }
212
213 dspi_pushr = SPI_PUSHR_TXDATA(d16) |
214 SPI_PUSHR_PCS(dspi->cs) |
215 SPI_PUSHR_CTAS(dspi->cs) |
216 SPI_PUSHR_CONT;
217
218 dspi->len -= 2;
219 } else {
220 if (!(dspi->dataflags & TRAN_STATE_TX_VOID)) {
221
222 d8 = *(u8 *)dspi->tx;
223 dspi->tx++;
224 } else {
225 d8 = (u8)dspi->void_write_data;
226 }
227
228 dspi_pushr = SPI_PUSHR_TXDATA(d8) |
229 SPI_PUSHR_PCS(dspi->cs) |
230 SPI_PUSHR_CTAS(dspi->cs) |
231 SPI_PUSHR_CONT;
232
233 dspi->len--;
234 }
235
236 if (dspi->len == 0 || tx_count == DSPI_FIFO_SIZE - 1) {
237 /* last transfer in the transfer */
238 dspi_pushr |= SPI_PUSHR_EOQ;
Chao Fu9298bc72015-01-27 16:27:22 +0530239 if ((dspi->cs_change) && (!dspi->len))
240 dspi_pushr &= ~SPI_PUSHR_CONT;
Chao Fu349ad662013-08-16 11:08:55 +0800241 } else if (tx_word && (dspi->len == 1))
242 dspi_pushr |= SPI_PUSHR_EOQ;
243
244 if (first) {
245 first = 0;
246 dspi_pushr |= SPI_PUSHR_CTCNT; /* clear counter */
247 }
248
Chao Fu1acbdeb2014-02-12 15:29:05 +0800249 regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
250
Chao Fu349ad662013-08-16 11:08:55 +0800251 tx_count++;
252 }
253
254 return tx_count * (tx_word + 1);
255}
256
257static int dspi_transfer_read(struct fsl_dspi *dspi)
258{
259 int rx_count = 0;
260 int rx_word = is_double_byte_mode(dspi);
261 u16 d;
Chao Fu9298bc72015-01-27 16:27:22 +0530262
Chao Fu349ad662013-08-16 11:08:55 +0800263 while ((dspi->rx < dspi->rx_end)
264 && (rx_count < DSPI_FIFO_SIZE)) {
265 if (rx_word) {
Chao Fu1acbdeb2014-02-12 15:29:05 +0800266 unsigned int val;
267
Chao Fu349ad662013-08-16 11:08:55 +0800268 if ((dspi->rx_end - dspi->rx) == 1)
269 break;
270
Chao Fu1acbdeb2014-02-12 15:29:05 +0800271 regmap_read(dspi->regmap, SPI_POPR, &val);
272 d = SPI_POPR_RXDATA(val);
Chao Fu349ad662013-08-16 11:08:55 +0800273
274 if (!(dspi->dataflags & TRAN_STATE_RX_VOID))
275 *(u16 *)dspi->rx = d;
276 dspi->rx += 2;
277
278 } else {
Chao Fu1acbdeb2014-02-12 15:29:05 +0800279 unsigned int val;
280
281 regmap_read(dspi->regmap, SPI_POPR, &val);
282 d = SPI_POPR_RXDATA(val);
Chao Fu349ad662013-08-16 11:08:55 +0800283 if (!(dspi->dataflags & TRAN_STATE_RX_VOID))
284 *(u8 *)dspi->rx = d;
285 dspi->rx++;
286 }
287 rx_count++;
288 }
289
290 return rx_count;
291}
292
Chao Fu9298bc72015-01-27 16:27:22 +0530293static int dspi_transfer_one_message(struct spi_master *master,
294 struct spi_message *message)
Chao Fu349ad662013-08-16 11:08:55 +0800295{
Chao Fu9298bc72015-01-27 16:27:22 +0530296 struct fsl_dspi *dspi = spi_master_get_devdata(master);
297 struct spi_device *spi = message->spi;
298 struct spi_transfer *transfer;
299 int status = 0;
300 message->actual_length = 0;
Chao Fu349ad662013-08-16 11:08:55 +0800301
Chao Fu9298bc72015-01-27 16:27:22 +0530302 list_for_each_entry(transfer, &message->transfers, transfer_list) {
303 dspi->cur_transfer = transfer;
304 dspi->cur_msg = message;
305 dspi->cur_chip = spi_get_ctldata(spi);
306 dspi->cs = spi->chip_select;
307 if (dspi->cur_transfer->transfer_list.next
308 == &dspi->cur_msg->transfers)
309 transfer->cs_change = 1;
310 dspi->cs_change = transfer->cs_change;
311 dspi->void_write_data = dspi->cur_chip->void_write_data;
Chao Fu349ad662013-08-16 11:08:55 +0800312
Chao Fu9298bc72015-01-27 16:27:22 +0530313 dspi->dataflags = 0;
314 dspi->tx = (void *)transfer->tx_buf;
315 dspi->tx_end = dspi->tx + transfer->len;
316 dspi->rx = transfer->rx_buf;
317 dspi->rx_end = dspi->rx + transfer->len;
318 dspi->len = transfer->len;
Chao Fu349ad662013-08-16 11:08:55 +0800319
Chao Fu9298bc72015-01-27 16:27:22 +0530320 if (!dspi->rx)
321 dspi->dataflags |= TRAN_STATE_RX_VOID;
Chao Fu349ad662013-08-16 11:08:55 +0800322
Chao Fu9298bc72015-01-27 16:27:22 +0530323 if (!dspi->tx)
324 dspi->dataflags |= TRAN_STATE_TX_VOID;
Chao Fu349ad662013-08-16 11:08:55 +0800325
Chao Fu9298bc72015-01-27 16:27:22 +0530326 regmap_write(dspi->regmap, SPI_MCR, dspi->cur_chip->mcr_val);
327 regmap_update_bits(dspi->regmap, SPI_MCR,
328 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
329 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
Chao Fu1acbdeb2014-02-12 15:29:05 +0800330 regmap_write(dspi->regmap, SPI_CTAR(dspi->cs),
331 dspi->cur_chip->ctar_val);
Chao Fu9298bc72015-01-27 16:27:22 +0530332 if (transfer->speed_hz)
333 regmap_write(dspi->regmap, SPI_CTAR(dspi->cs),
334 dspi->cur_chip->ctar_val);
Chao Fu349ad662013-08-16 11:08:55 +0800335
Chao Fu9298bc72015-01-27 16:27:22 +0530336 regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
337 message->actual_length += dspi_transfer_write(dspi);
Chao Fu349ad662013-08-16 11:08:55 +0800338
Chao Fu9298bc72015-01-27 16:27:22 +0530339 if (wait_event_interruptible(dspi->waitq, dspi->waitflags))
340 dev_err(&dspi->pdev->dev, "wait transfer complete fail!\n");
341 dspi->waitflags = 0;
Chao Fu349ad662013-08-16 11:08:55 +0800342
Chao Fu9298bc72015-01-27 16:27:22 +0530343 if (transfer->delay_usecs)
344 udelay(transfer->delay_usecs);
Chao Fu349ad662013-08-16 11:08:55 +0800345 }
346
Chao Fu9298bc72015-01-27 16:27:22 +0530347 message->status = status;
348 spi_finalize_current_message(master);
349
350 return status;
Chao Fu349ad662013-08-16 11:08:55 +0800351}
352
Chao Fu9298bc72015-01-27 16:27:22 +0530353static int dspi_setup(struct spi_device *spi)
Chao Fu349ad662013-08-16 11:08:55 +0800354{
355 struct chip_data *chip;
356 struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
357 unsigned char br = 0, pbr = 0, fmsz = 0;
358
Bhuvanchandra DVceadfd82015-01-31 22:03:25 +0530359 if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
360 fmsz = spi->bits_per_word - 1;
361 } else {
362 pr_err("Invalid wordsize\n");
363 return -ENODEV;
364 }
365
Chao Fu349ad662013-08-16 11:08:55 +0800366 /* Only alloc on first setup */
367 chip = spi_get_ctldata(spi);
368 if (chip == NULL) {
Bhuvanchandra DV973fbce2015-01-27 16:27:20 +0530369 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Chao Fu349ad662013-08-16 11:08:55 +0800370 if (!chip)
371 return -ENOMEM;
372 }
373
374 chip->mcr_val = SPI_MCR_MASTER | SPI_MCR_PCSIS |
375 SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
Chao Fu349ad662013-08-16 11:08:55 +0800376
377 chip->void_write_data = 0;
378
379 hz_to_spi_baud(&pbr, &br,
380 spi->max_speed_hz, clk_get_rate(dspi->clk));
381
382 chip->ctar_val = SPI_CTAR_FMSZ(fmsz)
383 | SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
384 | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
385 | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
386 | SPI_CTAR_PBR(pbr)
387 | SPI_CTAR_BR(br);
388
389 spi_set_ctldata(spi, chip);
390
391 return 0;
392}
393
Bhuvanchandra DV973fbce2015-01-27 16:27:20 +0530394static void dspi_cleanup(struct spi_device *spi)
395{
396 struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
397
398 dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
399 spi->master->bus_num, spi->chip_select);
400
401 kfree(chip);
402}
403
Chao Fu349ad662013-08-16 11:08:55 +0800404static irqreturn_t dspi_interrupt(int irq, void *dev_id)
405{
406 struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
407
Chao Fu9298bc72015-01-27 16:27:22 +0530408 struct spi_message *msg = dspi->cur_msg;
Chao Fu349ad662013-08-16 11:08:55 +0800409
Chao Fu9298bc72015-01-27 16:27:22 +0530410 regmap_write(dspi->regmap, SPI_SR, SPI_SR_EOQF);
Chao Fu349ad662013-08-16 11:08:55 +0800411 dspi_transfer_read(dspi);
412
413 if (!dspi->len) {
414 if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM)
Chao Fu1acbdeb2014-02-12 15:29:05 +0800415 regmap_update_bits(dspi->regmap, SPI_CTAR(dspi->cs),
Chao Fu9298bc72015-01-27 16:27:22 +0530416 SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(16));
Chao Fu1acbdeb2014-02-12 15:29:05 +0800417
Chao Fu349ad662013-08-16 11:08:55 +0800418 dspi->waitflags = 1;
419 wake_up_interruptible(&dspi->waitq);
Chao Fu9298bc72015-01-27 16:27:22 +0530420 } else
421 msg->actual_length += dspi_transfer_write(dspi);
Chao Fu349ad662013-08-16 11:08:55 +0800422
423 return IRQ_HANDLED;
424}
425
Jingoo Han790d1902014-05-07 16:45:41 +0900426static const struct of_device_id fsl_dspi_dt_ids[] = {
Chao Fu349ad662013-08-16 11:08:55 +0800427 { .compatible = "fsl,vf610-dspi", .data = NULL, },
428 { /* sentinel */ }
429};
430MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
431
432#ifdef CONFIG_PM_SLEEP
433static int dspi_suspend(struct device *dev)
434{
435 struct spi_master *master = dev_get_drvdata(dev);
436 struct fsl_dspi *dspi = spi_master_get_devdata(master);
437
438 spi_master_suspend(master);
439 clk_disable_unprepare(dspi->clk);
440
441 return 0;
442}
443
444static int dspi_resume(struct device *dev)
445{
Chao Fu349ad662013-08-16 11:08:55 +0800446 struct spi_master *master = dev_get_drvdata(dev);
447 struct fsl_dspi *dspi = spi_master_get_devdata(master);
448
449 clk_prepare_enable(dspi->clk);
450 spi_master_resume(master);
451
452 return 0;
453}
454#endif /* CONFIG_PM_SLEEP */
455
Jingoo Hanba811ad2014-02-26 10:30:14 +0900456static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
Chao Fu349ad662013-08-16 11:08:55 +0800457
Xiubo Li409851c2014-10-09 11:27:45 +0800458static const struct regmap_config dspi_regmap_config = {
Chao Fu1acbdeb2014-02-12 15:29:05 +0800459 .reg_bits = 32,
460 .val_bits = 32,
461 .reg_stride = 4,
462 .max_register = 0x88,
Chao Fu349ad662013-08-16 11:08:55 +0800463};
464
465static int dspi_probe(struct platform_device *pdev)
466{
467 struct device_node *np = pdev->dev.of_node;
468 struct spi_master *master;
469 struct fsl_dspi *dspi;
470 struct resource *res;
Chao Fu1acbdeb2014-02-12 15:29:05 +0800471 void __iomem *base;
Chao Fu349ad662013-08-16 11:08:55 +0800472 int ret = 0, cs_num, bus_num;
473
474 master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
475 if (!master)
476 return -ENOMEM;
477
478 dspi = spi_master_get_devdata(master);
479 dspi->pdev = pdev;
Chao Fu9298bc72015-01-27 16:27:22 +0530480 dspi->master = master;
481
482 master->transfer = NULL;
483 master->setup = dspi_setup;
484 master->transfer_one_message = dspi_transfer_one_message;
485 master->dev.of_node = pdev->dev.of_node;
Chao Fu349ad662013-08-16 11:08:55 +0800486
Bhuvanchandra DV973fbce2015-01-27 16:27:20 +0530487 master->cleanup = dspi_cleanup;
Chao Fu349ad662013-08-16 11:08:55 +0800488 master->mode_bits = SPI_CPOL | SPI_CPHA;
489 master->bits_per_word_mask = SPI_BPW_MASK(4) | SPI_BPW_MASK(8) |
490 SPI_BPW_MASK(16);
491
492 ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
493 if (ret < 0) {
494 dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
495 goto out_master_put;
496 }
497 master->num_chipselect = cs_num;
498
499 ret = of_property_read_u32(np, "bus-num", &bus_num);
500 if (ret < 0) {
501 dev_err(&pdev->dev, "can't get bus-num\n");
502 goto out_master_put;
503 }
504 master->bus_num = bus_num;
505
506 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Chao Fu1acbdeb2014-02-12 15:29:05 +0800507 base = devm_ioremap_resource(&pdev->dev, res);
508 if (IS_ERR(base)) {
509 ret = PTR_ERR(base);
Chao Fu349ad662013-08-16 11:08:55 +0800510 goto out_master_put;
511 }
512
Chao Fu1acbdeb2014-02-12 15:29:05 +0800513 dspi->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "dspi", base,
514 &dspi_regmap_config);
515 if (IS_ERR(dspi->regmap)) {
516 dev_err(&pdev->dev, "failed to init regmap: %ld\n",
517 PTR_ERR(dspi->regmap));
518 return PTR_ERR(dspi->regmap);
519 }
520
Chao Fu349ad662013-08-16 11:08:55 +0800521 dspi->irq = platform_get_irq(pdev, 0);
522 if (dspi->irq < 0) {
523 dev_err(&pdev->dev, "can't get platform irq\n");
524 ret = dspi->irq;
525 goto out_master_put;
526 }
527
528 ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt, 0,
529 pdev->name, dspi);
530 if (ret < 0) {
531 dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
532 goto out_master_put;
533 }
534
535 dspi->clk = devm_clk_get(&pdev->dev, "dspi");
536 if (IS_ERR(dspi->clk)) {
537 ret = PTR_ERR(dspi->clk);
538 dev_err(&pdev->dev, "unable to get clock\n");
539 goto out_master_put;
540 }
541 clk_prepare_enable(dspi->clk);
542
543 init_waitqueue_head(&dspi->waitq);
Axel Lin017145f2014-02-14 12:49:12 +0800544 platform_set_drvdata(pdev, master);
Chao Fu349ad662013-08-16 11:08:55 +0800545
Chao Fu9298bc72015-01-27 16:27:22 +0530546 ret = spi_register_master(master);
Chao Fu349ad662013-08-16 11:08:55 +0800547 if (ret != 0) {
548 dev_err(&pdev->dev, "Problem registering DSPI master\n");
549 goto out_clk_put;
550 }
551
Chao Fu349ad662013-08-16 11:08:55 +0800552 return ret;
553
554out_clk_put:
555 clk_disable_unprepare(dspi->clk);
556out_master_put:
557 spi_master_put(master);
Chao Fu349ad662013-08-16 11:08:55 +0800558
559 return ret;
560}
561
562static int dspi_remove(struct platform_device *pdev)
563{
Axel Lin017145f2014-02-14 12:49:12 +0800564 struct spi_master *master = platform_get_drvdata(pdev);
565 struct fsl_dspi *dspi = spi_master_get_devdata(master);
Chao Fu349ad662013-08-16 11:08:55 +0800566
567 /* Disconnect from the SPI framework */
Wei Yongjun05209f42013-10-12 15:15:31 +0800568 clk_disable_unprepare(dspi->clk);
Chao Fu9298bc72015-01-27 16:27:22 +0530569 spi_unregister_master(dspi->master);
570 spi_master_put(dspi->master);
Chao Fu349ad662013-08-16 11:08:55 +0800571
572 return 0;
573}
574
575static struct platform_driver fsl_dspi_driver = {
576 .driver.name = DRIVER_NAME,
577 .driver.of_match_table = fsl_dspi_dt_ids,
578 .driver.owner = THIS_MODULE,
579 .driver.pm = &dspi_pm,
580 .probe = dspi_probe,
581 .remove = dspi_remove,
582};
583module_platform_driver(fsl_dspi_driver);
584
585MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
Uwe Kleine-Königb444d1d2013-09-10 10:46:33 +0200586MODULE_LICENSE("GPL");
Chao Fu349ad662013-08-16 11:08:55 +0800587MODULE_ALIAS("platform:" DRIVER_NAME);