blob: eb53df27e7ea81af4eefa2912da48565724d6aaa [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 * @queue: To log SPI xfer requests.
176 * @lock: Controller specific lock.
177 * @state: Set of FLAGS to indicate status.
178 * @rx_dmach: Controller's DMA channel for Rx.
179 * @tx_dmach: Controller's DMA channel for Tx.
180 * @sfr_start: BUS address of SPI controller regs.
181 * @regs: Pointer to ioremap'ed controller registers.
Mark Brownc2573122011-11-10 10:57:32 +0000182 * @irq: interrupt
Jassi Brar230d42d2009-11-30 07:39:42 +0000183 * @xfer_completion: To indicate completion of xfer task.
184 * @cur_mode: Stores the active configuration of the controller.
185 * @cur_bpw: Stores the active bits per word settings.
186 * @cur_speed: Stores the active xfer clock speed.
187 */
188struct s3c64xx_spi_driver_data {
189 void __iomem *regs;
190 struct clk *clk;
Jassi Brarb0d5d6e2010-01-20 13:49:44 -0700191 struct clk *src_clk;
Jassi Brar230d42d2009-11-30 07:39:42 +0000192 struct platform_device *pdev;
193 struct spi_master *master;
Jassi Brarad7de722010-01-20 13:49:44 -0700194 struct s3c64xx_spi_info *cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +0000195 struct spi_device *tgl_spi;
Jassi Brar230d42d2009-11-30 07:39:42 +0000196 struct list_head queue;
197 spinlock_t lock;
Jassi Brar230d42d2009-11-30 07:39:42 +0000198 unsigned long sfr_start;
199 struct completion xfer_completion;
200 unsigned state;
201 unsigned cur_mode, cur_bpw;
202 unsigned cur_speed;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900203 struct s3c64xx_spi_dma_data rx_dma;
204 struct s3c64xx_spi_dma_data tx_dma;
Mark Brown563b4442013-04-18 18:06:05 +0100205#ifdef CONFIG_S3C_DMA
Boojin Kim39d3e802011-09-02 09:44:41 +0900206 struct samsung_dma_ops *ops;
Arnd Bergmann78843722013-04-11 22:42:03 +0200207#endif
Thomas Abrahama5238e32012-07-13 07:15:14 +0900208 struct s3c64xx_spi_port_config *port_conf;
209 unsigned int port_id;
Thomas Abraham2b908072012-07-13 07:15:15 +0900210 unsigned long gpios[4];
Girish K S3146bee2013-06-21 11:26:12 +0530211 bool cs_gpio;
Jassi Brar230d42d2009-11-30 07:39:42 +0000212};
213
Jassi Brar230d42d2009-11-30 07:39:42 +0000214static void flush_fifo(struct s3c64xx_spi_driver_data *sdd)
215{
Jassi Brar230d42d2009-11-30 07:39:42 +0000216 void __iomem *regs = sdd->regs;
217 unsigned long loops;
218 u32 val;
219
220 writel(0, regs + S3C64XX_SPI_PACKET_CNT);
221
222 val = readl(regs + S3C64XX_SPI_CH_CFG);
Kyoungil Kim7d859ff2012-05-23 21:29:51 +0900223 val &= ~(S3C64XX_SPI_CH_RXCH_ON | S3C64XX_SPI_CH_TXCH_ON);
224 writel(val, regs + S3C64XX_SPI_CH_CFG);
225
226 val = readl(regs + S3C64XX_SPI_CH_CFG);
Jassi Brar230d42d2009-11-30 07:39:42 +0000227 val |= S3C64XX_SPI_CH_SW_RST;
228 val &= ~S3C64XX_SPI_CH_HS_EN;
229 writel(val, regs + S3C64XX_SPI_CH_CFG);
230
231 /* Flush TxFIFO*/
232 loops = msecs_to_loops(1);
233 do {
234 val = readl(regs + S3C64XX_SPI_STATUS);
Thomas Abrahama5238e32012-07-13 07:15:14 +0900235 } while (TX_FIFO_LVL(val, sdd) && loops--);
Jassi Brar230d42d2009-11-30 07:39:42 +0000236
Mark Brownbe7852a2010-08-23 17:40:56 +0100237 if (loops == 0)
238 dev_warn(&sdd->pdev->dev, "Timed out flushing TX FIFO\n");
239
Jassi Brar230d42d2009-11-30 07:39:42 +0000240 /* Flush RxFIFO*/
241 loops = msecs_to_loops(1);
242 do {
243 val = readl(regs + S3C64XX_SPI_STATUS);
Thomas Abrahama5238e32012-07-13 07:15:14 +0900244 if (RX_FIFO_LVL(val, sdd))
Jassi Brar230d42d2009-11-30 07:39:42 +0000245 readl(regs + S3C64XX_SPI_RX_DATA);
246 else
247 break;
248 } while (loops--);
249
Mark Brownbe7852a2010-08-23 17:40:56 +0100250 if (loops == 0)
251 dev_warn(&sdd->pdev->dev, "Timed out flushing RX FIFO\n");
252
Jassi Brar230d42d2009-11-30 07:39:42 +0000253 val = readl(regs + S3C64XX_SPI_CH_CFG);
254 val &= ~S3C64XX_SPI_CH_SW_RST;
255 writel(val, regs + S3C64XX_SPI_CH_CFG);
256
257 val = readl(regs + S3C64XX_SPI_MODE_CFG);
258 val &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
259 writel(val, regs + S3C64XX_SPI_MODE_CFG);
Jassi Brar230d42d2009-11-30 07:39:42 +0000260}
261
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900262static void s3c64xx_spi_dmacb(void *data)
Boojin Kim39d3e802011-09-02 09:44:41 +0900263{
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900264 struct s3c64xx_spi_driver_data *sdd;
265 struct s3c64xx_spi_dma_data *dma = data;
Boojin Kim39d3e802011-09-02 09:44:41 +0900266 unsigned long flags;
267
Kyoungil Kim054ebcc2012-03-10 09:48:46 +0900268 if (dma->direction == DMA_DEV_TO_MEM)
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900269 sdd = container_of(data,
270 struct s3c64xx_spi_driver_data, rx_dma);
271 else
272 sdd = container_of(data,
273 struct s3c64xx_spi_driver_data, tx_dma);
274
Boojin Kim39d3e802011-09-02 09:44:41 +0900275 spin_lock_irqsave(&sdd->lock, flags);
276
Kyoungil Kim054ebcc2012-03-10 09:48:46 +0900277 if (dma->direction == DMA_DEV_TO_MEM) {
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900278 sdd->state &= ~RXBUSY;
279 if (!(sdd->state & TXBUSY))
280 complete(&sdd->xfer_completion);
281 } else {
282 sdd->state &= ~TXBUSY;
283 if (!(sdd->state & RXBUSY))
284 complete(&sdd->xfer_completion);
285 }
Boojin Kim39d3e802011-09-02 09:44:41 +0900286
287 spin_unlock_irqrestore(&sdd->lock, flags);
288}
289
Mark Brown563b4442013-04-18 18:06:05 +0100290#ifdef CONFIG_S3C_DMA
Arnd Bergmann78843722013-04-11 22:42:03 +0200291/* FIXME: remove this section once arch/arm/mach-s3c64xx uses dmaengine */
292
293static struct s3c2410_dma_client s3c64xx_spi_dma_client = {
294 .name = "samsung-spi-dma",
295};
296
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900297static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
298 unsigned len, dma_addr_t buf)
Boojin Kim39d3e802011-09-02 09:44:41 +0900299{
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900300 struct s3c64xx_spi_driver_data *sdd;
Boojin Kim4969c322012-06-19 13:27:03 +0900301 struct samsung_dma_prep info;
302 struct samsung_dma_config config;
Boojin Kim39d3e802011-09-02 09:44:41 +0900303
Boojin Kim4969c322012-06-19 13:27:03 +0900304 if (dma->direction == DMA_DEV_TO_MEM) {
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900305 sdd = container_of((void *)dma,
306 struct s3c64xx_spi_driver_data, rx_dma);
Boojin Kim4969c322012-06-19 13:27:03 +0900307 config.direction = sdd->rx_dma.direction;
308 config.fifo = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
309 config.width = sdd->cur_bpw / 8;
Arnd Bergmann78843722013-04-11 22:42:03 +0200310 sdd->ops->config((enum dma_ch)sdd->rx_dma.ch, &config);
Boojin Kim4969c322012-06-19 13:27:03 +0900311 } else {
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900312 sdd = container_of((void *)dma,
313 struct s3c64xx_spi_driver_data, tx_dma);
Boojin Kim4969c322012-06-19 13:27:03 +0900314 config.direction = sdd->tx_dma.direction;
315 config.fifo = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
316 config.width = sdd->cur_bpw / 8;
Arnd Bergmann78843722013-04-11 22:42:03 +0200317 sdd->ops->config((enum dma_ch)sdd->tx_dma.ch, &config);
Boojin Kim4969c322012-06-19 13:27:03 +0900318 }
Boojin Kim39d3e802011-09-02 09:44:41 +0900319
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900320 info.cap = DMA_SLAVE;
321 info.len = len;
322 info.fp = s3c64xx_spi_dmacb;
323 info.fp_param = dma;
324 info.direction = dma->direction;
325 info.buf = buf;
Boojin Kim39d3e802011-09-02 09:44:41 +0900326
Arnd Bergmann78843722013-04-11 22:42:03 +0200327 sdd->ops->prepare((enum dma_ch)dma->ch, &info);
328 sdd->ops->trigger((enum dma_ch)dma->ch);
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900329}
330
331static int acquire_dma(struct s3c64xx_spi_driver_data *sdd)
332{
Boojin Kim4969c322012-06-19 13:27:03 +0900333 struct samsung_dma_req req;
Padmavathi Vennab5be04d2013-01-18 17:17:03 +0530334 struct device *dev = &sdd->pdev->dev;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900335
336 sdd->ops = samsung_dma_get_ops();
337
Boojin Kim4969c322012-06-19 13:27:03 +0900338 req.cap = DMA_SLAVE;
339 req.client = &s3c64xx_spi_dma_client;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900340
Arnd Bergmann78843722013-04-11 22:42:03 +0200341 sdd->rx_dma.ch = (void *)sdd->ops->request(sdd->rx_dma.dmach, &req, dev, "rx");
342 sdd->tx_dma.ch = (void *)sdd->ops->request(sdd->tx_dma.dmach, &req, dev, "tx");
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900343
344 return 1;
Boojin Kim39d3e802011-09-02 09:44:41 +0900345}
346
Arnd Bergmann78843722013-04-11 22:42:03 +0200347static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
348{
349 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
350
Girish K S7e995552013-05-20 12:21:32 +0530351 /*
352 * If DMA resource was not available during
353 * probe, no need to continue with dma requests
354 * else Acquire DMA channels
355 */
356 while (!is_polling(sdd) && !acquire_dma(sdd))
Arnd Bergmann78843722013-04-11 22:42:03 +0200357 usleep_range(10000, 11000);
358
359 pm_runtime_get_sync(&sdd->pdev->dev);
360
361 return 0;
362}
363
364static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
365{
366 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
367
368 /* Free DMA channels */
Girish K S7e995552013-05-20 12:21:32 +0530369 if (!is_polling(sdd)) {
370 sdd->ops->release((enum dma_ch)sdd->rx_dma.ch,
371 &s3c64xx_spi_dma_client);
372 sdd->ops->release((enum dma_ch)sdd->tx_dma.ch,
373 &s3c64xx_spi_dma_client);
374 }
Arnd Bergmann78843722013-04-11 22:42:03 +0200375 pm_runtime_put(&sdd->pdev->dev);
376
377 return 0;
378}
379
380static void s3c64xx_spi_dma_stop(struct s3c64xx_spi_driver_data *sdd,
381 struct s3c64xx_spi_dma_data *dma)
382{
383 sdd->ops->stop((enum dma_ch)dma->ch);
384}
385#else
386
387static void prepare_dma(struct s3c64xx_spi_dma_data *dma,
388 unsigned len, dma_addr_t buf)
389{
390 struct s3c64xx_spi_driver_data *sdd;
391 struct dma_slave_config config;
392 struct scatterlist sg;
393 struct dma_async_tx_descriptor *desc;
394
395 if (dma->direction == DMA_DEV_TO_MEM) {
396 sdd = container_of((void *)dma,
397 struct s3c64xx_spi_driver_data, rx_dma);
398 config.direction = dma->direction;
399 config.src_addr = sdd->sfr_start + S3C64XX_SPI_RX_DATA;
400 config.src_addr_width = sdd->cur_bpw / 8;
401 config.src_maxburst = 1;
402 dmaengine_slave_config(dma->ch, &config);
403 } else {
404 sdd = container_of((void *)dma,
405 struct s3c64xx_spi_driver_data, tx_dma);
406 config.direction = dma->direction;
407 config.dst_addr = sdd->sfr_start + S3C64XX_SPI_TX_DATA;
408 config.dst_addr_width = sdd->cur_bpw / 8;
409 config.dst_maxburst = 1;
410 dmaengine_slave_config(dma->ch, &config);
411 }
412
413 sg_init_table(&sg, 1);
414 sg_dma_len(&sg) = len;
415 sg_set_page(&sg, pfn_to_page(PFN_DOWN(buf)),
416 len, offset_in_page(buf));
417 sg_dma_address(&sg) = buf;
418
419 desc = dmaengine_prep_slave_sg(dma->ch,
420 &sg, 1, dma->direction, DMA_PREP_INTERRUPT);
421
422 desc->callback = s3c64xx_spi_dmacb;
423 desc->callback_param = dma;
424
425 dmaengine_submit(desc);
426 dma_async_issue_pending(dma->ch);
427}
428
429static int s3c64xx_spi_prepare_transfer(struct spi_master *spi)
430{
431 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
432 dma_filter_fn filter = sdd->cntrlr_info->filter;
433 struct device *dev = &sdd->pdev->dev;
434 dma_cap_mask_t mask;
Mark Brownfb9d0442013-04-18 18:12:00 +0100435 int ret;
Arnd Bergmann78843722013-04-11 22:42:03 +0200436
437 dma_cap_zero(mask);
438 dma_cap_set(DMA_SLAVE, mask);
439
440 /* Acquire DMA channels */
441 sdd->rx_dma.ch = dma_request_slave_channel_compat(mask, filter,
442 (void*)sdd->rx_dma.dmach, dev, "rx");
Mark Brownfb9d0442013-04-18 18:12:00 +0100443 if (!sdd->rx_dma.ch) {
444 dev_err(dev, "Failed to get RX DMA channel\n");
445 ret = -EBUSY;
446 goto out;
447 }
448
Arnd Bergmann78843722013-04-11 22:42:03 +0200449 sdd->tx_dma.ch = dma_request_slave_channel_compat(mask, filter,
450 (void*)sdd->tx_dma.dmach, dev, "tx");
Mark Brownfb9d0442013-04-18 18:12:00 +0100451 if (!sdd->tx_dma.ch) {
452 dev_err(dev, "Failed to get TX DMA channel\n");
453 ret = -EBUSY;
454 goto out_rx;
455 }
456
457 ret = pm_runtime_get_sync(&sdd->pdev->dev);
Sylwester Nawrocki6c6cf642013-06-10 18:22:26 +0200458 if (ret < 0) {
Mark Brownfb9d0442013-04-18 18:12:00 +0100459 dev_err(dev, "Failed to enable device: %d\n", ret);
460 goto out_tx;
461 }
Arnd Bergmann78843722013-04-11 22:42:03 +0200462
463 return 0;
Mark Brownfb9d0442013-04-18 18:12:00 +0100464
465out_tx:
466 dma_release_channel(sdd->tx_dma.ch);
467out_rx:
468 dma_release_channel(sdd->rx_dma.ch);
469out:
470 return ret;
Arnd Bergmann78843722013-04-11 22:42:03 +0200471}
472
473static int s3c64xx_spi_unprepare_transfer(struct spi_master *spi)
474{
475 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(spi);
476
477 /* Free DMA channels */
Girish K S7e995552013-05-20 12:21:32 +0530478 if (!is_polling(sdd)) {
479 dma_release_channel(sdd->rx_dma.ch);
480 dma_release_channel(sdd->tx_dma.ch);
481 }
Arnd Bergmann78843722013-04-11 22:42:03 +0200482
483 pm_runtime_put(&sdd->pdev->dev);
484 return 0;
485}
486
487static void s3c64xx_spi_dma_stop(struct s3c64xx_spi_driver_data *sdd,
488 struct s3c64xx_spi_dma_data *dma)
489{
490 dmaengine_terminate_all(dma->ch);
491}
492#endif
493
Jassi Brar230d42d2009-11-30 07:39:42 +0000494static void enable_datapath(struct s3c64xx_spi_driver_data *sdd,
495 struct spi_device *spi,
496 struct spi_transfer *xfer, int dma_mode)
497{
Jassi Brar230d42d2009-11-30 07:39:42 +0000498 void __iomem *regs = sdd->regs;
499 u32 modecfg, chcfg;
500
501 modecfg = readl(regs + S3C64XX_SPI_MODE_CFG);
502 modecfg &= ~(S3C64XX_SPI_MODE_TXDMA_ON | S3C64XX_SPI_MODE_RXDMA_ON);
503
504 chcfg = readl(regs + S3C64XX_SPI_CH_CFG);
505 chcfg &= ~S3C64XX_SPI_CH_TXCH_ON;
506
507 if (dma_mode) {
508 chcfg &= ~S3C64XX_SPI_CH_RXCH_ON;
509 } else {
510 /* Always shift in data in FIFO, even if xfer is Tx only,
511 * this helps setting PCKT_CNT value for generating clocks
512 * as exactly needed.
513 */
514 chcfg |= S3C64XX_SPI_CH_RXCH_ON;
515 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
516 | S3C64XX_SPI_PACKET_CNT_EN,
517 regs + S3C64XX_SPI_PACKET_CNT);
518 }
519
520 if (xfer->tx_buf != NULL) {
521 sdd->state |= TXBUSY;
522 chcfg |= S3C64XX_SPI_CH_TXCH_ON;
523 if (dma_mode) {
524 modecfg |= S3C64XX_SPI_MODE_TXDMA_ON;
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900525 prepare_dma(&sdd->tx_dma, xfer->len, xfer->tx_dma);
Jassi Brar230d42d2009-11-30 07:39:42 +0000526 } else {
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900527 switch (sdd->cur_bpw) {
528 case 32:
529 iowrite32_rep(regs + S3C64XX_SPI_TX_DATA,
530 xfer->tx_buf, xfer->len / 4);
531 break;
532 case 16:
533 iowrite16_rep(regs + S3C64XX_SPI_TX_DATA,
534 xfer->tx_buf, xfer->len / 2);
535 break;
536 default:
537 iowrite8_rep(regs + S3C64XX_SPI_TX_DATA,
538 xfer->tx_buf, xfer->len);
539 break;
540 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000541 }
542 }
543
544 if (xfer->rx_buf != NULL) {
545 sdd->state |= RXBUSY;
546
Thomas Abrahama5238e32012-07-13 07:15:14 +0900547 if (sdd->port_conf->high_speed && sdd->cur_speed >= 30000000UL
Jassi Brar230d42d2009-11-30 07:39:42 +0000548 && !(sdd->cur_mode & SPI_CPHA))
549 chcfg |= S3C64XX_SPI_CH_HS_EN;
550
551 if (dma_mode) {
552 modecfg |= S3C64XX_SPI_MODE_RXDMA_ON;
553 chcfg |= S3C64XX_SPI_CH_RXCH_ON;
554 writel(((xfer->len * 8 / sdd->cur_bpw) & 0xffff)
555 | S3C64XX_SPI_PACKET_CNT_EN,
556 regs + S3C64XX_SPI_PACKET_CNT);
Boojin Kim82ab8cd2011-09-02 09:44:42 +0900557 prepare_dma(&sdd->rx_dma, xfer->len, xfer->rx_dma);
Jassi Brar230d42d2009-11-30 07:39:42 +0000558 }
559 }
560
561 writel(modecfg, regs + S3C64XX_SPI_MODE_CFG);
562 writel(chcfg, regs + S3C64XX_SPI_CH_CFG);
563}
564
565static inline void enable_cs(struct s3c64xx_spi_driver_data *sdd,
566 struct spi_device *spi)
567{
568 struct s3c64xx_spi_csinfo *cs;
569
570 if (sdd->tgl_spi != NULL) { /* If last device toggled after mssg */
571 if (sdd->tgl_spi != spi) { /* if last mssg on diff device */
572 /* Deselect the last toggled device */
573 cs = sdd->tgl_spi->controller_data;
Girish K S3146bee2013-06-21 11:26:12 +0530574 if (sdd->cs_gpio)
575 gpio_set_value(cs->line,
576 spi->mode & SPI_CS_HIGH ? 0 : 1);
Jassi Brar230d42d2009-11-30 07:39:42 +0000577 }
578 sdd->tgl_spi = NULL;
579 }
580
581 cs = spi->controller_data;
Girish K S3146bee2013-06-21 11:26:12 +0530582 if (sdd->cs_gpio)
583 gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 1 : 0);
Girish K S7e995552013-05-20 12:21:32 +0530584
585 /* Start the signals */
586 writel(0, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
587}
588
Mark Brown79617072013-06-19 19:12:39 +0100589static u32 s3c64xx_spi_wait_for_timeout(struct s3c64xx_spi_driver_data *sdd,
Girish K S7e995552013-05-20 12:21:32 +0530590 int timeout_ms)
591{
592 void __iomem *regs = sdd->regs;
593 unsigned long val = 1;
594 u32 status;
595
596 /* max fifo depth available */
597 u32 max_fifo = (FIFO_LVL_MASK(sdd) >> 1) + 1;
598
599 if (timeout_ms)
600 val = msecs_to_loops(timeout_ms);
601
602 do {
603 status = readl(regs + S3C64XX_SPI_STATUS);
604 } while (RX_FIFO_LVL(status, sdd) < max_fifo && --val);
605
606 /* return the actual received data length */
607 return RX_FIFO_LVL(status, sdd);
Jassi Brar230d42d2009-11-30 07:39:42 +0000608}
609
610static int wait_for_xfer(struct s3c64xx_spi_driver_data *sdd,
611 struct spi_transfer *xfer, int dma_mode)
612{
Jassi Brar230d42d2009-11-30 07:39:42 +0000613 void __iomem *regs = sdd->regs;
614 unsigned long val;
615 int ms;
616
617 /* millisecs to xfer 'len' bytes @ 'cur_speed' */
618 ms = xfer->len * 8 * 1000 / sdd->cur_speed;
Mark Brown9d8f86b2010-09-07 16:37:52 +0100619 ms += 10; /* some tolerance */
Jassi Brar230d42d2009-11-30 07:39:42 +0000620
621 if (dma_mode) {
622 val = msecs_to_jiffies(ms) + 10;
623 val = wait_for_completion_timeout(&sdd->xfer_completion, val);
624 } else {
Jassi Brarc3f139b2010-09-03 10:36:46 +0900625 u32 status;
Jassi Brar230d42d2009-11-30 07:39:42 +0000626 val = msecs_to_loops(ms);
627 do {
Jassi Brarc3f139b2010-09-03 10:36:46 +0900628 status = readl(regs + S3C64XX_SPI_STATUS);
Thomas Abrahama5238e32012-07-13 07:15:14 +0900629 } while (RX_FIFO_LVL(status, sdd) < xfer->len && --val);
Jassi Brar230d42d2009-11-30 07:39:42 +0000630 }
631
Jassi Brar230d42d2009-11-30 07:39:42 +0000632 if (dma_mode) {
633 u32 status;
634
635 /*
Girish K S7e995552013-05-20 12:21:32 +0530636 * If the previous xfer was completed within timeout, then
637 * proceed further else return -EIO.
Jassi Brar230d42d2009-11-30 07:39:42 +0000638 * DmaTx returns after simply writing data in the FIFO,
639 * w/o waiting for real transmission on the bus to finish.
640 * DmaRx returns only after Dma read data from FIFO which
641 * needs bus transmission to finish, so we don't worry if
642 * Xfer involved Rx(with or without Tx).
643 */
Girish K S7e995552013-05-20 12:21:32 +0530644 if (val && !xfer->rx_buf) {
Jassi Brar230d42d2009-11-30 07:39:42 +0000645 val = msecs_to_loops(10);
646 status = readl(regs + S3C64XX_SPI_STATUS);
Thomas Abrahama5238e32012-07-13 07:15:14 +0900647 while ((TX_FIFO_LVL(status, sdd)
648 || !S3C64XX_SPI_ST_TX_DONE(status, sdd))
Jassi Brar230d42d2009-11-30 07:39:42 +0000649 && --val) {
650 cpu_relax();
651 status = readl(regs + S3C64XX_SPI_STATUS);
652 }
653
Jassi Brar230d42d2009-11-30 07:39:42 +0000654 }
Girish K S7e995552013-05-20 12:21:32 +0530655
656 /* If timed out while checking rx/tx status return error */
657 if (!val)
658 return -EIO;
Jassi Brar230d42d2009-11-30 07:39:42 +0000659 } else {
Girish K S7e995552013-05-20 12:21:32 +0530660 int loops;
661 u32 cpy_len;
662 u8 *buf;
663
Jassi Brar230d42d2009-11-30 07:39:42 +0000664 /* If it was only Tx */
Girish K S7e995552013-05-20 12:21:32 +0530665 if (!xfer->rx_buf) {
Jassi Brar230d42d2009-11-30 07:39:42 +0000666 sdd->state &= ~TXBUSY;
667 return 0;
668 }
669
Girish K S7e995552013-05-20 12:21:32 +0530670 /*
671 * If the receive length is bigger than the controller fifo
672 * size, calculate the loops and read the fifo as many times.
673 * loops = length / max fifo size (calculated by using the
674 * fifo mask).
675 * For any size less than the fifo size the below code is
676 * executed atleast once.
677 */
678 loops = xfer->len / ((FIFO_LVL_MASK(sdd) >> 1) + 1);
679 buf = xfer->rx_buf;
680 do {
681 /* wait for data to be received in the fifo */
Mark Brown79617072013-06-19 19:12:39 +0100682 cpy_len = s3c64xx_spi_wait_for_timeout(sdd,
683 (loops ? ms : 0));
Girish K S7e995552013-05-20 12:21:32 +0530684
685 switch (sdd->cur_bpw) {
686 case 32:
687 ioread32_rep(regs + S3C64XX_SPI_RX_DATA,
688 buf, cpy_len / 4);
689 break;
690 case 16:
691 ioread16_rep(regs + S3C64XX_SPI_RX_DATA,
692 buf, cpy_len / 2);
693 break;
694 default:
695 ioread8_rep(regs + S3C64XX_SPI_RX_DATA,
696 buf, cpy_len);
697 break;
698 }
699
700 buf = buf + cpy_len;
701 } while (loops--);
Jassi Brar230d42d2009-11-30 07:39:42 +0000702 sdd->state &= ~RXBUSY;
703 }
704
705 return 0;
706}
707
708static inline void disable_cs(struct s3c64xx_spi_driver_data *sdd,
709 struct spi_device *spi)
710{
711 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
712
713 if (sdd->tgl_spi == spi)
714 sdd->tgl_spi = NULL;
715
Girish K S3146bee2013-06-21 11:26:12 +0530716 if (sdd->cs_gpio)
717 gpio_set_value(cs->line, spi->mode & SPI_CS_HIGH ? 0 : 1);
Girish K S7e995552013-05-20 12:21:32 +0530718
719 /* Quiese the signals */
720 writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
Jassi Brar230d42d2009-11-30 07:39:42 +0000721}
722
723static void s3c64xx_spi_config(struct s3c64xx_spi_driver_data *sdd)
724{
Jassi Brar230d42d2009-11-30 07:39:42 +0000725 void __iomem *regs = sdd->regs;
726 u32 val;
727
728 /* Disable Clock */
Thomas Abrahama5238e32012-07-13 07:15:14 +0900729 if (sdd->port_conf->clk_from_cmu) {
Thomas Abraham9f667bf2012-10-03 08:30:12 +0900730 clk_disable_unprepare(sdd->src_clk);
Jassi Brarb42a81c2010-09-29 17:31:33 +0900731 } else {
732 val = readl(regs + S3C64XX_SPI_CLK_CFG);
733 val &= ~S3C64XX_SPI_ENCLK_ENABLE;
734 writel(val, regs + S3C64XX_SPI_CLK_CFG);
735 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000736
737 /* Set Polarity and Phase */
738 val = readl(regs + S3C64XX_SPI_CH_CFG);
739 val &= ~(S3C64XX_SPI_CH_SLAVE |
740 S3C64XX_SPI_CPOL_L |
741 S3C64XX_SPI_CPHA_B);
742
743 if (sdd->cur_mode & SPI_CPOL)
744 val |= S3C64XX_SPI_CPOL_L;
745
746 if (sdd->cur_mode & SPI_CPHA)
747 val |= S3C64XX_SPI_CPHA_B;
748
749 writel(val, regs + S3C64XX_SPI_CH_CFG);
750
751 /* Set Channel & DMA Mode */
752 val = readl(regs + S3C64XX_SPI_MODE_CFG);
753 val &= ~(S3C64XX_SPI_MODE_BUS_TSZ_MASK
754 | S3C64XX_SPI_MODE_CH_TSZ_MASK);
755
756 switch (sdd->cur_bpw) {
757 case 32:
758 val |= S3C64XX_SPI_MODE_BUS_TSZ_WORD;
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900759 val |= S3C64XX_SPI_MODE_CH_TSZ_WORD;
Jassi Brar230d42d2009-11-30 07:39:42 +0000760 break;
761 case 16:
762 val |= S3C64XX_SPI_MODE_BUS_TSZ_HALFWORD;
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900763 val |= S3C64XX_SPI_MODE_CH_TSZ_HALFWORD;
Jassi Brar230d42d2009-11-30 07:39:42 +0000764 break;
765 default:
766 val |= S3C64XX_SPI_MODE_BUS_TSZ_BYTE;
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900767 val |= S3C64XX_SPI_MODE_CH_TSZ_BYTE;
Jassi Brar230d42d2009-11-30 07:39:42 +0000768 break;
769 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000770
771 writel(val, regs + S3C64XX_SPI_MODE_CFG);
772
Thomas Abrahama5238e32012-07-13 07:15:14 +0900773 if (sdd->port_conf->clk_from_cmu) {
Jassi Brarb42a81c2010-09-29 17:31:33 +0900774 /* Configure Clock */
775 /* There is half-multiplier before the SPI */
776 clk_set_rate(sdd->src_clk, sdd->cur_speed * 2);
777 /* Enable Clock */
Thomas Abraham9f667bf2012-10-03 08:30:12 +0900778 clk_prepare_enable(sdd->src_clk);
Jassi Brarb42a81c2010-09-29 17:31:33 +0900779 } else {
780 /* Configure Clock */
781 val = readl(regs + S3C64XX_SPI_CLK_CFG);
782 val &= ~S3C64XX_SPI_PSR_MASK;
783 val |= ((clk_get_rate(sdd->src_clk) / sdd->cur_speed / 2 - 1)
784 & S3C64XX_SPI_PSR_MASK);
785 writel(val, regs + S3C64XX_SPI_CLK_CFG);
Jassi Brar230d42d2009-11-30 07:39:42 +0000786
Jassi Brarb42a81c2010-09-29 17:31:33 +0900787 /* Enable Clock */
788 val = readl(regs + S3C64XX_SPI_CLK_CFG);
789 val |= S3C64XX_SPI_ENCLK_ENABLE;
790 writel(val, regs + S3C64XX_SPI_CLK_CFG);
791 }
Jassi Brar230d42d2009-11-30 07:39:42 +0000792}
793
Jassi Brar230d42d2009-11-30 07:39:42 +0000794#define XFER_DMAADDR_INVALID DMA_BIT_MASK(32)
795
796static int s3c64xx_spi_map_mssg(struct s3c64xx_spi_driver_data *sdd,
797 struct spi_message *msg)
798{
799 struct device *dev = &sdd->pdev->dev;
800 struct spi_transfer *xfer;
801
Girish K S7e995552013-05-20 12:21:32 +0530802 if (is_polling(sdd) || msg->is_dma_mapped)
Jassi Brar230d42d2009-11-30 07:39:42 +0000803 return 0;
804
805 /* First mark all xfer unmapped */
806 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
807 xfer->rx_dma = XFER_DMAADDR_INVALID;
808 xfer->tx_dma = XFER_DMAADDR_INVALID;
809 }
810
811 /* Map until end or first fail */
812 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
813
Thomas Abrahama5238e32012-07-13 07:15:14 +0900814 if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
Jassi Brare02ddd42010-09-29 17:31:31 +0900815 continue;
816
Jassi Brar230d42d2009-11-30 07:39:42 +0000817 if (xfer->tx_buf != NULL) {
Jassi Brar251ee472010-09-03 10:36:26 +0900818 xfer->tx_dma = dma_map_single(dev,
819 (void *)xfer->tx_buf, xfer->len,
820 DMA_TO_DEVICE);
Jassi Brar230d42d2009-11-30 07:39:42 +0000821 if (dma_mapping_error(dev, xfer->tx_dma)) {
822 dev_err(dev, "dma_map_single Tx failed\n");
823 xfer->tx_dma = XFER_DMAADDR_INVALID;
824 return -ENOMEM;
825 }
826 }
827
828 if (xfer->rx_buf != NULL) {
829 xfer->rx_dma = dma_map_single(dev, xfer->rx_buf,
830 xfer->len, DMA_FROM_DEVICE);
831 if (dma_mapping_error(dev, xfer->rx_dma)) {
832 dev_err(dev, "dma_map_single Rx failed\n");
833 dma_unmap_single(dev, xfer->tx_dma,
834 xfer->len, DMA_TO_DEVICE);
835 xfer->tx_dma = XFER_DMAADDR_INVALID;
836 xfer->rx_dma = XFER_DMAADDR_INVALID;
837 return -ENOMEM;
838 }
839 }
840 }
841
842 return 0;
843}
844
845static void s3c64xx_spi_unmap_mssg(struct s3c64xx_spi_driver_data *sdd,
846 struct spi_message *msg)
847{
848 struct device *dev = &sdd->pdev->dev;
849 struct spi_transfer *xfer;
850
Girish K S7e995552013-05-20 12:21:32 +0530851 if (is_polling(sdd) || msg->is_dma_mapped)
Jassi Brar230d42d2009-11-30 07:39:42 +0000852 return;
853
854 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
855
Thomas Abrahama5238e32012-07-13 07:15:14 +0900856 if (xfer->len <= ((FIFO_LVL_MASK(sdd) >> 1) + 1))
Jassi Brare02ddd42010-09-29 17:31:31 +0900857 continue;
858
Jassi Brar230d42d2009-11-30 07:39:42 +0000859 if (xfer->rx_buf != NULL
860 && xfer->rx_dma != XFER_DMAADDR_INVALID)
861 dma_unmap_single(dev, xfer->rx_dma,
862 xfer->len, DMA_FROM_DEVICE);
863
864 if (xfer->tx_buf != NULL
865 && xfer->tx_dma != XFER_DMAADDR_INVALID)
866 dma_unmap_single(dev, xfer->tx_dma,
867 xfer->len, DMA_TO_DEVICE);
868 }
869}
870
Mark Brownad2a99a2012-02-15 14:48:32 -0800871static int s3c64xx_spi_transfer_one_message(struct spi_master *master,
872 struct spi_message *msg)
Jassi Brar230d42d2009-11-30 07:39:42 +0000873{
Mark Brownad2a99a2012-02-15 14:48:32 -0800874 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brar230d42d2009-11-30 07:39:42 +0000875 struct spi_device *spi = msg->spi;
876 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
877 struct spi_transfer *xfer;
878 int status = 0, cs_toggle = 0;
879 u32 speed;
880 u8 bpw;
881
882 /* If Master's(controller) state differs from that needed by Slave */
883 if (sdd->cur_speed != spi->max_speed_hz
884 || sdd->cur_mode != spi->mode
885 || sdd->cur_bpw != spi->bits_per_word) {
886 sdd->cur_bpw = spi->bits_per_word;
887 sdd->cur_speed = spi->max_speed_hz;
888 sdd->cur_mode = spi->mode;
889 s3c64xx_spi_config(sdd);
890 }
891
892 /* Map all the transfers if needed */
893 if (s3c64xx_spi_map_mssg(sdd, msg)) {
894 dev_err(&spi->dev,
895 "Xfer: Unable to map message buffers!\n");
896 status = -ENOMEM;
897 goto out;
898 }
899
900 /* Configure feedback delay */
901 writel(cs->fb_delay & 0x3, sdd->regs + S3C64XX_SPI_FB_CLK);
902
903 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
904
905 unsigned long flags;
906 int use_dma;
907
908 INIT_COMPLETION(sdd->xfer_completion);
909
910 /* Only BPW and Speed may change across transfers */
Laxman Dewangan766ed702012-12-18 14:25:43 +0530911 bpw = xfer->bits_per_word;
Jassi Brar230d42d2009-11-30 07:39:42 +0000912 speed = xfer->speed_hz ? : spi->max_speed_hz;
913
Jassi Brar0c92ecf2010-09-29 17:31:33 +0900914 if (xfer->len % (bpw / 8)) {
915 dev_err(&spi->dev,
916 "Xfer length(%u) not a multiple of word size(%u)\n",
917 xfer->len, bpw / 8);
918 status = -EIO;
919 goto out;
920 }
921
Jassi Brar230d42d2009-11-30 07:39:42 +0000922 if (bpw != sdd->cur_bpw || speed != sdd->cur_speed) {
923 sdd->cur_bpw = bpw;
924 sdd->cur_speed = speed;
925 s3c64xx_spi_config(sdd);
926 }
927
928 /* Polling method for xfers not bigger than FIFO capacity */
Arnd Bergmann78843722013-04-11 22:42:03 +0200929 use_dma = 0;
Girish K S7e995552013-05-20 12:21:32 +0530930 if (!is_polling(sdd) &&
931 (sdd->rx_dma.ch && sdd->tx_dma.ch &&
932 (xfer->len > ((FIFO_LVL_MASK(sdd) >> 1) + 1))))
Jassi Brar230d42d2009-11-30 07:39:42 +0000933 use_dma = 1;
934
935 spin_lock_irqsave(&sdd->lock, flags);
936
937 /* Pending only which is to be done */
938 sdd->state &= ~RXBUSY;
939 sdd->state &= ~TXBUSY;
940
941 enable_datapath(sdd, spi, xfer, use_dma);
942
943 /* Slave Select */
944 enable_cs(sdd, spi);
945
Jassi Brar230d42d2009-11-30 07:39:42 +0000946 spin_unlock_irqrestore(&sdd->lock, flags);
947
948 status = wait_for_xfer(sdd, xfer, use_dma);
949
Jassi Brar230d42d2009-11-30 07:39:42 +0000950 if (status) {
Jingoo Han75bf3362013-01-31 15:25:01 +0900951 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 +0000952 xfer->rx_buf ? 1 : 0, xfer->tx_buf ? 1 : 0,
953 (sdd->state & RXBUSY) ? 'f' : 'p',
954 (sdd->state & TXBUSY) ? 'f' : 'p',
955 xfer->len);
956
957 if (use_dma) {
958 if (xfer->tx_buf != NULL
959 && (sdd->state & TXBUSY))
Arnd Bergmann78843722013-04-11 22:42:03 +0200960 s3c64xx_spi_dma_stop(sdd, &sdd->tx_dma);
Jassi Brar230d42d2009-11-30 07:39:42 +0000961 if (xfer->rx_buf != NULL
962 && (sdd->state & RXBUSY))
Arnd Bergmann78843722013-04-11 22:42:03 +0200963 s3c64xx_spi_dma_stop(sdd, &sdd->rx_dma);
Jassi Brar230d42d2009-11-30 07:39:42 +0000964 }
965
966 goto out;
967 }
968
969 if (xfer->delay_usecs)
970 udelay(xfer->delay_usecs);
971
972 if (xfer->cs_change) {
973 /* Hint that the next mssg is gonna be
974 for the same device */
975 if (list_is_last(&xfer->transfer_list,
976 &msg->transfers))
977 cs_toggle = 1;
Jassi Brar230d42d2009-11-30 07:39:42 +0000978 }
979
980 msg->actual_length += xfer->len;
981
982 flush_fifo(sdd);
983 }
984
985out:
986 if (!cs_toggle || status)
987 disable_cs(sdd, spi);
988 else
989 sdd->tgl_spi = spi;
990
991 s3c64xx_spi_unmap_mssg(sdd, msg);
992
993 msg->status = status;
994
Mark Brownad2a99a2012-02-15 14:48:32 -0800995 spi_finalize_current_message(master);
996
997 return 0;
Jassi Brar230d42d2009-11-30 07:39:42 +0000998}
999
Thomas Abraham2b908072012-07-13 07:15:15 +09001000static struct s3c64xx_spi_csinfo *s3c64xx_get_slave_ctrldata(
Thomas Abraham2b908072012-07-13 07:15:15 +09001001 struct spi_device *spi)
1002{
1003 struct s3c64xx_spi_csinfo *cs;
Arnd Bergmann4732cc62012-08-04 11:18:20 +00001004 struct device_node *slave_np, *data_np = NULL;
Girish K S3146bee2013-06-21 11:26:12 +05301005 struct s3c64xx_spi_driver_data *sdd;
Thomas Abraham2b908072012-07-13 07:15:15 +09001006 u32 fb_delay = 0;
1007
Girish K S3146bee2013-06-21 11:26:12 +05301008 sdd = spi_master_get_devdata(spi->master);
Thomas Abraham2b908072012-07-13 07:15:15 +09001009 slave_np = spi->dev.of_node;
1010 if (!slave_np) {
1011 dev_err(&spi->dev, "device node not found\n");
1012 return ERR_PTR(-EINVAL);
1013 }
1014
Srinivas Kandagatla06455bb2012-09-18 08:10:49 +01001015 data_np = of_get_child_by_name(slave_np, "controller-data");
Thomas Abraham2b908072012-07-13 07:15:15 +09001016 if (!data_np) {
1017 dev_err(&spi->dev, "child node 'controller-data' not found\n");
1018 return ERR_PTR(-EINVAL);
1019 }
1020
1021 cs = kzalloc(sizeof(*cs), GFP_KERNEL);
1022 if (!cs) {
Jingoo Han75bf3362013-01-31 15:25:01 +09001023 dev_err(&spi->dev, "could not allocate memory for controller data\n");
Srinivas Kandagatla06455bb2012-09-18 08:10:49 +01001024 of_node_put(data_np);
Thomas Abraham2b908072012-07-13 07:15:15 +09001025 return ERR_PTR(-ENOMEM);
1026 }
1027
Girish K S3146bee2013-06-21 11:26:12 +05301028 /* The CS line is asserted/deasserted by the gpio pin */
1029 if (sdd->cs_gpio)
1030 cs->line = of_get_named_gpio(data_np, "cs-gpio", 0);
1031
Thomas Abraham2b908072012-07-13 07:15:15 +09001032 if (!gpio_is_valid(cs->line)) {
Jingoo Han75bf3362013-01-31 15:25:01 +09001033 dev_err(&spi->dev, "chip select gpio is not specified or invalid\n");
Thomas Abraham2b908072012-07-13 07:15:15 +09001034 kfree(cs);
Srinivas Kandagatla06455bb2012-09-18 08:10:49 +01001035 of_node_put(data_np);
Thomas Abraham2b908072012-07-13 07:15:15 +09001036 return ERR_PTR(-EINVAL);
1037 }
1038
1039 of_property_read_u32(data_np, "samsung,spi-feedback-delay", &fb_delay);
1040 cs->fb_delay = fb_delay;
Srinivas Kandagatla06455bb2012-09-18 08:10:49 +01001041 of_node_put(data_np);
Thomas Abraham2b908072012-07-13 07:15:15 +09001042 return cs;
1043}
1044
Jassi Brar230d42d2009-11-30 07:39:42 +00001045/*
1046 * Here we only check the validity of requested configuration
1047 * and save the configuration in a local data-structure.
1048 * The controller is actually configured only just before we
1049 * get a message to transfer.
1050 */
1051static int s3c64xx_spi_setup(struct spi_device *spi)
1052{
1053 struct s3c64xx_spi_csinfo *cs = spi->controller_data;
1054 struct s3c64xx_spi_driver_data *sdd;
Jassi Brarad7de722010-01-20 13:49:44 -07001055 struct s3c64xx_spi_info *sci;
Jassi Brar230d42d2009-11-30 07:39:42 +00001056 struct spi_message *msg;
Jassi Brar230d42d2009-11-30 07:39:42 +00001057 unsigned long flags;
Thomas Abraham2b908072012-07-13 07:15:15 +09001058 int err;
Jassi Brar230d42d2009-11-30 07:39:42 +00001059
Thomas Abraham2b908072012-07-13 07:15:15 +09001060 sdd = spi_master_get_devdata(spi->master);
1061 if (!cs && spi->dev.of_node) {
Matthias Brugger5c725b32013-03-26 10:27:35 +01001062 cs = s3c64xx_get_slave_ctrldata(spi);
Thomas Abraham2b908072012-07-13 07:15:15 +09001063 spi->controller_data = cs;
1064 }
1065
1066 if (IS_ERR_OR_NULL(cs)) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001067 dev_err(&spi->dev, "No CS for SPI(%d)\n", spi->chip_select);
1068 return -ENODEV;
1069 }
1070
Girish K S3146bee2013-06-21 11:26:12 +05301071 /* Request gpio only if cs line is asserted by gpio pins */
1072 if (sdd->cs_gpio) {
Mark Brown707214d2012-07-19 14:36:16 +09001073 err = gpio_request_one(cs->line, GPIOF_OUT_INIT_HIGH,
1074 dev_name(&spi->dev));
Thomas Abraham1c20c202012-07-13 07:15:14 +09001075 if (err) {
Mark Brown49f3eac2012-07-19 14:36:13 +09001076 dev_err(&spi->dev,
1077 "Failed to get /CS gpio [%d]: %d\n",
1078 cs->line, err);
Thomas Abraham2b908072012-07-13 07:15:15 +09001079 goto err_gpio_req;
Thomas Abraham1c20c202012-07-13 07:15:14 +09001080 }
Thomas Abraham1c20c202012-07-13 07:15:14 +09001081 }
1082
Girish K S3146bee2013-06-21 11:26:12 +05301083 if (!spi_get_ctldata(spi))
1084 spi_set_ctldata(spi, cs);
1085
Jassi Brar230d42d2009-11-30 07:39:42 +00001086 sci = sdd->cntrlr_info;
1087
1088 spin_lock_irqsave(&sdd->lock, flags);
1089
1090 list_for_each_entry(msg, &sdd->queue, queue) {
1091 /* Is some mssg is already queued for this device */
1092 if (msg->spi == spi) {
1093 dev_err(&spi->dev,
1094 "setup: attempt while mssg in queue!\n");
1095 spin_unlock_irqrestore(&sdd->lock, flags);
Thomas Abraham2b908072012-07-13 07:15:15 +09001096 err = -EBUSY;
1097 goto err_msgq;
Jassi Brar230d42d2009-11-30 07:39:42 +00001098 }
1099 }
1100
Jassi Brar230d42d2009-11-30 07:39:42 +00001101 spin_unlock_irqrestore(&sdd->lock, flags);
1102
Mark Brownb97b6622011-12-04 00:58:06 +00001103 pm_runtime_get_sync(&sdd->pdev->dev);
1104
Jassi Brar230d42d2009-11-30 07:39:42 +00001105 /* Check if we can provide the requested rate */
Thomas Abrahama5238e32012-07-13 07:15:14 +09001106 if (!sdd->port_conf->clk_from_cmu) {
Jassi Brarb42a81c2010-09-29 17:31:33 +09001107 u32 psr, speed;
Jassi Brar230d42d2009-11-30 07:39:42 +00001108
Jassi Brarb42a81c2010-09-29 17:31:33 +09001109 /* Max possible */
1110 speed = clk_get_rate(sdd->src_clk) / 2 / (0 + 1);
Jassi Brar230d42d2009-11-30 07:39:42 +00001111
Jassi Brarb42a81c2010-09-29 17:31:33 +09001112 if (spi->max_speed_hz > speed)
1113 spi->max_speed_hz = speed;
Jassi Brar230d42d2009-11-30 07:39:42 +00001114
Jassi Brarb42a81c2010-09-29 17:31:33 +09001115 psr = clk_get_rate(sdd->src_clk) / 2 / spi->max_speed_hz - 1;
1116 psr &= S3C64XX_SPI_PSR_MASK;
1117 if (psr == S3C64XX_SPI_PSR_MASK)
1118 psr--;
1119
1120 speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
1121 if (spi->max_speed_hz < speed) {
1122 if (psr+1 < S3C64XX_SPI_PSR_MASK) {
1123 psr++;
1124 } else {
1125 err = -EINVAL;
1126 goto setup_exit;
1127 }
Jassi Brar230d42d2009-11-30 07:39:42 +00001128 }
Jassi Brar230d42d2009-11-30 07:39:42 +00001129
Jassi Brarb42a81c2010-09-29 17:31:33 +09001130 speed = clk_get_rate(sdd->src_clk) / 2 / (psr + 1);
Thomas Abraham2b908072012-07-13 07:15:15 +09001131 if (spi->max_speed_hz >= speed) {
Jassi Brarb42a81c2010-09-29 17:31:33 +09001132 spi->max_speed_hz = speed;
Thomas Abraham2b908072012-07-13 07:15:15 +09001133 } else {
Mark Browne1b0f0d2012-12-20 18:27:31 +00001134 dev_err(&spi->dev, "Can't set %dHz transfer speed\n",
1135 spi->max_speed_hz);
Jassi Brarb42a81c2010-09-29 17:31:33 +09001136 err = -EINVAL;
Thomas Abraham2b908072012-07-13 07:15:15 +09001137 goto setup_exit;
1138 }
Jassi Brarb42a81c2010-09-29 17:31:33 +09001139 }
Jassi Brar230d42d2009-11-30 07:39:42 +00001140
Mark Brownb97b6622011-12-04 00:58:06 +00001141 pm_runtime_put(&sdd->pdev->dev);
Thomas Abraham2b908072012-07-13 07:15:15 +09001142 disable_cs(sdd, spi);
1143 return 0;
Mark Brownb97b6622011-12-04 00:58:06 +00001144
Jassi Brar230d42d2009-11-30 07:39:42 +00001145setup_exit:
Jassi Brar230d42d2009-11-30 07:39:42 +00001146 /* setup() returns with device de-selected */
1147 disable_cs(sdd, spi);
1148
Thomas Abraham2b908072012-07-13 07:15:15 +09001149err_msgq:
1150 gpio_free(cs->line);
1151 spi_set_ctldata(spi, NULL);
1152
1153err_gpio_req:
Sylwester Nawrocki5bee3b92012-09-13 16:31:30 +02001154 if (spi->dev.of_node)
1155 kfree(cs);
Thomas Abraham2b908072012-07-13 07:15:15 +09001156
Jassi Brar230d42d2009-11-30 07:39:42 +00001157 return err;
1158}
1159
Thomas Abraham1c20c202012-07-13 07:15:14 +09001160static void s3c64xx_spi_cleanup(struct spi_device *spi)
1161{
1162 struct s3c64xx_spi_csinfo *cs = spi_get_ctldata(spi);
Girish K S3146bee2013-06-21 11:26:12 +05301163 struct s3c64xx_spi_driver_data *sdd;
Thomas Abraham1c20c202012-07-13 07:15:14 +09001164
Girish K S3146bee2013-06-21 11:26:12 +05301165 sdd = spi_master_get_devdata(spi->master);
1166 if (cs && sdd->cs_gpio) {
Thomas Abraham1c20c202012-07-13 07:15:14 +09001167 gpio_free(cs->line);
Thomas Abraham2b908072012-07-13 07:15:15 +09001168 if (spi->dev.of_node)
1169 kfree(cs);
1170 }
Thomas Abraham1c20c202012-07-13 07:15:14 +09001171 spi_set_ctldata(spi, NULL);
1172}
1173
Mark Brownc2573122011-11-10 10:57:32 +00001174static irqreturn_t s3c64xx_spi_irq(int irq, void *data)
1175{
1176 struct s3c64xx_spi_driver_data *sdd = data;
1177 struct spi_master *spi = sdd->master;
Girish K S375981f2013-03-13 12:13:30 +05301178 unsigned int val, clr = 0;
Mark Brownc2573122011-11-10 10:57:32 +00001179
Girish K S375981f2013-03-13 12:13:30 +05301180 val = readl(sdd->regs + S3C64XX_SPI_STATUS);
Mark Brownc2573122011-11-10 10:57:32 +00001181
Girish K S375981f2013-03-13 12:13:30 +05301182 if (val & S3C64XX_SPI_ST_RX_OVERRUN_ERR) {
1183 clr = S3C64XX_SPI_PND_RX_OVERRUN_CLR;
Mark Brownc2573122011-11-10 10:57:32 +00001184 dev_err(&spi->dev, "RX overrun\n");
Girish K S375981f2013-03-13 12:13:30 +05301185 }
1186 if (val & S3C64XX_SPI_ST_RX_UNDERRUN_ERR) {
1187 clr |= S3C64XX_SPI_PND_RX_UNDERRUN_CLR;
Mark Brownc2573122011-11-10 10:57:32 +00001188 dev_err(&spi->dev, "RX underrun\n");
Girish K S375981f2013-03-13 12:13:30 +05301189 }
1190 if (val & S3C64XX_SPI_ST_TX_OVERRUN_ERR) {
1191 clr |= S3C64XX_SPI_PND_TX_OVERRUN_CLR;
Mark Brownc2573122011-11-10 10:57:32 +00001192 dev_err(&spi->dev, "TX overrun\n");
Girish K S375981f2013-03-13 12:13:30 +05301193 }
1194 if (val & S3C64XX_SPI_ST_TX_UNDERRUN_ERR) {
1195 clr |= S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
Mark Brownc2573122011-11-10 10:57:32 +00001196 dev_err(&spi->dev, "TX underrun\n");
Girish K S375981f2013-03-13 12:13:30 +05301197 }
1198
1199 /* Clear the pending irq by setting and then clearing it */
1200 writel(clr, sdd->regs + S3C64XX_SPI_PENDING_CLR);
1201 writel(0, sdd->regs + S3C64XX_SPI_PENDING_CLR);
Mark Brownc2573122011-11-10 10:57:32 +00001202
1203 return IRQ_HANDLED;
1204}
1205
Jassi Brar230d42d2009-11-30 07:39:42 +00001206static void s3c64xx_spi_hwinit(struct s3c64xx_spi_driver_data *sdd, int channel)
1207{
Jassi Brarad7de722010-01-20 13:49:44 -07001208 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +00001209 void __iomem *regs = sdd->regs;
1210 unsigned int val;
1211
1212 sdd->cur_speed = 0;
1213
Mark Brown5fc3e832012-07-19 14:36:23 +09001214 writel(S3C64XX_SPI_SLAVE_SIG_INACT, sdd->regs + S3C64XX_SPI_SLAVE_SEL);
Jassi Brar230d42d2009-11-30 07:39:42 +00001215
1216 /* Disable Interrupts - we use Polling if not DMA mode */
1217 writel(0, regs + S3C64XX_SPI_INT_EN);
1218
Thomas Abrahama5238e32012-07-13 07:15:14 +09001219 if (!sdd->port_conf->clk_from_cmu)
Jassi Brarb42a81c2010-09-29 17:31:33 +09001220 writel(sci->src_clk_nr << S3C64XX_SPI_CLKSEL_SRCSHFT,
Jassi Brar230d42d2009-11-30 07:39:42 +00001221 regs + S3C64XX_SPI_CLK_CFG);
1222 writel(0, regs + S3C64XX_SPI_MODE_CFG);
1223 writel(0, regs + S3C64XX_SPI_PACKET_CNT);
1224
Girish K S375981f2013-03-13 12:13:30 +05301225 /* Clear any irq pending bits, should set and clear the bits */
1226 val = S3C64XX_SPI_PND_RX_OVERRUN_CLR |
1227 S3C64XX_SPI_PND_RX_UNDERRUN_CLR |
1228 S3C64XX_SPI_PND_TX_OVERRUN_CLR |
1229 S3C64XX_SPI_PND_TX_UNDERRUN_CLR;
1230 writel(val, regs + S3C64XX_SPI_PENDING_CLR);
1231 writel(0, regs + S3C64XX_SPI_PENDING_CLR);
Jassi Brar230d42d2009-11-30 07:39:42 +00001232
1233 writel(0, regs + S3C64XX_SPI_SWAP_CFG);
1234
1235 val = readl(regs + S3C64XX_SPI_MODE_CFG);
1236 val &= ~S3C64XX_SPI_MODE_4BURST;
1237 val &= ~(S3C64XX_SPI_MAX_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1238 val |= (S3C64XX_SPI_TRAILCNT << S3C64XX_SPI_TRAILCNT_OFF);
1239 writel(val, regs + S3C64XX_SPI_MODE_CFG);
1240
1241 flush_fifo(sdd);
1242}
1243
Thomas Abraham2b908072012-07-13 07:15:15 +09001244#ifdef CONFIG_OF
Jingoo Han75bf3362013-01-31 15:25:01 +09001245static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
Thomas Abraham2b908072012-07-13 07:15:15 +09001246{
1247 struct s3c64xx_spi_info *sci;
1248 u32 temp;
1249
1250 sci = devm_kzalloc(dev, sizeof(*sci), GFP_KERNEL);
1251 if (!sci) {
1252 dev_err(dev, "memory allocation for spi_info failed\n");
1253 return ERR_PTR(-ENOMEM);
1254 }
1255
1256 if (of_property_read_u32(dev->of_node, "samsung,spi-src-clk", &temp)) {
Jingoo Han75bf3362013-01-31 15:25:01 +09001257 dev_warn(dev, "spi bus clock parent not specified, using clock at index 0 as parent\n");
Thomas Abraham2b908072012-07-13 07:15:15 +09001258 sci->src_clk_nr = 0;
1259 } else {
1260 sci->src_clk_nr = temp;
1261 }
1262
1263 if (of_property_read_u32(dev->of_node, "num-cs", &temp)) {
Jingoo Han75bf3362013-01-31 15:25:01 +09001264 dev_warn(dev, "number of chip select lines not specified, assuming 1 chip select line\n");
Thomas Abraham2b908072012-07-13 07:15:15 +09001265 sci->num_cs = 1;
1266 } else {
1267 sci->num_cs = temp;
1268 }
1269
1270 return sci;
1271}
1272#else
1273static struct s3c64xx_spi_info *s3c64xx_spi_parse_dt(struct device *dev)
1274{
1275 return dev->platform_data;
1276}
Thomas Abraham2b908072012-07-13 07:15:15 +09001277#endif
1278
1279static const struct of_device_id s3c64xx_spi_dt_match[];
1280
Thomas Abrahama5238e32012-07-13 07:15:14 +09001281static inline struct s3c64xx_spi_port_config *s3c64xx_spi_get_port_config(
1282 struct platform_device *pdev)
1283{
Thomas Abraham2b908072012-07-13 07:15:15 +09001284#ifdef CONFIG_OF
1285 if (pdev->dev.of_node) {
1286 const struct of_device_id *match;
1287 match = of_match_node(s3c64xx_spi_dt_match, pdev->dev.of_node);
1288 return (struct s3c64xx_spi_port_config *)match->data;
1289 }
1290#endif
Thomas Abrahama5238e32012-07-13 07:15:14 +09001291 return (struct s3c64xx_spi_port_config *)
1292 platform_get_device_id(pdev)->driver_data;
1293}
1294
Grant Likely2deff8d2013-02-05 13:27:35 +00001295static int s3c64xx_spi_probe(struct platform_device *pdev)
Jassi Brar230d42d2009-11-30 07:39:42 +00001296{
Thomas Abraham2b908072012-07-13 07:15:15 +09001297 struct resource *mem_res;
Padmavathi Vennab5be04d2013-01-18 17:17:03 +05301298 struct resource *res;
Jassi Brar230d42d2009-11-30 07:39:42 +00001299 struct s3c64xx_spi_driver_data *sdd;
Thomas Abraham2b908072012-07-13 07:15:15 +09001300 struct s3c64xx_spi_info *sci = pdev->dev.platform_data;
Jassi Brar230d42d2009-11-30 07:39:42 +00001301 struct spi_master *master;
Mark Brownc2573122011-11-10 10:57:32 +00001302 int ret, irq;
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001303 char clk_name[16];
Jassi Brar230d42d2009-11-30 07:39:42 +00001304
Thomas Abraham2b908072012-07-13 07:15:15 +09001305 if (!sci && pdev->dev.of_node) {
1306 sci = s3c64xx_spi_parse_dt(&pdev->dev);
1307 if (IS_ERR(sci))
1308 return PTR_ERR(sci);
Jassi Brar230d42d2009-11-30 07:39:42 +00001309 }
1310
Thomas Abraham2b908072012-07-13 07:15:15 +09001311 if (!sci) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001312 dev_err(&pdev->dev, "platform_data missing!\n");
1313 return -ENODEV;
1314 }
1315
Jassi Brar230d42d2009-11-30 07:39:42 +00001316 mem_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1317 if (mem_res == NULL) {
1318 dev_err(&pdev->dev, "Unable to get SPI MEM resource\n");
1319 return -ENXIO;
1320 }
1321
Mark Brownc2573122011-11-10 10:57:32 +00001322 irq = platform_get_irq(pdev, 0);
1323 if (irq < 0) {
1324 dev_warn(&pdev->dev, "Failed to get IRQ: %d\n", irq);
1325 return irq;
1326 }
1327
Jassi Brar230d42d2009-11-30 07:39:42 +00001328 master = spi_alloc_master(&pdev->dev,
1329 sizeof(struct s3c64xx_spi_driver_data));
1330 if (master == NULL) {
1331 dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
1332 return -ENOMEM;
1333 }
1334
Jassi Brar230d42d2009-11-30 07:39:42 +00001335 platform_set_drvdata(pdev, master);
1336
1337 sdd = spi_master_get_devdata(master);
Thomas Abrahama5238e32012-07-13 07:15:14 +09001338 sdd->port_conf = s3c64xx_spi_get_port_config(pdev);
Jassi Brar230d42d2009-11-30 07:39:42 +00001339 sdd->master = master;
1340 sdd->cntrlr_info = sci;
1341 sdd->pdev = pdev;
1342 sdd->sfr_start = mem_res->start;
Girish K S3146bee2013-06-21 11:26:12 +05301343 sdd->cs_gpio = true;
Thomas Abraham2b908072012-07-13 07:15:15 +09001344 if (pdev->dev.of_node) {
Girish K S3146bee2013-06-21 11:26:12 +05301345 if (!of_find_property(pdev->dev.of_node, "cs-gpio", NULL))
1346 sdd->cs_gpio = false;
1347
Thomas Abraham2b908072012-07-13 07:15:15 +09001348 ret = of_alias_get_id(pdev->dev.of_node, "spi");
1349 if (ret < 0) {
Jingoo Han75bf3362013-01-31 15:25:01 +09001350 dev_err(&pdev->dev, "failed to get alias id, errno %d\n",
1351 ret);
Thomas Abraham2b908072012-07-13 07:15:15 +09001352 goto err0;
1353 }
1354 sdd->port_id = ret;
1355 } else {
1356 sdd->port_id = pdev->id;
1357 }
Jassi Brar230d42d2009-11-30 07:39:42 +00001358
1359 sdd->cur_bpw = 8;
1360
Padmavathi Vennab5be04d2013-01-18 17:17:03 +05301361 if (!sdd->pdev->dev.of_node) {
1362 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1363 if (!res) {
Girish K S7e995552013-05-20 12:21:32 +05301364 dev_warn(&pdev->dev, "Unable to get SPI tx dma "
1365 "resource. Switching to poll mode\n");
1366 sdd->port_conf->quirks = S3C64XX_SPI_QUIRK_POLL;
1367 } else
1368 sdd->tx_dma.dmach = res->start;
Thomas Abraham2b908072012-07-13 07:15:15 +09001369
Padmavathi Vennab5be04d2013-01-18 17:17:03 +05301370 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1371 if (!res) {
Girish K S7e995552013-05-20 12:21:32 +05301372 dev_warn(&pdev->dev, "Unable to get SPI rx dma "
1373 "resource. Switching to poll mode\n");
1374 sdd->port_conf->quirks = S3C64XX_SPI_QUIRK_POLL;
1375 } else
1376 sdd->rx_dma.dmach = res->start;
Padmavathi Vennab5be04d2013-01-18 17:17:03 +05301377 }
1378
1379 sdd->tx_dma.direction = DMA_MEM_TO_DEV;
1380 sdd->rx_dma.direction = DMA_DEV_TO_MEM;
Thomas Abraham2b908072012-07-13 07:15:15 +09001381
1382 master->dev.of_node = pdev->dev.of_node;
Thomas Abrahama5238e32012-07-13 07:15:14 +09001383 master->bus_num = sdd->port_id;
Jassi Brar230d42d2009-11-30 07:39:42 +00001384 master->setup = s3c64xx_spi_setup;
Thomas Abraham1c20c202012-07-13 07:15:14 +09001385 master->cleanup = s3c64xx_spi_cleanup;
Mark Brownad2a99a2012-02-15 14:48:32 -08001386 master->prepare_transfer_hardware = s3c64xx_spi_prepare_transfer;
1387 master->transfer_one_message = s3c64xx_spi_transfer_one_message;
1388 master->unprepare_transfer_hardware = s3c64xx_spi_unprepare_transfer;
Jassi Brar230d42d2009-11-30 07:39:42 +00001389 master->num_chipselect = sci->num_cs;
1390 master->dma_alignment = 8;
Stephen Warren24778be2013-05-21 20:36:35 -06001391 master->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(16) |
1392 SPI_BPW_MASK(8);
Jassi Brar230d42d2009-11-30 07:39:42 +00001393 /* the spi->mode bits understood by this driver: */
1394 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
1395
Thierry Redingb0ee5602013-01-21 11:09:18 +01001396 sdd->regs = devm_ioremap_resource(&pdev->dev, mem_res);
1397 if (IS_ERR(sdd->regs)) {
1398 ret = PTR_ERR(sdd->regs);
Jingoo Han4eb77002013-01-10 11:04:21 +09001399 goto err0;
Jassi Brar230d42d2009-11-30 07:39:42 +00001400 }
1401
Thomas Abraham00ab5392013-04-15 20:42:57 -07001402 if (sci->cfg_gpio && sci->cfg_gpio()) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001403 dev_err(&pdev->dev, "Unable to config gpio\n");
1404 ret = -EBUSY;
Jingoo Han4eb77002013-01-10 11:04:21 +09001405 goto err0;
Jassi Brar230d42d2009-11-30 07:39:42 +00001406 }
1407
1408 /* Setup clocks */
Jingoo Han4eb77002013-01-10 11:04:21 +09001409 sdd->clk = devm_clk_get(&pdev->dev, "spi");
Jassi Brar230d42d2009-11-30 07:39:42 +00001410 if (IS_ERR(sdd->clk)) {
1411 dev_err(&pdev->dev, "Unable to acquire clock 'spi'\n");
1412 ret = PTR_ERR(sdd->clk);
Thomas Abraham00ab5392013-04-15 20:42:57 -07001413 goto err0;
Jassi Brar230d42d2009-11-30 07:39:42 +00001414 }
1415
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001416 if (clk_prepare_enable(sdd->clk)) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001417 dev_err(&pdev->dev, "Couldn't enable clock 'spi'\n");
1418 ret = -EBUSY;
Thomas Abraham00ab5392013-04-15 20:42:57 -07001419 goto err0;
Jassi Brar230d42d2009-11-30 07:39:42 +00001420 }
1421
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001422 sprintf(clk_name, "spi_busclk%d", sci->src_clk_nr);
Jingoo Han4eb77002013-01-10 11:04:21 +09001423 sdd->src_clk = devm_clk_get(&pdev->dev, clk_name);
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001424 if (IS_ERR(sdd->src_clk)) {
Jassi Brar230d42d2009-11-30 07:39:42 +00001425 dev_err(&pdev->dev,
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001426 "Unable to acquire clock '%s'\n", clk_name);
Jassi Brarb0d5d6e2010-01-20 13:49:44 -07001427 ret = PTR_ERR(sdd->src_clk);
Jingoo Han4eb77002013-01-10 11:04:21 +09001428 goto err2;
Jassi Brar230d42d2009-11-30 07:39:42 +00001429 }
1430
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001431 if (clk_prepare_enable(sdd->src_clk)) {
Padmavathi Vennaa24d8502011-11-02 20:04:19 +09001432 dev_err(&pdev->dev, "Couldn't enable clock '%s'\n", clk_name);
Jassi Brar230d42d2009-11-30 07:39:42 +00001433 ret = -EBUSY;
Jingoo Han4eb77002013-01-10 11:04:21 +09001434 goto err2;
Jassi Brar230d42d2009-11-30 07:39:42 +00001435 }
1436
Jassi Brar230d42d2009-11-30 07:39:42 +00001437 /* Setup Deufult Mode */
Thomas Abrahama5238e32012-07-13 07:15:14 +09001438 s3c64xx_spi_hwinit(sdd, sdd->port_id);
Jassi Brar230d42d2009-11-30 07:39:42 +00001439
1440 spin_lock_init(&sdd->lock);
1441 init_completion(&sdd->xfer_completion);
Jassi Brar230d42d2009-11-30 07:39:42 +00001442 INIT_LIST_HEAD(&sdd->queue);
1443
Jingoo Han4eb77002013-01-10 11:04:21 +09001444 ret = devm_request_irq(&pdev->dev, irq, s3c64xx_spi_irq, 0,
1445 "spi-s3c64xx", sdd);
Mark Brownc2573122011-11-10 10:57:32 +00001446 if (ret != 0) {
1447 dev_err(&pdev->dev, "Failed to request IRQ %d: %d\n",
1448 irq, ret);
Jingoo Han4eb77002013-01-10 11:04:21 +09001449 goto err3;
Mark Brownc2573122011-11-10 10:57:32 +00001450 }
1451
1452 writel(S3C64XX_SPI_INT_RX_OVERRUN_EN | S3C64XX_SPI_INT_RX_UNDERRUN_EN |
1453 S3C64XX_SPI_INT_TX_OVERRUN_EN | S3C64XX_SPI_INT_TX_UNDERRUN_EN,
1454 sdd->regs + S3C64XX_SPI_INT_EN);
1455
Jassi Brar230d42d2009-11-30 07:39:42 +00001456 if (spi_register_master(master)) {
1457 dev_err(&pdev->dev, "cannot register SPI master\n");
1458 ret = -EBUSY;
Jingoo Han4eb77002013-01-10 11:04:21 +09001459 goto err3;
Jassi Brar230d42d2009-11-30 07:39:42 +00001460 }
1461
Jingoo Han75bf3362013-01-31 15:25:01 +09001462 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 +09001463 sdd->port_id, master->num_chipselect);
Joe Perches8a349d42010-02-02 07:22:13 +00001464 dev_dbg(&pdev->dev, "\tIOmem=[0x%x-0x%x]\tDMA=[Rx-%d, Tx-%d]\n",
Jassi Brar230d42d2009-11-30 07:39:42 +00001465 mem_res->end, mem_res->start,
Boojin Kim82ab8cd2011-09-02 09:44:42 +09001466 sdd->rx_dma.dmach, sdd->tx_dma.dmach);
Jassi Brar230d42d2009-11-30 07:39:42 +00001467
Mark Brownb97b6622011-12-04 00:58:06 +00001468 pm_runtime_enable(&pdev->dev);
1469
Jassi Brar230d42d2009-11-30 07:39:42 +00001470 return 0;
1471
Jassi Brar230d42d2009-11-30 07:39:42 +00001472err3:
Jingoo Han4eb77002013-01-10 11:04:21 +09001473 clk_disable_unprepare(sdd->src_clk);
1474err2:
1475 clk_disable_unprepare(sdd->clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001476err0:
Jassi Brar230d42d2009-11-30 07:39:42 +00001477 spi_master_put(master);
1478
1479 return ret;
1480}
1481
1482static int s3c64xx_spi_remove(struct platform_device *pdev)
1483{
1484 struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
1485 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001486
Mark Brownb97b6622011-12-04 00:58:06 +00001487 pm_runtime_disable(&pdev->dev);
1488
Jassi Brar230d42d2009-11-30 07:39:42 +00001489 spi_unregister_master(master);
1490
Mark Brownc2573122011-11-10 10:57:32 +00001491 writel(0, sdd->regs + S3C64XX_SPI_INT_EN);
1492
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001493 clk_disable_unprepare(sdd->src_clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001494
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001495 clk_disable_unprepare(sdd->clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001496
Jassi Brar230d42d2009-11-30 07:39:42 +00001497 spi_master_put(master);
1498
1499 return 0;
1500}
1501
Jingoo Han997230d2013-03-22 02:09:08 +00001502#ifdef CONFIG_PM_SLEEP
Mark Browne25d0bf2011-12-04 00:36:18 +00001503static int s3c64xx_spi_suspend(struct device *dev)
Jassi Brar230d42d2009-11-30 07:39:42 +00001504{
Guenter Roeck9a2a5242012-08-16 20:14:25 -07001505 struct spi_master *master = dev_get_drvdata(dev);
Jassi Brar230d42d2009-11-30 07:39:42 +00001506 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001507
Mark Brownad2a99a2012-02-15 14:48:32 -08001508 spi_master_suspend(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001509
1510 /* Disable the clock */
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001511 clk_disable_unprepare(sdd->src_clk);
1512 clk_disable_unprepare(sdd->clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001513
1514 sdd->cur_speed = 0; /* Output Clock is stopped */
1515
1516 return 0;
1517}
1518
Mark Browne25d0bf2011-12-04 00:36:18 +00001519static int s3c64xx_spi_resume(struct device *dev)
Jassi Brar230d42d2009-11-30 07:39:42 +00001520{
Guenter Roeck9a2a5242012-08-16 20:14:25 -07001521 struct spi_master *master = dev_get_drvdata(dev);
Jassi Brar230d42d2009-11-30 07:39:42 +00001522 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
Jassi Brarad7de722010-01-20 13:49:44 -07001523 struct s3c64xx_spi_info *sci = sdd->cntrlr_info;
Jassi Brar230d42d2009-11-30 07:39:42 +00001524
Thomas Abraham00ab5392013-04-15 20:42:57 -07001525 if (sci->cfg_gpio)
Thomas Abraham2b908072012-07-13 07:15:15 +09001526 sci->cfg_gpio();
Jassi Brar230d42d2009-11-30 07:39:42 +00001527
1528 /* Enable the clock */
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001529 clk_prepare_enable(sdd->src_clk);
1530 clk_prepare_enable(sdd->clk);
Jassi Brar230d42d2009-11-30 07:39:42 +00001531
Thomas Abrahama5238e32012-07-13 07:15:14 +09001532 s3c64xx_spi_hwinit(sdd, sdd->port_id);
Jassi Brar230d42d2009-11-30 07:39:42 +00001533
Mark Brownad2a99a2012-02-15 14:48:32 -08001534 spi_master_resume(master);
Jassi Brar230d42d2009-11-30 07:39:42 +00001535
1536 return 0;
1537}
Jingoo Han997230d2013-03-22 02:09:08 +00001538#endif /* CONFIG_PM_SLEEP */
Jassi Brar230d42d2009-11-30 07:39:42 +00001539
Mark Brownb97b6622011-12-04 00:58:06 +00001540#ifdef CONFIG_PM_RUNTIME
1541static int s3c64xx_spi_runtime_suspend(struct device *dev)
1542{
Guenter Roeck9a2a5242012-08-16 20:14:25 -07001543 struct spi_master *master = dev_get_drvdata(dev);
Mark Brownb97b6622011-12-04 00:58:06 +00001544 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1545
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001546 clk_disable_unprepare(sdd->clk);
1547 clk_disable_unprepare(sdd->src_clk);
Mark Brownb97b6622011-12-04 00:58:06 +00001548
1549 return 0;
1550}
1551
1552static int s3c64xx_spi_runtime_resume(struct device *dev)
1553{
Guenter Roeck9a2a5242012-08-16 20:14:25 -07001554 struct spi_master *master = dev_get_drvdata(dev);
Mark Brownb97b6622011-12-04 00:58:06 +00001555 struct s3c64xx_spi_driver_data *sdd = spi_master_get_devdata(master);
1556
Thomas Abraham9f667bf2012-10-03 08:30:12 +09001557 clk_prepare_enable(sdd->src_clk);
1558 clk_prepare_enable(sdd->clk);
Mark Brownb97b6622011-12-04 00:58:06 +00001559
1560 return 0;
1561}
1562#endif /* CONFIG_PM_RUNTIME */
1563
Mark Browne25d0bf2011-12-04 00:36:18 +00001564static const struct dev_pm_ops s3c64xx_spi_pm = {
1565 SET_SYSTEM_SLEEP_PM_OPS(s3c64xx_spi_suspend, s3c64xx_spi_resume)
Mark Brownb97b6622011-12-04 00:58:06 +00001566 SET_RUNTIME_PM_OPS(s3c64xx_spi_runtime_suspend,
1567 s3c64xx_spi_runtime_resume, NULL)
Mark Browne25d0bf2011-12-04 00:36:18 +00001568};
1569
Sachin Kamat10ce0472012-08-03 10:08:12 +05301570static struct s3c64xx_spi_port_config s3c2443_spi_port_config = {
Thomas Abrahama5238e32012-07-13 07:15:14 +09001571 .fifo_lvl_mask = { 0x7f },
1572 .rx_lvl_offset = 13,
1573 .tx_st_done = 21,
1574 .high_speed = true,
1575};
1576
Sachin Kamat10ce0472012-08-03 10:08:12 +05301577static struct s3c64xx_spi_port_config s3c6410_spi_port_config = {
Thomas Abrahama5238e32012-07-13 07:15:14 +09001578 .fifo_lvl_mask = { 0x7f, 0x7F },
1579 .rx_lvl_offset = 13,
1580 .tx_st_done = 21,
1581};
1582
Sachin Kamat10ce0472012-08-03 10:08:12 +05301583static struct s3c64xx_spi_port_config s5p64x0_spi_port_config = {
Thomas Abrahama5238e32012-07-13 07:15:14 +09001584 .fifo_lvl_mask = { 0x1ff, 0x7F },
1585 .rx_lvl_offset = 15,
1586 .tx_st_done = 25,
1587};
1588
Sachin Kamat10ce0472012-08-03 10:08:12 +05301589static struct s3c64xx_spi_port_config s5pc100_spi_port_config = {
Thomas Abrahama5238e32012-07-13 07:15:14 +09001590 .fifo_lvl_mask = { 0x7f, 0x7F },
1591 .rx_lvl_offset = 13,
1592 .tx_st_done = 21,
1593 .high_speed = true,
1594};
1595
Sachin Kamat10ce0472012-08-03 10:08:12 +05301596static struct s3c64xx_spi_port_config s5pv210_spi_port_config = {
Thomas Abrahama5238e32012-07-13 07:15:14 +09001597 .fifo_lvl_mask = { 0x1ff, 0x7F },
1598 .rx_lvl_offset = 15,
1599 .tx_st_done = 25,
1600 .high_speed = true,
1601};
1602
Sachin Kamat10ce0472012-08-03 10:08:12 +05301603static struct s3c64xx_spi_port_config exynos4_spi_port_config = {
Thomas Abrahama5238e32012-07-13 07:15:14 +09001604 .fifo_lvl_mask = { 0x1ff, 0x7F, 0x7F },
1605 .rx_lvl_offset = 15,
1606 .tx_st_done = 25,
1607 .high_speed = true,
1608 .clk_from_cmu = true,
1609};
1610
Girish K Sbff82032013-06-21 11:26:13 +05301611static struct s3c64xx_spi_port_config exynos5440_spi_port_config = {
1612 .fifo_lvl_mask = { 0x1ff },
1613 .rx_lvl_offset = 15,
1614 .tx_st_done = 25,
1615 .high_speed = true,
1616 .clk_from_cmu = true,
1617 .quirks = S3C64XX_SPI_QUIRK_POLL,
1618};
1619
Thomas Abrahama5238e32012-07-13 07:15:14 +09001620static struct platform_device_id s3c64xx_spi_driver_ids[] = {
1621 {
1622 .name = "s3c2443-spi",
1623 .driver_data = (kernel_ulong_t)&s3c2443_spi_port_config,
1624 }, {
1625 .name = "s3c6410-spi",
1626 .driver_data = (kernel_ulong_t)&s3c6410_spi_port_config,
1627 }, {
1628 .name = "s5p64x0-spi",
1629 .driver_data = (kernel_ulong_t)&s5p64x0_spi_port_config,
1630 }, {
1631 .name = "s5pc100-spi",
1632 .driver_data = (kernel_ulong_t)&s5pc100_spi_port_config,
1633 }, {
1634 .name = "s5pv210-spi",
1635 .driver_data = (kernel_ulong_t)&s5pv210_spi_port_config,
1636 }, {
1637 .name = "exynos4210-spi",
1638 .driver_data = (kernel_ulong_t)&exynos4_spi_port_config,
1639 },
1640 { },
1641};
1642
Thomas Abraham2b908072012-07-13 07:15:15 +09001643static const struct of_device_id s3c64xx_spi_dt_match[] = {
1644 { .compatible = "samsung,exynos4210-spi",
1645 .data = (void *)&exynos4_spi_port_config,
1646 },
Girish K Sbff82032013-06-21 11:26:13 +05301647 { .compatible = "samsung,exynos5440-spi",
1648 .data = (void *)&exynos5440_spi_port_config,
1649 },
Thomas Abraham2b908072012-07-13 07:15:15 +09001650 { },
1651};
1652MODULE_DEVICE_TABLE(of, s3c64xx_spi_dt_match);
Thomas Abraham2b908072012-07-13 07:15:15 +09001653
Jassi Brar230d42d2009-11-30 07:39:42 +00001654static struct platform_driver s3c64xx_spi_driver = {
1655 .driver = {
1656 .name = "s3c64xx-spi",
1657 .owner = THIS_MODULE,
Mark Browne25d0bf2011-12-04 00:36:18 +00001658 .pm = &s3c64xx_spi_pm,
Thomas Abraham2b908072012-07-13 07:15:15 +09001659 .of_match_table = of_match_ptr(s3c64xx_spi_dt_match),
Jassi Brar230d42d2009-11-30 07:39:42 +00001660 },
1661 .remove = s3c64xx_spi_remove,
Thomas Abrahama5238e32012-07-13 07:15:14 +09001662 .id_table = s3c64xx_spi_driver_ids,
Jassi Brar230d42d2009-11-30 07:39:42 +00001663};
1664MODULE_ALIAS("platform:s3c64xx-spi");
1665
1666static int __init s3c64xx_spi_init(void)
1667{
1668 return platform_driver_probe(&s3c64xx_spi_driver, s3c64xx_spi_probe);
1669}
Mark Brownd2a787f2010-09-07 11:29:17 +01001670subsys_initcall(s3c64xx_spi_init);
Jassi Brar230d42d2009-11-30 07:39:42 +00001671
1672static void __exit s3c64xx_spi_exit(void)
1673{
1674 platform_driver_unregister(&s3c64xx_spi_driver);
1675}
1676module_exit(s3c64xx_spi_exit);
1677
1678MODULE_AUTHOR("Jaswinder Singh <jassi.brar@samsung.com>");
1679MODULE_DESCRIPTION("S3C64XX SPI Controller Driver");
1680MODULE_LICENSE("GPL");