blob: a85b7496a3cd951548ffb3e30ebe92673402c0a7 [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{
Jarkko Nikula03fbf482015-06-04 16:55:10 +030077 switch (drv_data->ssp_type) {
78 case LPSS_LPT_SSP:
79 case LPSS_BYT_SSP:
80 return true;
81 default:
82 return false;
83 }
Mika Westerberga0d26422013-01-22 12:26:32 +020084}
85
Weike Chene5262d02014-11-26 02:35:10 -080086static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
87{
88 return drv_data->ssp_type == QUARK_X1000_SSP;
89}
90
Weike Chen4fdb2422014-10-08 08:50:22 -070091static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
92{
93 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -080094 case QUARK_X1000_SSP:
95 return QUARK_X1000_SSCR1_CHANGE_MASK;
Weike Chen4fdb2422014-10-08 08:50:22 -070096 default:
97 return SSCR1_CHANGE_MASK;
98 }
99}
100
101static u32
102pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
103{
104 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800105 case QUARK_X1000_SSP:
106 return RX_THRESH_QUARK_X1000_DFLT;
Weike Chen4fdb2422014-10-08 08:50:22 -0700107 default:
108 return RX_THRESH_DFLT;
109 }
110}
111
112static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
113{
Weike Chen4fdb2422014-10-08 08:50:22 -0700114 u32 mask;
115
116 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800117 case QUARK_X1000_SSP:
118 mask = QUARK_X1000_SSSR_TFL_MASK;
119 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700120 default:
121 mask = SSSR_TFL_MASK;
122 break;
123 }
124
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200125 return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
Weike Chen4fdb2422014-10-08 08:50:22 -0700126}
127
128static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
129 u32 *sccr1_reg)
130{
131 u32 mask;
132
133 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800134 case QUARK_X1000_SSP:
135 mask = QUARK_X1000_SSCR1_RFT;
136 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700137 default:
138 mask = SSCR1_RFT;
139 break;
140 }
141 *sccr1_reg &= ~mask;
142}
143
144static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
145 u32 *sccr1_reg, u32 threshold)
146{
147 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800148 case QUARK_X1000_SSP:
149 *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
150 break;
Weike Chen4fdb2422014-10-08 08:50:22 -0700151 default:
152 *sccr1_reg |= SSCR1_RxTresh(threshold);
153 break;
154 }
155}
156
157static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
158 u32 clk_div, u8 bits)
159{
160 switch (drv_data->ssp_type) {
Weike Chene5262d02014-11-26 02:35:10 -0800161 case QUARK_X1000_SSP:
162 return clk_div
163 | QUARK_X1000_SSCR0_Motorola
164 | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
165 | SSCR0_SSE;
Weike Chen4fdb2422014-10-08 08:50:22 -0700166 default:
167 return clk_div
168 | SSCR0_Motorola
169 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
170 | SSCR0_SSE
171 | (bits > 16 ? SSCR0_EDSS : 0);
172 }
173}
174
Mika Westerberga0d26422013-01-22 12:26:32 +0200175/*
176 * Read and write LPSS SSP private registers. Caller must first check that
177 * is_lpss_ssp() returns true before these can be called.
178 */
179static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
180{
181 WARN_ON(!drv_data->lpss_base);
182 return readl(drv_data->lpss_base + offset);
183}
184
185static void __lpss_ssp_write_priv(struct driver_data *drv_data,
186 unsigned offset, u32 value)
187{
188 WARN_ON(!drv_data->lpss_base);
189 writel(value, drv_data->lpss_base + offset);
190}
191
192/*
193 * lpss_ssp_setup - perform LPSS SSP specific setup
194 * @drv_data: pointer to the driver private data
195 *
196 * Perform LPSS SSP specific setup. This function must be called first if
197 * one is going to use LPSS SSP private registers.
198 */
199static void lpss_ssp_setup(struct driver_data *drv_data)
200{
201 unsigned offset = 0x400;
202 u32 value, orig;
203
Mika Westerberga0d26422013-01-22 12:26:32 +0200204 /*
205 * Perform auto-detection of the LPSS SSP private registers. They
206 * can be either at 1k or 2k offset from the base address.
207 */
208 orig = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
209
Chew, Chiau Eee61f4872014-06-13 23:57:25 +0800210 /* Test SPI_CS_CONTROL_SW_MODE bit enabling */
Mika Westerberga0d26422013-01-22 12:26:32 +0200211 value = orig | SPI_CS_CONTROL_SW_MODE;
212 writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL);
213 value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
214 if (value != (orig | SPI_CS_CONTROL_SW_MODE)) {
215 offset = 0x800;
216 goto detection_done;
217 }
218
Chew, Chiau Eee61f4872014-06-13 23:57:25 +0800219 orig = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
220
221 /* Test SPI_CS_CONTROL_SW_MODE bit disabling */
222 value = orig & ~SPI_CS_CONTROL_SW_MODE;
Mika Westerberga0d26422013-01-22 12:26:32 +0200223 writel(value, drv_data->ioaddr + offset + SPI_CS_CONTROL);
224 value = readl(drv_data->ioaddr + offset + SPI_CS_CONTROL);
Chew, Chiau Eee61f4872014-06-13 23:57:25 +0800225 if (value != (orig & ~SPI_CS_CONTROL_SW_MODE)) {
Mika Westerberga0d26422013-01-22 12:26:32 +0200226 offset = 0x800;
227 goto detection_done;
228 }
229
230detection_done:
231 /* Now set the LPSS base */
232 drv_data->lpss_base = drv_data->ioaddr + offset;
233
234 /* Enable software chip select control */
235 value = SPI_CS_CONTROL_SW_MODE | SPI_CS_CONTROL_CS_HIGH;
236 __lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value);
Mika Westerberg0054e282013-03-05 12:05:17 +0200237
238 /* Enable multiblock DMA transfers */
Mika Westerberg1de70612013-07-03 13:25:06 +0300239 if (drv_data->master_info->enable_dma) {
Mika Westerberg0054e282013-03-05 12:05:17 +0200240 __lpss_ssp_write_priv(drv_data, SSP_REG, 1);
Mika Westerberg1de70612013-07-03 13:25:06 +0300241
242 value = __lpss_ssp_read_priv(drv_data, GENERAL_REG);
243 value |= GENERAL_REG_RXTO_HOLDOFF_DISABLE;
244 __lpss_ssp_write_priv(drv_data, GENERAL_REG, value);
245 }
Mika Westerberga0d26422013-01-22 12:26:32 +0200246}
247
248static void lpss_ssp_cs_control(struct driver_data *drv_data, bool enable)
249{
250 u32 value;
251
Mika Westerberga0d26422013-01-22 12:26:32 +0200252 value = __lpss_ssp_read_priv(drv_data, SPI_CS_CONTROL);
253 if (enable)
254 value &= ~SPI_CS_CONTROL_CS_HIGH;
255 else
256 value |= SPI_CS_CONTROL_CS_HIGH;
257 __lpss_ssp_write_priv(drv_data, SPI_CS_CONTROL, value);
258}
259
Eric Miaoa7bb3902009-04-06 19:00:54 -0700260static void cs_assert(struct driver_data *drv_data)
261{
262 struct chip_data *chip = drv_data->cur_chip;
263
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800264 if (drv_data->ssp_type == CE4100_SSP) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200265 pxa2xx_spi_write(drv_data, SSSR, drv_data->cur_chip->frm);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800266 return;
267 }
268
Eric Miaoa7bb3902009-04-06 19:00:54 -0700269 if (chip->cs_control) {
270 chip->cs_control(PXA2XX_CS_ASSERT);
271 return;
272 }
273
Mika Westerberga0d26422013-01-22 12:26:32 +0200274 if (gpio_is_valid(chip->gpio_cs)) {
Eric Miaoa7bb3902009-04-06 19:00:54 -0700275 gpio_set_value(chip->gpio_cs, chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200276 return;
277 }
278
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200279 if (is_lpss_ssp(drv_data))
280 lpss_ssp_cs_control(drv_data, true);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700281}
282
283static void cs_deassert(struct driver_data *drv_data)
284{
285 struct chip_data *chip = drv_data->cur_chip;
286
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800287 if (drv_data->ssp_type == CE4100_SSP)
288 return;
289
Eric Miaoa7bb3902009-04-06 19:00:54 -0700290 if (chip->cs_control) {
Daniel Ribeiro2b2562d2009-04-08 22:48:03 -0300291 chip->cs_control(PXA2XX_CS_DEASSERT);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700292 return;
293 }
294
Mika Westerberga0d26422013-01-22 12:26:32 +0200295 if (gpio_is_valid(chip->gpio_cs)) {
Eric Miaoa7bb3902009-04-06 19:00:54 -0700296 gpio_set_value(chip->gpio_cs, !chip->gpio_cs_inverted);
Mika Westerberga0d26422013-01-22 12:26:32 +0200297 return;
298 }
299
Jarkko Nikula7566bcc2014-12-18 15:04:20 +0200300 if (is_lpss_ssp(drv_data))
301 lpss_ssp_cs_control(drv_data, false);
Eric Miaoa7bb3902009-04-06 19:00:54 -0700302}
303
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200304int pxa2xx_spi_flush(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800305{
306 unsigned long limit = loops_per_jiffy << 1;
307
Stephen Streete0c99052006-03-07 23:53:24 -0800308 do {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200309 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
310 pxa2xx_spi_read(drv_data, SSDR);
311 } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800312 write_SSSR_CS(drv_data, SSSR_ROR);
Stephen Streete0c99052006-03-07 23:53:24 -0800313
314 return limit;
315}
316
Stephen Street8d94cc52006-12-10 02:18:54 -0800317static int null_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800318{
Stephen Street9708c122006-03-28 14:05:23 -0800319 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800320
Weike Chen4fdb2422014-10-08 08:50:22 -0700321 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800322 || (drv_data->tx == drv_data->tx_end))
323 return 0;
324
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200325 pxa2xx_spi_write(drv_data, SSDR, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800326 drv_data->tx += n_bytes;
327
328 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800329}
330
Stephen Street8d94cc52006-12-10 02:18:54 -0800331static int null_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800332{
Stephen Street9708c122006-03-28 14:05:23 -0800333 u8 n_bytes = drv_data->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800334
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200335 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
336 && (drv_data->rx < drv_data->rx_end)) {
337 pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800338 drv_data->rx += n_bytes;
339 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800340
341 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800342}
343
Stephen Street8d94cc52006-12-10 02:18:54 -0800344static int u8_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800345{
Weike Chen4fdb2422014-10-08 08:50:22 -0700346 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800347 || (drv_data->tx == drv_data->tx_end))
348 return 0;
349
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200350 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800351 ++drv_data->tx;
352
353 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800354}
355
Stephen Street8d94cc52006-12-10 02:18:54 -0800356static int u8_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800357{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200358 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
359 && (drv_data->rx < drv_data->rx_end)) {
360 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800361 ++drv_data->rx;
362 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800363
364 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800365}
366
Stephen Street8d94cc52006-12-10 02:18:54 -0800367static int u16_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800368{
Weike Chen4fdb2422014-10-08 08:50:22 -0700369 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800370 || (drv_data->tx == drv_data->tx_end))
371 return 0;
372
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200373 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800374 drv_data->tx += 2;
375
376 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800377}
378
Stephen Street8d94cc52006-12-10 02:18:54 -0800379static int u16_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800380{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200381 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
382 && (drv_data->rx < drv_data->rx_end)) {
383 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800384 drv_data->rx += 2;
385 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800386
387 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800388}
Stephen Street8d94cc52006-12-10 02:18:54 -0800389
390static int u32_writer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800391{
Weike Chen4fdb2422014-10-08 08:50:22 -0700392 if (pxa2xx_spi_txfifo_full(drv_data)
Stephen Street8d94cc52006-12-10 02:18:54 -0800393 || (drv_data->tx == drv_data->tx_end))
394 return 0;
395
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200396 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
Stephen Street8d94cc52006-12-10 02:18:54 -0800397 drv_data->tx += 4;
398
399 return 1;
Stephen Streete0c99052006-03-07 23:53:24 -0800400}
401
Stephen Street8d94cc52006-12-10 02:18:54 -0800402static int u32_reader(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800403{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200404 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
405 && (drv_data->rx < drv_data->rx_end)) {
406 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
Stephen Streete0c99052006-03-07 23:53:24 -0800407 drv_data->rx += 4;
408 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800409
410 return drv_data->rx == drv_data->rx_end;
Stephen Streete0c99052006-03-07 23:53:24 -0800411}
412
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200413void *pxa2xx_spi_next_transfer(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800414{
415 struct spi_message *msg = drv_data->cur_msg;
416 struct spi_transfer *trans = drv_data->cur_transfer;
417
418 /* Move to next transfer */
419 if (trans->transfer_list.next != &msg->transfers) {
420 drv_data->cur_transfer =
421 list_entry(trans->transfer_list.next,
422 struct spi_transfer,
423 transfer_list);
424 return RUNNING_STATE;
425 } else
426 return DONE_STATE;
427}
428
Stephen Streete0c99052006-03-07 23:53:24 -0800429/* caller already set message->status; dma and pio irqs are blocked */
Stephen Street5daa3ba2006-05-20 15:00:19 -0700430static void giveback(struct driver_data *drv_data)
Stephen Streete0c99052006-03-07 23:53:24 -0800431{
432 struct spi_transfer* last_transfer;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700433 struct spi_message *msg;
Stephen Streete0c99052006-03-07 23:53:24 -0800434
Stephen Street5daa3ba2006-05-20 15:00:19 -0700435 msg = drv_data->cur_msg;
436 drv_data->cur_msg = NULL;
437 drv_data->cur_transfer = NULL;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700438
Axel Lin23e2c2a2014-02-12 22:13:27 +0800439 last_transfer = list_last_entry(&msg->transfers, struct spi_transfer,
Stephen Streete0c99052006-03-07 23:53:24 -0800440 transfer_list);
441
Ned Forrester84235972008-09-13 02:33:17 -0700442 /* Delay if requested before any change in chip select */
443 if (last_transfer->delay_usecs)
444 udelay(last_transfer->delay_usecs);
445
446 /* Drop chip select UNLESS cs_change is true or we are returning
447 * a message with an error, or next message is for another chip
448 */
Stephen Streete0c99052006-03-07 23:53:24 -0800449 if (!last_transfer->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700450 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700451 else {
452 struct spi_message *next_msg;
453
454 /* Holding of cs was hinted, but we need to make sure
455 * the next message is for the same chip. Don't waste
456 * time with the following tests unless this was hinted.
457 *
458 * We cannot postpone this until pump_messages, because
459 * after calling msg->complete (below) the driver that
460 * sent the current message could be unloaded, which
461 * could invalidate the cs_control() callback...
462 */
463
464 /* get a pointer to the next message, if any */
Mika Westerberg7f86bde2013-01-22 12:26:26 +0200465 next_msg = spi_get_next_queued_message(drv_data->master);
Ned Forrester84235972008-09-13 02:33:17 -0700466
467 /* see if the next and current messages point
468 * to the same chip
469 */
470 if (next_msg && next_msg->spi != msg->spi)
471 next_msg = NULL;
472 if (!next_msg || msg->state == ERROR_STATE)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700473 cs_deassert(drv_data);
Ned Forrester84235972008-09-13 02:33:17 -0700474 }
Stephen Streete0c99052006-03-07 23:53:24 -0800475
Eric Miaoa7bb3902009-04-06 19:00:54 -0700476 drv_data->cur_chip = NULL;
Mika Westerbergc957e8f2014-12-29 10:33:36 +0200477 spi_finalize_current_message(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -0800478}
479
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800480static void reset_sccr1(struct driver_data *drv_data)
481{
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800482 struct chip_data *chip = drv_data->cur_chip;
483 u32 sccr1_reg;
484
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200485 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800486 sccr1_reg &= ~SSCR1_RFT;
487 sccr1_reg |= chip->threshold;
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200488 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800489}
490
Stephen Street8d94cc52006-12-10 02:18:54 -0800491static void int_error_stop(struct driver_data *drv_data, const char* msg)
492{
Stephen Street8d94cc52006-12-10 02:18:54 -0800493 /* Stop and reset SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800494 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800495 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800496 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200497 pxa2xx_spi_write(drv_data, SSTO, 0);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200498 pxa2xx_spi_flush(drv_data);
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200499 pxa2xx_spi_write(drv_data, SSCR0,
500 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
Stephen Street8d94cc52006-12-10 02:18:54 -0800501
502 dev_err(&drv_data->pdev->dev, "%s\n", msg);
503
504 drv_data->cur_msg->state = ERROR_STATE;
505 tasklet_schedule(&drv_data->pump_transfers);
506}
507
508static void int_transfer_complete(struct driver_data *drv_data)
509{
Stephen Street8d94cc52006-12-10 02:18:54 -0800510 /* Stop SSP */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800511 write_SSSR_CS(drv_data, drv_data->clear_sr);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800512 reset_sccr1(drv_data);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800513 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200514 pxa2xx_spi_write(drv_data, SSTO, 0);
Stephen Street8d94cc52006-12-10 02:18:54 -0800515
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300516 /* Update total byte transferred return count actual bytes read */
Stephen Street8d94cc52006-12-10 02:18:54 -0800517 drv_data->cur_msg->actual_length += drv_data->len -
518 (drv_data->rx_end - drv_data->rx);
519
Ned Forrester84235972008-09-13 02:33:17 -0700520 /* Transfer delays and chip select release are
521 * handled in pump_transfers or giveback
522 */
Stephen Street8d94cc52006-12-10 02:18:54 -0800523
524 /* Move to next transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200525 drv_data->cur_msg->state = pxa2xx_spi_next_transfer(drv_data);
Stephen Street8d94cc52006-12-10 02:18:54 -0800526
527 /* Schedule transfer tasklet */
528 tasklet_schedule(&drv_data->pump_transfers);
529}
530
Stephen Streete0c99052006-03-07 23:53:24 -0800531static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
532{
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200533 u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
534 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
Stephen Street8d94cc52006-12-10 02:18:54 -0800535
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200536 u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
Stephen Streete0c99052006-03-07 23:53:24 -0800537
Stephen Street8d94cc52006-12-10 02:18:54 -0800538 if (irq_status & SSSR_ROR) {
539 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
540 return IRQ_HANDLED;
541 }
Stephen Streete0c99052006-03-07 23:53:24 -0800542
Stephen Street8d94cc52006-12-10 02:18:54 -0800543 if (irq_status & SSSR_TINT) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200544 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
Stephen Street8d94cc52006-12-10 02:18:54 -0800545 if (drv_data->read(drv_data)) {
546 int_transfer_complete(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800547 return IRQ_HANDLED;
548 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800549 }
Stephen Streete0c99052006-03-07 23:53:24 -0800550
Stephen Street8d94cc52006-12-10 02:18:54 -0800551 /* Drain rx fifo, Fill tx fifo and prevent overruns */
552 do {
553 if (drv_data->read(drv_data)) {
554 int_transfer_complete(drv_data);
555 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800556 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800557 } while (drv_data->write(drv_data));
Stephen Streete0c99052006-03-07 23:53:24 -0800558
Stephen Street8d94cc52006-12-10 02:18:54 -0800559 if (drv_data->read(drv_data)) {
560 int_transfer_complete(drv_data);
561 return IRQ_HANDLED;
562 }
Stephen Streete0c99052006-03-07 23:53:24 -0800563
Stephen Street8d94cc52006-12-10 02:18:54 -0800564 if (drv_data->tx == drv_data->tx_end) {
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800565 u32 bytes_left;
566 u32 sccr1_reg;
567
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200568 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800569 sccr1_reg &= ~SSCR1_TIE;
570
571 /*
572 * PXA25x_SSP has no timeout, set up rx threshould for the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300573 * remaining RX bytes.
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800574 */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800575 if (pxa25x_ssp_comp(drv_data)) {
Weike Chen4fdb2422014-10-08 08:50:22 -0700576 u32 rx_thre;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800577
Weike Chen4fdb2422014-10-08 08:50:22 -0700578 pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800579
580 bytes_left = drv_data->rx_end - drv_data->rx;
581 switch (drv_data->n_bytes) {
582 case 4:
583 bytes_left >>= 1;
584 case 2:
585 bytes_left >>= 1;
Stephen Street8d94cc52006-12-10 02:18:54 -0800586 }
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800587
Weike Chen4fdb2422014-10-08 08:50:22 -0700588 rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
589 if (rx_thre > bytes_left)
590 rx_thre = bytes_left;
Sebastian Andrzej Siewior579d3bb2010-11-22 17:12:17 -0800591
Weike Chen4fdb2422014-10-08 08:50:22 -0700592 pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
Stephen Streete0c99052006-03-07 23:53:24 -0800593 }
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200594 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
Stephen Streete0c99052006-03-07 23:53:24 -0800595 }
596
Stephen Street5daa3ba2006-05-20 15:00:19 -0700597 /* We did something */
598 return IRQ_HANDLED;
Stephen Streete0c99052006-03-07 23:53:24 -0800599}
600
David Howells7d12e782006-10-05 14:55:46 +0100601static irqreturn_t ssp_int(int irq, void *dev_id)
Stephen Streete0c99052006-03-07 23:53:24 -0800602{
Jeff Garzikc7bec5a2006-10-06 15:00:58 -0400603 struct driver_data *drv_data = dev_id;
Mika Westerberg7d94a502013-01-22 12:26:30 +0200604 u32 sccr1_reg;
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800605 u32 mask = drv_data->mask_sr;
606 u32 status;
607
Mika Westerberg7d94a502013-01-22 12:26:30 +0200608 /*
609 * The IRQ might be shared with other peripherals so we must first
610 * check that are we RPM suspended or not. If we are we assume that
611 * the IRQ was not for us (we shouldn't be RPM suspended when the
612 * interrupt is enabled).
613 */
614 if (pm_runtime_suspended(&drv_data->pdev->dev))
615 return IRQ_NONE;
616
Mika Westerberg269e4a42013-09-04 13:37:43 +0300617 /*
618 * If the device is not yet in RPM suspended state and we get an
619 * interrupt that is meant for another device, check if status bits
620 * are all set to one. That means that the device is already
621 * powered off.
622 */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200623 status = pxa2xx_spi_read(drv_data, SSSR);
Mika Westerberg269e4a42013-09-04 13:37:43 +0300624 if (status == ~0)
625 return IRQ_NONE;
626
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200627 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -0800628
629 /* Ignore possible writes if we don't need to write */
630 if (!(sccr1_reg & SSCR1_TIE))
631 mask &= ~SSSR_TFS;
632
633 if (!(status & mask))
634 return IRQ_NONE;
Stephen Streete0c99052006-03-07 23:53:24 -0800635
636 if (!drv_data->cur_msg) {
Stephen Street5daa3ba2006-05-20 15:00:19 -0700637
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200638 pxa2xx_spi_write(drv_data, SSCR0,
639 pxa2xx_spi_read(drv_data, SSCR0)
640 & ~SSCR0_SSE);
641 pxa2xx_spi_write(drv_data, SSCR1,
642 pxa2xx_spi_read(drv_data, SSCR1)
643 & ~drv_data->int_cr1);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800644 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200645 pxa2xx_spi_write(drv_data, SSTO, 0);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800646 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street5daa3ba2006-05-20 15:00:19 -0700647
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300648 dev_err(&drv_data->pdev->dev,
649 "bad message state in interrupt handler\n");
Stephen Street5daa3ba2006-05-20 15:00:19 -0700650
Stephen Streete0c99052006-03-07 23:53:24 -0800651 /* Never fail */
652 return IRQ_HANDLED;
653 }
654
655 return drv_data->transfer_handler(drv_data);
656}
657
Weike Chene5262d02014-11-26 02:35:10 -0800658/*
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200659 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
660 * input frequency by fractions of 2^24. It also has a divider by 5.
661 *
662 * There are formulas to get baud rate value for given input frequency and
663 * divider parameters, such as DDS_CLK_RATE and SCR:
664 *
665 * Fsys = 200MHz
666 *
667 * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
668 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
669 *
670 * DDS_CLK_RATE either 2^n or 2^n / 5.
671 * SCR is in range 0 .. 255
672 *
673 * Divisor = 5^i * 2^j * 2 * k
674 * i = [0, 1] i = 1 iff j = 0 or j > 3
675 * j = [0, 23] j = 0 iff i = 1
676 * k = [1, 256]
677 * Special case: j = 0, i = 1: Divisor = 2 / 5
678 *
679 * Accordingly to the specification the recommended values for DDS_CLK_RATE
680 * are:
681 * Case 1: 2^n, n = [0, 23]
682 * Case 2: 2^24 * 2 / 5 (0x666666)
683 * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
684 *
685 * In all cases the lowest possible value is better.
686 *
687 * The function calculates parameters for all cases and chooses the one closest
688 * to the asked baud rate.
Weike Chene5262d02014-11-26 02:35:10 -0800689 */
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200690static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
Weike Chene5262d02014-11-26 02:35:10 -0800691{
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200692 unsigned long xtal = 200000000;
693 unsigned long fref = xtal / 2; /* mandatory division by 2,
694 see (2) */
695 /* case 3 */
696 unsigned long fref1 = fref / 2; /* case 1 */
697 unsigned long fref2 = fref * 2 / 5; /* case 2 */
698 unsigned long scale;
699 unsigned long q, q1, q2;
700 long r, r1, r2;
701 u32 mul;
Weike Chene5262d02014-11-26 02:35:10 -0800702
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200703 /* Case 1 */
704
705 /* Set initial value for DDS_CLK_RATE */
706 mul = (1 << 24) >> 1;
707
708 /* Calculate initial quot */
709 q1 = DIV_ROUND_CLOSEST(fref1, rate);
710
711 /* Scale q1 if it's too big */
712 if (q1 > 256) {
713 /* Scale q1 to range [1, 512] */
714 scale = fls_long(q1 - 1);
715 if (scale > 9) {
716 q1 >>= scale - 9;
717 mul >>= scale - 9;
718 }
719
720 /* Round the result if we have a remainder */
721 q1 += q1 & 1;
722 }
723
724 /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
725 scale = __ffs(q1);
726 q1 >>= scale;
727 mul >>= scale;
728
729 /* Get the remainder */
730 r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
731
732 /* Case 2 */
733
734 q2 = DIV_ROUND_CLOSEST(fref2, rate);
735 r2 = abs(fref2 / q2 - rate);
736
737 /*
738 * Choose the best between two: less remainder we have the better. We
739 * can't go case 2 if q2 is greater than 256 since SCR register can
740 * hold only values 0 .. 255.
741 */
742 if (r2 >= r1 || q2 > 256) {
743 /* case 1 is better */
744 r = r1;
745 q = q1;
746 } else {
747 /* case 2 is better */
748 r = r2;
749 q = q2;
750 mul = (1 << 24) * 2 / 5;
751 }
752
753 /* Check case 3 only If the divisor is big enough */
754 if (fref / rate >= 80) {
755 u64 fssp;
756 u32 m;
757
758 /* Calculate initial quot */
759 q1 = DIV_ROUND_CLOSEST(fref, rate);
760 m = (1 << 24) / q1;
761
762 /* Get the remainder */
763 fssp = (u64)fref * m;
764 do_div(fssp, 1 << 24);
765 r1 = abs(fssp - rate);
766
767 /* Choose this one if it suits better */
768 if (r1 < r) {
769 /* case 3 is better */
770 q = 1;
771 mul = m;
Weike Chene5262d02014-11-26 02:35:10 -0800772 }
773 }
774
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200775 *dds = mul;
776 return q - 1;
Weike Chene5262d02014-11-26 02:35:10 -0800777}
778
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200779static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
eric miao2f1a74e2007-11-21 18:50:53 +0800780{
Mika Westerberg3343b7a2013-01-22 12:26:27 +0200781 unsigned long ssp_clk = drv_data->max_clk_rate;
782 const struct ssp_device *ssp = drv_data->ssp;
783
784 rate = min_t(int, ssp_clk, rate);
eric miao2f1a74e2007-11-21 18:50:53 +0800785
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800786 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200787 return (ssp_clk / (2 * rate) - 1) & 0xff;
eric miao2f1a74e2007-11-21 18:50:53 +0800788 else
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200789 return (ssp_clk / rate - 1) & 0xfff;
eric miao2f1a74e2007-11-21 18:50:53 +0800790}
791
Weike Chene5262d02014-11-26 02:35:10 -0800792static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
793 struct chip_data *chip, int rate)
794{
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200795 unsigned int clk_div;
Weike Chene5262d02014-11-26 02:35:10 -0800796
797 switch (drv_data->ssp_type) {
798 case QUARK_X1000_SSP:
Andy Shevchenko9df461e2015-03-25 15:06:16 +0200799 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300800 break;
Weike Chene5262d02014-11-26 02:35:10 -0800801 default:
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200802 clk_div = ssp_get_clk_div(drv_data, rate);
Dan Carpentereecacf72015-03-31 16:49:38 +0300803 break;
Weike Chene5262d02014-11-26 02:35:10 -0800804 }
Andy Shevchenko025ffe82015-03-24 17:43:21 +0200805 return clk_div << 8;
Weike Chene5262d02014-11-26 02:35:10 -0800806}
807
Stephen Streete0c99052006-03-07 23:53:24 -0800808static void pump_transfers(unsigned long data)
809{
810 struct driver_data *drv_data = (struct driver_data *)data;
811 struct spi_message *message = NULL;
812 struct spi_transfer *transfer = NULL;
813 struct spi_transfer *previous = NULL;
814 struct chip_data *chip = NULL;
Stephen Street9708c122006-03-28 14:05:23 -0800815 u32 clk_div = 0;
816 u8 bits = 0;
817 u32 speed = 0;
818 u32 cr0;
Stephen Street8d94cc52006-12-10 02:18:54 -0800819 u32 cr1;
820 u32 dma_thresh = drv_data->cur_chip->dma_threshold;
821 u32 dma_burst = drv_data->cur_chip->dma_burst_size;
Weike Chen4fdb2422014-10-08 08:50:22 -0700822 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800823
824 /* Get current state information */
825 message = drv_data->cur_msg;
826 transfer = drv_data->cur_transfer;
827 chip = drv_data->cur_chip;
828
829 /* Handle for abort */
830 if (message->state == ERROR_STATE) {
831 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700832 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800833 return;
834 }
835
836 /* Handle end of message */
837 if (message->state == DONE_STATE) {
838 message->status = 0;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700839 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800840 return;
841 }
842
Ned Forrester84235972008-09-13 02:33:17 -0700843 /* Delay if requested at end of transfer before CS change */
Stephen Streete0c99052006-03-07 23:53:24 -0800844 if (message->state == RUNNING_STATE) {
845 previous = list_entry(transfer->transfer_list.prev,
846 struct spi_transfer,
847 transfer_list);
848 if (previous->delay_usecs)
849 udelay(previous->delay_usecs);
Ned Forrester84235972008-09-13 02:33:17 -0700850
851 /* Drop chip select only if cs_change is requested */
852 if (previous->cs_change)
Eric Miaoa7bb3902009-04-06 19:00:54 -0700853 cs_deassert(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800854 }
855
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200856 /* Check if we can DMA this transfer */
857 if (!pxa2xx_spi_dma_is_possible(transfer->len) && chip->enable_dma) {
Ned Forrester7e964452008-09-13 02:33:18 -0700858
859 /* reject already-mapped transfers; PIO won't always work */
860 if (message->is_dma_mapped
861 || transfer->rx_dma || transfer->tx_dma) {
862 dev_err(&drv_data->pdev->dev,
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300863 "pump_transfers: mapped transfer length of "
864 "%u is greater than %d\n",
Ned Forrester7e964452008-09-13 02:33:18 -0700865 transfer->len, MAX_DMA_LEN);
866 message->status = -EINVAL;
867 giveback(drv_data);
868 return;
869 }
870
871 /* warn ... we force this to PIO mode */
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300872 dev_warn_ratelimited(&message->spi->dev,
873 "pump_transfers: DMA disabled for transfer length %ld "
874 "greater than %d\n",
875 (long)drv_data->len, MAX_DMA_LEN);
Stephen Street8d94cc52006-12-10 02:18:54 -0800876 }
877
Stephen Streete0c99052006-03-07 23:53:24 -0800878 /* Setup the transfer state based on the type of transfer */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200879 if (pxa2xx_spi_flush(drv_data) == 0) {
Stephen Streete0c99052006-03-07 23:53:24 -0800880 dev_err(&drv_data->pdev->dev, "pump_transfers: flush failed\n");
881 message->status = -EIO;
Stephen Street5daa3ba2006-05-20 15:00:19 -0700882 giveback(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800883 return;
884 }
Stephen Street9708c122006-03-28 14:05:23 -0800885 drv_data->n_bytes = chip->n_bytes;
Stephen Streete0c99052006-03-07 23:53:24 -0800886 drv_data->tx = (void *)transfer->tx_buf;
887 drv_data->tx_end = drv_data->tx + transfer->len;
888 drv_data->rx = transfer->rx_buf;
889 drv_data->rx_end = drv_data->rx + transfer->len;
890 drv_data->rx_dma = transfer->rx_dma;
891 drv_data->tx_dma = transfer->tx_dma;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200892 drv_data->len = transfer->len;
Stephen Streete0c99052006-03-07 23:53:24 -0800893 drv_data->write = drv_data->tx ? chip->write : null_writer;
894 drv_data->read = drv_data->rx ? chip->read : null_reader;
Stephen Street9708c122006-03-28 14:05:23 -0800895
896 /* Change speed and bit per word on a per transfer */
Stephen Street8d94cc52006-12-10 02:18:54 -0800897 cr0 = chip->cr0;
Stephen Street9708c122006-03-28 14:05:23 -0800898 if (transfer->speed_hz || transfer->bits_per_word) {
899
Stephen Street9708c122006-03-28 14:05:23 -0800900 bits = chip->bits_per_word;
901 speed = chip->speed_hz;
902
903 if (transfer->speed_hz)
904 speed = transfer->speed_hz;
905
906 if (transfer->bits_per_word)
907 bits = transfer->bits_per_word;
908
Weike Chene5262d02014-11-26 02:35:10 -0800909 clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, speed);
Stephen Street9708c122006-03-28 14:05:23 -0800910
911 if (bits <= 8) {
912 drv_data->n_bytes = 1;
Stephen Street9708c122006-03-28 14:05:23 -0800913 drv_data->read = drv_data->read != null_reader ?
914 u8_reader : null_reader;
915 drv_data->write = drv_data->write != null_writer ?
916 u8_writer : null_writer;
917 } else if (bits <= 16) {
918 drv_data->n_bytes = 2;
Stephen Street9708c122006-03-28 14:05:23 -0800919 drv_data->read = drv_data->read != null_reader ?
920 u16_reader : null_reader;
921 drv_data->write = drv_data->write != null_writer ?
922 u16_writer : null_writer;
923 } else if (bits <= 32) {
924 drv_data->n_bytes = 4;
Stephen Street9708c122006-03-28 14:05:23 -0800925 drv_data->read = drv_data->read != null_reader ?
926 u32_reader : null_reader;
927 drv_data->write = drv_data->write != null_writer ?
928 u32_writer : null_writer;
929 }
Stephen Street8d94cc52006-12-10 02:18:54 -0800930 /* if bits/word is changed in dma mode, then must check the
931 * thresholds and burst also */
932 if (chip->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200933 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
934 message->spi,
Stephen Street8d94cc52006-12-10 02:18:54 -0800935 bits, &dma_burst,
936 &dma_thresh))
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +0300937 dev_warn_ratelimited(&message->spi->dev,
938 "pump_transfers: DMA burst size reduced to match bits_per_word\n");
Stephen Street8d94cc52006-12-10 02:18:54 -0800939 }
Stephen Street9708c122006-03-28 14:05:23 -0800940
Weike Chen4fdb2422014-10-08 08:50:22 -0700941 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
Stephen Street9708c122006-03-28 14:05:23 -0800942 }
943
Stephen Streete0c99052006-03-07 23:53:24 -0800944 message->state = RUNNING_STATE;
945
Ned Forrester7e964452008-09-13 02:33:18 -0700946 drv_data->dma_mapped = 0;
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200947 if (pxa2xx_spi_dma_is_possible(drv_data->len))
948 drv_data->dma_mapped = pxa2xx_spi_map_dma_buffers(drv_data);
Ned Forrester7e964452008-09-13 02:33:18 -0700949 if (drv_data->dma_mapped) {
Stephen Streete0c99052006-03-07 23:53:24 -0800950
951 /* Ensure we have the correct interrupt handler */
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200952 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
Stephen Streete0c99052006-03-07 23:53:24 -0800953
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200954 pxa2xx_spi_dma_prepare(drv_data, dma_burst);
Stephen Streete0c99052006-03-07 23:53:24 -0800955
Stephen Street8d94cc52006-12-10 02:18:54 -0800956 /* Clear status and start DMA engine */
957 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200958 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
Mika Westerbergcd7bed02013-01-22 12:26:28 +0200959
960 pxa2xx_spi_dma_start(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -0800961 } else {
962 /* Ensure we have the correct interrupt handler */
963 drv_data->transfer_handler = interrupt_transfer;
964
Stephen Street8d94cc52006-12-10 02:18:54 -0800965 /* Clear status */
966 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800967 write_SSSR_CS(drv_data, drv_data->clear_sr);
Stephen Street8d94cc52006-12-10 02:18:54 -0800968 }
969
Mika Westerberga0d26422013-01-22 12:26:32 +0200970 if (is_lpss_ssp(drv_data)) {
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200971 if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
972 != chip->lpss_rx_threshold)
973 pxa2xx_spi_write(drv_data, SSIRF,
974 chip->lpss_rx_threshold);
975 if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
976 != chip->lpss_tx_threshold)
977 pxa2xx_spi_write(drv_data, SSITF,
978 chip->lpss_tx_threshold);
Mika Westerberga0d26422013-01-22 12:26:32 +0200979 }
980
Weike Chene5262d02014-11-26 02:35:10 -0800981 if (is_quark_x1000_ssp(drv_data) &&
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200982 (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
983 pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
Weike Chene5262d02014-11-26 02:35:10 -0800984
Stephen Street8d94cc52006-12-10 02:18:54 -0800985 /* see if we need to reload the config registers */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200986 if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
987 || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
988 != (cr1 & change_mask)) {
Ned Forresterb97c74b2008-02-23 15:23:40 -0800989 /* stop the SSP, and update the other bits */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200990 pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800991 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200992 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800993 /* first set CR1 without interrupt and service enables */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200994 pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800995 /* restart the SSP */
Jarkko Nikulac039dd22014-12-18 15:04:23 +0200996 pxa2xx_spi_write(drv_data, SSCR0, cr0);
Ned Forresterb97c74b2008-02-23 15:23:40 -0800997
Stephen Street8d94cc52006-12-10 02:18:54 -0800998 } else {
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -0800999 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001000 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
Stephen Streete0c99052006-03-07 23:53:24 -08001001 }
Ned Forresterb97c74b2008-02-23 15:23:40 -08001002
Eric Miaoa7bb3902009-04-06 19:00:54 -07001003 cs_assert(drv_data);
Ned Forresterb97c74b2008-02-23 15:23:40 -08001004
1005 /* after chip select, release the data by enabling service
1006 * requests and interrupts, without changing any mode bits */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001007 pxa2xx_spi_write(drv_data, SSCR1, cr1);
Stephen Streete0c99052006-03-07 23:53:24 -08001008}
1009
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001010static int pxa2xx_spi_transfer_one_message(struct spi_master *master,
1011 struct spi_message *msg)
Stephen Streete0c99052006-03-07 23:53:24 -08001012{
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001013 struct driver_data *drv_data = spi_master_get_devdata(master);
Stephen Streete0c99052006-03-07 23:53:24 -08001014
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001015 drv_data->cur_msg = msg;
Stephen Streete0c99052006-03-07 23:53:24 -08001016 /* Initial message state*/
1017 drv_data->cur_msg->state = START_STATE;
1018 drv_data->cur_transfer = list_entry(drv_data->cur_msg->transfers.next,
1019 struct spi_transfer,
1020 transfer_list);
1021
Stephen Street8d94cc52006-12-10 02:18:54 -08001022 /* prepare to setup the SSP, in pump_transfers, using the per
1023 * chip configuration */
Stephen Streete0c99052006-03-07 23:53:24 -08001024 drv_data->cur_chip = spi_get_ctldata(drv_data->cur_msg->spi);
Stephen Streete0c99052006-03-07 23:53:24 -08001025
1026 /* Mark as busy and launch transfers */
1027 tasklet_schedule(&drv_data->pump_transfers);
Stephen Streete0c99052006-03-07 23:53:24 -08001028 return 0;
1029}
1030
Mika Westerberg7d94a502013-01-22 12:26:30 +02001031static int pxa2xx_spi_unprepare_transfer(struct spi_master *master)
1032{
1033 struct driver_data *drv_data = spi_master_get_devdata(master);
1034
1035 /* Disable the SSP now */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001036 pxa2xx_spi_write(drv_data, SSCR0,
1037 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
Mika Westerberg7d94a502013-01-22 12:26:30 +02001038
Mika Westerberg7d94a502013-01-22 12:26:30 +02001039 return 0;
1040}
1041
Eric Miaoa7bb3902009-04-06 19:00:54 -07001042static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1043 struct pxa2xx_spi_chip *chip_info)
1044{
1045 int err = 0;
1046
1047 if (chip == NULL || chip_info == NULL)
1048 return 0;
1049
1050 /* NOTE: setup() can be called multiple times, possibly with
1051 * different chip_info, release previously requested GPIO
1052 */
1053 if (gpio_is_valid(chip->gpio_cs))
1054 gpio_free(chip->gpio_cs);
1055
1056 /* If (*cs_control) is provided, ignore GPIO chip select */
1057 if (chip_info->cs_control) {
1058 chip->cs_control = chip_info->cs_control;
1059 return 0;
1060 }
1061
1062 if (gpio_is_valid(chip_info->gpio_cs)) {
1063 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1064 if (err) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001065 dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1066 chip_info->gpio_cs);
Eric Miaoa7bb3902009-04-06 19:00:54 -07001067 return err;
1068 }
1069
1070 chip->gpio_cs = chip_info->gpio_cs;
1071 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1072
1073 err = gpio_direction_output(chip->gpio_cs,
1074 !chip->gpio_cs_inverted);
1075 }
1076
1077 return err;
1078}
1079
Stephen Streete0c99052006-03-07 23:53:24 -08001080static int setup(struct spi_device *spi)
1081{
1082 struct pxa2xx_spi_chip *chip_info = NULL;
1083 struct chip_data *chip;
1084 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
1085 unsigned int clk_div;
Mika Westerberga0d26422013-01-22 12:26:32 +02001086 uint tx_thres, tx_hi_thres, rx_thres;
1087
Weike Chene5262d02014-11-26 02:35:10 -08001088 switch (drv_data->ssp_type) {
1089 case QUARK_X1000_SSP:
1090 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1091 tx_hi_thres = 0;
1092 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1093 break;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001094 case LPSS_LPT_SSP:
1095 case LPSS_BYT_SSP:
Mika Westerberga0d26422013-01-22 12:26:32 +02001096 tx_thres = LPSS_TX_LOTHRESH_DFLT;
1097 tx_hi_thres = LPSS_TX_HITHRESH_DFLT;
1098 rx_thres = LPSS_RX_THRESH_DFLT;
Weike Chene5262d02014-11-26 02:35:10 -08001099 break;
1100 default:
Mika Westerberga0d26422013-01-22 12:26:32 +02001101 tx_thres = TX_THRESH_DFLT;
1102 tx_hi_thres = 0;
1103 rx_thres = RX_THRESH_DFLT;
Weike Chene5262d02014-11-26 02:35:10 -08001104 break;
Mika Westerberga0d26422013-01-22 12:26:32 +02001105 }
Stephen Streete0c99052006-03-07 23:53:24 -08001106
Stephen Street8d94cc52006-12-10 02:18:54 -08001107 /* Only alloc on first setup */
Stephen Streete0c99052006-03-07 23:53:24 -08001108 chip = spi_get_ctldata(spi);
Stephen Street8d94cc52006-12-10 02:18:54 -08001109 if (!chip) {
Stephen Streete0c99052006-03-07 23:53:24 -08001110 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001111 if (!chip)
Stephen Streete0c99052006-03-07 23:53:24 -08001112 return -ENOMEM;
1113
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001114 if (drv_data->ssp_type == CE4100_SSP) {
1115 if (spi->chip_select > 4) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001116 dev_err(&spi->dev,
1117 "failed setup: cs number must not be > 4.\n");
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001118 kfree(chip);
1119 return -EINVAL;
1120 }
1121
1122 chip->frm = spi->chip_select;
1123 } else
1124 chip->gpio_cs = -1;
Stephen Streete0c99052006-03-07 23:53:24 -08001125 chip->enable_dma = 0;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001126 chip->timeout = TIMOUT_DFLT;
Stephen Streete0c99052006-03-07 23:53:24 -08001127 }
1128
Stephen Street8d94cc52006-12-10 02:18:54 -08001129 /* protocol drivers may change the chip settings, so...
1130 * if chip_info exists, use it */
1131 chip_info = spi->controller_data;
1132
Stephen Streete0c99052006-03-07 23:53:24 -08001133 /* chip_info isn't always needed */
Stephen Street8d94cc52006-12-10 02:18:54 -08001134 chip->cr1 = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001135 if (chip_info) {
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001136 if (chip_info->timeout)
1137 chip->timeout = chip_info->timeout;
1138 if (chip_info->tx_threshold)
1139 tx_thres = chip_info->tx_threshold;
Mika Westerberga0d26422013-01-22 12:26:32 +02001140 if (chip_info->tx_hi_threshold)
1141 tx_hi_thres = chip_info->tx_hi_threshold;
Vernon Sauderf1f640a2008-10-15 22:02:43 -07001142 if (chip_info->rx_threshold)
1143 rx_thres = chip_info->rx_threshold;
1144 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -08001145 chip->dma_threshold = 0;
Stephen Streete0c99052006-03-07 23:53:24 -08001146 if (chip_info->enable_loopback)
1147 chip->cr1 = SSCR1_LBM;
Mika Westerberga3496852013-01-22 12:26:33 +02001148 } else if (ACPI_HANDLE(&spi->dev)) {
1149 /*
1150 * Slave devices enumerated from ACPI namespace don't
1151 * usually have chip_info but we still might want to use
1152 * DMA with them.
1153 */
1154 chip->enable_dma = drv_data->master_info->enable_dma;
Stephen Streete0c99052006-03-07 23:53:24 -08001155 }
1156
Mika Westerberga0d26422013-01-22 12:26:32 +02001157 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1158 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1159 | SSITF_TxHiThresh(tx_hi_thres);
1160
Stephen Street8d94cc52006-12-10 02:18:54 -08001161 /* set dma burst and threshold outside of chip_info path so that if
1162 * chip_info goes away after setting chip->enable_dma, the
1163 * burst and threshold can still respond to changes in bits_per_word */
1164 if (chip->enable_dma) {
1165 /* set up legal burst and threshold for dma */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001166 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1167 spi->bits_per_word,
Stephen Street8d94cc52006-12-10 02:18:54 -08001168 &chip->dma_burst_size,
1169 &chip->dma_threshold)) {
Jarkko Nikulaf6bd03a2013-10-11 13:54:00 +03001170 dev_warn(&spi->dev,
1171 "in setup: DMA burst size reduced to match bits_per_word\n");
Stephen Street8d94cc52006-12-10 02:18:54 -08001172 }
1173 }
1174
Weike Chene5262d02014-11-26 02:35:10 -08001175 clk_div = pxa2xx_ssp_get_clk_div(drv_data, chip, spi->max_speed_hz);
Stephen Street9708c122006-03-28 14:05:23 -08001176 chip->speed_hz = spi->max_speed_hz;
Stephen Streete0c99052006-03-07 23:53:24 -08001177
Weike Chen4fdb2422014-10-08 08:50:22 -07001178 chip->cr0 = pxa2xx_configure_sscr0(drv_data, clk_div,
1179 spi->bits_per_word);
Weike Chene5262d02014-11-26 02:35:10 -08001180 switch (drv_data->ssp_type) {
1181 case QUARK_X1000_SSP:
1182 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1183 & QUARK_X1000_SSCR1_RFT)
1184 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1185 & QUARK_X1000_SSCR1_TFT);
1186 break;
1187 default:
1188 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1189 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1190 break;
1191 }
1192
Justin Clacherty7f6ee1a2007-01-26 00:56:44 -08001193 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1194 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1195 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001196
Mika Westerbergb8331722013-01-22 12:26:31 +02001197 if (spi->mode & SPI_LOOP)
1198 chip->cr1 |= SSCR1_LBM;
1199
Stephen Streete0c99052006-03-07 23:53:24 -08001200 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001201 if (!pxa25x_ssp_comp(drv_data))
David Brownell7d077192009-06-17 16:26:03 -07001202 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001203 drv_data->max_clk_rate
Eric Miaoc9840da2010-03-16 16:48:01 +08001204 / (1 + ((chip->cr0 & SSCR0_SCR(0xfff)) >> 8)),
1205 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001206 else
David Brownell7d077192009-06-17 16:26:03 -07001207 dev_dbg(&spi->dev, "%ld Hz actual, %s\n",
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001208 drv_data->max_clk_rate / 2
Eric Miaoc9840da2010-03-16 16:48:01 +08001209 / (1 + ((chip->cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1210 chip->enable_dma ? "DMA" : "PIO");
Stephen Streete0c99052006-03-07 23:53:24 -08001211
1212 if (spi->bits_per_word <= 8) {
1213 chip->n_bytes = 1;
Stephen Streete0c99052006-03-07 23:53:24 -08001214 chip->read = u8_reader;
1215 chip->write = u8_writer;
1216 } else if (spi->bits_per_word <= 16) {
1217 chip->n_bytes = 2;
Stephen Streete0c99052006-03-07 23:53:24 -08001218 chip->read = u16_reader;
1219 chip->write = u16_writer;
1220 } else if (spi->bits_per_word <= 32) {
Weike Chene5262d02014-11-26 02:35:10 -08001221 if (!is_quark_x1000_ssp(drv_data))
1222 chip->cr0 |= SSCR0_EDSS;
Stephen Streete0c99052006-03-07 23:53:24 -08001223 chip->n_bytes = 4;
Stephen Streete0c99052006-03-07 23:53:24 -08001224 chip->read = u32_reader;
1225 chip->write = u32_writer;
Stephen Streete0c99052006-03-07 23:53:24 -08001226 }
Stephen Street9708c122006-03-28 14:05:23 -08001227 chip->bits_per_word = spi->bits_per_word;
Stephen Streete0c99052006-03-07 23:53:24 -08001228
1229 spi_set_ctldata(spi, chip);
1230
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001231 if (drv_data->ssp_type == CE4100_SSP)
1232 return 0;
1233
Eric Miaoa7bb3902009-04-06 19:00:54 -07001234 return setup_cs(spi, chip, chip_info);
Stephen Streete0c99052006-03-07 23:53:24 -08001235}
1236
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001237static void cleanup(struct spi_device *spi)
Stephen Streete0c99052006-03-07 23:53:24 -08001238{
Hans-Peter Nilsson0ffa0282007-02-12 00:52:45 -08001239 struct chip_data *chip = spi_get_ctldata(spi);
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001240 struct driver_data *drv_data = spi_master_get_devdata(spi->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001241
Daniel Ribeiro7348d822009-05-12 13:19:36 -07001242 if (!chip)
1243 return;
1244
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001245 if (drv_data->ssp_type != CE4100_SSP && gpio_is_valid(chip->gpio_cs))
Eric Miaoa7bb3902009-04-06 19:00:54 -07001246 gpio_free(chip->gpio_cs);
1247
Stephen Streete0c99052006-03-07 23:53:24 -08001248 kfree(chip);
1249}
1250
Mika Westerberga3496852013-01-22 12:26:33 +02001251#ifdef CONFIG_ACPI
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001252
1253static struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1254 { "INT33C0", LPSS_LPT_SSP },
1255 { "INT33C1", LPSS_LPT_SSP },
1256 { "INT3430", LPSS_LPT_SSP },
1257 { "INT3431", LPSS_LPT_SSP },
1258 { "80860F0E", LPSS_BYT_SSP },
1259 { "8086228E", LPSS_BYT_SSP },
1260 { },
1261};
1262MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1263
Mika Westerberga3496852013-01-22 12:26:33 +02001264static struct pxa2xx_spi_master *
1265pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1266{
1267 struct pxa2xx_spi_master *pdata;
Mika Westerberga3496852013-01-22 12:26:33 +02001268 struct acpi_device *adev;
1269 struct ssp_device *ssp;
1270 struct resource *res;
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001271 const struct acpi_device_id *id;
1272 int devid, type;
Mika Westerberga3496852013-01-22 12:26:33 +02001273
1274 if (!ACPI_HANDLE(&pdev->dev) ||
1275 acpi_bus_get_device(ACPI_HANDLE(&pdev->dev), &adev))
1276 return NULL;
1277
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001278 id = acpi_match_device(pdev->dev.driver->acpi_match_table, &pdev->dev);
1279 if (id)
1280 type = (int)id->driver_data;
1281 else
1282 return NULL;
1283
Mika Westerbergcc0ee982013-06-20 17:44:22 +03001284 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
Jingoo Han9deae452014-04-29 17:19:38 +09001285 if (!pdata)
Mika Westerberga3496852013-01-22 12:26:33 +02001286 return NULL;
Mika Westerberga3496852013-01-22 12:26:33 +02001287
1288 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1289 if (!res)
1290 return NULL;
1291
1292 ssp = &pdata->ssp;
1293
1294 ssp->phys_base = res->start;
Sachin Kamatcbfd6a22013-04-08 15:49:33 +05301295 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1296 if (IS_ERR(ssp->mmio_base))
Mika Westerberg6dc81f62013-05-13 13:45:09 +03001297 return NULL;
Mika Westerberga3496852013-01-22 12:26:33 +02001298
1299 ssp->clk = devm_clk_get(&pdev->dev, NULL);
1300 ssp->irq = platform_get_irq(pdev, 0);
Jarkko Nikula03fbf482015-06-04 16:55:10 +03001301 ssp->type = type;
Mika Westerberga3496852013-01-22 12:26:33 +02001302 ssp->pdev = pdev;
1303
1304 ssp->port_id = -1;
1305 if (adev->pnp.unique_id && !kstrtoint(adev->pnp.unique_id, 0, &devid))
1306 ssp->port_id = devid;
1307
1308 pdata->num_chipselect = 1;
Mika Westerbergcddb3392013-05-13 13:45:10 +03001309 pdata->enable_dma = true;
Mika Westerberga3496852013-01-22 12:26:33 +02001310
1311 return pdata;
1312}
1313
Mika Westerberga3496852013-01-22 12:26:33 +02001314#else
1315static inline struct pxa2xx_spi_master *
1316pxa2xx_spi_acpi_get_pdata(struct platform_device *pdev)
1317{
1318 return NULL;
1319}
1320#endif
1321
Grant Likelyfd4a3192012-12-07 16:57:14 +00001322static int pxa2xx_spi_probe(struct platform_device *pdev)
Stephen Streete0c99052006-03-07 23:53:24 -08001323{
1324 struct device *dev = &pdev->dev;
1325 struct pxa2xx_spi_master *platform_info;
1326 struct spi_master *master;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001327 struct driver_data *drv_data;
eric miao2f1a74e2007-11-21 18:50:53 +08001328 struct ssp_device *ssp;
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001329 int status;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001330 u32 tmp;
Stephen Streete0c99052006-03-07 23:53:24 -08001331
Mika Westerberg851bacf2013-01-07 12:44:33 +02001332 platform_info = dev_get_platdata(dev);
1333 if (!platform_info) {
Mika Westerberga3496852013-01-22 12:26:33 +02001334 platform_info = pxa2xx_spi_acpi_get_pdata(pdev);
1335 if (!platform_info) {
1336 dev_err(&pdev->dev, "missing platform data\n");
1337 return -ENODEV;
1338 }
Mika Westerberg851bacf2013-01-07 12:44:33 +02001339 }
Stephen Streete0c99052006-03-07 23:53:24 -08001340
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001341 ssp = pxa_ssp_request(pdev->id, pdev->name);
Mika Westerberg851bacf2013-01-07 12:44:33 +02001342 if (!ssp)
1343 ssp = &platform_info->ssp;
1344
1345 if (!ssp->mmio_base) {
1346 dev_err(&pdev->dev, "failed to get ssp\n");
Stephen Streete0c99052006-03-07 23:53:24 -08001347 return -ENODEV;
1348 }
1349
1350 /* Allocate master with space for drv_data and null dma buffer */
1351 master = spi_alloc_master(dev, sizeof(struct driver_data) + 16);
1352 if (!master) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001353 dev_err(&pdev->dev, "cannot alloc spi_master\n");
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001354 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001355 return -ENOMEM;
1356 }
1357 drv_data = spi_master_get_devdata(master);
1358 drv_data->master = master;
1359 drv_data->master_info = platform_info;
1360 drv_data->pdev = pdev;
eric miao2f1a74e2007-11-21 18:50:53 +08001361 drv_data->ssp = ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001362
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001363 master->dev.parent = &pdev->dev;
Sebastian Andrzej Siewior21486af2010-10-08 18:11:19 +02001364 master->dev.of_node = pdev->dev.of_node;
David Brownelle7db06b2009-06-17 16:26:04 -07001365 /* the spi->mode bits understood by this driver: */
Mika Westerbergb8331722013-01-22 12:26:31 +02001366 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
David Brownelle7db06b2009-06-17 16:26:04 -07001367
Mika Westerberg851bacf2013-01-07 12:44:33 +02001368 master->bus_num = ssp->port_id;
Stephen Streete0c99052006-03-07 23:53:24 -08001369 master->num_chipselect = platform_info->num_chipselect;
Mike Rapoport7ad0ba92009-04-06 19:00:57 -07001370 master->dma_alignment = DMA_ALIGNMENT;
Stephen Streete0c99052006-03-07 23:53:24 -08001371 master->cleanup = cleanup;
1372 master->setup = setup;
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001373 master->transfer_one_message = pxa2xx_spi_transfer_one_message;
Mika Westerberg7d94a502013-01-22 12:26:30 +02001374 master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
Mark Brown7dd62782013-07-28 15:35:21 +01001375 master->auto_runtime_pm = true;
Stephen Streete0c99052006-03-07 23:53:24 -08001376
eric miao2f1a74e2007-11-21 18:50:53 +08001377 drv_data->ssp_type = ssp->type;
Mika Westerberg2b9b84f2013-01-22 12:26:25 +02001378 drv_data->null_dma_buf = (u32 *)PTR_ALIGN(&drv_data[1], DMA_ALIGNMENT);
Stephen Streete0c99052006-03-07 23:53:24 -08001379
eric miao2f1a74e2007-11-21 18:50:53 +08001380 drv_data->ioaddr = ssp->mmio_base;
1381 drv_data->ssdr_physical = ssp->phys_base + SSDR;
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001382 if (pxa25x_ssp_comp(drv_data)) {
Weike Chene5262d02014-11-26 02:35:10 -08001383 switch (drv_data->ssp_type) {
1384 case QUARK_X1000_SSP:
1385 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1386 break;
1387 default:
1388 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1389 break;
1390 }
1391
Stephen Streete0c99052006-03-07 23:53:24 -08001392 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1393 drv_data->dma_cr1 = 0;
1394 drv_data->clear_sr = SSSR_ROR;
1395 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1396 } else {
Stephen Warren24778be2013-05-21 20:36:35 -06001397 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
Stephen Streete0c99052006-03-07 23:53:24 -08001398 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
Mika Westerberg59288082013-01-22 12:26:29 +02001399 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
Stephen Streete0c99052006-03-07 23:53:24 -08001400 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1401 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1402 }
1403
Sebastian Andrzej Siewior49cbb1e2010-11-22 17:12:14 -08001404 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1405 drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001406 if (status < 0) {
Guennadi Liakhovetski65a00a22008-10-15 22:02:42 -07001407 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
Stephen Streete0c99052006-03-07 23:53:24 -08001408 goto out_error_master_alloc;
1409 }
1410
1411 /* Setup DMA if requested */
1412 drv_data->tx_channel = -1;
1413 drv_data->rx_channel = -1;
1414 if (platform_info->enable_dma) {
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001415 status = pxa2xx_spi_dma_setup(drv_data);
1416 if (status) {
Mika Westerbergcddb3392013-05-13 13:45:10 +03001417 dev_dbg(dev, "no DMA channels available, using PIO\n");
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001418 platform_info->enable_dma = false;
Stephen Streete0c99052006-03-07 23:53:24 -08001419 }
Stephen Streete0c99052006-03-07 23:53:24 -08001420 }
1421
1422 /* Enable SOC clock */
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001423 clk_prepare_enable(ssp->clk);
1424
1425 drv_data->max_clk_rate = clk_get_rate(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001426
1427 /* Load default SSP configuration */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001428 pxa2xx_spi_write(drv_data, SSCR0, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001429 switch (drv_data->ssp_type) {
1430 case QUARK_X1000_SSP:
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001431 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT)
1432 | QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1433 pxa2xx_spi_write(drv_data, SSCR1, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001434
1435 /* using the Motorola SPI protocol and use 8 bit frame */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001436 pxa2xx_spi_write(drv_data, SSCR0,
1437 QUARK_X1000_SSCR0_Motorola
1438 | QUARK_X1000_SSCR0_DataSize(8));
Weike Chene5262d02014-11-26 02:35:10 -08001439 break;
1440 default:
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001441 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1442 SSCR1_TxTresh(TX_THRESH_DFLT);
1443 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1444 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1445 pxa2xx_spi_write(drv_data, SSCR0, tmp);
Weike Chene5262d02014-11-26 02:35:10 -08001446 break;
1447 }
1448
Sebastian Andrzej Siewior2a8626a2010-11-22 17:12:17 -08001449 if (!pxa25x_ssp_comp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001450 pxa2xx_spi_write(drv_data, SSTO, 0);
Weike Chene5262d02014-11-26 02:35:10 -08001451
1452 if (!is_quark_x1000_ssp(drv_data))
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001453 pxa2xx_spi_write(drv_data, SSPSP, 0);
Stephen Streete0c99052006-03-07 23:53:24 -08001454
Jarkko Nikula7566bcc2014-12-18 15:04:20 +02001455 if (is_lpss_ssp(drv_data))
1456 lpss_ssp_setup(drv_data);
Mika Westerberga0d26422013-01-22 12:26:32 +02001457
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001458 tasklet_init(&drv_data->pump_transfers, pump_transfers,
1459 (unsigned long)drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001460
Antonio Ospite836d1a222014-05-30 18:18:09 +02001461 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1462 pm_runtime_use_autosuspend(&pdev->dev);
1463 pm_runtime_set_active(&pdev->dev);
1464 pm_runtime_enable(&pdev->dev);
1465
Stephen Streete0c99052006-03-07 23:53:24 -08001466 /* Register with the SPI framework */
1467 platform_set_drvdata(pdev, drv_data);
Jingoo Hana807fcd2013-09-24 13:46:55 +09001468 status = devm_spi_register_master(&pdev->dev, master);
Stephen Streete0c99052006-03-07 23:53:24 -08001469 if (status != 0) {
1470 dev_err(&pdev->dev, "problem registering spi master\n");
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001471 goto out_error_clock_enabled;
Stephen Streete0c99052006-03-07 23:53:24 -08001472 }
1473
1474 return status;
1475
Stephen Streete0c99052006-03-07 23:53:24 -08001476out_error_clock_enabled:
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001477 clk_disable_unprepare(ssp->clk);
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001478 pxa2xx_spi_dma_release(drv_data);
eric miao2f1a74e2007-11-21 18:50:53 +08001479 free_irq(ssp->irq, drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001480
1481out_error_master_alloc:
1482 spi_master_put(master);
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001483 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001484 return status;
1485}
1486
1487static int pxa2xx_spi_remove(struct platform_device *pdev)
1488{
1489 struct driver_data *drv_data = platform_get_drvdata(pdev);
Julia Lawall51e911e2009-01-06 14:41:45 -08001490 struct ssp_device *ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001491
1492 if (!drv_data)
1493 return 0;
Julia Lawall51e911e2009-01-06 14:41:45 -08001494 ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001495
Mika Westerberg7d94a502013-01-22 12:26:30 +02001496 pm_runtime_get_sync(&pdev->dev);
1497
Stephen Streete0c99052006-03-07 23:53:24 -08001498 /* Disable the SSP at the peripheral and SOC level */
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001499 pxa2xx_spi_write(drv_data, SSCR0, 0);
Mika Westerberg3343b7a2013-01-22 12:26:27 +02001500 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001501
1502 /* Release DMA */
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001503 if (drv_data->master_info->enable_dma)
1504 pxa2xx_spi_dma_release(drv_data);
Stephen Streete0c99052006-03-07 23:53:24 -08001505
Mika Westerberg7d94a502013-01-22 12:26:30 +02001506 pm_runtime_put_noidle(&pdev->dev);
1507 pm_runtime_disable(&pdev->dev);
1508
Stephen Streete0c99052006-03-07 23:53:24 -08001509 /* Release IRQ */
eric miao2f1a74e2007-11-21 18:50:53 +08001510 free_irq(ssp->irq, drv_data);
1511
1512 /* Release SSP */
Haojian Zhuangbaffe162010-05-05 10:11:15 -04001513 pxa_ssp_free(ssp);
Stephen Streete0c99052006-03-07 23:53:24 -08001514
Stephen Streete0c99052006-03-07 23:53:24 -08001515 return 0;
1516}
1517
1518static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1519{
1520 int status = 0;
1521
1522 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1523 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1524}
1525
Mika Westerberg382cebb2014-01-16 14:50:55 +02001526#ifdef CONFIG_PM_SLEEP
Mike Rapoport86d25932009-07-21 17:50:16 +03001527static int pxa2xx_spi_suspend(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001528{
Mike Rapoport86d25932009-07-21 17:50:16 +03001529 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001530 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001531 int status = 0;
1532
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001533 status = spi_master_suspend(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001534 if (status != 0)
1535 return status;
Jarkko Nikulac039dd22014-12-18 15:04:23 +02001536 pxa2xx_spi_write(drv_data, SSCR0, 0);
Dmitry Eremin-Solenikov2b9375b2014-11-06 14:08:29 +03001537
1538 if (!pm_runtime_suspended(dev))
1539 clk_disable_unprepare(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001540
1541 return 0;
1542}
1543
Mike Rapoport86d25932009-07-21 17:50:16 +03001544static int pxa2xx_spi_resume(struct device *dev)
Stephen Streete0c99052006-03-07 23:53:24 -08001545{
Mike Rapoport86d25932009-07-21 17:50:16 +03001546 struct driver_data *drv_data = dev_get_drvdata(dev);
eric miao2f1a74e2007-11-21 18:50:53 +08001547 struct ssp_device *ssp = drv_data->ssp;
Stephen Streete0c99052006-03-07 23:53:24 -08001548 int status = 0;
1549
Mika Westerbergcd7bed02013-01-22 12:26:28 +02001550 pxa2xx_spi_dma_resume(drv_data);
Daniel Ribeiro148da332009-04-21 12:24:43 -07001551
Stephen Streete0c99052006-03-07 23:53:24 -08001552 /* Enable the SSP clock */
Dmitry Eremin-Solenikov2b9375b2014-11-06 14:08:29 +03001553 if (!pm_runtime_suspended(dev))
1554 clk_prepare_enable(ssp->clk);
Stephen Streete0c99052006-03-07 23:53:24 -08001555
Chew, Chiau Eec50325f2013-11-29 02:13:11 +08001556 /* Restore LPSS private register bits */
Jarkko Nikula48421ad2015-01-28 10:09:42 +02001557 if (is_lpss_ssp(drv_data))
1558 lpss_ssp_setup(drv_data);
Chew, Chiau Eec50325f2013-11-29 02:13:11 +08001559
Stephen Streete0c99052006-03-07 23:53:24 -08001560 /* Start the queue running */
Mika Westerberg7f86bde2013-01-22 12:26:26 +02001561 status = spi_master_resume(drv_data->master);
Stephen Streete0c99052006-03-07 23:53:24 -08001562 if (status != 0) {
Mike Rapoport86d25932009-07-21 17:50:16 +03001563 dev_err(dev, "problem starting queue (%d)\n", status);
Stephen Streete0c99052006-03-07 23:53:24 -08001564 return status;
1565 }
1566
1567 return 0;
1568}
Mika Westerberg7d94a502013-01-22 12:26:30 +02001569#endif
1570
Rafael J. Wysockiec833052014-12-13 00:41:15 +01001571#ifdef CONFIG_PM
Mika Westerberg7d94a502013-01-22 12:26:30 +02001572static int pxa2xx_spi_runtime_suspend(struct device *dev)
1573{
1574 struct driver_data *drv_data = dev_get_drvdata(dev);
1575
1576 clk_disable_unprepare(drv_data->ssp->clk);
1577 return 0;
1578}
1579
1580static int pxa2xx_spi_runtime_resume(struct device *dev)
1581{
1582 struct driver_data *drv_data = dev_get_drvdata(dev);
1583
1584 clk_prepare_enable(drv_data->ssp->clk);
1585 return 0;
1586}
1587#endif
Mike Rapoport86d25932009-07-21 17:50:16 +03001588
Alexey Dobriyan47145212009-12-14 18:00:08 -08001589static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
Mika Westerberg7d94a502013-01-22 12:26:30 +02001590 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1591 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1592 pxa2xx_spi_runtime_resume, NULL)
Mike Rapoport86d25932009-07-21 17:50:16 +03001593};
Stephen Streete0c99052006-03-07 23:53:24 -08001594
1595static struct platform_driver driver = {
1596 .driver = {
Mike Rapoport86d25932009-07-21 17:50:16 +03001597 .name = "pxa2xx-spi",
Mike Rapoport86d25932009-07-21 17:50:16 +03001598 .pm = &pxa2xx_spi_pm_ops,
Mika Westerberga3496852013-01-22 12:26:33 +02001599 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
Stephen Streete0c99052006-03-07 23:53:24 -08001600 },
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001601 .probe = pxa2xx_spi_probe,
David Brownelld1e44d92007-10-16 01:27:46 -07001602 .remove = pxa2xx_spi_remove,
Stephen Streete0c99052006-03-07 23:53:24 -08001603 .shutdown = pxa2xx_spi_shutdown,
Stephen Streete0c99052006-03-07 23:53:24 -08001604};
1605
1606static int __init pxa2xx_spi_init(void)
1607{
Sebastian Andrzej Siewiorfbd29a12010-11-19 09:00:11 -08001608 return platform_driver_register(&driver);
Stephen Streete0c99052006-03-07 23:53:24 -08001609}
Antonio Ospite5b61a742009-09-22 16:46:10 -07001610subsys_initcall(pxa2xx_spi_init);
Stephen Streete0c99052006-03-07 23:53:24 -08001611
1612static void __exit pxa2xx_spi_exit(void)
1613{
1614 platform_driver_unregister(&driver);
1615}
1616module_exit(pxa2xx_spi_exit);