blob: 43caeee8641a4390bf87f6157654c9a60dd5405a [file] [log] [blame]
Grant Likelyca632f52011-06-06 01:16:30 -06001/*
Jassi Brar230d42d2009-11-30 07:39:42 +00002 * Copyright (C) 2009 Samsung Electronics Ltd.
3 * Jaswinder Singh <jassi.brar@samsung.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/workqueue.h>
Mark Brownc2573122011-11-10 10:57:32 +000023#include <linux/interrupt.h>
Jassi Brar230d42d2009-11-30 07:39:42 +000024#include <linux/delay.h>
25#include <linux/clk.h>
26#include <linux/dma-mapping.h>
Arnd Bergmann78843722013-04-11 22:42:03 +020027#include <linux/dmaengine.h>
Jassi Brar230d42d2009-11-30 07:39:42 +000028#include <linux/platform_device.h>
Mark Brownb97b6622011-12-04 00:58:06 +000029#include <linux/pm_runtime.h>
Jassi Brar230d42d2009-11-30 07:39:42 +000030#include <linux/spi/spi.h>
Thomas Abraham1c20c202012-07-13 07:15:14 +090031#include <linux/gpio.h>
Thomas Abraham2b908072012-07-13 07:15:15 +090032#include <linux/of.h>
33#include <linux/of_gpio.h>
Jassi Brar230d42d2009-11-30 07:39:42 +000034
Arnd Bergmann436d42c2012-08-24 15:22:12 +020035#include <linux/platform_data/spi-s3c64xx.h>
Jassi Brar230d42d2009-11-30 07:39:42 +000036
Mark Brown563b4442013-04-18 18:06:05 +010037#ifdef CONFIG_S3C_DMA
Arnd Bergmann78843722013-04-11 22:42:03 +020038#include <mach/dma.h>
39#endif
40
Thomas Abrahama5238e32012-07-13 07:15:14 +090041#define MAX_SPI_PORTS 3
Girish K S7e995552013-05-20 12:21:32 +053042#define S3C64XX_SPI_QUIRK_POLL (1 << 0)
Thomas Abrahama5238e32012-07-13 07:15:14 +090043
Jassi Brar230d42d2009-11-30 07:39:42 +000044/* Registers and bit-fields */
45
46#define S3C64XX_SPI_CH_CFG 0x00
47#define S3C64XX_SPI_CLK_CFG 0x04
48#define S3C64XX_SPI_MODE_CFG 0x08
49#define S3C64XX_SPI_SLAVE_SEL 0x0C
50#define S3C64XX_SPI_INT_EN 0x10
51#define S3C64XX_SPI_STATUS 0x14
52#define S3C64XX_SPI_TX_DATA 0x18
53#define S3C64XX_SPI_RX_DATA 0x1C
54#define S3C64XX_SPI_PACKET_CNT 0x20
55#define S3C64XX_SPI_PENDING_CLR 0x24
56#define S3C64XX_SPI_SWAP_CFG 0x28
57#define S3C64XX_SPI_FB_CLK 0x2C
58
59#define S3C64XX_SPI_CH_HS_EN (1<<6) /* High Speed Enable */
60#define S3C64XX_SPI_CH_SW_RST (1<<5)
61#define S3C64XX_SPI_CH_SLAVE (1<<4)
62#define S3C64XX_SPI_CPOL_L (1<<3)
63#define S3C64XX_SPI_CPHA_B (1<<2)
64#define S3C64XX_SPI_CH_RXCH_ON (1<<1)
65#define S3C64XX_SPI_CH_TXCH_ON (1<<0)
66
67#define S3C64XX_SPI_CLKSEL_SRCMSK (3<<9)
68#define S3C64XX_SPI_CLKSEL_SRCSHFT 9
69#define S3C64XX_SPI_ENCLK_ENABLE (1<<8)
Jingoo Han75bf3362013-01-31 15:25:01 +090070#define S3C64XX_SPI_PSR_MASK 0xff
Jassi Brar230d42d2009-11-30 07:39:42 +000071
72#define S3C64XX_SPI_MODE_CH_TSZ_BYTE (0<<29)
73#define S3C64XX_SPI_MODE_CH_TSZ_HALFWORD (1<<29)
74#define S3C64XX_SPI_MODE_CH_TSZ_WORD (2<<29)
75#define S3C64XX_SPI_MODE_CH_TSZ_MASK (3<<29)
76#define S3C64XX_SPI_MODE_BUS_TSZ_BYTE (0<<17)
77#define S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD (1<<17)
78#define S3C64XX_SPI_MODE_BUS_TSZ_WORD (2<<17)
79#define S3C64XX_SPI_MODE_BUS_TSZ_MASK (3<<17)
80#define S3C64XX_SPI_MODE_RXDMA_ON (1<<2)
81#define S3C64XX_SPI_MODE_TXDMA_ON (1<<1)
82#define S3C64XX_SPI_MODE_4BURST (1<<0)
83
84#define S3C64XX_SPI_SLAVE_AUTO (1<<1)
85#define S3C64XX_SPI_SLAVE_SIG_INACT (1<<0)
86
Jassi Brar230d42d2009-11-30 07:39:42 +000087#define S3C64XX_SPI_INT_TRAILING_EN (1<<6)
88#define S3C64XX_SPI_INT_RX_OVERRUN_EN (1<<5)
89#define S3C64XX_SPI_INT_RX_UNDERRUN_EN (1<<4)
90#define S3C64XX_SPI_INT_TX_OVERRUN_EN (1<<3)
91#define S3C64XX_SPI_INT_TX_UNDERRUN_EN (1<<2)
92#define S3C64XX_SPI_INT_RX_FIFORDY_EN (1<<1)
93#define S3C64XX_SPI_INT_TX_FIFORDY_EN (1<<0)
94
95#define S3C64XX_SPI_ST_RX_OVERRUN_ERR (1<<5)
96#define S3C64XX_SPI_ST_RX_UNDERRUN_ERR (1<<4)
97#define S3C64XX_SPI_ST_TX_OVERRUN_ERR (1<<3)
98#define S3C64XX_SPI_ST_TX_UNDERRUN_ERR (1<<2)
99#define S3C64XX_SPI_ST_RX_FIFORDY (1<<1)
100#define S3C64XX_SPI_ST_TX_FIFORDY (1<<0)
101
102#define S3C64XX_SPI_PACKET_CNT_EN (1<<16)
103
104#define S3C64XX_SPI_PND_TX_UNDERRUN_CLR (1<<4)
105#define S3C64XX_SPI_PND_TX_OVERRUN_CLR (1<<3)
106#define S3C64XX_SPI_PND_RX_UNDERRUN_CLR (1<<2)
107#define S3C64XX_SPI_PND_RX_OVERRUN_CLR (1<<1)
108#define S3C64XX_SPI_PND_TRAILING_CLR (1<<0)
109
110#define S3C64XX_SPI_SWAP_RX_HALF_WORD (1<<7)
111#define S3C64XX_SPI_SWAP_RX_BYTE (1<<6)
112#define S3C64XX_SPI_SWAP_RX_BIT (1<<5)
113#define S3C64XX_SPI_SWAP_RX_EN (1<<4)
114#define S3C64XX_SPI_SWAP_TX_HALF_WORD (1<<3)
115#define S3C64XX_SPI_SWAP_TX_BYTE (1<<2)
116#define S3C64XX_SPI_SWAP_TX_BIT (1<<1)
117#define S3C64XX_SPI_SWAP_TX_EN (1<<0)
118
119#define S3C64XX_SPI_FBCLK_MSK (3<<0)
120
Thomas Abrahama5238e32012-07-13 07:15:14 +0900121#define FIFO_LVL_MASK(i) ((i)->port_conf->fifo_lvl_mask[i->port_id])
122#define S3C64XX_SPI_ST_TX_DONE(v, i) (((v) & \
123 (1 << (i)->port_conf->tx_st_done)) ? 1 : 0)
124#define TX_FIFO_LVL(v, i) (((v) >> 6) & FIFO_LVL_MASK(i))
125#define RX_FIFO_LVL(v, i) (((v) >> (i)->port_conf->rx_lvl_offset) & \
126 FIFO_LVL_MASK(i))
Jassi Brar230d42d2009-11-30 07:39:42 +0000127
128#define S3C64XX_SPI_MAX_TRAILCNT 0x3ff
129#define S3C64XX_SPI_TRAILCNT_OFF 19
130
131#define S3C64XX_SPI_TRAILCNT S3C64XX_SPI_MAX_TRAILCNT
132
133#define msecs_to_loops(t) (loops_per_jiffy / 1000 * HZ * t)
Girish K S7e995552013-05-20 12:21:32 +0530134#define is_polling(x) (x->port_conf->quirks & S3C64XX_SPI_QUIRK_POLL)
Jassi Brar230d42d2009-11-30 07:39:42 +0000135
Jassi Brar230d42d2009-11-30 07:39:42 +0000136#define RXBUSY (1<<2)
137#define TXBUSY (1<<3)
138
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900139struct s3c64xx_spi_dma_data {
Arnd Bergmann78843722013-04-11 22:42:03 +0200140 struct dma_chan *ch;
Arnd Bergmannc10356b2012-04-30 16:31:27 +0000141 enum dma_transfer_direction direction;
Arnd Bergmann78843722013-04-11 22:42:03 +0200142 unsigned int dmach;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900143};
144
Jassi Brar230d42d2009-11-30 07:39:42 +0000145/**
Thomas Abrahama5238e32012-07-13 07:15:14 +0900146 * struct s3c64xx_spi_info - SPI Controller hardware info
147 * @fifo_lvl_mask: Bit-mask for {TX|RX}_FIFO_LVL bits in SPI_STATUS register.
148 * @rx_lvl_offset: Bit offset of RX_FIFO_LVL bits in SPI_STATUS regiter.
149 * @tx_st_done: Bit offset of TX_DONE bit in SPI_STATUS regiter.
150 * @high_speed: True, if the controller supports HIGH_SPEED_EN bit.
151 * @clk_from_cmu: True, if the controller does not include a clock mux and
152 * prescaler unit.
153 *
154 * The Samsung s3c64xx SPI controller are used on various Samsung SoC's but
155 * differ in some aspects such as the size of the fifo and spi bus clock
156 * setup. Such differences are specified to the driver using this structure
157 * which is provided as driver data to the driver.
158 */
159struct s3c64xx_spi_port_config {
160 int fifo_lvl_mask[MAX_SPI_PORTS];
161 int rx_lvl_offset;
162 int tx_st_done;
Girish K S7e995552013-05-20 12:21:32 +0530163 int quirks;
Thomas Abrahama5238e32012-07-13 07:15:14 +0900164 bool high_speed;
165 bool clk_from_cmu;
166};
167
168/**
Jassi Brar230d42d2009-11-30 07:39:42 +0000169 * struct s3c64xx_spi_driver_data - Runtime info holder for SPI driver.
170 * @clk: Pointer to the spi clock.
Jassi Brarb0d5d6e2010-01-20 13:49:44 -0700171 * @src_clk: Pointer to the clock used to generate SPI signals.
Jassi Brar230d42d2009-11-30 07:39:42 +0000172 * @master: Pointer to the SPI Protocol master.
Jassi Brar230d42d2009-11-30 07:39:42 +0000173 * @cntrlr_info: Platform specific data for the controller this driver manages.
174 * @tgl_spi: Pointer to the last CS left untoggled by the cs_change hint.
Jassi Brar230d42d2009-11-30 07:39:42 +0000175 * @lock: Controller specific lock.
176 * @state: Set of FLAGS to indicate status.
177 * @rx_dmach: Controller's DMA channel for Rx.
178 * @tx_dmach: Controller's DMA channel for Tx.
179 * @sfr_start: BUS address of SPI controller regs.
180 * @regs: Pointer to ioremap'ed controller registers.
Mark Brownc2573122011-11-10 10:57:32 +0000181 * @irq: interrupt
Jassi Brar230d42d2009-11-30 07:39:42 +0000182 * @xfer_completion: To indicate completion of xfer task.
183 * @cur_mode: Stores the active configuration of the controller.
184 * @cur_bpw: Stores the active bits per word settings.
185 * @cur_speed: Stores the active xfer clock speed.
186 */
187struct s3c64xx_spi_driver_data {
188 void __iomem *regs;
189 struct clk *clk;
Jassi Brarb0d5d6e2010-01-20 13:49:44 -0700190 struct clk *src_clk;
Jassi Brar230d42d2009-11-30 07:39:42 +0000191 struct platform_device *pdev;
192 struct spi_master *master;
Jassi Brarad7de722010-01-20 13:49:44 -0700193 struct s3c64xx_spi_info *cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +0000194 struct spi_device *tgl_spi;
Jassi Brar230d42d2009-11-30 07:39:42 +0000195 spinlock_t lock;
Jassi Brar230d42d2009-11-30 07:39:42 +0000196 unsigned long sfr_start;
197 struct completion xfer_completion;
198 unsigned state;
199 unsigned cur_mode, cur_bpw;
200 unsigned cur_speed;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900201 struct s3c64xx_spi_dma_data rx_dma;
202 struct s3c64xx_spi_dma_data tx_dma;
Mark Brown563b4442013-04-18 18:06:05 +0100203#ifdef CONFIG_S3C_DMA
Boojin Kim39d3e802011-09-02 09:44:41 +0900204 struct samsung_dma_ops *ops;
Arnd Bergmann78843722013-04-11 22:42:03 +0200205#endif
Thomas Abrahama5238e32012-07-13 07:15:14 +0900206 struct s3c64xx_spi_port_config *port_conf;
207 unsigned int port_id;
Girish K S3146bee2013-06-21 11:26:12 +0530208 bool cs_gpio;
Jassi Brar230d42d2009-11-30 07:39:42 +0000209};
210
Jassi Brar230d42d2009-11-30 07:39:42 +0000211static void flush_fifo(struct s3c64xx_spi_driver_data *sdd)
212{
Jassi Brar230d42d2009-11-30 07:39:42 +0000213 void __iomem *regs = sdd->regs;
214 unsigned long loops;
215 u32 val;
216
217 writel(0, regs + S3C64XX_SPI_PACKET_CNT);
218
219 val = readl(regs + S3C64XX_SPI_CH_CFG);
Kyoungil Kim7d859ff2012-05-23 21:29:51 +0900220 val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
221 writel(val, regs + S3C64XX_SPI_CH_CFG);
222
223 val = readl(regs + S3C64XX_SPI_CH_CFG);
Jassi Brar230d42d2009-11-30 07:39:42 +0000224 val |= S3C64XX_SPI_CH_SW_RST;
225 val &= ~S3C64XX_SPI_CH_HS_EN;
226 writel(val, regs + S3C64XX_SPI_CH_CFG);
227
228 /* Flush TxFIFO*/
229 loops = msecs_to_loops(1);
230 do {
231 val = readl(regs + S3C64XX_SPI_STATUS);
Thomas Abrahama5238e32012-07-13 07:15:14 +0900232 } while (TX_FIFO_LVL(val, sdd) && loops--);
Jassi Brar230d42d2009-11-30 07:39:42 +0000233
Mark Brownbe7852a2010-08-23 17:40:56 +0100234 if (loops == 0)
235 dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
236
Jassi Brar230d42d2009-11-30 07:39:42 +0000237 /* Flush RxFIFO*/
238 loops = msecs_to_loops(1);
239 do {
240 val = readl(regs + S3C64XX_SPI_STATUS);
Thomas Abrahama5238e32012-07-13 07:15:14 +0900241 if (RX_FIFO_LVL(val, sdd))
Jassi Brar230d42d2009-11-30 07:39:42 +0000242 readl(regs + S3C64XX_SPI_RX_DATA);
243 else
244 break;
245 } while (loops--);
246
Mark Brownbe7852a2010-08-23 17:40:56 +0100247 if (loops == 0)
248 dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
249
Jassi Brar230d42d2009-11-30 07:39:42 +0000250 val = readl(regs + S3C64XX_SPI_CH_CFG);
251 val &= ~S3C64XX_SPI_CH_SW_RST;
252 writel(val, regs + S3C64XX_SPI_CH_CFG);
253
254 val = readl(regs + S3C64XX_SPI_MODE_CFG);
255 val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
256 writel(val, regs + S3C64XX_SPI_MODE_CFG);
Jassi Brar230d42d2009-11-30 07:39:42 +0000257}
258
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900259static void s3c64xx_spi_dmacb(void *data)
Boojin Kim39d3e802011-09-02 09:44:41 +0900260{
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900261 struct s3c64xx_spi_driver_data *sdd;
262 struct s3c64xx_spi_dma_data *dma = data;
Boojin Kim39d3e802011-09-02 09:44:41 +0900263 unsigned long flags;
264
Kyoungil Kim054ebcc2012-03-10 09:48:46 +0900265 if (dma->direction == DMA_DEV_TO_MEM)
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900266 sdd = container_of(data,
267 struct s3c64xx_spi_driver_data, rx_dma);
268 else
269 sdd = container_of(data,
270 struct s3c64xx_spi_driver_data, tx_dma);
271
Boojin Kim39d3e802011-09-02 09:44:41 +0900272 spin_lock_irqsave(&sdd->lock, flags);
273
Kyoungil Kim054ebcc2012-03-10 09:48:46 +0900274 if (dma->direction == DMA_DEV_TO_MEM) {
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900275 sdd->state &= ~RXBUSY;
276 if (!(sdd->state & TXBUSY))
277 complete(&sdd->xfer_completion);
278 } else {
279 sdd->state &= ~TXBUSY;
280 if (!(sdd->state & RXBUSY))
281 complete(&sdd->xfer_completion);
282 }
Boojin Kim39d3e802011-09-02 09:44:41 +0900283
284 spin_unlock_irqrestore(&sdd->lock, flags);
285}
286
Mark Brown563b4442013-04-18 18:06:05 +0100287#ifdef CONFIG_S3C_DMA
Arnd Bergmann78843722013-04-11 22:42:03 +0200288/* FIXME: remove this section once arch/arm/mach-s3c64xx uses dmaengine */
289
290static struct s3c2410_dma_client s3c64xx_spi_dma_client = {
291 .name = "samsung-spi-dma",
292};
293
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900294static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
295 unsigned len, dma_addr_t buf)
Boojin Kim39d3e802011-09-02 09:44:41 +0900296{
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900297 struct s3c64xx_spi_driver_data *sdd;
Boojin Kim4969c322012-06-19 13:27:03 +0900298 struct samsung_dma_prep info;
299 struct samsung_dma_config config;
Boojin Kim39d3e802011-09-02 09:44:41 +0900300
Boojin Kim4969c322012-06-19 13:27:03 +0900301 if (dma->direction == DMA_DEV_TO_MEM) {
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900302 sdd = container_of((void *)dma,
303 struct s3c64xx_spi_driver_data, rx_dma);
Boojin Kim4969c322012-06-19 13:27:03 +0900304 config.direction = sdd->rx_dma.direction;
305 config.fifo = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
306 config.width = sdd->cur_bpw / 8;
Arnd Bergmann78843722013-04-11 22:42:03 +0200307 sdd->ops->config((enum dma_ch)sdd->rx_dma.ch, &config);
Boojin Kim4969c322012-06-19 13:27:03 +0900308 } else {
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900309 sdd = container_of((void *)dma,
310 struct s3c64xx_spi_driver_data, tx_dma);
Boojin Kim4969c322012-06-19 13:27:03 +0900311 config.direction = sdd->tx_dma.direction;
312 config.fifo = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
313 config.width = sdd->cur_bpw / 8;
Arnd Bergmann78843722013-04-11 22:42:03 +0200314 sdd->ops->config((enum dma_ch)sdd->tx_dma.ch, &config);
Boojin Kim4969c322012-06-19 13:27:03 +0900315 }
Boojin Kim39d3e802011-09-02 09:44:41 +0900316
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900317 info.cap = DMA_SLAVE;
318 info.len = len;
319 info.fp = s3c64xx_spi_dmacb;
320 info.fp_param = dma;
321 info.direction = dma->direction;
322 info.buf = buf;
Boojin Kim39d3e802011-09-02 09:44:41 +0900323
Arnd Bergmann78843722013-04-11 22:42:03 +0200324 sdd->ops->prepare((enum dma_ch)dma->ch, &info);
325 sdd->ops->trigger((enum dma_ch)dma->ch);
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900326}
327
328static int acquire_dma(struct s3c64xx_spi_driver_data *sdd)
329{
Boojin Kim4969c322012-06-19 13:27:03 +0900330 struct samsung_dma_req req;
Padmavathi Vennab5be04d2013-01-18 17:17:03 +0530331 struct device *dev = &sdd->pdev->dev;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900332
333 sdd->ops = samsung_dma_get_ops();
334
Boojin Kim4969c322012-06-19 13:27:03 +0900335 req.cap = DMA_SLAVE;
336 req.client = &s3c64xx_spi_dma_client;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900337
Jingoo Hanb998aca82013-07-17 17:54:11 +0900338 sdd->rx_dma.ch = (struct dma_chan *)(unsigned long)sdd->ops->request(
339 sdd->rx_dma.dmach, &req, dev, "rx");
340 sdd->tx_dma.ch = (struct dma_chan *)(unsigned long)sdd->ops->request(
341 sdd->tx_dma.dmach, &req, dev, "tx");
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900342
343 return 1;
Boojin Kim39d3e802011-09-02 09:44:41 +0900344}
345
Arnd Bergmann78843722013-04-11 22:42:03 +0200346static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
347{
348 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
349
Girish K S7e995552013-05-20 12:21:32 +0530350 /*
351 * If DMA resource was not available during
352 * probe, no need to continue with dma requests
353 * else Acquire DMA channels
354 */
355 while (!is_polling(sdd) && !acquire_dma(sdd))
Arnd Bergmann78843722013-04-11 22:42:03 +0200356 usleep_range(10000, 11000);
357
Arnd Bergmann78843722013-04-11 22:42:03 +0200358 return 0;
359}
360
361static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
362{
363 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
364
365 /* Free DMA channels */
Girish K S7e995552013-05-20 12:21:32 +0530366 if (!is_polling(sdd)) {
367 sdd->ops->release((enum dma_ch)sdd->rx_dma.ch,
368 &s3c64xx_spi_dma_client);
369 sdd->ops->release((enum dma_ch)sdd->tx_dma.ch,
370 &s3c64xx_spi_dma_client);
371 }
Arnd Bergmann78843722013-04-11 22:42:03 +0200372
373 return 0;
374}
375
376static void s3c64xx_spi_dma_stop(struct s3c64xx_spi_driver_data *sdd,
377 struct s3c64xx_spi_dma_data *dma)
378{
379 sdd->ops->stop((enum dma_ch)dma->ch);
380}
381#else
382
383static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
384 unsigned len, dma_addr_t buf)
385{
386 struct s3c64xx_spi_driver_data *sdd;
387 struct dma_slave_config config;
Arnd Bergmann78843722013-04-11 22:42:03 +0200388 struct dma_async_tx_descriptor *desc;
389
Tomasz Figab1a8e782013-08-11 02:33:28 +0200390 memset(&config, 0, sizeof(config));
391
Arnd Bergmann78843722013-04-11 22:42:03 +0200392 if (dma->direction == DMA_DEV_TO_MEM) {
393 sdd = container_of((void *)dma,
394 struct s3c64xx_spi_driver_data, rx_dma);
395 config.direction = dma->direction;
396 config.src_addr = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
397 config.src_addr_width = sdd->cur_bpw / 8;
398 config.src_maxburst = 1;
399 dmaengine_slave_config(dma->ch, &config);
400 } else {
401 sdd = container_of((void *)dma,
402 struct s3c64xx_spi_driver_data, tx_dma);
403 config.direction = dma->direction;
404 config.dst_addr = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
405 config.dst_addr_width = sdd->cur_bpw / 8;
406 config.dst_maxburst = 1;
407 dmaengine_slave_config(dma->ch, &config);
408 }
409
Tomasz Figa90438c42013-08-11 02:33:30 +0200410 desc = dmaengine_prep_slave_single(dma->ch, buf, len,
411 dma->direction, DMA_PREP_INTERRUPT);
Arnd Bergmann78843722013-04-11 22:42:03 +0200412
413 desc->callback = s3c64xx_spi_dmacb;
414 desc->callback_param = dma;
415
416 dmaengine_submit(desc);
417 dma_async_issue_pending(dma->ch);
418}
419
420static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
421{
422 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
423 dma_filter_fn filter = sdd->cntrlr_info->filter;
424 struct device *dev = &sdd->pdev->dev;
425 dma_cap_mask_t mask;
Mark Brownfb9d0442013-04-18 18:12:00 +0100426 int ret;
Arnd Bergmann78843722013-04-11 22:42:03 +0200427
Mark Brownc12f9642013-08-13 19:03:01 +0100428 if (!is_polling(sdd)) {
429 dma_cap_zero(mask);
430 dma_cap_set(DMA_SLAVE, mask);
Girish K Sd96760f92013-06-27 12:26:53 +0530431
Mark Brownc12f9642013-08-13 19:03:01 +0100432 /* Acquire DMA channels */
433 sdd->rx_dma.ch = dma_request_slave_channel_compat(mask, filter,
434 (void *)sdd->rx_dma.dmach, dev, "rx");
435 if (!sdd->rx_dma.ch) {
436 dev_err(dev, "Failed to get RX DMA channel\n");
437 ret = -EBUSY;
438 goto out;
439 }
Arnd Bergmann78843722013-04-11 22:42:03 +0200440
Mark Brownc12f9642013-08-13 19:03:01 +0100441 sdd->tx_dma.ch = dma_request_slave_channel_compat(mask, filter,
442 (void *)sdd->tx_dma.dmach, dev, "tx");
443 if (!sdd->tx_dma.ch) {
444 dev_err(dev, "Failed to get TX DMA channel\n");
445 ret = -EBUSY;
446 goto out_rx;
447 }
Mark Brownfb9d0442013-04-18 18:12:00 +0100448 }
449
450 ret = pm_runtime_get_sync(&sdd->pdev->dev);
Sylwester Nawrocki6c6cf642013-06-10 18:22:26 +0200451 if (ret < 0) {
Mark Brownfb9d0442013-04-18 18:12:00 +0100452 dev_err(dev, "Failed to enable device: %d\n", ret);
453 goto out_tx;
454 }
Arnd Bergmann78843722013-04-11 22:42:03 +0200455
456 return 0;
Mark Brownfb9d0442013-04-18 18:12:00 +0100457
458out_tx:
459 dma_release_channel(sdd->tx_dma.ch);
460out_rx:
461 dma_release_channel(sdd->rx_dma.ch);
462out:
463 return ret;
Arnd Bergmann78843722013-04-11 22:42:03 +0200464}
465
466static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
467{
468 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
469
470 /* Free DMA channels */
Girish K S7e995552013-05-20 12:21:32 +0530471 if (!is_polling(sdd)) {
472 dma_release_channel(sdd->rx_dma.ch);
473 dma_release_channel(sdd->tx_dma.ch);
474 }
Arnd Bergmann78843722013-04-11 22:42:03 +0200475
476 pm_runtime_put(&sdd->pdev->dev);
477 return 0;
478}
479
480static void s3c64xx_spi_dma_stop(struct s3c64xx_spi_driver_data *sdd,
481 struct s3c64xx_spi_dma_data *dma)
482{
483 dmaengine_terminate_all(dma->ch);
484}
485#endif
486
Jassi Brar230d42d2009-11-30 07:39:42 +0000487static void enable_datapath(struct s3c64xx_spi_driver_data *sdd,
488 struct spi_device *spi,
489 struct spi_transfer *xfer, int dma_mode)
490{
Jassi Brar230d42d2009-11-30 07:39:42 +0000491 void __iomem *regs = sdd->regs;
492 u32 modecfg, chcfg;
493
494 modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
495 modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
496
497 chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
498 chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;
499
500 if (dma_mode) {
501 chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
502 } else {
503 /* Always shift in data in FIFO, even if xfer is Tx only,
504 * this helps setting PCKT_CNT value for generating clocks
505 * as exactly needed.
506 */
507 chcfg |= S3C64XX_SPI_CH_RXCH_ON;
508 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
509 | S3C64XX_SPI_PACKET_CNT_EN,
510 regs + S3C64XX_SPI_PACKET_CNT);
511 }
512
513 if (xfer->tx_buf != NULL) {
514 sdd->state |= TXBUSY;
515 chcfg |= S3C64XX_SPI_CH_TXCH_ON;
516 if (dma_mode) {
517 modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900518 prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma);
Jassi Brar230d42d2009-11-30 07:39:42 +0000519 } else {
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900520 switch (sdd->cur_bpw) {
521 case 32:
522 iowrite32_rep(regs + S3C64XX_SPI_TX_DATA,
523 xfer->tx_buf, xfer->len / 4);
524 break;
525 case 16:
526 iowrite16_rep(regs + S3C64XX_SPI_TX_DATA,
527 xfer->tx_buf, xfer->len / 2);
528 break;
529 default:
530 iowrite8_rep(regs + S3C64XX_SPI_TX_DATA,
531 xfer->tx_buf, xfer->len);
532 break;
533 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000534 }
535 }
536
537 if (xfer->rx_buf != NULL) {
538 sdd->state |= RXBUSY;
539
Thomas Abrahama5238e32012-07-13 07:15:14 +0900540 if (sdd->port_conf->high_speed && sdd->cur_speed >= 30000000UL
Jassi Brar230d42d2009-11-30 07:39:42 +0000541 && !(sdd->cur_mode & SPI_CPHA))
542 chcfg |= S3C64XX_SPI_CH_HS_EN;
543
544 if (dma_mode) {
545 modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
546 chcfg |= S3C64XX_SPI_CH_RXCH_ON;
547 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
548 | S3C64XX_SPI_PACKET_CNT_EN,
549 regs + S3C64XX_SPI_PACKET_CNT);
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900550 prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma);
Jassi Brar230d42d2009-11-30 07:39:42 +0000551 }
552 }
553
554 writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
555 writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
556}
557
558static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd,
559 struct spi_device *spi)
560{
Jassi Brar230d42d2009-11-30 07:39:42 +0000561 if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */
562 if (sdd->tgl_spi != spi) { /* if last mssg on diff device */
563 /* Deselect the last toggled device */
Mark Browndd97e262013-09-27 18:58:55 +0100564 if (spi->cs_gpio >= 0)
565 gpio_set_value(spi->cs_gpio,
Girish K S3146bee2013-06-21 11:26:12 +0530566 spi->mode & SPI_CS_HIGH ? 0 : 1);
Jassi Brar230d42d2009-11-30 07:39:42 +0000567 }
568 sdd->tgl_spi = NULL;
569 }
570
Mark Browndd97e262013-09-27 18:58:55 +0100571 if (spi->cs_gpio >= 0)
572 gpio_set_value(spi->cs_gpio, spi->mode & SPI_CS_HIGH ? 1 : 0);
Girish K S7e995552013-05-20 12:21:32 +0530573
574 /* Start the signals */
575 writel(0, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
576}
577
Mark Brown79617072013-06-19 19:12:39 +0100578static u32 s3c64xx_spi_wait_for_timeout(struct s3c64xx_spi_driver_data *sdd,
Girish K S7e995552013-05-20 12:21:32 +0530579 int timeout_ms)
580{
581 void __iomem *regs = sdd->regs;
582 unsigned long val = 1;
583 u32 status;
584
585 /* max fifo depth available */
586 u32 max_fifo = (FIFO_LVL_MASK(sdd) >> 1) + 1;
587
588 if (timeout_ms)
589 val = msecs_to_loops(timeout_ms);
590
591 do {
592 status = readl(regs + S3C64XX_SPI_STATUS);
593 } while (RX_FIFO_LVL(status, sdd) < max_fifo && --val);
594
595 /* return the actual received data length */
596 return RX_FIFO_LVL(status, sdd);
Jassi Brar230d42d2009-11-30 07:39:42 +0000597}
598
599static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd,
600 struct spi_transfer *xfer, int dma_mode)
601{
Jassi Brar230d42d2009-11-30 07:39:42 +0000602 void __iomem *regs = sdd->regs;
603 unsigned long val;
604 int ms;
605
606 /* millisecs to xfer 'len' bytes @ 'cur_speed' */
607 ms = xfer->len * 8 * 1000 / sdd->cur_speed;
Mark Brown9d8f86b2010-09-07 16:37:52 +0100608 ms += 10; /* some tolerance */
Jassi Brar230d42d2009-11-30 07:39:42 +0000609
610 if (dma_mode) {
611 val = msecs_to_jiffies(ms) + 10;
612 val = wait_for_completion_timeout(&sdd->xfer_completion, val);
613 } else {
Jassi Brarc3f139b2010-09-03 10:36:46 +0900614 u32 status;
Jassi Brar230d42d2009-11-30 07:39:42 +0000615 val = msecs_to_loops(ms);
616 do {
Jassi Brarc3f139b2010-09-03 10:36:46 +0900617 status = readl(regs + S3C64XX_SPI_STATUS);
Thomas Abrahama5238e32012-07-13 07:15:14 +0900618 } while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);
Jassi Brar230d42d2009-11-30 07:39:42 +0000619 }
620
Jassi Brar230d42d2009-11-30 07:39:42 +0000621 if (dma_mode) {
622 u32 status;
623
624 /*
Girish K S7e995552013-05-20 12:21:32 +0530625 * If the previous xfer was completed within timeout, then
626 * proceed further else return -EIO.
Jassi Brar230d42d2009-11-30 07:39:42 +0000627 * DmaTx returns after simply writing data in the FIFO,
628 * w/o waiting for real transmission on the bus to finish.
629 * DmaRx returns only after Dma read data from FIFO which
630 * needs bus transmission to finish, so we don't worry if
631 * Xfer involved Rx(with or without Tx).
632 */
Girish K S7e995552013-05-20 12:21:32 +0530633 if (val && !xfer->rx_buf) {
Jassi Brar230d42d2009-11-30 07:39:42 +0000634 val = msecs_to_loops(10);
635 status = readl(regs + S3C64XX_SPI_STATUS);
Thomas Abrahama5238e32012-07-13 07:15:14 +0900636 while ((TX_FIFO_LVL(status, sdd)
637 || !S3C64XX_SPI_ST_TX_DONE(status, sdd))
Jassi Brar230d42d2009-11-30 07:39:42 +0000638 && --val) {
639 cpu_relax();
640 status = readl(regs + S3C64XX_SPI_STATUS);
641 }
642
Jassi Brar230d42d2009-11-30 07:39:42 +0000643 }
Girish K S7e995552013-05-20 12:21:32 +0530644
645 /* If timed out while checking rx/tx status return error */
646 if (!val)
647 return -EIO;
Jassi Brar230d42d2009-11-30 07:39:42 +0000648 } else {
Girish K S7e995552013-05-20 12:21:32 +0530649 int loops;
650 u32 cpy_len;
651 u8 *buf;
652
Jassi Brar230d42d2009-11-30 07:39:42 +0000653 /* If it was only Tx */
Girish K S7e995552013-05-20 12:21:32 +0530654 if (!xfer->rx_buf) {
Jassi Brar230d42d2009-11-30 07:39:42 +0000655 sdd->state &= ~TXBUSY;
656 return 0;
657 }
658
Girish K S7e995552013-05-20 12:21:32 +0530659 /*
660 * If the receive length is bigger than the controller fifo
661 * size, calculate the loops and read the fifo as many times.
662 * loops = length / max fifo size (calculated by using the
663 * fifo mask).
664 * For any size less than the fifo size the below code is
665 * executed atleast once.
666 */
667 loops = xfer->len / ((FIFO_LVL_MASK(sdd) >> 1) + 1);
668 buf = xfer->rx_buf;
669 do {
670 /* wait for data to be received in the fifo */
Mark Brown79617072013-06-19 19:12:39 +0100671 cpy_len = s3c64xx_spi_wait_for_timeout(sdd,
672 (loops ? ms : 0));
Girish K S7e995552013-05-20 12:21:32 +0530673
674 switch (sdd->cur_bpw) {
675 case 32:
676 ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
677 buf, cpy_len / 4);
678 break;
679 case 16:
680 ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
681 buf, cpy_len / 2);
682 break;
683 default:
684 ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
685 buf, cpy_len);
686 break;
687 }
688
689 buf = buf + cpy_len;
690 } while (loops--);
Jassi Brar230d42d2009-11-30 07:39:42 +0000691 sdd->state &= ~RXBUSY;
692 }
693
694 return 0;
695}
696
697static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd,
698 struct spi_device *spi)
699{
Jassi Brar230d42d2009-11-30 07:39:42 +0000700 if (sdd->tgl_spi == spi)
701 sdd->tgl_spi = NULL;
702
Mark Browndd97e262013-09-27 18:58:55 +0100703 if (spi->cs_gpio >= 0)
704 gpio_set_value(spi->cs_gpio, spi->mode & SPI_CS_HIGH ? 0 : 1);
Girish K S7e995552013-05-20 12:21:32 +0530705
706 /* Quiese the signals */
707 writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
Jassi Brar230d42d2009-11-30 07:39:42 +0000708}
709
710static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
711{
Jassi Brar230d42d2009-11-30 07:39:42 +0000712 void __iomem *regs = sdd->regs;
713 u32 val;
714
715 /* Disable Clock */
Thomas Abrahama5238e32012-07-13 07:15:14 +0900716 if (sdd->port_conf->clk_from_cmu) {
Thomas Abraham9f667bf2012-10-03 08:30:12 +0900717 clk_disable_unprepare(sdd->src_clk);
Jassi Brarb42a81c2010-09-29 17:31:33 +0900718 } else {
719 val = readl(regs + S3C64XX_SPI_CLK_CFG);
720 val &= ~S3C64XX_SPI_ENCLK_ENABLE;
721 writel(val, regs + S3C64XX_SPI_CLK_CFG);
722 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000723
724 /* Set Polarity and Phase */
725 val = readl(regs + S3C64XX_SPI_CH_CFG);
726 val &= ~(S3C64XX_SPI_CH_SLAVE |
727 S3C64XX_SPI_CPOL_L |
728 S3C64XX_SPI_CPHA_B);
729
730 if (sdd->cur_mode & SPI_CPOL)
731 val |= S3C64XX_SPI_CPOL_L;
732
733 if (sdd->cur_mode & SPI_CPHA)
734 val |= S3C64XX_SPI_CPHA_B;
735
736 writel(val, regs + S3C64XX_SPI_CH_CFG);
737
738 /* Set Channel & DMA Mode */
739 val = readl(regs + S3C64XX_SPI_MODE_CFG);
740 val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
741 | S3C64XX_SPI_MODE_CH_TSZ_MASK);
742
743 switch (sdd->cur_bpw) {
744 case 32:
745 val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900746 val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
Jassi Brar230d42d2009-11-30 07:39:42 +0000747 break;
748 case 16:
749 val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900750 val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
Jassi Brar230d42d2009-11-30 07:39:42 +0000751 break;
752 default:
753 val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900754 val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
Jassi Brar230d42d2009-11-30 07:39:42 +0000755 break;
756 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000757
758 writel(val, regs + S3C64XX_SPI_MODE_CFG);
759
Thomas Abrahama5238e32012-07-13 07:15:14 +0900760 if (sdd->port_conf->clk_from_cmu) {
Jassi Brarb42a81c2010-09-29 17:31:33 +0900761 /* Configure Clock */
762 /* There is half-multiplier before the SPI */
763 clk_set_rate(sdd->src_clk, sdd->cur_speed * 2);
764 /* Enable Clock */
Thomas Abraham9f667bf2012-10-03 08:30:12 +0900765 clk_prepare_enable(sdd->src_clk);
Jassi Brarb42a81c2010-09-29 17:31:33 +0900766 } else {
767 /* Configure Clock */
768 val = readl(regs + S3C64XX_SPI_CLK_CFG);
769 val &= ~S3C64XX_SPI_PSR_MASK;
770 val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / 2 - 1)
771 & S3C64XX_SPI_PSR_MASK);
772 writel(val, regs + S3C64XX_SPI_CLK_CFG);
Jassi Brar230d42d2009-11-30 07:39:42 +0000773
Jassi Brarb42a81c2010-09-29 17:31:33 +0900774 /* Enable Clock */
775 val = readl(regs + S3C64XX_SPI_CLK_CFG);
776 val |= S3C64XX_SPI_ENCLK_ENABLE;
777 writel(val, regs + S3C64XX_SPI_CLK_CFG);
778 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000779}
780
Jassi Brar230d42d2009-11-30 07:39:42 +0000781#define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
782
783static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data *sdd,
784 struct spi_message *msg)
785{
786 struct device *dev = &sdd->pdev->dev;
787 struct spi_transfer *xfer;
788
Girish K S7e995552013-05-20 12:21:32 +0530789 if (is_polling(sdd) || msg->is_dma_mapped)
Jassi Brar230d42d2009-11-30 07:39:42 +0000790 return 0;
791
792 /* First mark all xfer unmapped */
793 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
794 xfer->rx_dma = XFER_DMAADDR_INVALID;
795 xfer->tx_dma = XFER_DMAADDR_INVALID;
796 }
797
798 /* Map until end or first fail */
799 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
800
Thomas Abrahama5238e32012-07-13 07:15:14 +0900801 if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
Jassi Brare02ddd42010-09-29 17:31:31 +0900802 continue;
803
Jassi Brar230d42d2009-11-30 07:39:42 +0000804 if (xfer->tx_buf != NULL) {
Jassi Brar251ee472010-09-03 10:36:26 +0900805 xfer->tx_dma = dma_map_single(dev,
806 (void *)xfer->tx_buf, xfer->len,
807 DMA_TO_DEVICE);
Jassi Brar230d42d2009-11-30 07:39:42 +0000808 if (dma_mapping_error(dev, xfer->tx_dma)) {
809 dev_err(dev, "dma_map_single Tx failed\n");
810 xfer->tx_dma = XFER_DMAADDR_INVALID;
811 return -ENOMEM;
812 }
813 }
814
815 if (xfer->rx_buf != NULL) {
816 xfer->rx_dma = dma_map_single(dev, xfer->rx_buf,
817 xfer->len, DMA_FROM_DEVICE);
818 if (dma_mapping_error(dev, xfer->rx_dma)) {
819 dev_err(dev, "dma_map_single Rx failed\n");
820 dma_unmap_single(dev, xfer->tx_dma,
821 xfer->len, DMA_TO_DEVICE);
822 xfer->tx_dma = XFER_DMAADDR_INVALID;
823 xfer->rx_dma = XFER_DMAADDR_INVALID;
824 return -ENOMEM;
825 }
826 }
827 }
828
829 return 0;
830}
831
832static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data *sdd,
833 struct spi_message *msg)
834{
835 struct device *dev = &sdd->pdev->dev;
836 struct spi_transfer *xfer;
837
Girish K S7e995552013-05-20 12:21:32 +0530838 if (is_polling(sdd) || msg->is_dma_mapped)
Jassi Brar230d42d2009-11-30 07:39:42 +0000839 return;
840
841 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
842
Thomas Abrahama5238e32012-07-13 07:15:14 +0900843 if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
Jassi Brare02ddd42010-09-29 17:31:31 +0900844 continue;
845
Jassi Brar230d42d2009-11-30 07:39:42 +0000846 if (xfer->rx_buf != NULL
847 && xfer->rx_dma != XFER_DMAADDR_INVALID)
848 dma_unmap_single(dev, xfer->rx_dma,
849 xfer->len, DMA_FROM_DEVICE);
850
851 if (xfer->tx_buf != NULL
852 && xfer->tx_dma != XFER_DMAADDR_INVALID)
853 dma_unmap_single(dev, xfer->tx_dma,
854 xfer->len, DMA_TO_DEVICE);
855 }
856}
857
Mark Brownad2a99a2012-02-15 14:48:32 -0800858static int s3c64xx_spi_transfer_one_message(struct spi_master *master,
859 struct spi_message *msg)
Jassi Brar230d42d2009-11-30 07:39:42 +0000860{
Mark Brownad2a99a2012-02-15 14:48:32 -0800861 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brar230d42d2009-11-30 07:39:42 +0000862 struct spi_device *spi = msg->spi;
863 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
864 struct spi_transfer *xfer;
865 int status = 0, cs_toggle = 0;
866 u32 speed;
867 u8 bpw;
868
869 /* If Master's(controller) state differs from that needed by Slave */
870 if (sdd->cur_speed != spi->max_speed_hz
871 || sdd->cur_mode != spi->mode
872 || sdd->cur_bpw != spi->bits_per_word) {
873 sdd->cur_bpw = spi->bits_per_word;
874 sdd->cur_speed = spi->max_speed_hz;
875 sdd->cur_mode = spi->mode;
876 s3c64xx_spi_config(sdd);
877 }
878
879 /* Map all the transfers if needed */
880 if (s3c64xx_spi_map_mssg(sdd, msg)) {
881 dev_err(&spi->dev,
882 "Xfer: Unable to map message buffers!\n");
883 status = -ENOMEM;
884 goto out;
885 }
886
887 /* Configure feedback delay */
888 writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);
889
890 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
891
892 unsigned long flags;
893 int use_dma;
894
895 INIT_COMPLETION(sdd->xfer_completion);
896
897 /* Only BPW and Speed may change across transfers */
Laxman Dewangan766ed702012-12-18 14:25:43 +0530898 bpw = xfer->bits_per_word;
Jassi Brar230d42d2009-11-30 07:39:42 +0000899 speed = xfer->speed_hz ? : spi->max_speed_hz;
900
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900901 if (xfer->len % (bpw / 8)) {
902 dev_err(&spi->dev,
903 "Xfer length(%u) not a multiple of word size(%u)\n",
904 xfer->len, bpw / 8);
905 status = -EIO;
906 goto out;
907 }
908
Jassi Brar230d42d2009-11-30 07:39:42 +0000909 if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
910 sdd->cur_bpw = bpw;
911 sdd->cur_speed = speed;
912 s3c64xx_spi_config(sdd);
913 }
914
915 /* Polling method for xfers not bigger than FIFO capacity */
Arnd Bergmann78843722013-04-11 22:42:03 +0200916 use_dma = 0;
Girish K S7e995552013-05-20 12:21:32 +0530917 if (!is_polling(sdd) &&
918 (sdd->rx_dma.ch && sdd->tx_dma.ch &&
919 (xfer->len > ((FIFO_LVL_MASK(sdd) >> 1) + 1))))
Jassi Brar230d42d2009-11-30 07:39:42 +0000920 use_dma = 1;
921
922 spin_lock_irqsave(&sdd->lock, flags);
923
924 /* Pending only which is to be done */
925 sdd->state &= ~RXBUSY;
926 sdd->state &= ~TXBUSY;
927
928 enable_datapath(sdd, spi, xfer, use_dma);
929
930 /* Slave Select */
931 enable_cs(sdd, spi);
932
Jassi Brar230d42d2009-11-30 07:39:42 +0000933 spin_unlock_irqrestore(&sdd->lock, flags);
934
935 status = wait_for_xfer(sdd, xfer, use_dma);
936
Jassi Brar230d42d2009-11-30 07:39:42 +0000937 if (status) {
Jingoo Han75bf3362013-01-31 15:25:01 +0900938 dev_err(&spi->dev, "I/O Error: rx-%d tx-%d res:rx-%c tx-%c len-%d\n",
Jassi Brar230d42d2009-11-30 07:39:42 +0000939 xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
940 (sdd->state & RXBUSY) ? 'f' : 'p',
941 (sdd->state & TXBUSY) ? 'f' : 'p',
942 xfer->len);
943
944 if (use_dma) {
945 if (xfer->tx_buf != NULL
946 && (sdd->state & TXBUSY))
Arnd Bergmann78843722013-04-11 22:42:03 +0200947 s3c64xx_spi_dma_stop(sdd, &sdd->tx_dma);
Jassi Brar230d42d2009-11-30 07:39:42 +0000948 if (xfer->rx_buf != NULL
949 && (sdd->state & RXBUSY))
Arnd Bergmann78843722013-04-11 22:42:03 +0200950 s3c64xx_spi_dma_stop(sdd, &sdd->rx_dma);
Jassi Brar230d42d2009-11-30 07:39:42 +0000951 }
952
953 goto out;
954 }
955
Mark Brown67651b22013-09-27 20:10:26 +0100956 flush_fifo(sdd);
957
Jassi Brar230d42d2009-11-30 07:39:42 +0000958 if (xfer->delay_usecs)
959 udelay(xfer->delay_usecs);
960
961 if (xfer->cs_change) {
962 /* Hint that the next mssg is gonna be
963 for the same device */
964 if (list_is_last(&xfer->transfer_list,
965 &msg->transfers))
966 cs_toggle = 1;
Jassi Brar230d42d2009-11-30 07:39:42 +0000967 }
968
969 msg->actual_length += xfer->len;
Jassi Brar230d42d2009-11-30 07:39:42 +0000970 }
971
972out:
973 if (!cs_toggle || status)
974 disable_cs(sdd, spi);
975 else
976 sdd->tgl_spi = spi;
977
978 s3c64xx_spi_unmap_mssg(sdd, msg);
979
980 msg->status = status;
981
Mark Brownad2a99a2012-02-15 14:48:32 -0800982 spi_finalize_current_message(master);
983
984 return 0;
Jassi Brar230d42d2009-11-30 07:39:42 +0000985}
986
Thomas Abraham2b908072012-07-13 07:15:15 +0900987static struct s3c64xx_spi_csinfo *s3c64xx_get_slave_ctrldata(
Thomas Abraham2b908072012-07-13 07:15:15 +0900988 struct spi_device *spi)
989{
990 struct s3c64xx_spi_csinfo *cs;
Arnd Bergmann4732cc62012-08-04 11:18:20 +0000991 struct device_node *slave_np, *data_np = NULL;
Girish K S3146bee2013-06-21 11:26:12 +0530992 struct s3c64xx_spi_driver_data *sdd;
Thomas Abraham2b908072012-07-13 07:15:15 +0900993 u32 fb_delay = 0;
994
Girish K S3146bee2013-06-21 11:26:12 +0530995 sdd = spi_master_get_devdata(spi->master);
Thomas Abraham2b908072012-07-13 07:15:15 +0900996 slave_np = spi->dev.of_node;
997 if (!slave_np) {
998 dev_err(&spi->dev, "device node not found\n");
999 return ERR_PTR(-EINVAL);
1000 }
1001
Srinivas Kandagatla06455bb2012-09-18 08:10:49 +01001002 data_np = of_get_child_by_name(slave_np, "controller-data");
Thomas Abraham2b908072012-07-13 07:15:15 +09001003 if (!data_np) {
1004 dev_err(&spi->dev, "child node 'controller-data' not found\n");
1005 return ERR_PTR(-EINVAL);
1006 }
1007
1008 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
1009 if (!cs) {
Jingoo Han75bf3362013-01-31 15:25:01 +09001010 dev_err(&spi->dev, "could not allocate memory for controller data\n");
Srinivas Kandagatla06455bb2012-09-18 08:10:49 +01001011 of_node_put(data_np);
Thomas Abraham2b908072012-07-13 07:15:15 +09001012 return ERR_PTR(-ENOMEM);
1013 }
1014
Girish K S3146bee2013-06-21 11:26:12 +05301015 /* The CS line is asserted/deasserted by the gpio pin */
1016 if (sdd->cs_gpio)
1017 cs->line = of_get_named_gpio(data_np, "cs-gpio", 0);
1018
Thomas Abraham2b908072012-07-13 07:15:15 +09001019 if (!gpio_is_valid(cs->line)) {
Jingoo Han75bf3362013-01-31 15:25:01 +09001020 dev_err(&spi->dev, "chip select gpio is not specified or invalid\n");
Thomas Abraham2b908072012-07-13 07:15:15 +09001021 kfree(cs);
Srinivas Kandagatla06455bb2012-09-18 08:10:49 +01001022 of_node_put(data_np);
Thomas Abraham2b908072012-07-13 07:15:15 +09001023 return ERR_PTR(-EINVAL);
1024 }
1025
1026 of_property_read_u32(data_np, "samsung,spi-feedback-delay", &fb_delay);
1027 cs->fb_delay = fb_delay;
Srinivas Kandagatla06455bb2012-09-18 08:10:49 +01001028 of_node_put(data_np);
Thomas Abraham2b908072012-07-13 07:15:15 +09001029 return cs;
1030}
1031
Jassi Brar230d42d2009-11-30 07:39:42 +00001032/*
1033 * Here we only check the validity of requested configuration
1034 * and save the configuration in a local data-structure.
1035 * The controller is actually configured only just before we
1036 * get a message to transfer.
1037 */
1038static int s3c64xx_spi_setup(struct spi_device *spi)
1039{
1040 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
1041 struct s3c64xx_spi_driver_data *sdd;
Jassi Brarad7de722010-01-20 13:49:44 -07001042 struct s3c64xx_spi_info *sci;
Thomas Abraham2b908072012-07-13 07:15:15 +09001043 int err;
Jassi Brar230d42d2009-11-30 07:39:42 +00001044
Thomas Abraham2b908072012-07-13 07:15:15 +09001045 sdd = spi_master_get_devdata(spi->master);
1046 if (!cs && spi->dev.of_node) {
Matthias Brugger5c725b32013-03-26 10:27:35 +01001047 cs = s3c64xx_get_slave_ctrldata(spi);
Thomas Abraham2b908072012-07-13 07:15:15 +09001048 spi->controller_data = cs;
1049 }
1050
1051 if (IS_ERR_OR_NULL(cs)) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001052 dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select);
1053 return -ENODEV;
1054 }
1055
Tomasz Figa01498712013-08-11 02:33:29 +02001056 if (!spi_get_ctldata(spi)) {
1057 /* Request gpio only if cs line is asserted by gpio pins */
1058 if (sdd->cs_gpio) {
1059 err = gpio_request_one(cs->line, GPIOF_OUT_INIT_HIGH,
1060 dev_name(&spi->dev));
1061 if (err) {
1062 dev_err(&spi->dev,
1063 "Failed to get /CS gpio [%d]: %d\n",
1064 cs->line, err);
1065 goto err_gpio_req;
1066 }
Mark Browndd97e262013-09-27 18:58:55 +01001067
1068 spi->cs_gpio = cs->line;
Thomas Abraham1c20c202012-07-13 07:15:14 +09001069 }
Thomas Abraham1c20c202012-07-13 07:15:14 +09001070
Girish K S3146bee2013-06-21 11:26:12 +05301071 spi_set_ctldata(spi, cs);
Tomasz Figa01498712013-08-11 02:33:29 +02001072 }
Girish K S3146bee2013-06-21 11:26:12 +05301073
Jassi Brar230d42d2009-11-30 07:39:42 +00001074 sci = sdd->cntrlr_info;
1075
Mark Brownb97b6622011-12-04 00:58:06 +00001076 pm_runtime_get_sync(&sdd->pdev->dev);
1077
Jassi Brar230d42d2009-11-30 07:39:42 +00001078 /* Check if we can provide the requested rate */
Thomas Abrahama5238e32012-07-13 07:15:14 +09001079 if (!sdd->port_conf->clk_from_cmu) {
Jassi Brarb42a81c2010-09-29 17:31:33 +09001080 u32 psr, speed;
Jassi Brar230d42d2009-11-30 07:39:42 +00001081
Jassi Brarb42a81c2010-09-29 17:31:33 +09001082 /* Max possible */
1083 speed = clk_get_rate(sdd->src_clk) / 2 / (0 + 1);
Jassi Brar230d42d2009-11-30 07:39:42 +00001084
Jassi Brarb42a81c2010-09-29 17:31:33 +09001085 if (spi->max_speed_hz > speed)
1086 spi->max_speed_hz = speed;
Jassi Brar230d42d2009-11-30 07:39:42 +00001087
Jassi Brarb42a81c2010-09-29 17:31:33 +09001088 psr = clk_get_rate(sdd->src_clk) / 2 / spi->max_speed_hz - 1;
1089 psr &= S3C64XX_SPI_PSR_MASK;
1090 if (psr == S3C64XX_SPI_PSR_MASK)
1091 psr--;
1092
1093 speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
1094 if (spi->max_speed_hz < speed) {
1095 if (psr+1 < S3C64XX_SPI_PSR_MASK) {
1096 psr++;
1097 } else {
1098 err = -EINVAL;
1099 goto setup_exit;
1100 }
Jassi Brar230d42d2009-11-30 07:39:42 +00001101 }
Jassi Brar230d42d2009-11-30 07:39:42 +00001102
Jassi Brarb42a81c2010-09-29 17:31:33 +09001103 speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
Thomas Abraham2b908072012-07-13 07:15:15 +09001104 if (spi->max_speed_hz >= speed) {
Jassi Brarb42a81c2010-09-29 17:31:33 +09001105 spi->max_speed_hz = speed;
Thomas Abraham2b908072012-07-13 07:15:15 +09001106 } else {
Mark Browne1b0f0d2012-12-20 18:27:31 +00001107 dev_err(&spi->dev, "Can't set %dHz transfer speed\n",
1108 spi->max_speed_hz);
Jassi Brarb42a81c2010-09-29 17:31:33 +09001109 err = -EINVAL;
Thomas Abraham2b908072012-07-13 07:15:15 +09001110 goto setup_exit;
1111 }
Jassi Brarb42a81c2010-09-29 17:31:33 +09001112 }
Jassi Brar230d42d2009-11-30 07:39:42 +00001113
Mark Brownb97b6622011-12-04 00:58:06 +00001114 pm_runtime_put(&sdd->pdev->dev);
Thomas Abraham2b908072012-07-13 07:15:15 +09001115 disable_cs(sdd, spi);
1116 return 0;
Mark Brownb97b6622011-12-04 00:58:06 +00001117
Jassi Brar230d42d2009-11-30 07:39:42 +00001118setup_exit:
Jassi Brar230d42d2009-11-30 07:39:42 +00001119 /* setup() returns with device de-selected */
1120 disable_cs(sdd, spi);
1121
Thomas Abraham2b908072012-07-13 07:15:15 +09001122 gpio_free(cs->line);
1123 spi_set_ctldata(spi, NULL);
1124
1125err_gpio_req:
Sylwester Nawrocki5bee3b92012-09-13 16:31:30 +02001126 if (spi->dev.of_node)
1127 kfree(cs);
Thomas Abraham2b908072012-07-13 07:15:15 +09001128
Jassi Brar230d42d2009-11-30 07:39:42 +00001129 return err;
1130}
1131
Thomas Abraham1c20c202012-07-13 07:15:14 +09001132static void s3c64xx_spi_cleanup(struct spi_device *spi)
1133{
1134 struct s3c64xx_spi_csinfo *cs = spi_get_ctldata(spi);
Girish K S3146bee2013-06-21 11:26:12 +05301135 struct s3c64xx_spi_driver_data *sdd;
Thomas Abraham1c20c202012-07-13 07:15:14 +09001136
Girish K S3146bee2013-06-21 11:26:12 +05301137 sdd = spi_master_get_devdata(spi->master);
Mark Browndd97e262013-09-27 18:58:55 +01001138 if (spi->cs_gpio) {
1139 gpio_free(spi->cs_gpio);
Thomas Abraham2b908072012-07-13 07:15:15 +09001140 if (spi->dev.of_node)
1141 kfree(cs);
1142 }
Thomas Abraham1c20c202012-07-13 07:15:14 +09001143 spi_set_ctldata(spi, NULL);
1144}
1145
Mark Brownc2573122011-11-10 10:57:32 +00001146static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
1147{
1148 struct s3c64xx_spi_driver_data *sdd = data;
1149 struct spi_master *spi = sdd->master;
Girish K S375981f2013-03-13 12:13:30 +05301150 unsigned int val, clr = 0;
Mark Brownc2573122011-11-10 10:57:32 +00001151
Girish K S375981f2013-03-13 12:13:30 +05301152 val = readl(sdd->regs + S3C64XX_SPI_STATUS);
Mark Brownc2573122011-11-10 10:57:32 +00001153
Girish K S375981f2013-03-13 12:13:30 +05301154 if (val & S3C64XX_SPI_ST_RX_OVERRUN_ERR) {
1155 clr = S3C64XX_SPI_PND_RX_OVERRUN_CLR;
Mark Brownc2573122011-11-10 10:57:32 +00001156 dev_err(&spi->dev, "RX overrun\n");
Girish K S375981f2013-03-13 12:13:30 +05301157 }
1158 if (val & S3C64XX_SPI_ST_RX_UNDERRUN_ERR) {
1159 clr |= S3C64XX_SPI_PND_RX_UNDERRUN_CLR;
Mark Brownc2573122011-11-10 10:57:32 +00001160 dev_err(&spi->dev, "RX underrun\n");
Girish K S375981f2013-03-13 12:13:30 +05301161 }
1162 if (val & S3C64XX_SPI_ST_TX_OVERRUN_ERR) {
1163 clr |= S3C64XX_SPI_PND_TX_OVERRUN_CLR;
Mark Brownc2573122011-11-10 10:57:32 +00001164 dev_err(&spi->dev, "TX overrun\n");
Girish K S375981f2013-03-13 12:13:30 +05301165 }
1166 if (val & S3C64XX_SPI_ST_TX_UNDERRUN_ERR) {
1167 clr |= S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
Mark Brownc2573122011-11-10 10:57:32 +00001168 dev_err(&spi->dev, "TX underrun\n");
Girish K S375981f2013-03-13 12:13:30 +05301169 }
1170
1171 /* Clear the pending irq by setting and then clearing it */
1172 writel(clr, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1173 writel(0, sdd->regs + S3C64XX_SPI_PENDING_CLR);
Mark Brownc2573122011-11-10 10:57:32 +00001174
1175 return IRQ_HANDLED;
1176}
1177
Jassi Brar230d42d2009-11-30 07:39:42 +00001178static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd, int channel)
1179{
Jassi Brarad7de722010-01-20 13:49:44 -07001180 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +00001181 void __iomem *regs = sdd->regs;
1182 unsigned int val;
1183
1184 sdd->cur_speed = 0;
1185
Mark Brown5fc3e832012-07-19 14:36:23 +09001186 writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
Jassi Brar230d42d2009-11-30 07:39:42 +00001187
1188 /* Disable Interrupts - we use Polling if not DMA mode */
1189 writel(0, regs + S3C64XX_SPI_INT_EN);
1190
Thomas Abrahama5238e32012-07-13 07:15:14 +09001191 if (!sdd->port_conf->clk_from_cmu)
Jassi Brarb42a81c2010-09-29 17:31:33 +09001192 writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
Jassi Brar230d42d2009-11-30 07:39:42 +00001193 regs + S3C64XX_SPI_CLK_CFG);
1194 writel(0, regs + S3C64XX_SPI_MODE_CFG);
1195 writel(0, regs + S3C64XX_SPI_PACKET_CNT);
1196
Girish K S375981f2013-03-13 12:13:30 +05301197 /* Clear any irq pending bits, should set and clear the bits */
1198 val = S3C64XX_SPI_PND_RX_OVERRUN_CLR |
1199 S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
1200 S3C64XX_SPI_PND_TX_OVERRUN_CLR |
1201 S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
1202 writel(val, regs + S3C64XX_SPI_PENDING_CLR);
1203 writel(0, regs + S3C64XX_SPI_PENDING_CLR);
Jassi Brar230d42d2009-11-30 07:39:42 +00001204
1205 writel(0, regs + S3C64XX_SPI_SWAP_CFG);
1206
1207 val = readl(regs + S3C64XX_SPI_MODE_CFG);
1208 val &= ~S3C64XX_SPI_MODE_4BURST;
1209 val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1210 val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1211 writel(val, regs + S3C64XX_SPI_MODE_CFG);
1212
1213 flush_fifo(sdd);
1214}
1215
Thomas Abraham2b908072012-07-13 07:15:15 +09001216#ifdef CONFIG_OF
Jingoo Han75bf3362013-01-31 15:25:01 +09001217static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
Thomas Abraham2b908072012-07-13 07:15:15 +09001218{
1219 struct s3c64xx_spi_info *sci;
1220 u32 temp;
1221
1222 sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
1223 if (!sci) {
1224 dev_err(dev, "memory allocation for spi_info failed\n");
1225 return ERR_PTR(-ENOMEM);
1226 }
1227
1228 if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
Jingoo Han75bf3362013-01-31 15:25:01 +09001229 dev_warn(dev, "spi bus clock parent not specified, using clock at index 0 as parent\n");
Thomas Abraham2b908072012-07-13 07:15:15 +09001230 sci->src_clk_nr = 0;
1231 } else {
1232 sci->src_clk_nr = temp;
1233 }
1234
1235 if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
Jingoo Han75bf3362013-01-31 15:25:01 +09001236 dev_warn(dev, "number of chip select lines not specified, assuming 1 chip select line\n");
Thomas Abraham2b908072012-07-13 07:15:15 +09001237 sci->num_cs = 1;
1238 } else {
1239 sci->num_cs = temp;
1240 }
1241
1242 return sci;
1243}
1244#else
1245static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
1246{
Jingoo Han8074cf02013-07-30 16:58:59 +09001247 return dev_get_platdata(dev);
Thomas Abraham2b908072012-07-13 07:15:15 +09001248}
Thomas Abraham2b908072012-07-13 07:15:15 +09001249#endif
1250
1251static const struct of_device_id s3c64xx_spi_dt_match[];
1252
Thomas Abrahama5238e32012-07-13 07:15:14 +09001253static inline struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
1254 struct platform_device *pdev)
1255{
Thomas Abraham2b908072012-07-13 07:15:15 +09001256#ifdef CONFIG_OF
1257 if (pdev->dev.of_node) {
1258 const struct of_device_id *match;
1259 match = of_match_node(s3c64xx_spi_dt_match, pdev->dev.of_node);
1260 return (struct s3c64xx_spi_port_config *)match->data;
1261 }
1262#endif
Thomas Abrahama5238e32012-07-13 07:15:14 +09001263 return (struct s3c64xx_spi_port_config *)
1264 platform_get_device_id(pdev)->driver_data;
1265}
1266
Grant Likely2deff8d2013-02-05 13:27:35 +00001267static int s3c64xx_spi_probe(struct platform_device *pdev)
Jassi Brar230d42d2009-11-30 07:39:42 +00001268{
Thomas Abraham2b908072012-07-13 07:15:15 +09001269 struct resource *mem_res;
Padmavathi Vennab5be04d2013-01-18 17:17:03 +05301270 struct resource *res;
Jassi Brar230d42d2009-11-30 07:39:42 +00001271 struct s3c64xx_spi_driver_data *sdd;
Jingoo Han8074cf02013-07-30 16:58:59 +09001272 struct s3c64xx_spi_info *sci = dev_get_platdata(&pdev->dev);
Jassi Brar230d42d2009-11-30 07:39:42 +00001273 struct spi_master *master;
Mark Brownc2573122011-11-10 10:57:32 +00001274 int ret, irq;
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001275 char clk_name[16];
Jassi Brar230d42d2009-11-30 07:39:42 +00001276
Thomas Abraham2b908072012-07-13 07:15:15 +09001277 if (!sci && pdev->dev.of_node) {
1278 sci = s3c64xx_spi_parse_dt(&pdev->dev);
1279 if (IS_ERR(sci))
1280 return PTR_ERR(sci);
Jassi Brar230d42d2009-11-30 07:39:42 +00001281 }
1282
Thomas Abraham2b908072012-07-13 07:15:15 +09001283 if (!sci) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001284 dev_err(&pdev->dev, "platform_data missing!\n");
1285 return -ENODEV;
1286 }
1287
Jassi Brar230d42d2009-11-30 07:39:42 +00001288 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1289 if (mem_res == NULL) {
1290 dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
1291 return -ENXIO;
1292 }
1293
Mark Brownc2573122011-11-10 10:57:32 +00001294 irq = platform_get_irq(pdev, 0);
1295 if (irq < 0) {
1296 dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
1297 return irq;
1298 }
1299
Jassi Brar230d42d2009-11-30 07:39:42 +00001300 master = spi_alloc_master(&pdev->dev,
1301 sizeof(struct s3c64xx_spi_driver_data));
1302 if (master == NULL) {
1303 dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
1304 return -ENOMEM;
1305 }
1306
Jassi Brar230d42d2009-11-30 07:39:42 +00001307 platform_set_drvdata(pdev, master);
1308
1309 sdd = spi_master_get_devdata(master);
Thomas Abrahama5238e32012-07-13 07:15:14 +09001310 sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
Jassi Brar230d42d2009-11-30 07:39:42 +00001311 sdd->master = master;
1312 sdd->cntrlr_info = sci;
1313 sdd->pdev = pdev;
1314 sdd->sfr_start = mem_res->start;
Girish K S3146bee2013-06-21 11:26:12 +05301315 sdd->cs_gpio = true;
Thomas Abraham2b908072012-07-13 07:15:15 +09001316 if (pdev->dev.of_node) {
Girish K S3146bee2013-06-21 11:26:12 +05301317 if (!of_find_property(pdev->dev.of_node, "cs-gpio", NULL))
1318 sdd->cs_gpio = false;
1319
Thomas Abraham2b908072012-07-13 07:15:15 +09001320 ret = of_alias_get_id(pdev->dev.of_node, "spi");
1321 if (ret < 0) {
Jingoo Han75bf3362013-01-31 15:25:01 +09001322 dev_err(&pdev->dev, "failed to get alias id, errno %d\n",
1323 ret);
Thomas Abraham2b908072012-07-13 07:15:15 +09001324 goto err0;
1325 }
1326 sdd->port_id = ret;
1327 } else {
1328 sdd->port_id = pdev->id;
1329 }
Jassi Brar230d42d2009-11-30 07:39:42 +00001330
1331 sdd->cur_bpw = 8;
1332
Padmavathi Vennab5be04d2013-01-18 17:17:03 +05301333 if (!sdd->pdev->dev.of_node) {
1334 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1335 if (!res) {
Jingoo Handb0606e2013-07-15 15:11:57 +09001336 dev_warn(&pdev->dev, "Unable to get SPI tx dma resource. Switching to poll mode\n");
Girish K S7e995552013-05-20 12:21:32 +05301337 sdd->port_conf->quirks = S3C64XX_SPI_QUIRK_POLL;
1338 } else
1339 sdd->tx_dma.dmach = res->start;
Thomas Abraham2b908072012-07-13 07:15:15 +09001340
Padmavathi Vennab5be04d2013-01-18 17:17:03 +05301341 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1342 if (!res) {
Jingoo Handb0606e2013-07-15 15:11:57 +09001343 dev_warn(&pdev->dev, "Unable to get SPI rx dma resource. Switching to poll mode\n");
Girish K S7e995552013-05-20 12:21:32 +05301344 sdd->port_conf->quirks = S3C64XX_SPI_QUIRK_POLL;
1345 } else
1346 sdd->rx_dma.dmach = res->start;
Padmavathi Vennab5be04d2013-01-18 17:17:03 +05301347 }
1348
1349 sdd->tx_dma.direction = DMA_MEM_TO_DEV;
1350 sdd->rx_dma.direction = DMA_DEV_TO_MEM;
Thomas Abraham2b908072012-07-13 07:15:15 +09001351
1352 master->dev.of_node = pdev->dev.of_node;
Thomas Abrahama5238e32012-07-13 07:15:14 +09001353 master->bus_num = sdd->port_id;
Jassi Brar230d42d2009-11-30 07:39:42 +00001354 master->setup = s3c64xx_spi_setup;
Thomas Abraham1c20c202012-07-13 07:15:14 +09001355 master->cleanup = s3c64xx_spi_cleanup;
Mark Brownad2a99a2012-02-15 14:48:32 -08001356 master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
1357 master->transfer_one_message = s3c64xx_spi_transfer_one_message;
1358 master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
Jassi Brar230d42d2009-11-30 07:39:42 +00001359 master->num_chipselect = sci->num_cs;
1360 master->dma_alignment = 8;
Stephen Warren24778be2013-05-21 20:36:35 -06001361 master->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(16) |
1362 SPI_BPW_MASK(8);
Jassi Brar230d42d2009-11-30 07:39:42 +00001363 /* the spi->mode bits understood by this driver: */
1364 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
Mark Brownfc0f81b2013-07-28 15:24:54 +01001365 master->auto_runtime_pm = true;
Jassi Brar230d42d2009-11-30 07:39:42 +00001366
Thierry Redingb0ee5602013-01-21 11:09:18 +01001367 sdd->regs = devm_ioremap_resource(&pdev->dev, mem_res);
1368 if (IS_ERR(sdd->regs)) {
1369 ret = PTR_ERR(sdd->regs);
Jingoo Han4eb77002013-01-10 11:04:21 +09001370 goto err0;
Jassi Brar230d42d2009-11-30 07:39:42 +00001371 }
1372
Thomas Abraham00ab5392013-04-15 20:42:57 -07001373 if (sci->cfg_gpio && sci->cfg_gpio()) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001374 dev_err(&pdev->dev, "Unable to config gpio\n");
1375 ret = -EBUSY;
Jingoo Han4eb77002013-01-10 11:04:21 +09001376 goto err0;
Jassi Brar230d42d2009-11-30 07:39:42 +00001377 }
1378
1379 /* Setup clocks */
Jingoo Han4eb77002013-01-10 11:04:21 +09001380 sdd->clk = devm_clk_get(&pdev->dev, "spi");
Jassi Brar230d42d2009-11-30 07:39:42 +00001381 if (IS_ERR(sdd->clk)) {
1382 dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
1383 ret = PTR_ERR(sdd->clk);
Thomas Abraham00ab5392013-04-15 20:42:57 -07001384 goto err0;
Jassi Brar230d42d2009-11-30 07:39:42 +00001385 }
1386
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001387 if (clk_prepare_enable(sdd->clk)) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001388 dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
1389 ret = -EBUSY;
Thomas Abraham00ab5392013-04-15 20:42:57 -07001390 goto err0;
Jassi Brar230d42d2009-11-30 07:39:42 +00001391 }
1392
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001393 sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
Jingoo Han4eb77002013-01-10 11:04:21 +09001394 sdd->src_clk = devm_clk_get(&pdev->dev, clk_name);
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001395 if (IS_ERR(sdd->src_clk)) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001396 dev_err(&pdev->dev,
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001397 "Unable to acquire clock '%s'\n", clk_name);
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001398 ret = PTR_ERR(sdd->src_clk);
Jingoo Han4eb77002013-01-10 11:04:21 +09001399 goto err2;
Jassi Brar230d42d2009-11-30 07:39:42 +00001400 }
1401
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001402 if (clk_prepare_enable(sdd->src_clk)) {
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001403 dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
Jassi Brar230d42d2009-11-30 07:39:42 +00001404 ret = -EBUSY;
Jingoo Han4eb77002013-01-10 11:04:21 +09001405 goto err2;
Jassi Brar230d42d2009-11-30 07:39:42 +00001406 }
1407
Jassi Brar230d42d2009-11-30 07:39:42 +00001408 /* Setup Deufult Mode */
Thomas Abrahama5238e32012-07-13 07:15:14 +09001409 s3c64xx_spi_hwinit(sdd, sdd->port_id);
Jassi Brar230d42d2009-11-30 07:39:42 +00001410
1411 spin_lock_init(&sdd->lock);
1412 init_completion(&sdd->xfer_completion);
Jassi Brar230d42d2009-11-30 07:39:42 +00001413
Jingoo Han4eb77002013-01-10 11:04:21 +09001414 ret = devm_request_irq(&pdev->dev, irq, s3c64xx_spi_irq, 0,
1415 "spi-s3c64xx", sdd);
Mark Brownc2573122011-11-10 10:57:32 +00001416 if (ret != 0) {
1417 dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
1418 irq, ret);
Jingoo Han4eb77002013-01-10 11:04:21 +09001419 goto err3;
Mark Brownc2573122011-11-10 10:57:32 +00001420 }
1421
1422 writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
1423 S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
1424 sdd->regs + S3C64XX_SPI_INT_EN);
1425
Mark Brown3e2bd642013-09-27 11:52:35 +01001426 pm_runtime_enable(&pdev->dev);
1427
Mark Brown91800f02013-08-31 18:55:53 +01001428 ret = devm_spi_register_master(&pdev->dev, master);
1429 if (ret != 0) {
1430 dev_err(&pdev->dev, "cannot register SPI master: %d\n", ret);
Jingoo Han4eb77002013-01-10 11:04:21 +09001431 goto err3;
Jassi Brar230d42d2009-11-30 07:39:42 +00001432 }
1433
Jingoo Han75bf3362013-01-31 15:25:01 +09001434 dev_dbg(&pdev->dev, "Samsung SoC SPI Driver loaded for Bus SPI-%d with %d Slaves attached\n",
Thomas Abrahama5238e32012-07-13 07:15:14 +09001435 sdd->port_id, master->num_chipselect);
Jingoo Hanc65bc4a2013-07-16 08:53:33 +09001436 dev_dbg(&pdev->dev, "\tIOmem=[%pR]\tDMA=[Rx-%d, Tx-%d]\n",
1437 mem_res,
Boojin Kim82ab8cd2011-09-02 09:44:42 +09001438 sdd->rx_dma.dmach, sdd->tx_dma.dmach);
Jassi Brar230d42d2009-11-30 07:39:42 +00001439
1440 return 0;
1441
Jassi Brar230d42d2009-11-30 07:39:42 +00001442err3:
Jingoo Han4eb77002013-01-10 11:04:21 +09001443 clk_disable_unprepare(sdd->src_clk);
1444err2:
1445 clk_disable_unprepare(sdd->clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001446err0:
Jassi Brar230d42d2009-11-30 07:39:42 +00001447 spi_master_put(master);
1448
1449 return ret;
1450}
1451
1452static int s3c64xx_spi_remove(struct platform_device *pdev)
1453{
1454 struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
1455 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001456
Mark Brownb97b6622011-12-04 00:58:06 +00001457 pm_runtime_disable(&pdev->dev);
1458
Mark Brownc2573122011-11-10 10:57:32 +00001459 writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
1460
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001461 clk_disable_unprepare(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001462
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001463 clk_disable_unprepare(sdd->clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001464
Jassi Brar230d42d2009-11-30 07:39:42 +00001465 return 0;
1466}
1467
Jingoo Han997230d2013-03-22 02:09:08 +00001468#ifdef CONFIG_PM_SLEEP
Mark Browne25d0bf2011-12-04 00:36:18 +00001469static int s3c64xx_spi_suspend(struct device *dev)
Jassi Brar230d42d2009-11-30 07:39:42 +00001470{
Guenter Roeck9a2a5242012-08-16 20:14:25 -07001471 struct spi_master *master = dev_get_drvdata(dev);
Jassi Brar230d42d2009-11-30 07:39:42 +00001472 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001473
Mark Brownad2a99a2012-02-15 14:48:32 -08001474 spi_master_suspend(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001475
1476 /* Disable the clock */
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001477 clk_disable_unprepare(sdd->src_clk);
1478 clk_disable_unprepare(sdd->clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001479
1480 sdd->cur_speed = 0; /* Output Clock is stopped */
1481
1482 return 0;
1483}
1484
Mark Browne25d0bf2011-12-04 00:36:18 +00001485static int s3c64xx_spi_resume(struct device *dev)
Jassi Brar230d42d2009-11-30 07:39:42 +00001486{
Guenter Roeck9a2a5242012-08-16 20:14:25 -07001487 struct spi_master *master = dev_get_drvdata(dev);
Jassi Brar230d42d2009-11-30 07:39:42 +00001488 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brarad7de722010-01-20 13:49:44 -07001489 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +00001490
Thomas Abraham00ab5392013-04-15 20:42:57 -07001491 if (sci->cfg_gpio)
Thomas Abraham2b908072012-07-13 07:15:15 +09001492 sci->cfg_gpio();
Jassi Brar230d42d2009-11-30 07:39:42 +00001493
1494 /* Enable the clock */
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001495 clk_prepare_enable(sdd->src_clk);
1496 clk_prepare_enable(sdd->clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001497
Thomas Abrahama5238e32012-07-13 07:15:14 +09001498 s3c64xx_spi_hwinit(sdd, sdd->port_id);
Jassi Brar230d42d2009-11-30 07:39:42 +00001499
Mark Brownad2a99a2012-02-15 14:48:32 -08001500 spi_master_resume(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001501
1502 return 0;
1503}
Jingoo Han997230d2013-03-22 02:09:08 +00001504#endif /* CONFIG_PM_SLEEP */
Jassi Brar230d42d2009-11-30 07:39:42 +00001505
Mark Brownb97b6622011-12-04 00:58:06 +00001506#ifdef CONFIG_PM_RUNTIME
1507static int s3c64xx_spi_runtime_suspend(struct device *dev)
1508{
Guenter Roeck9a2a5242012-08-16 20:14:25 -07001509 struct spi_master *master = dev_get_drvdata(dev);
Mark Brownb97b6622011-12-04 00:58:06 +00001510 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1511
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001512 clk_disable_unprepare(sdd->clk);
1513 clk_disable_unprepare(sdd->src_clk);
Mark Brownb97b6622011-12-04 00:58:06 +00001514
1515 return 0;
1516}
1517
1518static int s3c64xx_spi_runtime_resume(struct device *dev)
1519{
Guenter Roeck9a2a5242012-08-16 20:14:25 -07001520 struct spi_master *master = dev_get_drvdata(dev);
Mark Brownb97b6622011-12-04 00:58:06 +00001521 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Mark Brown8b06d5b2013-09-27 18:44:53 +01001522 int ret;
Mark Brownb97b6622011-12-04 00:58:06 +00001523
Mark Brown8b06d5b2013-09-27 18:44:53 +01001524 ret = clk_prepare_enable(sdd->src_clk);
1525 if (ret != 0)
1526 return ret;
1527
1528 ret = clk_prepare_enable(sdd->clk);
1529 if (ret != 0) {
1530 clk_disable_unprepare(sdd->src_clk);
1531 return ret;
1532 }
Mark Brownb97b6622011-12-04 00:58:06 +00001533
1534 return 0;
1535}
1536#endif /* CONFIG_PM_RUNTIME */
1537
Mark Browne25d0bf2011-12-04 00:36:18 +00001538static const struct dev_pm_ops s3c64xx_spi_pm = {
1539 SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
Mark Brownb97b6622011-12-04 00:58:06 +00001540 SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
1541 s3c64xx_spi_runtime_resume, NULL)
Mark Browne25d0bf2011-12-04 00:36:18 +00001542};
1543
Sachin Kamat10ce0472012-08-03 10:08:12 +05301544static struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
Thomas Abrahama5238e32012-07-13 07:15:14 +09001545 .fifo_lvl_mask = { 0x7f },
1546 .rx_lvl_offset = 13,
1547 .tx_st_done = 21,
1548 .high_speed = true,
1549};
1550
Sachin Kamat10ce0472012-08-03 10:08:12 +05301551static struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
Thomas Abrahama5238e32012-07-13 07:15:14 +09001552 .fifo_lvl_mask = { 0x7f, 0x7F },
1553 .rx_lvl_offset = 13,
1554 .tx_st_done = 21,
1555};
1556
Sachin Kamat10ce0472012-08-03 10:08:12 +05301557static struct s3c64xx_spi_port_config s5p64x0_spi_port_config = {
Thomas Abrahama5238e32012-07-13 07:15:14 +09001558 .fifo_lvl_mask = { 0x1ff, 0x7F },
1559 .rx_lvl_offset = 15,
1560 .tx_st_done = 25,
1561};
1562
Sachin Kamat10ce0472012-08-03 10:08:12 +05301563static struct s3c64xx_spi_port_config s5pc100_spi_port_config = {
Thomas Abrahama5238e32012-07-13 07:15:14 +09001564 .fifo_lvl_mask = { 0x7f, 0x7F },
1565 .rx_lvl_offset = 13,
1566 .tx_st_done = 21,
1567 .high_speed = true,
1568};
1569
Sachin Kamat10ce0472012-08-03 10:08:12 +05301570static struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
Thomas Abrahama5238e32012-07-13 07:15:14 +09001571 .fifo_lvl_mask = { 0x1ff, 0x7F },
1572 .rx_lvl_offset = 15,
1573 .tx_st_done = 25,
1574 .high_speed = true,
1575};
1576
Sachin Kamat10ce0472012-08-03 10:08:12 +05301577static struct s3c64xx_spi_port_config exynos4_spi_port_config = {
Thomas Abrahama5238e32012-07-13 07:15:14 +09001578 .fifo_lvl_mask = { 0x1ff, 0x7F, 0x7F },
1579 .rx_lvl_offset = 15,
1580 .tx_st_done = 25,
1581 .high_speed = true,
1582 .clk_from_cmu = true,
1583};
1584
Girish K Sbff82032013-06-21 11:26:13 +05301585static struct s3c64xx_spi_port_config exynos5440_spi_port_config = {
1586 .fifo_lvl_mask = { 0x1ff },
1587 .rx_lvl_offset = 15,
1588 .tx_st_done = 25,
1589 .high_speed = true,
1590 .clk_from_cmu = true,
1591 .quirks = S3C64XX_SPI_QUIRK_POLL,
1592};
1593
Thomas Abrahama5238e32012-07-13 07:15:14 +09001594static struct platform_device_id s3c64xx_spi_driver_ids[] = {
1595 {
1596 .name = "s3c2443-spi",
1597 .driver_data = (kernel_ulong_t)&s3c2443_spi_port_config,
1598 }, {
1599 .name = "s3c6410-spi",
1600 .driver_data = (kernel_ulong_t)&s3c6410_spi_port_config,
1601 }, {
1602 .name = "s5p64x0-spi",
1603 .driver_data = (kernel_ulong_t)&s5p64x0_spi_port_config,
1604 }, {
1605 .name = "s5pc100-spi",
1606 .driver_data = (kernel_ulong_t)&s5pc100_spi_port_config,
1607 }, {
1608 .name = "s5pv210-spi",
1609 .driver_data = (kernel_ulong_t)&s5pv210_spi_port_config,
1610 }, {
1611 .name = "exynos4210-spi",
1612 .driver_data = (kernel_ulong_t)&exynos4_spi_port_config,
1613 },
1614 { },
1615};
1616
Thomas Abraham2b908072012-07-13 07:15:15 +09001617static const struct of_device_id s3c64xx_spi_dt_match[] = {
Mateusz Krawczuka3b924d2013-09-23 11:45:45 +02001618 { .compatible = "samsung,s3c2443-spi",
1619 .data = (void *)&s3c2443_spi_port_config,
1620 },
1621 { .compatible = "samsung,s3c6410-spi",
1622 .data = (void *)&s3c6410_spi_port_config,
1623 },
1624 { .compatible = "samsung,s5pc100-spi",
1625 .data = (void *)&s5pc100_spi_port_config,
1626 },
1627 { .compatible = "samsung,s5pv210-spi",
1628 .data = (void *)&s5pv210_spi_port_config,
1629 },
Thomas Abraham2b908072012-07-13 07:15:15 +09001630 { .compatible = "samsung,exynos4210-spi",
1631 .data = (void *)&exynos4_spi_port_config,
1632 },
Girish K Sbff82032013-06-21 11:26:13 +05301633 { .compatible = "samsung,exynos5440-spi",
1634 .data = (void *)&exynos5440_spi_port_config,
1635 },
Thomas Abraham2b908072012-07-13 07:15:15 +09001636 { },
1637};
1638MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);
Thomas Abraham2b908072012-07-13 07:15:15 +09001639
Jassi Brar230d42d2009-11-30 07:39:42 +00001640static struct platform_driver s3c64xx_spi_driver = {
1641 .driver = {
1642 .name = "s3c64xx-spi",
1643 .owner = THIS_MODULE,
Mark Browne25d0bf2011-12-04 00:36:18 +00001644 .pm = &s3c64xx_spi_pm,
Thomas Abraham2b908072012-07-13 07:15:15 +09001645 .of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
Jassi Brar230d42d2009-11-30 07:39:42 +00001646 },
Lukasz Czerwinski50c959f2013-09-09 16:09:25 +02001647 .probe = s3c64xx_spi_probe,
Jassi Brar230d42d2009-11-30 07:39:42 +00001648 .remove = s3c64xx_spi_remove,
Thomas Abrahama5238e32012-07-13 07:15:14 +09001649 .id_table = s3c64xx_spi_driver_ids,
Jassi Brar230d42d2009-11-30 07:39:42 +00001650};
1651MODULE_ALIAS("platform:s3c64xx-spi");
1652
Lukasz Czerwinski50c959f2013-09-09 16:09:25 +02001653module_platform_driver(s3c64xx_spi_driver);
Jassi Brar230d42d2009-11-30 07:39:42 +00001654
1655MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
1656MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
1657MODULE_LICENSE("GPL");