blob: e3223ac75a7c57d55e1c56e90dfac2aea877f8d5 [file] [log] [blame]
Stephen Streete0c99052006-03-07 23:53:24 -08001/*
2 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
Mika Westerberga0d26422013-01-22 12:26:32 +02003 * Copyright (C) 2013, Intel Corporation
Stephen Streete0c99052006-03-07 23:53:24 -08004 *
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.
Stephen Streete0c99052006-03-07 23:53:24 -080014 */
15
16#include <linux/init.h>
17#include <linux/module.h>
18#include <linux/device.h>
19#include <linux/ioport.h>
20#include <linux/errno.h>
Sachin Kamatcbfd6a22013-04-08 15:49:33 +053021#include <linux/err.h>
Stephen Streete0c99052006-03-07 23:53:24 -080022#include <linux/interrupt.h>
Andy Shevchenko9df461e2015-03-25 15:06:16 +020023#include <linux/kernel.h>
Stephen Streete0c99052006-03-07 23:53:24 -080024#include <linux/platform_device.h>
Sebastian Andrzej Siewior8348c252010-11-22 17:12:15 -080025#include <linux/spi/pxa2xx_spi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080026#include <linux/spi/spi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080027#include <linux/delay.h>
Eric Miaoa7bb3902009-04-06 19:00:54 -070028#include <linux/gpio.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Mika Westerberg3343b7a2013-01-22 12:26:27 +020030#include <linux/clk.h>
Mika Westerberg7d94a502013-01-22 12:26:30 +020031#include <linux/pm_runtime.h>
Mika Westerberga3496852013-01-22 12:26:33 +020032#include <linux/acpi.h>
Stephen Streete0c99052006-03-07 23:53:24 -080033
Mika Westerbergcd7bed02013-01-22 12:26:28 +020034#include "spi-pxa2xx.h"
Stephen Streete0c99052006-03-07 23:53:24 -080035
36MODULE_AUTHOR("Stephen Street");
Will Newton037cdaf2007-12-10 15:49:25 -080037MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
Stephen Streete0c99052006-03-07 23:53:24 -080038MODULE_LICENSE("GPL");
Kay Sievers7e38c3c2008-04-10 21:29:20 -070039MODULE_ALIAS("platform:pxa2xx-spi");
Stephen Streete0c99052006-03-07 23:53:24 -080040
Vernon Sauderf1f640a2008-10-15 22:02:43 -070041#define TIMOUT_DFLT 1000
42
Ned Forresterb97c74b2008-02-23 15:23:40 -080043/*
44 * for testing SSCR1 changes that require SSP restart, basically
45 * everything except the service and interrupt enables, the pxa270 developer
46 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
47 * list, but the PXA255 dev man says all bits without really meaning the
48 * service and interrupt enables
49 */
50#define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
Stephen Street8d94cc52006-12-10 02:18:54 -080051 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
Ned Forresterb97c74b2008-02-23 15:23:40 -080052 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
53 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
54 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
55 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
Stephen Street8d94cc52006-12-10 02:18:54 -080056
Weike Chene5262d02014-11-26 02:35:10 -080057#define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
58 | QUARK_X1000_SSCR1_EFWR \
59 | QUARK_X1000_SSCR1_RFT \
60 | QUARK_X1000_SSCR1_TFT \
61 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
62
Mika Westerberga0d26422013-01-22 12:26:32 +020063#define LPSS_RX_THRESH_DFLT 64
64#define LPSS_TX_LOTHRESH_DFLT 160
65#define LPSS_TX_HITHRESH_DFLT 224
66
67/* Offset from drv_data->lpss_base */
Mika Westerberg1de70612013-07-03 13:25:06 +030068#define GENERAL_REG 0x08
69#define GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
Mika Westerberg0054e282013-03-05 12:05:17 +020070#define SSP_REG 0x0c
Mika Westerberga0d26422013-01-22 12:26:32 +020071#define SPI_CS_CONTROL 0x18
72#define SPI_CS_CONTROL_SW_MODE BIT(0)
73#define SPI_CS_CONTROL_CS_HIGH BIT(1)
74
75static bool is_lpss_ssp(const struct driver_data *drv_data)
76{
77 return drv_data->ssp_type == LPSS_SSP;
78}
79
Weike Chene5262d02014-11-26 02:35:10 -080080static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
81{
82 return drv_data->ssp_type == QUARK_X1000_SSP;
83}
84
Weike Chen4fdb2422014-10-08 08:50:22 -070085static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
86{
87 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -080088 case QUARK_X1000_SSP:
89 return QUARK_X1000_SSCR1_CHANGE_MASK;
Weike Chen4fdb2422014-10-08 08:50:22 -070090 default:
91 return SSCR1_CHANGE_MASK;
92 }
93}
94
95static u32
96pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
97{
98 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -080099 case QUARK_X1000_SSP:
100 return RX_THRESH_QUARK_X1000_DFLT;
Weike Chen4fdb2422014-10-08 08:50:22 -0700101 default:
102 return RX_THRESH_DFLT;
103 }
104}
105
106static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
107{
Weike Chen4fdb2422014-10-08 08:50:22 -0700108 u32 mask;
109
110 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800111 case QUARK_X1000_SSP:
112 mask = QUARK_X1000_SSSR_TFL_MASK;
113 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700114 default:
115 mask = SSSR_TFL_MASK;
116 break;
117 }
118
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200119 return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
Weike Chen4fdb2422014-10-08 08:50:22 -0700120}
121
122static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
123 u32 *sccr1_reg)
124{
125 u32 mask;
126
127 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800128 case QUARK_X1000_SSP:
129 mask = QUARK_X1000_SSCR1_RFT;
130 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700131 default:
132 mask = SSCR1_RFT;
133 break;
134 }
135 *sccr1_reg &= ~mask;
136}
137
138static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
139 u32 *sccr1_reg, u32 threshold)
140{
141 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800142 case QUARK_X1000_SSP:
143 *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
144 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700145 default:
146 *sccr1_reg |= SSCR1_RxTresh(threshold);
147 break;
148 }
149}
150
151static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
152 u32 clk_div, u8 bits)
153{
154 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800155 case QUARK_X1000_SSP:
156 return clk_div
157 | QUARK_X1000_SSCR0_Motorola
158 | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
159 | SSCR0_SSE;
Weike Chen4fdb2422014-10-08 08:50:22 -0700160 default:
161 return clk_div
162 | SSCR0_Motorola
163 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
164 | SSCR0_SSE
165 | (bits > 16 ? SSCR0_EDSS : 0);
166 }
167}
168
Mika Westerberga0d26422013-01-22 12:26:32 +0200169/*
170 * Read and write LPSS SSP private registers. Caller must first check that
171 * is_lpss_ssp() returns true before these can be called.
172 */
173static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
174{
175 WARN_ON(!drv_data->lpss_base);
176 return readl(drv_data->lpss_base + offset);
177}
178
179static void __lpss_ssp_write_priv(struct driver_data *drv_data,
180 unsigned offset, u32 value)
181{
182 WARN_ON(!drv_data->lpss_base);
183 writel(value, drv_data->lpss_base + offset);
184}
185
186/*
187 * lpss_ssp_setup - perform LPSS SSP specific setup
188 * @drv_data: pointer to the driver private data
189 *
190 * Perform LPSS SSP specific setup. This function must be called first if
191 * one is going to use LPSS SSP private registers.
192 */
193static void lpss_ssp_setup(struct driver_data *drv_data)
194{
195 unsigned offset = 0x400;
196 u32 value, orig;
197
Mika Westerberga0d26422013-01-22 12:26:32 +0200198 /*
199 * Perform auto-detection of the LPSS SSP private registers. They
200 * can be either at 1k or 2k offset from the base address.
201 */
202 orig = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
203
Chew, Chiau Eee61f4872014-06-13 23:57:25 +0800204 /* Test SPI_CS_CONTROL_SW_MODE bit enabling */
Mika Westerberga0d26422013-01-22 12:26:32 +0200205 value = orig | SPI_CS_CONTROL_SW_MODE;
206 writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL);
207 value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
208 if (value != (orig | SPI_CS_CONTROL_SW_MODE)) {
209 offset = 0x800;
210 goto detection_done;
211 }
212
Chew, Chiau Eee61f4872014-06-13 23:57:25 +0800213 orig = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
214
215 /* Test SPI_CS_CONTROL_SW_MODE bit disabling */
216 value = orig & ~SPI_CS_CONTROL_SW_MODE;
Mika Westerberga0d26422013-01-22 12:26:32 +0200217 writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL);
218 value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
Chew, Chiau Eee61f4872014-06-13 23:57:25 +0800219 if (value != (orig & ~SPI_CS_CONTROL_SW_MODE)) {
Mika Westerberga0d26422013-01-22 12:26:32 +0200220 offset = 0x800;
221 goto detection_done;
222 }
223
224detection_done:
225 /* Now set the LPSS base */
226 drv_data->lpss_base = drv_data->ioaddr + offset;
227
228 /* Enable software chip select control */
229 value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH;
230 __lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value);
Mika Westerberg0054e282013-03-05 12:05:17 +0200231
232 /* Enable multiblock DMA transfers */
Mika Westerberg1de70612013-07-03 13:25:06 +0300233 if (drv_data->master_info->enable_dma) {
Mika Westerberg0054e282013-03-05 12:05:17 +0200234 __lpss_ssp_write_priv(drv_data, SSP_REG, 1);
Mika Westerberg1de70612013-07-03 13:25:06 +0300235
236 value = __lpss_ssp_read_priv(drv_data, GENERAL_REG);
237 value |= GENERAL_REG_RXTO_HOLDOFF_DISABLE;
238 __lpss_ssp_write_priv(drv_data, GENERAL_REG, value);
239 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200240}
241
242static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
243{
244 u32 value;
245
Mika Westerberga0d26422013-01-22 12:26:32 +0200246 value = __lpss_ssp_read_priv(drv_data, SPI_CS_CONTROL);
247 if (enable)
248 value &= ~SPI_CS_CONTROL_CS_HIGH;
249 else
250 value |= SPI_CS_CONTROL_CS_HIGH;
251 __lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value);
252}
253
Eric Miaoa7bb3902009-04-06 19:00:54 -0700254static void cs_assert(struct driver_data *drv_data)
255{
256 struct chip_data *chip = drv_data->cur_chip;
257
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800258 if (drv_data->ssp_type == CE4100_SSP) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200259 pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800260 return;
261 }
262
Eric Miaoa7bb3902009-04-06 19:00:54 -0700263 if (chip->cs_control) {
264 chip->cs_control(PXA2XX_CS_ASSERT);
265 return;
266 }
267
Mika Westerberga0d26422013-01-22 12:26:32 +0200268 if (gpio_is_valid(chip->gpio_cs)) {
Eric Miaoa7bb3902009-04-06 19:00:54 -0700269 gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200270 return;
271 }
272
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200273 if (is_lpss_ssp(drv_data))
274 lpss_ssp_cs_control(drv_data, true);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700275}
276
277static void cs_deassert(struct driver_data *drv_data)
278{
279 struct chip_data *chip = drv_data->cur_chip;
280
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800281 if (drv_data->ssp_type == CE4100_SSP)
282 return;
283
Eric Miaoa7bb3902009-04-06 19:00:54 -0700284 if (chip->cs_control) {
Daniel Ribeiro2b2562d2009-04-08 22:48:03 -0300285 chip->cs_control(PXA2XX_CS_DEASSERT);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700286 return;
287 }
288
Mika Westerberga0d26422013-01-22 12:26:32 +0200289 if (gpio_is_valid(chip->gpio_cs)) {
Eric Miaoa7bb3902009-04-06 19:00:54 -0700290 gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200291 return;
292 }
293
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200294 if (is_lpss_ssp(drv_data))
295 lpss_ssp_cs_control(drv_data, false);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700296}
297
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200298int pxa2xx_spi_flush(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800299{
300 unsigned long limit = loops_per_jiffy << 1;
301
Stephen Streete0c99052006-03-07 23:53:24 -0800302 do {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200303 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
304 pxa2xx_spi_read(drv_data, SSDR);
305 } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800306 write_SSSR_CS(drv_data, SSSR_ROR);
Stephen Streete0c99052006-03-07 23:53:24 -0800307
308 return limit;
309}
310
Stephen Street8d94cc52006-12-10 02:18:54 -0800311static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800312{
Stephen Street9708c122006-03-28 14:05:23 -0800313 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800314
Weike Chen4fdb2422014-10-08 08:50:22 -0700315 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800316 || (drv_data->tx == drv_data->tx_end))
317 return 0;
318
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200319 pxa2xx_spi_write(drv_data, SSDR, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800320 drv_data->tx += n_bytes;
321
322 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800323}
324
Stephen Street8d94cc52006-12-10 02:18:54 -0800325static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800326{
Stephen Street9708c122006-03-28 14:05:23 -0800327 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800328
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200329 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
330 && (drv_data->rx < drv_data->rx_end)) {
331 pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800332 drv_data->rx += n_bytes;
333 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800334
335 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800336}
337
Stephen Street8d94cc52006-12-10 02:18:54 -0800338static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800339{
Weike Chen4fdb2422014-10-08 08:50:22 -0700340 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800341 || (drv_data->tx == drv_data->tx_end))
342 return 0;
343
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200344 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800345 ++drv_data->tx;
346
347 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800348}
349
Stephen Street8d94cc52006-12-10 02:18:54 -0800350static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800351{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200352 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
353 && (drv_data->rx < drv_data->rx_end)) {
354 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800355 ++drv_data->rx;
356 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800357
358 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800359}
360
Stephen Street8d94cc52006-12-10 02:18:54 -0800361static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800362{
Weike Chen4fdb2422014-10-08 08:50:22 -0700363 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800364 || (drv_data->tx == drv_data->tx_end))
365 return 0;
366
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200367 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800368 drv_data->tx += 2;
369
370 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800371}
372
Stephen Street8d94cc52006-12-10 02:18:54 -0800373static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800374{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200375 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
376 && (drv_data->rx < drv_data->rx_end)) {
377 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800378 drv_data->rx += 2;
379 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800380
381 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800382}
Stephen Street8d94cc52006-12-10 02:18:54 -0800383
384static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800385{
Weike Chen4fdb2422014-10-08 08:50:22 -0700386 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800387 || (drv_data->tx == drv_data->tx_end))
388 return 0;
389
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200390 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800391 drv_data->tx += 4;
392
393 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800394}
395
Stephen Street8d94cc52006-12-10 02:18:54 -0800396static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800397{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200398 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
399 && (drv_data->rx < drv_data->rx_end)) {
400 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800401 drv_data->rx += 4;
402 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800403
404 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800405}
406
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200407void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800408{
409 struct spi_message *msg = drv_data->cur_msg;
410 struct spi_transfer *trans = drv_data->cur_transfer;
411
412 /* Move to next transfer */
413 if (trans->transfer_list.next != &msg->transfers) {
414 drv_data->cur_transfer =
415 list_entry(trans->transfer_list.next,
416 struct spi_transfer,
417 transfer_list);
418 return RUNNING_STATE;
419 } else
420 return DONE_STATE;
421}
422
Stephen Streete0c99052006-03-07 23:53:24 -0800423/* caller already set message->status; dma and pio irqs are blocked */
Stephen Street5daa3ba2006-05-20 15:00:19 -0700424static void giveback(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800425{
426 struct spi_transfer* last_transfer;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700427 struct spi_message *msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800428
Stephen Street5daa3ba2006-05-20 15:00:19 -0700429 msg = drv_data->cur_msg;
430 drv_data->cur_msg = NULL;
431 drv_data->cur_transfer = NULL;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700432
Axel Lin23e2c2a2014-02-12 22:13:27 +0800433 last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
Stephen Streete0c99052006-03-07 23:53:24 -0800434 transfer_list);
435
Ned Forrester84235972008-09-13 02:33:17 -0700436 /* Delay if requested before any change in chip select */
437 if (last_transfer->delay_usecs)
438 udelay(last_transfer->delay_usecs);
439
440 /* Drop chip select UNLESS cs_change is true or we are returning
441 * a message with an error, or next message is for another chip
442 */
Stephen Streete0c99052006-03-07 23:53:24 -0800443 if (!last_transfer->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700444 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700445 else {
446 struct spi_message *next_msg;
447
448 /* Holding of cs was hinted, but we need to make sure
449 * the next message is for the same chip. Don't waste
450 * time with the following tests unless this was hinted.
451 *
452 * We cannot postpone this until pump_messages, because
453 * after calling msg->complete (below) the driver that
454 * sent the current message could be unloaded, which
455 * could invalidate the cs_control() callback...
456 */
457
458 /* get a pointer to the next message, if any */
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200459 next_msg = spi_get_next_queued_message(drv_data->master);
Ned Forrester84235972008-09-13 02:33:17 -0700460
461 /* see if the next and current messages point
462 * to the same chip
463 */
464 if (next_msg && next_msg->spi != msg->spi)
465 next_msg = NULL;
466 if (!next_msg || msg->state == ERROR_STATE)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700467 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700468 }
Stephen Streete0c99052006-03-07 23:53:24 -0800469
Eric Miaoa7bb3902009-04-06 19:00:54 -0700470 drv_data->cur_chip = NULL;
Mika Westerbergc957e8f2014-12-29 10:33:36 +0200471 spi_finalize_current_message(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -0800472}
473
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800474static void reset_sccr1(struct driver_data *drv_data)
475{
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800476 struct chip_data *chip = drv_data->cur_chip;
477 u32 sccr1_reg;
478
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200479 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800480 sccr1_reg &= ~SSCR1_RFT;
481 sccr1_reg |= chip->threshold;
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200482 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800483}
484
Stephen Street8d94cc52006-12-10 02:18:54 -0800485static void int_error_stop(struct driver_data *drv_data, const char* msg)
486{
Stephen Street8d94cc52006-12-10 02:18:54 -0800487 /* Stop and reset SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800488 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800489 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800490 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200491 pxa2xx_spi_write(drv_data, SSTO, 0);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200492 pxa2xx_spi_flush(drv_data);
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200493 pxa2xx_spi_write(drv_data, SSCR0,
494 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
Stephen Street8d94cc52006-12-10 02:18:54 -0800495
496 dev_err(&drv_data->pdev->dev, "%s\n", msg);
497
498 drv_data->cur_msg->state = ERROR_STATE;
499 tasklet_schedule(&drv_data->pump_transfers);
500}
501
502static void int_transfer_complete(struct driver_data *drv_data)
503{
Stephen Street8d94cc52006-12-10 02:18:54 -0800504 /* Stop SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800505 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800506 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800507 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200508 pxa2xx_spi_write(drv_data, SSTO, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800509
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300510 /* Update total byte transferred return count actual bytes read */
Stephen Street8d94cc52006-12-10 02:18:54 -0800511 drv_data->cur_msg->actual_length += drv_data->len -
512 (drv_data->rx_end - drv_data->rx);
513
Ned Forrester84235972008-09-13 02:33:17 -0700514 /* Transfer delays and chip select release are
515 * handled in pump_transfers or giveback
516 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800517
518 /* Move to next transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200519 drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800520
521 /* Schedule transfer tasklet */
522 tasklet_schedule(&drv_data->pump_transfers);
523}
524
Stephen Streete0c99052006-03-07 23:53:24 -0800525static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
526{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200527 u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
528 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Street8d94cc52006-12-10 02:18:54 -0800529
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200530 u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800531
Stephen Street8d94cc52006-12-10 02:18:54 -0800532 if (irq_status & SSSR_ROR) {
533 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
534 return IRQ_HANDLED;
535 }
Stephen Streete0c99052006-03-07 23:53:24 -0800536
Stephen Street8d94cc52006-12-10 02:18:54 -0800537 if (irq_status & SSSR_TINT) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200538 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
Stephen Street8d94cc52006-12-10 02:18:54 -0800539 if (drv_data->read(drv_data)) {
540 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800541 return IRQ_HANDLED;
542 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800543 }
Stephen Streete0c99052006-03-07 23:53:24 -0800544
Stephen Street8d94cc52006-12-10 02:18:54 -0800545 /* Drain rx fifo, Fill tx fifo and prevent overruns */
546 do {
547 if (drv_data->read(drv_data)) {
548 int_transfer_complete(drv_data);
549 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800550 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800551 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800552
Stephen Street8d94cc52006-12-10 02:18:54 -0800553 if (drv_data->read(drv_data)) {
554 int_transfer_complete(drv_data);
555 return IRQ_HANDLED;
556 }
Stephen Streete0c99052006-03-07 23:53:24 -0800557
Stephen Street8d94cc52006-12-10 02:18:54 -0800558 if (drv_data->tx == drv_data->tx_end) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800559 u32 bytes_left;
560 u32 sccr1_reg;
561
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200562 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800563 sccr1_reg &= ~SSCR1_TIE;
564
565 /*
566 * PXA25x_SSP has no timeout, set up rx threshould for the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300567 * remaining RX bytes.
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800568 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800569 if (pxa25x_ssp_comp(drv_data)) {
Weike Chen4fdb2422014-10-08 08:50:22 -0700570 u32 rx_thre;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800571
Weike Chen4fdb2422014-10-08 08:50:22 -0700572 pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800573
574 bytes_left = drv_data->rx_end - drv_data->rx;
575 switch (drv_data->n_bytes) {
576 case 4:
577 bytes_left >>= 1;
578 case 2:
579 bytes_left >>= 1;
Stephen Street8d94cc52006-12-10 02:18:54 -0800580 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800581
Weike Chen4fdb2422014-10-08 08:50:22 -0700582 rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
583 if (rx_thre > bytes_left)
584 rx_thre = bytes_left;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800585
Weike Chen4fdb2422014-10-08 08:50:22 -0700586 pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
Stephen Streete0c99052006-03-07 23:53:24 -0800587 }
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200588 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800589 }
590
Stephen Street5daa3ba2006-05-20 15:00:19 -0700591 /* We did something */
592 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800593}
594
David Howells7d12e782006-10-05 14:55:46 +0100595static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800596{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400597 struct driver_data *drv_data = dev_id;
Mika Westerberg7d94a502013-01-22 12:26:30 +0200598 u32 sccr1_reg;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800599 u32 mask = drv_data->mask_sr;
600 u32 status;
601
Mika Westerberg7d94a502013-01-22 12:26:30 +0200602 /*
603 * The IRQ might be shared with other peripherals so we must first
604 * check that are we RPM suspended or not. If we are we assume that
605 * the IRQ was not for us (we shouldn't be RPM suspended when the
606 * interrupt is enabled).
607 */
608 if (pm_runtime_suspended(&drv_data->pdev->dev))
609 return IRQ_NONE;
610
Mika Westerberg269e4a42013-09-04 13:37:43 +0300611 /*
612 * If the device is not yet in RPM suspended state and we get an
613 * interrupt that is meant for another device, check if status bits
614 * are all set to one. That means that the device is already
615 * powered off.
616 */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200617 status = pxa2xx_spi_read(drv_data, SSSR);
Mika Westerberg269e4a42013-09-04 13:37:43 +0300618 if (status == ~0)
619 return IRQ_NONE;
620
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200621 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800622
623 /* Ignore possible writes if we don't need to write */
624 if (!(sccr1_reg & SSCR1_TIE))
625 mask &= ~SSSR_TFS;
626
627 if (!(status & mask))
628 return IRQ_NONE;
Stephen Streete0c99052006-03-07 23:53:24 -0800629
630 if (!drv_data->cur_msg) {
Stephen Street5daa3ba2006-05-20 15:00:19 -0700631
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200632 pxa2xx_spi_write(drv_data, SSCR0,
633 pxa2xx_spi_read(drv_data, SSCR0)
634 & ~SSCR0_SSE);
635 pxa2xx_spi_write(drv_data, SSCR1,
636 pxa2xx_spi_read(drv_data, SSCR1)
637 & ~drv_data->int_cr1);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800638 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200639 pxa2xx_spi_write(drv_data, SSTO, 0);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800640 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street5daa3ba2006-05-20 15:00:19 -0700641
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300642 dev_err(&drv_data->pdev->dev,
643 "bad message state in interrupt handler\n");
Stephen Street5daa3ba2006-05-20 15:00:19 -0700644
Stephen Streete0c99052006-03-07 23:53:24 -0800645 /* Never fail */
646 return IRQ_HANDLED;
647 }
648
649 return drv_data->transfer_handler(drv_data);
650}
651
Weike Chene5262d02014-11-26 02:35:10 -0800652/*
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200653 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
654 * input frequency by fractions of 2^24. It also has a divider by 5.
655 *
656 * There are formulas to get baud rate value for given input frequency and
657 * divider parameters, such as DDS_CLK_RATE and SCR:
658 *
659 * Fsys = 200MHz
660 *
661 * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
662 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
663 *
664 * DDS_CLK_RATE either 2^n or 2^n / 5.
665 * SCR is in range 0 .. 255
666 *
667 * Divisor = 5^i * 2^j * 2 * k
668 * i = [0, 1] i = 1 iff j = 0 or j > 3
669 * j = [0, 23] j = 0 iff i = 1
670 * k = [1, 256]
671 * Special case: j = 0, i = 1: Divisor = 2 / 5
672 *
673 * Accordingly to the specification the recommended values for DDS_CLK_RATE
674 * are:
675 * Case 1: 2^n, n = [0, 23]
676 * Case 2: 2^24 * 2 / 5 (0x666666)
677 * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
678 *
679 * In all cases the lowest possible value is better.
680 *
681 * The function calculates parameters for all cases and chooses the one closest
682 * to the asked baud rate.
Weike Chene5262d02014-11-26 02:35:10 -0800683 */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200684static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
Weike Chene5262d02014-11-26 02:35:10 -0800685{
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200686 unsigned long xtal = 200000000;
687 unsigned long fref = xtal / 2; /* mandatory division by 2,
688 see (2) */
689 /* case 3 */
690 unsigned long fref1 = fref / 2; /* case 1 */
691 unsigned long fref2 = fref * 2 / 5; /* case 2 */
692 unsigned long scale;
693 unsigned long q, q1, q2;
694 long r, r1, r2;
695 u32 mul;
Weike Chene5262d02014-11-26 02:35:10 -0800696
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200697 /* Case 1 */
698
699 /* Set initial value for DDS_CLK_RATE */
700 mul = (1 << 24) >> 1;
701
702 /* Calculate initial quot */
703 q1 = DIV_ROUND_CLOSEST(fref1, rate);
704
705 /* Scale q1 if it's too big */
706 if (q1 > 256) {
707 /* Scale q1 to range [1, 512] */
708 scale = fls_long(q1 - 1);
709 if (scale > 9) {
710 q1 >>= scale - 9;
711 mul >>= scale - 9;
712 }
713
714 /* Round the result if we have a remainder */
715 q1 += q1 & 1;
716 }
717
718 /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
719 scale = __ffs(q1);
720 q1 >>= scale;
721 mul >>= scale;
722
723 /* Get the remainder */
724 r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
725
726 /* Case 2 */
727
728 q2 = DIV_ROUND_CLOSEST(fref2, rate);
729 r2 = abs(fref2 / q2 - rate);
730
731 /*
732 * Choose the best between two: less remainder we have the better. We
733 * can't go case 2 if q2 is greater than 256 since SCR register can
734 * hold only values 0 .. 255.
735 */
736 if (r2 >= r1 || q2 > 256) {
737 /* case 1 is better */
738 r = r1;
739 q = q1;
740 } else {
741 /* case 2 is better */
742 r = r2;
743 q = q2;
744 mul = (1 << 24) * 2 / 5;
745 }
746
747 /* Check case 3 only If the divisor is big enough */
748 if (fref / rate >= 80) {
749 u64 fssp;
750 u32 m;
751
752 /* Calculate initial quot */
753 q1 = DIV_ROUND_CLOSEST(fref, rate);
754 m = (1 << 24) / q1;
755
756 /* Get the remainder */
757 fssp = (u64)fref * m;
758 do_div(fssp, 1 << 24);
759 r1 = abs(fssp - rate);
760
761 /* Choose this one if it suits better */
762 if (r1 < r) {
763 /* case 3 is better */
764 q = 1;
765 mul = m;
Weike Chene5262d02014-11-26 02:35:10 -0800766 }
767 }
768
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200769 *dds = mul;
770 return q - 1;
Weike Chene5262d02014-11-26 02:35:10 -0800771}
772
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200773static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
eric miao2f1a74e2007-11-21 18:50:53 +0800774{
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200775 unsigned long ssp_clk = drv_data->max_clk_rate;
776 const struct ssp_device *ssp = drv_data->ssp;
777
778 rate = min_t(int, ssp_clk, rate);
eric miao2f1a74e2007-11-21 18:50:53 +0800779
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800780 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200781 return (ssp_clk / (2 * rate) - 1) & 0xff;
eric miao2f1a74e2007-11-21 18:50:53 +0800782 else
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200783 return (ssp_clk / rate - 1) & 0xfff;
eric miao2f1a74e2007-11-21 18:50:53 +0800784}
785
Weike Chene5262d02014-11-26 02:35:10 -0800786static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
787 struct chip_data *chip, int rate)
788{
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200789 unsigned int clk_div;
Weike Chene5262d02014-11-26 02:35:10 -0800790
791 switch (drv_data->ssp_type) {
792 case QUARK_X1000_SSP:
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200793 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300794 break;
Weike Chene5262d02014-11-26 02:35:10 -0800795 default:
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200796 clk_div = ssp_get_clk_div(drv_data, rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300797 break;
Weike Chene5262d02014-11-26 02:35:10 -0800798 }
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200799 return clk_div << 8;
Weike Chene5262d02014-11-26 02:35:10 -0800800}
801
Stephen Streete0c99052006-03-07 23:53:24 -0800802static void pump_transfers(unsigned long data)
803{
804 struct driver_data *drv_data = (struct driver_data *)data;
805 struct spi_message *message = NULL;
806 struct spi_transfer *transfer = NULL;
807 struct spi_transfer *previous = NULL;
808 struct chip_data *chip = NULL;
Stephen Street9708c122006-03-28 14:05:23 -0800809 u32 clk_div = 0;
810 u8 bits = 0;
811 u32 speed = 0;
812 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800813 u32 cr1;
814 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
815 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
Weike Chen4fdb2422014-10-08 08:50:22 -0700816 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800817
818 /* Get current state information */
819 message = drv_data->cur_msg;
820 transfer = drv_data->cur_transfer;
821 chip = drv_data->cur_chip;
822
823 /* Handle for abort */
824 if (message->state == ERROR_STATE) {
825 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700826 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800827 return;
828 }
829
830 /* Handle end of message */
831 if (message->state == DONE_STATE) {
832 message->status = 0;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700833 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800834 return;
835 }
836
Ned Forrester84235972008-09-13 02:33:17 -0700837 /* Delay if requested at end of transfer before CS change */
Stephen Streete0c99052006-03-07 23:53:24 -0800838 if (message->state == RUNNING_STATE) {
839 previous = list_entry(transfer->transfer_list.prev,
840 struct spi_transfer,
841 transfer_list);
842 if (previous->delay_usecs)
843 udelay(previous->delay_usecs);
Ned Forrester84235972008-09-13 02:33:17 -0700844
845 /* Drop chip select only if cs_change is requested */
846 if (previous->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700847 cs_deassert(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800848 }
849
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200850 /* Check if we can DMA this transfer */
851 if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
Ned Forrester7e964452008-09-13 02:33:18 -0700852
853 /* reject already-mapped transfers; PIO won't always work */
854 if (message->is_dma_mapped
855 || transfer->rx_dma || transfer->tx_dma) {
856 dev_err(&drv_data->pdev->dev,
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300857 "pump_transfers: mapped transfer length of "
858 "%u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700859 transfer->len, MAX_DMA_LEN);
860 message->status = -EINVAL;
861 giveback(drv_data);
862 return;
863 }
864
865 /* warn ... we force this to PIO mode */
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300866 dev_warn_ratelimited(&message->spi->dev,
867 "pump_transfers: DMA disabled for transfer length %ld "
868 "greater than %d\n",
869 (long)drv_data->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800870 }
871
Stephen Streete0c99052006-03-07 23:53:24 -0800872 /* Setup the transfer state based on the type of transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200873 if (pxa2xx_spi_flush(drv_data) == 0) {
Stephen Streete0c99052006-03-07 23:53:24 -0800874 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
875 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700876 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800877 return;
878 }
Stephen Street9708c122006-03-28 14:05:23 -0800879 drv_data->n_bytes = chip->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800880 drv_data->tx = (void *)transfer->tx_buf;
881 drv_data->tx_end = drv_data->tx + transfer->len;
882 drv_data->rx = transfer->rx_buf;
883 drv_data->rx_end = drv_data->rx + transfer->len;
884 drv_data->rx_dma = transfer->rx_dma;
885 drv_data->tx_dma = transfer->tx_dma;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200886 drv_data->len = transfer->len;
Stephen Streete0c99052006-03-07 23:53:24 -0800887 drv_data->write = drv_data->tx ? chip->write : null_writer;
888 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -0800889
890 /* Change speed and bit per word on a per transfer */
Stephen Street8d94cc52006-12-10 02:18:54 -0800891 cr0 = chip->cr0;
Stephen Street9708c122006-03-28 14:05:23 -0800892 if (transfer->speed_hz || transfer->bits_per_word) {
893
Stephen Street9708c122006-03-28 14:05:23 -0800894 bits = chip->bits_per_word;
895 speed = chip->speed_hz;
896
897 if (transfer->speed_hz)
898 speed = transfer->speed_hz;
899
900 if (transfer->bits_per_word)
901 bits = transfer->bits_per_word;
902
Weike Chene5262d02014-11-26 02:35:10 -0800903 clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, speed);
Stephen Street9708c122006-03-28 14:05:23 -0800904
905 if (bits <= 8) {
906 drv_data->n_bytes = 1;
Stephen Street9708c122006-03-28 14:05:23 -0800907 drv_data->read = drv_data->read != null_reader ?
908 u8_reader : null_reader;
909 drv_data->write = drv_data->write != null_writer ?
910 u8_writer : null_writer;
911 } else if (bits <= 16) {
912 drv_data->n_bytes = 2;
Stephen Street9708c122006-03-28 14:05:23 -0800913 drv_data->read = drv_data->read != null_reader ?
914 u16_reader : null_reader;
915 drv_data->write = drv_data->write != null_writer ?
916 u16_writer : null_writer;
917 } else if (bits <= 32) {
918 drv_data->n_bytes = 4;
Stephen Street9708c122006-03-28 14:05:23 -0800919 drv_data->read = drv_data->read != null_reader ?
920 u32_reader : null_reader;
921 drv_data->write = drv_data->write != null_writer ?
922 u32_writer : null_writer;
923 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800924 /* if bits/word is changed in dma mode, then must check the
925 * thresholds and burst also */
926 if (chip->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200927 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
928 message->spi,
Stephen Street8d94cc52006-12-10 02:18:54 -0800929 bits, &dma_burst,
930 &dma_thresh))
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300931 dev_warn_ratelimited(&message->spi->dev,
932 "pump_transfers: DMA burst size reduced to match bits_per_word\n");
Stephen Street8d94cc52006-12-10 02:18:54 -0800933 }
Stephen Street9708c122006-03-28 14:05:23 -0800934
Weike Chen4fdb2422014-10-08 08:50:22 -0700935 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
Stephen Street9708c122006-03-28 14:05:23 -0800936 }
937
Stephen Streete0c99052006-03-07 23:53:24 -0800938 message->state = RUNNING_STATE;
939
Ned Forrester7e964452008-09-13 02:33:18 -0700940 drv_data->dma_mapped = 0;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200941 if (pxa2xx_spi_dma_is_possible(drv_data->len))
942 drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
Ned Forrester7e964452008-09-13 02:33:18 -0700943 if (drv_data->dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -0800944
945 /* Ensure we have the correct interrupt handler */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200946 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
Stephen Streete0c99052006-03-07 23:53:24 -0800947
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200948 pxa2xx_spi_dma_prepare(drv_data, dma_burst);
Stephen Streete0c99052006-03-07 23:53:24 -0800949
Stephen Street8d94cc52006-12-10 02:18:54 -0800950 /* Clear status and start DMA engine */
951 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200952 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200953
954 pxa2xx_spi_dma_start(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800955 } else {
956 /* Ensure we have the correct interrupt handler */
957 drv_data->transfer_handler = interrupt_transfer;
958
Stephen Street8d94cc52006-12-10 02:18:54 -0800959 /* Clear status */
960 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800961 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -0800962 }
963
Mika Westerberga0d26422013-01-22 12:26:32 +0200964 if (is_lpss_ssp(drv_data)) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200965 if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
966 != chip->lpss_rx_threshold)
967 pxa2xx_spi_write(drv_data, SSIRF,
968 chip->lpss_rx_threshold);
969 if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
970 != chip->lpss_tx_threshold)
971 pxa2xx_spi_write(drv_data, SSITF,
972 chip->lpss_tx_threshold);
Mika Westerberga0d26422013-01-22 12:26:32 +0200973 }
974
Weike Chene5262d02014-11-26 02:35:10 -0800975 if (is_quark_x1000_ssp(drv_data) &&
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200976 (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
977 pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
Weike Chene5262d02014-11-26 02:35:10 -0800978
Stephen Street8d94cc52006-12-10 02:18:54 -0800979 /* see if we need to reload the config registers */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200980 if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
981 || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
982 != (cr1 & change_mask)) {
Ned Forresterb97c74b2008-02-23 15:23:40 -0800983 /* stop the SSP, and update the other bits */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200984 pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800985 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200986 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800987 /* first set CR1 without interrupt and service enables */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200988 pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800989 /* restart the SSP */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200990 pxa2xx_spi_write(drv_data, SSCR0, cr0);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800991
Stephen Street8d94cc52006-12-10 02:18:54 -0800992 } else {
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800993 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200994 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
Stephen Streete0c99052006-03-07 23:53:24 -0800995 }
Ned Forresterb97c74b2008-02-23 15:23:40 -0800996
Eric Miaoa7bb3902009-04-06 19:00:54 -0700997 cs_assert(drv_data);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800998
999 /* after chip select, release the data by enabling service
1000 * requests and interrupts, without changing any mode bits */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001001 pxa2xx_spi_write(drv_data, SSCR1, cr1);
Stephen Streete0c99052006-03-07 23:53:24 -08001002}
1003
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001004static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
1005 struct spi_message *msg)
Stephen Streete0c99052006-03-07 23:53:24 -08001006{
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001007 struct driver_data *drv_data = spi_master_get_devdata(master);
Stephen Streete0c99052006-03-07 23:53:24 -08001008
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001009 drv_data->cur_msg = msg;
Stephen Streete0c99052006-03-07 23:53:24 -08001010 /* Initial message state*/
1011 drv_data->cur_msg->state = START_STATE;
1012 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1013 struct spi_transfer,
1014 transfer_list);
1015
Stephen Street8d94cc52006-12-10 02:18:54 -08001016 /* prepare to setup the SSP, in pump_transfers, using the per
1017 * chip configuration */
Stephen Streete0c99052006-03-07 23:53:24 -08001018 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001019
1020 /* Mark as busy and launch transfers */
1021 tasklet_schedule(&drv_data->pump_transfers);
Stephen Streete0c99052006-03-07 23:53:24 -08001022 return 0;
1023}
1024
Mika Westerberg7d94a502013-01-22 12:26:30 +02001025static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
1026{
1027 struct driver_data *drv_data = spi_master_get_devdata(master);
1028
1029 /* Disable the SSP now */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001030 pxa2xx_spi_write(drv_data, SSCR0,
1031 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
Mika Westerberg7d94a502013-01-22 12:26:30 +02001032
Mika Westerberg7d94a502013-01-22 12:26:30 +02001033 return 0;
1034}
1035
Eric Miaoa7bb3902009-04-06 19:00:54 -07001036static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1037 struct pxa2xx_spi_chip *chip_info)
1038{
1039 int err = 0;
1040
1041 if (chip == NULL || chip_info == NULL)
1042 return 0;
1043
1044 /* NOTE: setup() can be called multiple times, possibly with
1045 * different chip_info, release previously requested GPIO
1046 */
1047 if (gpio_is_valid(chip->gpio_cs))
1048 gpio_free(chip->gpio_cs);
1049
1050 /* If (*cs_control) is provided, ignore GPIO chip select */
1051 if (chip_info->cs_control) {
1052 chip->cs_control = chip_info->cs_control;
1053 return 0;
1054 }
1055
1056 if (gpio_is_valid(chip_info->gpio_cs)) {
1057 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1058 if (err) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001059 dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1060 chip_info->gpio_cs);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001061 return err;
1062 }
1063
1064 chip->gpio_cs = chip_info->gpio_cs;
1065 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1066
1067 err = gpio_direction_output(chip->gpio_cs,
1068 !chip->gpio_cs_inverted);
1069 }
1070
1071 return err;
1072}
1073
Stephen Streete0c99052006-03-07 23:53:24 -08001074static int setup(struct spi_device *spi)
1075{
1076 struct pxa2xx_spi_chip *chip_info = NULL;
1077 struct chip_data *chip;
1078 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1079 unsigned int clk_div;
Mika Westerberga0d26422013-01-22 12:26:32 +02001080 uint tx_thres, tx_hi_thres, rx_thres;
1081
Weike Chene5262d02014-11-26 02:35:10 -08001082 switch (drv_data->ssp_type) {
1083 case QUARK_X1000_SSP:
1084 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1085 tx_hi_thres = 0;
1086 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1087 break;
1088 case LPSS_SSP:
Mika Westerberga0d26422013-01-22 12:26:32 +02001089 tx_thres = LPSS_TX_LOTHRESH_DFLT;
1090 tx_hi_thres = LPSS_TX_HITHRESH_DFLT;
1091 rx_thres = LPSS_RX_THRESH_DFLT;
Weike Chene5262d02014-11-26 02:35:10 -08001092 break;
1093 default:
Mika Westerberga0d26422013-01-22 12:26:32 +02001094 tx_thres = TX_THRESH_DFLT;
1095 tx_hi_thres = 0;
1096 rx_thres = RX_THRESH_DFLT;
Weike Chene5262d02014-11-26 02:35:10 -08001097 break;
Mika Westerberga0d26422013-01-22 12:26:32 +02001098 }
Stephen Streete0c99052006-03-07 23:53:24 -08001099
Stephen Street8d94cc52006-12-10 02:18:54 -08001100 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -08001101 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -08001102 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -08001103 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001104 if (!chip)
Stephen Streete0c99052006-03-07 23:53:24 -08001105 return -ENOMEM;
1106
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001107 if (drv_data->ssp_type == CE4100_SSP) {
1108 if (spi->chip_select > 4) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001109 dev_err(&spi->dev,
1110 "failed setup: cs number must not be > 4.\n");
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001111 kfree(chip);
1112 return -EINVAL;
1113 }
1114
1115 chip->frm = spi->chip_select;
1116 } else
1117 chip->gpio_cs = -1;
Stephen Streete0c99052006-03-07 23:53:24 -08001118 chip->enable_dma = 0;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001119 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001120 }
1121
Stephen Street8d94cc52006-12-10 02:18:54 -08001122 /* protocol drivers may change the chip settings, so...
1123 * if chip_info exists, use it */
1124 chip_info = spi->controller_data;
1125
Stephen Streete0c99052006-03-07 23:53:24 -08001126 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -08001127 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001128 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001129 if (chip_info->timeout)
1130 chip->timeout = chip_info->timeout;
1131 if (chip_info->tx_threshold)
1132 tx_thres = chip_info->tx_threshold;
Mika Westerberga0d26422013-01-22 12:26:32 +02001133 if (chip_info->tx_hi_threshold)
1134 tx_hi_thres = chip_info->tx_hi_threshold;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001135 if (chip_info->rx_threshold)
1136 rx_thres = chip_info->rx_threshold;
1137 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -08001138 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001139 if (chip_info->enable_loopback)
1140 chip->cr1 = SSCR1_LBM;
Mika Westerberga3496852013-01-22 12:26:33 +02001141 } else if (ACPI_HANDLE(&spi->dev)) {
1142 /*
1143 * Slave devices enumerated from ACPI namespace don't
1144 * usually have chip_info but we still might want to use
1145 * DMA with them.
1146 */
1147 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -08001148 }
1149
Mika Westerberga0d26422013-01-22 12:26:32 +02001150 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1151 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1152 | SSITF_TxHiThresh(tx_hi_thres);
1153
Stephen Street8d94cc52006-12-10 02:18:54 -08001154 /* set dma burst and threshold outside of chip_info path so that if
1155 * chip_info goes away after setting chip->enable_dma, the
1156 * burst and threshold can still respond to changes in bits_per_word */
1157 if (chip->enable_dma) {
1158 /* set up legal burst and threshold for dma */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001159 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1160 spi->bits_per_word,
Stephen Street8d94cc52006-12-10 02:18:54 -08001161 &chip->dma_burst_size,
1162 &chip->dma_threshold)) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001163 dev_warn(&spi->dev,
1164 "in setup: DMA burst size reduced to match bits_per_word\n");
Stephen Street8d94cc52006-12-10 02:18:54 -08001165 }
1166 }
1167
Weike Chene5262d02014-11-26 02:35:10 -08001168 clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, spi->max_speed_hz);
Stephen Street9708c122006-03-28 14:05:23 -08001169 chip->speed_hz = spi->max_speed_hz;
Stephen Streete0c99052006-03-07 23:53:24 -08001170
Weike Chen4fdb2422014-10-08 08:50:22 -07001171 chip->cr0 = pxa2xx_configure_sscr0(drv_data, clk_div,
1172 spi->bits_per_word);
Weike Chene5262d02014-11-26 02:35:10 -08001173 switch (drv_data->ssp_type) {
1174 case QUARK_X1000_SSP:
1175 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1176 & QUARK_X1000_SSCR1_RFT)
1177 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1178 & QUARK_X1000_SSCR1_TFT);
1179 break;
1180 default:
1181 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1182 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1183 break;
1184 }
1185
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -08001186 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1187 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1188 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001189
Mika Westerbergb8331722013-01-22 12:26:31 +02001190 if (spi->mode & SPI_LOOP)
1191 chip->cr1 |= SSCR1_LBM;
1192
Stephen Streete0c99052006-03-07 23:53:24 -08001193 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001194 if (!pxa25x_ssp_comp(drv_data))
David Brownell7d077192009-06-17 16:26:03 -07001195 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001196 drv_data->max_clk_rate
Eric Miaoc9840da2010-03-16 16:48:01 +08001197 / (1 + ((chip->cr0 & SSCR0_SCR(0xfff)) >> 8)),
1198 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001199 else
David Brownell7d077192009-06-17 16:26:03 -07001200 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001201 drv_data->max_clk_rate / 2
Eric Miaoc9840da2010-03-16 16:48:01 +08001202 / (1 + ((chip->cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1203 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001204
1205 if (spi->bits_per_word <= 8) {
1206 chip->n_bytes = 1;
Stephen Streete0c99052006-03-07 23:53:24 -08001207 chip->read = u8_reader;
1208 chip->write = u8_writer;
1209 } else if (spi->bits_per_word <= 16) {
1210 chip->n_bytes = 2;
Stephen Streete0c99052006-03-07 23:53:24 -08001211 chip->read = u16_reader;
1212 chip->write = u16_writer;
1213 } else if (spi->bits_per_word <= 32) {
Weike Chene5262d02014-11-26 02:35:10 -08001214 if (!is_quark_x1000_ssp(drv_data))
1215 chip->cr0 |= SSCR0_EDSS;
Stephen Streete0c99052006-03-07 23:53:24 -08001216 chip->n_bytes = 4;
Stephen Streete0c99052006-03-07 23:53:24 -08001217 chip->read = u32_reader;
1218 chip->write = u32_writer;
Stephen Streete0c99052006-03-07 23:53:24 -08001219 }
Stephen Street9708c122006-03-28 14:05:23 -08001220 chip->bits_per_word = spi->bits_per_word;
Stephen Streete0c99052006-03-07 23:53:24 -08001221
1222 spi_set_ctldata(spi, chip);
1223
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001224 if (drv_data->ssp_type == CE4100_SSP)
1225 return 0;
1226
Eric Miaoa7bb3902009-04-06 19:00:54 -07001227 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001228}
1229
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001230static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001231{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001232 struct chip_data *chip = spi_get_ctldata(spi);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001233 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001234
Daniel Ribeiro7348d822009-05-12 13:19:36 -07001235 if (!chip)
1236 return;
1237
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001238 if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
Eric Miaoa7bb3902009-04-06 19:00:54 -07001239 gpio_free(chip->gpio_cs);
1240
Stephen Streete0c99052006-03-07 23:53:24 -08001241 kfree(chip);
1242}
1243
Mika Westerberga3496852013-01-22 12:26:33 +02001244#ifdef CONFIG_ACPI
Mika Westerberga3496852013-01-22 12:26:33 +02001245static struct pxa2xx_spi_master *
1246pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1247{
1248 struct pxa2xx_spi_master *pdata;
Mika Westerberga3496852013-01-22 12:26:33 +02001249 struct acpi_device *adev;
1250 struct ssp_device *ssp;
1251 struct resource *res;
1252 int devid;
1253
1254 if (!ACPI_HANDLE(&pdev->dev) ||
1255 acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
1256 return NULL;
1257
Mika Westerbergcc0ee982013-06-20 17:44:22 +03001258 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001259 if (!pdata)
Mika Westerberga3496852013-01-22 12:26:33 +02001260 return NULL;
Mika Westerberga3496852013-01-22 12:26:33 +02001261
1262 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1263 if (!res)
1264 return NULL;
1265
1266 ssp = &pdata->ssp;
1267
1268 ssp->phys_base = res->start;
Sachin Kamatcbfd6a22013-04-08 15:49:33 +05301269 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1270 if (IS_ERR(ssp->mmio_base))
Mika Westerberg6dc81f62013-05-13 13:45:09 +03001271 return NULL;
Mika Westerberga3496852013-01-22 12:26:33 +02001272
1273 ssp->clk = devm_clk_get(&pdev->dev, NULL);
1274 ssp->irq = platform_get_irq(pdev, 0);
1275 ssp->type = LPSS_SSP;
1276 ssp->pdev = pdev;
1277
1278 ssp->port_id = -1;
1279 if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid))
1280 ssp->port_id = devid;
1281
1282 pdata->num_chipselect = 1;
Mika Westerbergcddb3392013-05-13 13:45:10 +03001283 pdata->enable_dma = true;
Mika Westerberga3496852013-01-22 12:26:33 +02001284
1285 return pdata;
1286}
1287
1288static struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1289 { "INT33C0", 0 },
1290 { "INT33C1", 0 },
Mika Westerberg54acbd92013-11-12 12:06:21 +02001291 { "INT3430", 0 },
1292 { "INT3431", 0 },
Mika Westerberg4b30f2a2013-05-13 13:45:11 +03001293 { "80860F0E", 0 },
Alan Coxaca26362014-08-20 13:57:26 +03001294 { "8086228E", 0 },
Mika Westerberga3496852013-01-22 12:26:33 +02001295 { },
1296};
1297MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1298#else
1299static inline struct pxa2xx_spi_master *
1300pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1301{
1302 return NULL;
1303}
1304#endif
1305
Grant Likelyfd4a3192012-12-07 16:57:14 +00001306static int pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001307{
1308 struct device *dev = &pdev->dev;
1309 struct pxa2xx_spi_master *platform_info;
1310 struct spi_master *master;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001311 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001312 struct ssp_device *ssp;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001313 int status;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001314 u32 tmp;
Stephen Streete0c99052006-03-07 23:53:24 -08001315
Mika Westerberg851bacf2013-01-07 12:44:33 +02001316 platform_info = dev_get_platdata(dev);
1317 if (!platform_info) {
Mika Westerberga3496852013-01-22 12:26:33 +02001318 platform_info = pxa2xx_spi_acpi_get_pdata(pdev);
1319 if (!platform_info) {
1320 dev_err(&pdev->dev, "missing platform data\n");
1321 return -ENODEV;
1322 }
Mika Westerberg851bacf2013-01-07 12:44:33 +02001323 }
Stephen Streete0c99052006-03-07 23:53:24 -08001324
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001325 ssp = pxa_ssp_request(pdev->id, pdev->name);
Mika Westerberg851bacf2013-01-07 12:44:33 +02001326 if (!ssp)
1327 ssp = &platform_info->ssp;
1328
1329 if (!ssp->mmio_base) {
1330 dev_err(&pdev->dev, "failed to get ssp\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001331 return -ENODEV;
1332 }
1333
1334 /* Allocate master with space for drv_data and null dma buffer */
1335 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1336 if (!master) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001337 dev_err(&pdev->dev, "cannot alloc spi_master\n");
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001338 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001339 return -ENOMEM;
1340 }
1341 drv_data = spi_master_get_devdata(master);
1342 drv_data->master = master;
1343 drv_data->master_info = platform_info;
1344 drv_data->pdev = pdev;
eric miao2f1a74e2007-11-21 18:50:53 +08001345 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001346
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001347 master->dev.parent = &pdev->dev;
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001348 master->dev.of_node = pdev->dev.of_node;
David Brownelle7db06b2009-06-17 16:26:04 -07001349 /* the spi->mode bits understood by this driver: */
Mika Westerbergb8331722013-01-22 12:26:31 +02001350 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
David Brownelle7db06b2009-06-17 16:26:04 -07001351
Mika Westerberg851bacf2013-01-07 12:44:33 +02001352 master->bus_num = ssp->port_id;
Stephen Streete0c99052006-03-07 23:53:24 -08001353 master->num_chipselect = platform_info->num_chipselect;
Mike Rapoport7ad0ba92009-04-06 19:00:57 -07001354 master->dma_alignment = DMA_ALIGNMENT;
Stephen Streete0c99052006-03-07 23:53:24 -08001355 master->cleanup = cleanup;
1356 master->setup = setup;
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001357 master->transfer_one_message = pxa2xx_spi_transfer_one_message;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001358 master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
Mark Brown7dd62782013-07-28 15:35:21 +01001359 master->auto_runtime_pm = true;
Stephen Streete0c99052006-03-07 23:53:24 -08001360
eric miao2f1a74e2007-11-21 18:50:53 +08001361 drv_data->ssp_type = ssp->type;
Mika Westerberg2b9b84f2013-01-22 12:26:25 +02001362 drv_data->null_dma_buf = (u32 *)PTR_ALIGN(&drv_data[1], DMA_ALIGNMENT);
Stephen Streete0c99052006-03-07 23:53:24 -08001363
eric miao2f1a74e2007-11-21 18:50:53 +08001364 drv_data->ioaddr = ssp->mmio_base;
1365 drv_data->ssdr_physical = ssp->phys_base + SSDR;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001366 if (pxa25x_ssp_comp(drv_data)) {
Weike Chene5262d02014-11-26 02:35:10 -08001367 switch (drv_data->ssp_type) {
1368 case QUARK_X1000_SSP:
1369 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1370 break;
1371 default:
1372 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1373 break;
1374 }
1375
Stephen Streete0c99052006-03-07 23:53:24 -08001376 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1377 drv_data->dma_cr1 = 0;
1378 drv_data->clear_sr = SSSR_ROR;
1379 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1380 } else {
Stephen Warren24778be2013-05-21 20:36:35 -06001381 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Stephen Streete0c99052006-03-07 23:53:24 -08001382 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
Mika Westerberg59288082013-01-22 12:26:29 +02001383 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
Stephen Streete0c99052006-03-07 23:53:24 -08001384 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1385 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1386 }
1387
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001388 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1389 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001390 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001391 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Stephen Streete0c99052006-03-07 23:53:24 -08001392 goto out_error_master_alloc;
1393 }
1394
1395 /* Setup DMA if requested */
1396 drv_data->tx_channel = -1;
1397 drv_data->rx_channel = -1;
1398 if (platform_info->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001399 status = pxa2xx_spi_dma_setup(drv_data);
1400 if (status) {
Mika Westerbergcddb3392013-05-13 13:45:10 +03001401 dev_dbg(dev, "no DMA channels available, using PIO\n");
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001402 platform_info->enable_dma = false;
Stephen Streete0c99052006-03-07 23:53:24 -08001403 }
Stephen Streete0c99052006-03-07 23:53:24 -08001404 }
1405
1406 /* Enable SOC clock */
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001407 clk_prepare_enable(ssp->clk);
1408
1409 drv_data->max_clk_rate = clk_get_rate(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001410
1411 /* Load default SSP configuration */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001412 pxa2xx_spi_write(drv_data, SSCR0, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001413 switch (drv_data->ssp_type) {
1414 case QUARK_X1000_SSP:
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001415 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
1416 | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1417 pxa2xx_spi_write(drv_data, SSCR1, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001418
1419 /* using the Motorola SPI protocol and use 8 bit frame */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001420 pxa2xx_spi_write(drv_data, SSCR0,
1421 QUARK_X1000_SSCR0_Motorola
1422 | QUARK_X1000_SSCR0_DataSize(8));
Weike Chene5262d02014-11-26 02:35:10 -08001423 break;
1424 default:
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001425 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1426 SSCR1_TxTresh(TX_THRESH_DFLT);
1427 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1428 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1429 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001430 break;
1431 }
1432
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001433 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001434 pxa2xx_spi_write(drv_data, SSTO, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001435
1436 if (!is_quark_x1000_ssp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001437 pxa2xx_spi_write(drv_data, SSPSP, 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001438
Jarkko Nikula7566bcc2014-12-18 15:04:20 +02001439 if (is_lpss_ssp(drv_data))
1440 lpss_ssp_setup(drv_data);
Mika Westerberga0d26422013-01-22 12:26:32 +02001441
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001442 tasklet_init(&drv_data->pump_transfers, pump_transfers,
1443 (unsigned long)drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001444
Antonio Ospite836d1a22014-05-30 18:18:09 +02001445 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1446 pm_runtime_use_autosuspend(&pdev->dev);
1447 pm_runtime_set_active(&pdev->dev);
1448 pm_runtime_enable(&pdev->dev);
1449
Stephen Streete0c99052006-03-07 23:53:24 -08001450 /* Register with the SPI framework */
1451 platform_set_drvdata(pdev, drv_data);
Jingoo Hana807fcd2013-09-24 13:46:55 +09001452 status = devm_spi_register_master(&pdev->dev, master);
Stephen Streete0c99052006-03-07 23:53:24 -08001453 if (status != 0) {
1454 dev_err(&pdev->dev, "problem registering spi master\n");
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001455 goto out_error_clock_enabled;
Stephen Streete0c99052006-03-07 23:53:24 -08001456 }
1457
1458 return status;
1459
Stephen Streete0c99052006-03-07 23:53:24 -08001460out_error_clock_enabled:
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001461 clk_disable_unprepare(ssp->clk);
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001462 pxa2xx_spi_dma_release(drv_data);
eric miao2f1a74e2007-11-21 18:50:53 +08001463 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001464
1465out_error_master_alloc:
1466 spi_master_put(master);
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001467 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001468 return status;
1469}
1470
1471static int pxa2xx_spi_remove(struct platform_device *pdev)
1472{
1473 struct driver_data *drv_data = platform_get_drvdata(pdev);
Julia Lawall51e911e2009-01-06 14:41:45 -08001474 struct ssp_device *ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001475
1476 if (!drv_data)
1477 return 0;
Julia Lawall51e911e2009-01-06 14:41:45 -08001478 ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001479
Mika Westerberg7d94a502013-01-22 12:26:30 +02001480 pm_runtime_get_sync(&pdev->dev);
1481
Stephen Streete0c99052006-03-07 23:53:24 -08001482 /* Disable the SSP at the peripheral and SOC level */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001483 pxa2xx_spi_write(drv_data, SSCR0, 0);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001484 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001485
1486 /* Release DMA */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001487 if (drv_data->master_info->enable_dma)
1488 pxa2xx_spi_dma_release(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001489
Mika Westerberg7d94a502013-01-22 12:26:30 +02001490 pm_runtime_put_noidle(&pdev->dev);
1491 pm_runtime_disable(&pdev->dev);
1492
Stephen Streete0c99052006-03-07 23:53:24 -08001493 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001494 free_irq(ssp->irq, drv_data);
1495
1496 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001497 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001498
Stephen Streete0c99052006-03-07 23:53:24 -08001499 return 0;
1500}
1501
1502static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1503{
1504 int status = 0;
1505
1506 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1507 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1508}
1509
Mika Westerberg382cebb2014-01-16 14:50:55 +02001510#ifdef CONFIG_PM_SLEEP
Mike Rapoport86d25932009-07-21 17:50:16 +03001511static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001512{
Mike Rapoport86d25932009-07-21 17:50:16 +03001513 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001514 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001515 int status = 0;
1516
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001517 status = spi_master_suspend(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001518 if (status != 0)
1519 return status;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001520 pxa2xx_spi_write(drv_data, SSCR0, 0);
Dmitry Eremin-Solenikov2b9375b2014-11-06 14:08:29 +03001521
1522 if (!pm_runtime_suspended(dev))
1523 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001524
1525 return 0;
1526}
1527
Mike Rapoport86d25932009-07-21 17:50:16 +03001528static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001529{
Mike Rapoport86d25932009-07-21 17:50:16 +03001530 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001531 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001532 int status = 0;
1533
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001534 pxa2xx_spi_dma_resume(drv_data);
Daniel Ribeiro148da332009-04-21 12:24:43 -07001535
Stephen Streete0c99052006-03-07 23:53:24 -08001536 /* Enable the SSP clock */
Dmitry Eremin-Solenikov2b9375b2014-11-06 14:08:29 +03001537 if (!pm_runtime_suspended(dev))
1538 clk_prepare_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001539
Chew, Chiau Eec50325f2013-11-29 02:13:11 +08001540 /* Restore LPSS private register bits */
Jarkko Nikula48421ad2015-01-28 10:09:42 +02001541 if (is_lpss_ssp(drv_data))
1542 lpss_ssp_setup(drv_data);
Chew, Chiau Eec50325f2013-11-29 02:13:11 +08001543
Stephen Streete0c99052006-03-07 23:53:24 -08001544 /* Start the queue running */
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001545 status = spi_master_resume(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001546 if (status != 0) {
Mike Rapoport86d25932009-07-21 17:50:16 +03001547 dev_err(dev, "problem starting queue (%d)\n", status);
Stephen Streete0c99052006-03-07 23:53:24 -08001548 return status;
1549 }
1550
1551 return 0;
1552}
Mika Westerberg7d94a502013-01-22 12:26:30 +02001553#endif
1554
Rafael J. Wysockiec833052014-12-13 00:41:15 +01001555#ifdef CONFIG_PM
Mika Westerberg7d94a502013-01-22 12:26:30 +02001556static int pxa2xx_spi_runtime_suspend(struct device *dev)
1557{
1558 struct driver_data *drv_data = dev_get_drvdata(dev);
1559
1560 clk_disable_unprepare(drv_data->ssp->clk);
1561 return 0;
1562}
1563
1564static int pxa2xx_spi_runtime_resume(struct device *dev)
1565{
1566 struct driver_data *drv_data = dev_get_drvdata(dev);
1567
1568 clk_prepare_enable(drv_data->ssp->clk);
1569 return 0;
1570}
1571#endif
Mike Rapoport86d25932009-07-21 17:50:16 +03001572
Alexey Dobriyan47145212009-12-14 18:00:08 -08001573static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mika Westerberg7d94a502013-01-22 12:26:30 +02001574 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1575 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1576 pxa2xx_spi_runtime_resume, NULL)
Mike Rapoport86d25932009-07-21 17:50:16 +03001577};
Stephen Streete0c99052006-03-07 23:53:24 -08001578
1579static struct platform_driver driver = {
1580 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001581 .name = "pxa2xx-spi",
Mike Rapoport86d25932009-07-21 17:50:16 +03001582 .pm = &pxa2xx_spi_pm_ops,
Mika Westerberga3496852013-01-22 12:26:33 +02001583 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
Stephen Streete0c99052006-03-07 23:53:24 -08001584 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001585 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07001586 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001587 .shutdown = pxa2xx_spi_shutdown,
Stephen Streete0c99052006-03-07 23:53:24 -08001588};
1589
1590static int __init pxa2xx_spi_init(void)
1591{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001592 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08001593}
Antonio Ospite5b61a742009-09-22 16:46:10 -07001594subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08001595
1596static void __exit pxa2xx_spi_exit(void)
1597{
1598 platform_driver_unregister(&driver);
1599}
1600module_exit(pxa2xx_spi_exit);