1 /*
2 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
3 * Copyright (C) 2013, Intel Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16 #include <linux/bitops.h>
17 #include <linux/init.h>
18 #include <linux/module.h>
19 #include <linux/device.h>
20 #include <linux/ioport.h>
21 #include <linux/errno.h>
22 #include <linux/err.h>
23 #include <linux/interrupt.h>
24 #include <linux/kernel.h>
25 #include <linux/pci.h>
26 #include <linux/platform_device.h>
27 #include <linux/spi/pxa2xx_spi.h>
28 #include <linux/spi/spi.h>
29 #include <linux/delay.h>
30 #include <linux/gpio.h>
31 #include <linux/gpio/consumer.h>
32 #include <linux/slab.h>
33 #include <linux/clk.h>
34 #include <linux/pm_runtime.h>
35 #include <linux/acpi.h>
36
37 #include "spi-pxa2xx.h"
38
39 MODULE_AUTHOR("Stephen Street");
40 MODULE_DESCRIPTION("PXA2xx SSP SPI Controller");
41 MODULE_LICENSE("GPL");
42 MODULE_ALIAS("platform:pxa2xx-spi");
43
44 #define TIMOUT_DFLT 1000
45
46 /*
47 * for testing SSCR1 changes that require SSP restart, basically
48 * everything except the service and interrupt enables, the pxa270 developer
49 * manual says only SSCR1_SCFR, SSCR1_SPH, SSCR1_SPO need to be in this
50 * list, but the PXA255 dev man says all bits without really meaning the
51 * service and interrupt enables
52 */
53 #define SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
54 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
55 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
56 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
57 | SSCR1_RFT | SSCR1_TFT | SSCR1_MWDS \
58 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
59
60 #define QUARK_X1000_SSCR1_CHANGE_MASK (QUARK_X1000_SSCR1_STRF \
61 | QUARK_X1000_SSCR1_EFWR \
62 | QUARK_X1000_SSCR1_RFT \
63 | QUARK_X1000_SSCR1_TFT \
64 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
65
66 #define CE4100_SSCR1_CHANGE_MASK (SSCR1_TTELP | SSCR1_TTE | SSCR1_SCFR \
67 | SSCR1_ECRA | SSCR1_ECRB | SSCR1_SCLKDIR \
68 | SSCR1_SFRMDIR | SSCR1_RWOT | SSCR1_TRAIL \
69 | SSCR1_IFS | SSCR1_STRF | SSCR1_EFWR \
70 | CE4100_SSCR1_RFT | CE4100_SSCR1_TFT | SSCR1_MWDS \
71 | SSCR1_SPH | SSCR1_SPO | SSCR1_LBM)
72
73 #define LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE BIT(24)
74 #define LPSS_CS_CONTROL_SW_MODE BIT(0)
75 #define LPSS_CS_CONTROL_CS_HIGH BIT(1)
76 #define LPSS_CAPS_CS_EN_SHIFT 9
77 #define LPSS_CAPS_CS_EN_MASK (0xf << LPSS_CAPS_CS_EN_SHIFT)
78
79 #define LPSS_PRIV_CLOCK_GATE 0x38
80 #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK 0x3
81 #define LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON 0x3
82
83 struct lpss_config {
84 /* LPSS offset from drv_data->ioaddr */
85 unsigned offset;
86 /* Register offsets from drv_data->lpss_base or -1 */
87 int reg_general;
88 int reg_ssp;
89 int reg_cs_ctrl;
90 int reg_capabilities;
91 /* FIFO thresholds */
92 u32 rx_threshold;
93 u32 tx_threshold_lo;
94 u32 tx_threshold_hi;
95 /* Chip select control */
96 unsigned cs_sel_shift;
97 unsigned cs_sel_mask;
98 unsigned cs_num;
99 /* Quirks */
100 unsigned cs_clk_stays_gated : 1;
101 };
102
103 /* Keep these sorted with enum pxa_ssp_type */
104 static const struct lpss_config lpss_platforms[] = {
105 { /* LPSS_LPT_SSP */
106 .offset = 0x800,
107 .reg_general = 0x08,
108 .reg_ssp = 0x0c,
109 .reg_cs_ctrl = 0x18,
110 .reg_capabilities = -1,
111 .rx_threshold = 64,
112 .tx_threshold_lo = 160,
113 .tx_threshold_hi = 224,
114 },
115 { /* LPSS_BYT_SSP */
116 .offset = 0x400,
117 .reg_general = 0x08,
118 .reg_ssp = 0x0c,
119 .reg_cs_ctrl = 0x18,
120 .reg_capabilities = -1,
121 .rx_threshold = 64,
122 .tx_threshold_lo = 160,
123 .tx_threshold_hi = 224,
124 },
125 { /* LPSS_BSW_SSP */
126 .offset = 0x400,
127 .reg_general = 0x08,
128 .reg_ssp = 0x0c,
129 .reg_cs_ctrl = 0x18,
130 .reg_capabilities = -1,
131 .rx_threshold = 64,
132 .tx_threshold_lo = 160,
133 .tx_threshold_hi = 224,
134 .cs_sel_shift = 2,
135 .cs_sel_mask = 1 << 2,
136 .cs_num = 2,
137 },
138 { /* LPSS_SPT_SSP */
139 .offset = 0x200,
140 .reg_general = -1,
141 .reg_ssp = 0x20,
142 .reg_cs_ctrl = 0x24,
143 .reg_capabilities = -1,
144 .rx_threshold = 1,
145 .tx_threshold_lo = 32,
146 .tx_threshold_hi = 56,
147 },
148 { /* LPSS_BXT_SSP */
149 .offset = 0x200,
150 .reg_general = -1,
151 .reg_ssp = 0x20,
152 .reg_cs_ctrl = 0x24,
153 .reg_capabilities = 0xfc,
154 .rx_threshold = 1,
155 .tx_threshold_lo = 16,
156 .tx_threshold_hi = 48,
157 .cs_sel_shift = 8,
158 .cs_sel_mask = 3 << 8,
159 .cs_clk_stays_gated = true,
160 },
161 { /* LPSS_CNL_SSP */
162 .offset = 0x200,
163 .reg_general = -1,
164 .reg_ssp = 0x20,
165 .reg_cs_ctrl = 0x24,
166 .reg_capabilities = 0xfc,
167 .rx_threshold = 1,
168 .tx_threshold_lo = 32,
169 .tx_threshold_hi = 56,
170 .cs_sel_shift = 8,
171 .cs_sel_mask = 3 << 8,
172 .cs_clk_stays_gated = true,
173 },
174 };
175
176 static inline const struct lpss_config
lpss_get_config(const struct driver_data * drv_data)177 *lpss_get_config(const struct driver_data *drv_data)
178 {
179 return &lpss_platforms[drv_data->ssp_type - LPSS_LPT_SSP];
180 }
181
is_lpss_ssp(const struct driver_data * drv_data)182 static bool is_lpss_ssp(const struct driver_data *drv_data)
183 {
184 switch (drv_data->ssp_type) {
185 case LPSS_LPT_SSP:
186 case LPSS_BYT_SSP:
187 case LPSS_BSW_SSP:
188 case LPSS_SPT_SSP:
189 case LPSS_BXT_SSP:
190 case LPSS_CNL_SSP:
191 return true;
192 default:
193 return false;
194 }
195 }
196
is_quark_x1000_ssp(const struct driver_data * drv_data)197 static bool is_quark_x1000_ssp(const struct driver_data *drv_data)
198 {
199 return drv_data->ssp_type == QUARK_X1000_SSP;
200 }
201
pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data * drv_data)202 static u32 pxa2xx_spi_get_ssrc1_change_mask(const struct driver_data *drv_data)
203 {
204 switch (drv_data->ssp_type) {
205 case QUARK_X1000_SSP:
206 return QUARK_X1000_SSCR1_CHANGE_MASK;
207 case CE4100_SSP:
208 return CE4100_SSCR1_CHANGE_MASK;
209 default:
210 return SSCR1_CHANGE_MASK;
211 }
212 }
213
214 static u32
pxa2xx_spi_get_rx_default_thre(const struct driver_data * drv_data)215 pxa2xx_spi_get_rx_default_thre(const struct driver_data *drv_data)
216 {
217 switch (drv_data->ssp_type) {
218 case QUARK_X1000_SSP:
219 return RX_THRESH_QUARK_X1000_DFLT;
220 case CE4100_SSP:
221 return RX_THRESH_CE4100_DFLT;
222 default:
223 return RX_THRESH_DFLT;
224 }
225 }
226
pxa2xx_spi_txfifo_full(const struct driver_data * drv_data)227 static bool pxa2xx_spi_txfifo_full(const struct driver_data *drv_data)
228 {
229 u32 mask;
230
231 switch (drv_data->ssp_type) {
232 case QUARK_X1000_SSP:
233 mask = QUARK_X1000_SSSR_TFL_MASK;
234 break;
235 case CE4100_SSP:
236 mask = CE4100_SSSR_TFL_MASK;
237 break;
238 default:
239 mask = SSSR_TFL_MASK;
240 break;
241 }
242
243 return (pxa2xx_spi_read(drv_data, SSSR) & mask) == mask;
244 }
245
pxa2xx_spi_clear_rx_thre(const struct driver_data * drv_data,u32 * sccr1_reg)246 static void pxa2xx_spi_clear_rx_thre(const struct driver_data *drv_data,
247 u32 *sccr1_reg)
248 {
249 u32 mask;
250
251 switch (drv_data->ssp_type) {
252 case QUARK_X1000_SSP:
253 mask = QUARK_X1000_SSCR1_RFT;
254 break;
255 case CE4100_SSP:
256 mask = CE4100_SSCR1_RFT;
257 break;
258 default:
259 mask = SSCR1_RFT;
260 break;
261 }
262 *sccr1_reg &= ~mask;
263 }
264
pxa2xx_spi_set_rx_thre(const struct driver_data * drv_data,u32 * sccr1_reg,u32 threshold)265 static void pxa2xx_spi_set_rx_thre(const struct driver_data *drv_data,
266 u32 *sccr1_reg, u32 threshold)
267 {
268 switch (drv_data->ssp_type) {
269 case QUARK_X1000_SSP:
270 *sccr1_reg |= QUARK_X1000_SSCR1_RxTresh(threshold);
271 break;
272 case CE4100_SSP:
273 *sccr1_reg |= CE4100_SSCR1_RxTresh(threshold);
274 break;
275 default:
276 *sccr1_reg |= SSCR1_RxTresh(threshold);
277 break;
278 }
279 }
280
pxa2xx_configure_sscr0(const struct driver_data * drv_data,u32 clk_div,u8 bits)281 static u32 pxa2xx_configure_sscr0(const struct driver_data *drv_data,
282 u32 clk_div, u8 bits)
283 {
284 switch (drv_data->ssp_type) {
285 case QUARK_X1000_SSP:
286 return clk_div
287 | QUARK_X1000_SSCR0_Motorola
288 | QUARK_X1000_SSCR0_DataSize(bits > 32 ? 8 : bits)
289 | SSCR0_SSE;
290 default:
291 return clk_div
292 | SSCR0_Motorola
293 | SSCR0_DataSize(bits > 16 ? bits - 16 : bits)
294 | SSCR0_SSE
295 | (bits > 16 ? SSCR0_EDSS : 0);
296 }
297 }
298
299 /*
300 * Read and write LPSS SSP private registers. Caller must first check that
301 * is_lpss_ssp() returns true before these can be called.
302 */
__lpss_ssp_read_priv(struct driver_data * drv_data,unsigned offset)303 static u32 __lpss_ssp_read_priv(struct driver_data *drv_data, unsigned offset)
304 {
305 WARN_ON(!drv_data->lpss_base);
306 return readl(drv_data->lpss_base + offset);
307 }
308
__lpss_ssp_write_priv(struct driver_data * drv_data,unsigned offset,u32 value)309 static void __lpss_ssp_write_priv(struct driver_data *drv_data,
310 unsigned offset, u32 value)
311 {
312 WARN_ON(!drv_data->lpss_base);
313 writel(value, drv_data->lpss_base + offset);
314 }
315
316 /*
317 * lpss_ssp_setup - perform LPSS SSP specific setup
318 * @drv_data: pointer to the driver private data
319 *
320 * Perform LPSS SSP specific setup. This function must be called first if
321 * one is going to use LPSS SSP private registers.
322 */
lpss_ssp_setup(struct driver_data * drv_data)323 static void lpss_ssp_setup(struct driver_data *drv_data)
324 {
325 const struct lpss_config *config;
326 u32 value;
327
328 config = lpss_get_config(drv_data);
329 drv_data->lpss_base = drv_data->ioaddr + config->offset;
330
331 /* Enable software chip select control */
332 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
333 value &= ~(LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH);
334 value |= LPSS_CS_CONTROL_SW_MODE | LPSS_CS_CONTROL_CS_HIGH;
335 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
336
337 /* Enable multiblock DMA transfers */
338 if (drv_data->master_info->enable_dma) {
339 __lpss_ssp_write_priv(drv_data, config->reg_ssp, 1);
340
341 if (config->reg_general >= 0) {
342 value = __lpss_ssp_read_priv(drv_data,
343 config->reg_general);
344 value |= LPSS_GENERAL_REG_RXTO_HOLDOFF_DISABLE;
345 __lpss_ssp_write_priv(drv_data,
346 config->reg_general, value);
347 }
348 }
349 }
350
lpss_ssp_select_cs(struct spi_device * spi,const struct lpss_config * config)351 static void lpss_ssp_select_cs(struct spi_device *spi,
352 const struct lpss_config *config)
353 {
354 struct driver_data *drv_data =
355 spi_controller_get_devdata(spi->controller);
356 u32 value, cs;
357
358 if (!config->cs_sel_mask)
359 return;
360
361 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
362
363 cs = spi->chip_select;
364 cs <<= config->cs_sel_shift;
365 if (cs != (value & config->cs_sel_mask)) {
366 /*
367 * When switching another chip select output active the
368 * output must be selected first and wait 2 ssp_clk cycles
369 * before changing state to active. Otherwise a short
370 * glitch will occur on the previous chip select since
371 * output select is latched but state control is not.
372 */
373 value &= ~config->cs_sel_mask;
374 value |= cs;
375 __lpss_ssp_write_priv(drv_data,
376 config->reg_cs_ctrl, value);
377 ndelay(1000000000 /
378 (drv_data->master->max_speed_hz / 2));
379 }
380 }
381
lpss_ssp_cs_control(struct spi_device * spi,bool enable)382 static void lpss_ssp_cs_control(struct spi_device *spi, bool enable)
383 {
384 struct driver_data *drv_data =
385 spi_controller_get_devdata(spi->controller);
386 const struct lpss_config *config;
387 u32 value;
388
389 config = lpss_get_config(drv_data);
390
391 if (enable)
392 lpss_ssp_select_cs(spi, config);
393
394 value = __lpss_ssp_read_priv(drv_data, config->reg_cs_ctrl);
395 if (enable)
396 value &= ~LPSS_CS_CONTROL_CS_HIGH;
397 else
398 value |= LPSS_CS_CONTROL_CS_HIGH;
399 __lpss_ssp_write_priv(drv_data, config->reg_cs_ctrl, value);
400 if (config->cs_clk_stays_gated) {
401 u32 clkgate;
402
403 /*
404 * Changing CS alone when dynamic clock gating is on won't
405 * actually flip CS at that time. This ruins SPI transfers
406 * that specify delays, or have no data. Toggle the clock mode
407 * to force on briefly to poke the CS pin to move.
408 */
409 clkgate = __lpss_ssp_read_priv(drv_data, LPSS_PRIV_CLOCK_GATE);
410 value = (clkgate & ~LPSS_PRIV_CLOCK_GATE_CLK_CTL_MASK) |
411 LPSS_PRIV_CLOCK_GATE_CLK_CTL_FORCE_ON;
412
413 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, value);
414 __lpss_ssp_write_priv(drv_data, LPSS_PRIV_CLOCK_GATE, clkgate);
415 }
416 }
417
cs_assert(struct spi_device * spi)418 static void cs_assert(struct spi_device *spi)
419 {
420 struct chip_data *chip = spi_get_ctldata(spi);
421 struct driver_data *drv_data =
422 spi_controller_get_devdata(spi->controller);
423
424 if (drv_data->ssp_type == CE4100_SSP) {
425 pxa2xx_spi_write(drv_data, SSSR, chip->frm);
426 return;
427 }
428
429 if (chip->cs_control) {
430 chip->cs_control(PXA2XX_CS_ASSERT);
431 return;
432 }
433
434 if (chip->gpiod_cs) {
435 gpiod_set_value(chip->gpiod_cs, chip->gpio_cs_inverted);
436 return;
437 }
438
439 if (is_lpss_ssp(drv_data))
440 lpss_ssp_cs_control(spi, true);
441 }
442
cs_deassert(struct spi_device * spi)443 static void cs_deassert(struct spi_device *spi)
444 {
445 struct chip_data *chip = spi_get_ctldata(spi);
446 struct driver_data *drv_data =
447 spi_controller_get_devdata(spi->controller);
448 unsigned long timeout;
449
450 if (drv_data->ssp_type == CE4100_SSP)
451 return;
452
453 /* Wait until SSP becomes idle before deasserting the CS */
454 timeout = jiffies + msecs_to_jiffies(10);
455 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY &&
456 !time_after(jiffies, timeout))
457 cpu_relax();
458
459 if (chip->cs_control) {
460 chip->cs_control(PXA2XX_CS_DEASSERT);
461 return;
462 }
463
464 if (chip->gpiod_cs) {
465 gpiod_set_value(chip->gpiod_cs, !chip->gpio_cs_inverted);
466 return;
467 }
468
469 if (is_lpss_ssp(drv_data))
470 lpss_ssp_cs_control(spi, false);
471 }
472
pxa2xx_spi_set_cs(struct spi_device * spi,bool level)473 static void pxa2xx_spi_set_cs(struct spi_device *spi, bool level)
474 {
475 if (level)
476 cs_deassert(spi);
477 else
478 cs_assert(spi);
479 }
480
pxa2xx_spi_flush(struct driver_data * drv_data)481 int pxa2xx_spi_flush(struct driver_data *drv_data)
482 {
483 unsigned long limit = loops_per_jiffy << 1;
484
485 do {
486 while (pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
487 pxa2xx_spi_read(drv_data, SSDR);
488 } while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_BSY) && --limit);
489 write_SSSR_CS(drv_data, SSSR_ROR);
490
491 return limit;
492 }
493
null_writer(struct driver_data * drv_data)494 static int null_writer(struct driver_data *drv_data)
495 {
496 u8 n_bytes = drv_data->n_bytes;
497
498 if (pxa2xx_spi_txfifo_full(drv_data)
499 || (drv_data->tx == drv_data->tx_end))
500 return 0;
501
502 pxa2xx_spi_write(drv_data, SSDR, 0);
503 drv_data->tx += n_bytes;
504
505 return 1;
506 }
507
null_reader(struct driver_data * drv_data)508 static int null_reader(struct driver_data *drv_data)
509 {
510 u8 n_bytes = drv_data->n_bytes;
511
512 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
513 && (drv_data->rx < drv_data->rx_end)) {
514 pxa2xx_spi_read(drv_data, SSDR);
515 drv_data->rx += n_bytes;
516 }
517
518 return drv_data->rx == drv_data->rx_end;
519 }
520
u8_writer(struct driver_data * drv_data)521 static int u8_writer(struct driver_data *drv_data)
522 {
523 if (pxa2xx_spi_txfifo_full(drv_data)
524 || (drv_data->tx == drv_data->tx_end))
525 return 0;
526
527 pxa2xx_spi_write(drv_data, SSDR, *(u8 *)(drv_data->tx));
528 ++drv_data->tx;
529
530 return 1;
531 }
532
u8_reader(struct driver_data * drv_data)533 static int u8_reader(struct driver_data *drv_data)
534 {
535 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
536 && (drv_data->rx < drv_data->rx_end)) {
537 *(u8 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
538 ++drv_data->rx;
539 }
540
541 return drv_data->rx == drv_data->rx_end;
542 }
543
u16_writer(struct driver_data * drv_data)544 static int u16_writer(struct driver_data *drv_data)
545 {
546 if (pxa2xx_spi_txfifo_full(drv_data)
547 || (drv_data->tx == drv_data->tx_end))
548 return 0;
549
550 pxa2xx_spi_write(drv_data, SSDR, *(u16 *)(drv_data->tx));
551 drv_data->tx += 2;
552
553 return 1;
554 }
555
u16_reader(struct driver_data * drv_data)556 static int u16_reader(struct driver_data *drv_data)
557 {
558 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
559 && (drv_data->rx < drv_data->rx_end)) {
560 *(u16 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
561 drv_data->rx += 2;
562 }
563
564 return drv_data->rx == drv_data->rx_end;
565 }
566
u32_writer(struct driver_data * drv_data)567 static int u32_writer(struct driver_data *drv_data)
568 {
569 if (pxa2xx_spi_txfifo_full(drv_data)
570 || (drv_data->tx == drv_data->tx_end))
571 return 0;
572
573 pxa2xx_spi_write(drv_data, SSDR, *(u32 *)(drv_data->tx));
574 drv_data->tx += 4;
575
576 return 1;
577 }
578
u32_reader(struct driver_data * drv_data)579 static int u32_reader(struct driver_data *drv_data)
580 {
581 while ((pxa2xx_spi_read(drv_data, SSSR) & SSSR_RNE)
582 && (drv_data->rx < drv_data->rx_end)) {
583 *(u32 *)(drv_data->rx) = pxa2xx_spi_read(drv_data, SSDR);
584 drv_data->rx += 4;
585 }
586
587 return drv_data->rx == drv_data->rx_end;
588 }
589
reset_sccr1(struct driver_data * drv_data)590 static void reset_sccr1(struct driver_data *drv_data)
591 {
592 struct chip_data *chip =
593 spi_get_ctldata(drv_data->master->cur_msg->spi);
594 u32 sccr1_reg;
595
596 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1;
597 switch (drv_data->ssp_type) {
598 case QUARK_X1000_SSP:
599 sccr1_reg &= ~QUARK_X1000_SSCR1_RFT;
600 break;
601 case CE4100_SSP:
602 sccr1_reg &= ~CE4100_SSCR1_RFT;
603 break;
604 default:
605 sccr1_reg &= ~SSCR1_RFT;
606 break;
607 }
608 sccr1_reg |= chip->threshold;
609 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
610 }
611
int_error_stop(struct driver_data * drv_data,const char * msg)612 static void int_error_stop(struct driver_data *drv_data, const char* msg)
613 {
614 /* Stop and reset SSP */
615 write_SSSR_CS(drv_data, drv_data->clear_sr);
616 reset_sccr1(drv_data);
617 if (!pxa25x_ssp_comp(drv_data))
618 pxa2xx_spi_write(drv_data, SSTO, 0);
619 pxa2xx_spi_flush(drv_data);
620 pxa2xx_spi_write(drv_data, SSCR0,
621 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
622
623 dev_err(&drv_data->pdev->dev, "%s\n", msg);
624
625 drv_data->master->cur_msg->status = -EIO;
626 spi_finalize_current_transfer(drv_data->master);
627 }
628
int_transfer_complete(struct driver_data * drv_data)629 static void int_transfer_complete(struct driver_data *drv_data)
630 {
631 /* Clear and disable interrupts */
632 write_SSSR_CS(drv_data, drv_data->clear_sr);
633 reset_sccr1(drv_data);
634 if (!pxa25x_ssp_comp(drv_data))
635 pxa2xx_spi_write(drv_data, SSTO, 0);
636
637 spi_finalize_current_transfer(drv_data->master);
638 }
639
interrupt_transfer(struct driver_data * drv_data)640 static irqreturn_t interrupt_transfer(struct driver_data *drv_data)
641 {
642 u32 irq_mask = (pxa2xx_spi_read(drv_data, SSCR1) & SSCR1_TIE) ?
643 drv_data->mask_sr : drv_data->mask_sr & ~SSSR_TFS;
644
645 u32 irq_status = pxa2xx_spi_read(drv_data, SSSR) & irq_mask;
646
647 if (irq_status & SSSR_ROR) {
648 int_error_stop(drv_data, "interrupt_transfer: fifo overrun");
649 return IRQ_HANDLED;
650 }
651
652 if (irq_status & SSSR_TINT) {
653 pxa2xx_spi_write(drv_data, SSSR, SSSR_TINT);
654 if (drv_data->read(drv_data)) {
655 int_transfer_complete(drv_data);
656 return IRQ_HANDLED;
657 }
658 }
659
660 /* Drain rx fifo, Fill tx fifo and prevent overruns */
661 do {
662 if (drv_data->read(drv_data)) {
663 int_transfer_complete(drv_data);
664 return IRQ_HANDLED;
665 }
666 } while (drv_data->write(drv_data));
667
668 if (drv_data->read(drv_data)) {
669 int_transfer_complete(drv_data);
670 return IRQ_HANDLED;
671 }
672
673 if (drv_data->tx == drv_data->tx_end) {
674 u32 bytes_left;
675 u32 sccr1_reg;
676
677 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
678 sccr1_reg &= ~SSCR1_TIE;
679
680 /*
681 * PXA25x_SSP has no timeout, set up rx threshould for the
682 * remaining RX bytes.
683 */
684 if (pxa25x_ssp_comp(drv_data)) {
685 u32 rx_thre;
686
687 pxa2xx_spi_clear_rx_thre(drv_data, &sccr1_reg);
688
689 bytes_left = drv_data->rx_end - drv_data->rx;
690 switch (drv_data->n_bytes) {
691 case 4:
692 bytes_left >>= 1;
693 case 2:
694 bytes_left >>= 1;
695 }
696
697 rx_thre = pxa2xx_spi_get_rx_default_thre(drv_data);
698 if (rx_thre > bytes_left)
699 rx_thre = bytes_left;
700
701 pxa2xx_spi_set_rx_thre(drv_data, &sccr1_reg, rx_thre);
702 }
703 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
704 }
705
706 /* We did something */
707 return IRQ_HANDLED;
708 }
709
handle_bad_msg(struct driver_data * drv_data)710 static void handle_bad_msg(struct driver_data *drv_data)
711 {
712 pxa2xx_spi_write(drv_data, SSCR0,
713 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
714 pxa2xx_spi_write(drv_data, SSCR1,
715 pxa2xx_spi_read(drv_data, SSCR1) & ~drv_data->int_cr1);
716 if (!pxa25x_ssp_comp(drv_data))
717 pxa2xx_spi_write(drv_data, SSTO, 0);
718 write_SSSR_CS(drv_data, drv_data->clear_sr);
719
720 dev_err(&drv_data->pdev->dev,
721 "bad message state in interrupt handler\n");
722 }
723
ssp_int(int irq,void * dev_id)724 static irqreturn_t ssp_int(int irq, void *dev_id)
725 {
726 struct driver_data *drv_data = dev_id;
727 u32 sccr1_reg;
728 u32 mask = drv_data->mask_sr;
729 u32 status;
730
731 /*
732 * The IRQ might be shared with other peripherals so we must first
733 * check that are we RPM suspended or not. If we are we assume that
734 * the IRQ was not for us (we shouldn't be RPM suspended when the
735 * interrupt is enabled).
736 */
737 if (pm_runtime_suspended(&drv_data->pdev->dev))
738 return IRQ_NONE;
739
740 /*
741 * If the device is not yet in RPM suspended state and we get an
742 * interrupt that is meant for another device, check if status bits
743 * are all set to one. That means that the device is already
744 * powered off.
745 */
746 status = pxa2xx_spi_read(drv_data, SSSR);
747 if (status == ~0)
748 return IRQ_NONE;
749
750 sccr1_reg = pxa2xx_spi_read(drv_data, SSCR1);
751
752 /* Ignore possible writes if we don't need to write */
753 if (!(sccr1_reg & SSCR1_TIE))
754 mask &= ~SSSR_TFS;
755
756 /* Ignore RX timeout interrupt if it is disabled */
757 if (!(sccr1_reg & SSCR1_TINTE))
758 mask &= ~SSSR_TINT;
759
760 if (!(status & mask))
761 return IRQ_NONE;
762
763 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg & ~drv_data->int_cr1);
764 pxa2xx_spi_write(drv_data, SSCR1, sccr1_reg);
765
766 if (!drv_data->master->cur_msg) {
767 handle_bad_msg(drv_data);
768 /* Never fail */
769 return IRQ_HANDLED;
770 }
771
772 return drv_data->transfer_handler(drv_data);
773 }
774
775 /*
776 * The Quark SPI has an additional 24 bit register (DDS_CLK_RATE) to multiply
777 * input frequency by fractions of 2^24. It also has a divider by 5.
778 *
779 * There are formulas to get baud rate value for given input frequency and
780 * divider parameters, such as DDS_CLK_RATE and SCR:
781 *
782 * Fsys = 200MHz
783 *
784 * Fssp = Fsys * DDS_CLK_RATE / 2^24 (1)
785 * Baud rate = Fsclk = Fssp / (2 * (SCR + 1)) (2)
786 *
787 * DDS_CLK_RATE either 2^n or 2^n / 5.
788 * SCR is in range 0 .. 255
789 *
790 * Divisor = 5^i * 2^j * 2 * k
791 * i = [0, 1] i = 1 iff j = 0 or j > 3
792 * j = [0, 23] j = 0 iff i = 1
793 * k = [1, 256]
794 * Special case: j = 0, i = 1: Divisor = 2 / 5
795 *
796 * Accordingly to the specification the recommended values for DDS_CLK_RATE
797 * are:
798 * Case 1: 2^n, n = [0, 23]
799 * Case 2: 2^24 * 2 / 5 (0x666666)
800 * Case 3: less than or equal to 2^24 / 5 / 16 (0x33333)
801 *
802 * In all cases the lowest possible value is better.
803 *
804 * The function calculates parameters for all cases and chooses the one closest
805 * to the asked baud rate.
806 */
quark_x1000_get_clk_div(int rate,u32 * dds)807 static unsigned int quark_x1000_get_clk_div(int rate, u32 *dds)
808 {
809 unsigned long xtal = 200000000;
810 unsigned long fref = xtal / 2; /* mandatory division by 2,
811 see (2) */
812 /* case 3 */
813 unsigned long fref1 = fref / 2; /* case 1 */
814 unsigned long fref2 = fref * 2 / 5; /* case 2 */
815 unsigned long scale;
816 unsigned long q, q1, q2;
817 long r, r1, r2;
818 u32 mul;
819
820 /* Case 1 */
821
822 /* Set initial value for DDS_CLK_RATE */
823 mul = (1 << 24) >> 1;
824
825 /* Calculate initial quot */
826 q1 = DIV_ROUND_UP(fref1, rate);
827
828 /* Scale q1 if it's too big */
829 if (q1 > 256) {
830 /* Scale q1 to range [1, 512] */
831 scale = fls_long(q1 - 1);
832 if (scale > 9) {
833 q1 >>= scale - 9;
834 mul >>= scale - 9;
835 }
836
837 /* Round the result if we have a remainder */
838 q1 += q1 & 1;
839 }
840
841 /* Decrease DDS_CLK_RATE as much as we can without loss in precision */
842 scale = __ffs(q1);
843 q1 >>= scale;
844 mul >>= scale;
845
846 /* Get the remainder */
847 r1 = abs(fref1 / (1 << (24 - fls_long(mul))) / q1 - rate);
848
849 /* Case 2 */
850
851 q2 = DIV_ROUND_UP(fref2, rate);
852 r2 = abs(fref2 / q2 - rate);
853
854 /*
855 * Choose the best between two: less remainder we have the better. We
856 * can't go case 2 if q2 is greater than 256 since SCR register can
857 * hold only values 0 .. 255.
858 */
859 if (r2 >= r1 || q2 > 256) {
860 /* case 1 is better */
861 r = r1;
862 q = q1;
863 } else {
864 /* case 2 is better */
865 r = r2;
866 q = q2;
867 mul = (1 << 24) * 2 / 5;
868 }
869
870 /* Check case 3 only if the divisor is big enough */
871 if (fref / rate >= 80) {
872 u64 fssp;
873 u32 m;
874
875 /* Calculate initial quot */
876 q1 = DIV_ROUND_UP(fref, rate);
877 m = (1 << 24) / q1;
878
879 /* Get the remainder */
880 fssp = (u64)fref * m;
881 do_div(fssp, 1 << 24);
882 r1 = abs(fssp - rate);
883
884 /* Choose this one if it suits better */
885 if (r1 < r) {
886 /* case 3 is better */
887 q = 1;
888 mul = m;
889 }
890 }
891
892 *dds = mul;
893 return q - 1;
894 }
895
ssp_get_clk_div(struct driver_data * drv_data,int rate)896 static unsigned int ssp_get_clk_div(struct driver_data *drv_data, int rate)
897 {
898 unsigned long ssp_clk = drv_data->master->max_speed_hz;
899 const struct ssp_device *ssp = drv_data->ssp;
900
901 rate = min_t(int, ssp_clk, rate);
902
903 /*
904 * Calculate the divisor for the SCR (Serial Clock Rate), avoiding
905 * that the SSP transmission rate can be greater than the device rate
906 */
907 if (ssp->type == PXA25x_SSP || ssp->type == CE4100_SSP)
908 return (DIV_ROUND_UP(ssp_clk, 2 * rate) - 1) & 0xff;
909 else
910 return (DIV_ROUND_UP(ssp_clk, rate) - 1) & 0xfff;
911 }
912
pxa2xx_ssp_get_clk_div(struct driver_data * drv_data,int rate)913 static unsigned int pxa2xx_ssp_get_clk_div(struct driver_data *drv_data,
914 int rate)
915 {
916 struct chip_data *chip =
917 spi_get_ctldata(drv_data->master->cur_msg->spi);
918 unsigned int clk_div;
919
920 switch (drv_data->ssp_type) {
921 case QUARK_X1000_SSP:
922 clk_div = quark_x1000_get_clk_div(rate, &chip->dds_rate);
923 break;
924 default:
925 clk_div = ssp_get_clk_div(drv_data, rate);
926 break;
927 }
928 return clk_div << 8;
929 }
930
pxa2xx_spi_can_dma(struct spi_controller * master,struct spi_device * spi,struct spi_transfer * xfer)931 static bool pxa2xx_spi_can_dma(struct spi_controller *master,
932 struct spi_device *spi,
933 struct spi_transfer *xfer)
934 {
935 struct chip_data *chip = spi_get_ctldata(spi);
936
937 return chip->enable_dma &&
938 xfer->len <= MAX_DMA_LEN &&
939 xfer->len >= chip->dma_burst_size;
940 }
941
pxa2xx_spi_transfer_one(struct spi_controller * master,struct spi_device * spi,struct spi_transfer * transfer)942 static int pxa2xx_spi_transfer_one(struct spi_controller *master,
943 struct spi_device *spi,
944 struct spi_transfer *transfer)
945 {
946 struct driver_data *drv_data = spi_controller_get_devdata(master);
947 struct spi_message *message = master->cur_msg;
948 struct chip_data *chip = spi_get_ctldata(message->spi);
949 u32 dma_thresh = chip->dma_threshold;
950 u32 dma_burst = chip->dma_burst_size;
951 u32 change_mask = pxa2xx_spi_get_ssrc1_change_mask(drv_data);
952 u32 clk_div;
953 u8 bits;
954 u32 speed;
955 u32 cr0;
956 u32 cr1;
957 int err;
958 int dma_mapped;
959
960 /* Check if we can DMA this transfer */
961 if (transfer->len > MAX_DMA_LEN && chip->enable_dma) {
962
963 /* reject already-mapped transfers; PIO won't always work */
964 if (message->is_dma_mapped
965 || transfer->rx_dma || transfer->tx_dma) {
966 dev_err(&drv_data->pdev->dev,
967 "Mapped transfer length of %u is greater than %d\n",
968 transfer->len, MAX_DMA_LEN);
969 return -EINVAL;
970 }
971
972 /* warn ... we force this to PIO mode */
973 dev_warn_ratelimited(&message->spi->dev,
974 "DMA disabled for transfer length %ld greater than %d\n",
975 (long)transfer->len, MAX_DMA_LEN);
976 }
977
978 /* Setup the transfer state based on the type of transfer */
979 if (pxa2xx_spi_flush(drv_data) == 0) {
980 dev_err(&drv_data->pdev->dev, "Flush failed\n");
981 return -EIO;
982 }
983 drv_data->n_bytes = chip->n_bytes;
984 drv_data->tx = (void *)transfer->tx_buf;
985 drv_data->tx_end = drv_data->tx + transfer->len;
986 drv_data->rx = transfer->rx_buf;
987 drv_data->rx_end = drv_data->rx + transfer->len;
988 drv_data->write = drv_data->tx ? chip->write : null_writer;
989 drv_data->read = drv_data->rx ? chip->read : null_reader;
990
991 /* Change speed and bit per word on a per transfer */
992 bits = transfer->bits_per_word;
993 speed = transfer->speed_hz;
994
995 clk_div = pxa2xx_ssp_get_clk_div(drv_data, speed);
996
997 if (bits <= 8) {
998 drv_data->n_bytes = 1;
999 drv_data->read = drv_data->read != null_reader ?
1000 u8_reader : null_reader;
1001 drv_data->write = drv_data->write != null_writer ?
1002 u8_writer : null_writer;
1003 } else if (bits <= 16) {
1004 drv_data->n_bytes = 2;
1005 drv_data->read = drv_data->read != null_reader ?
1006 u16_reader : null_reader;
1007 drv_data->write = drv_data->write != null_writer ?
1008 u16_writer : null_writer;
1009 } else if (bits <= 32) {
1010 drv_data->n_bytes = 4;
1011 drv_data->read = drv_data->read != null_reader ?
1012 u32_reader : null_reader;
1013 drv_data->write = drv_data->write != null_writer ?
1014 u32_writer : null_writer;
1015 }
1016 /*
1017 * if bits/word is changed in dma mode, then must check the
1018 * thresholds and burst also
1019 */
1020 if (chip->enable_dma) {
1021 if (pxa2xx_spi_set_dma_burst_and_threshold(chip,
1022 message->spi,
1023 bits, &dma_burst,
1024 &dma_thresh))
1025 dev_warn_ratelimited(&message->spi->dev,
1026 "DMA burst size reduced to match bits_per_word\n");
1027 }
1028
1029 dma_mapped = master->can_dma &&
1030 master->can_dma(master, message->spi, transfer) &&
1031 master->cur_msg_mapped;
1032 if (dma_mapped) {
1033
1034 /* Ensure we have the correct interrupt handler */
1035 drv_data->transfer_handler = pxa2xx_spi_dma_transfer;
1036
1037 err = pxa2xx_spi_dma_prepare(drv_data, transfer);
1038 if (err)
1039 return err;
1040
1041 /* Clear status and start DMA engine */
1042 cr1 = chip->cr1 | dma_thresh | drv_data->dma_cr1;
1043 pxa2xx_spi_write(drv_data, SSSR, drv_data->clear_sr);
1044
1045 pxa2xx_spi_dma_start(drv_data);
1046 } else {
1047 /* Ensure we have the correct interrupt handler */
1048 drv_data->transfer_handler = interrupt_transfer;
1049
1050 /* Clear status */
1051 cr1 = chip->cr1 | chip->threshold | drv_data->int_cr1;
1052 write_SSSR_CS(drv_data, drv_data->clear_sr);
1053 }
1054
1055 /* NOTE: PXA25x_SSP _could_ use external clocking ... */
1056 cr0 = pxa2xx_configure_sscr0(drv_data, clk_div, bits);
1057 if (!pxa25x_ssp_comp(drv_data))
1058 dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
1059 master->max_speed_hz
1060 / (1 + ((cr0 & SSCR0_SCR(0xfff)) >> 8)),
1061 dma_mapped ? "DMA" : "PIO");
1062 else
1063 dev_dbg(&message->spi->dev, "%u Hz actual, %s\n",
1064 master->max_speed_hz / 2
1065 / (1 + ((cr0 & SSCR0_SCR(0x0ff)) >> 8)),
1066 dma_mapped ? "DMA" : "PIO");
1067
1068 if (is_lpss_ssp(drv_data)) {
1069 if ((pxa2xx_spi_read(drv_data, SSIRF) & 0xff)
1070 != chip->lpss_rx_threshold)
1071 pxa2xx_spi_write(drv_data, SSIRF,
1072 chip->lpss_rx_threshold);
1073 if ((pxa2xx_spi_read(drv_data, SSITF) & 0xffff)
1074 != chip->lpss_tx_threshold)
1075 pxa2xx_spi_write(drv_data, SSITF,
1076 chip->lpss_tx_threshold);
1077 }
1078
1079 if (is_quark_x1000_ssp(drv_data) &&
1080 (pxa2xx_spi_read(drv_data, DDS_RATE) != chip->dds_rate))
1081 pxa2xx_spi_write(drv_data, DDS_RATE, chip->dds_rate);
1082
1083 /* see if we need to reload the config registers */
1084 if ((pxa2xx_spi_read(drv_data, SSCR0) != cr0)
1085 || (pxa2xx_spi_read(drv_data, SSCR1) & change_mask)
1086 != (cr1 & change_mask)) {
1087 /* stop the SSP, and update the other bits */
1088 pxa2xx_spi_write(drv_data, SSCR0, cr0 & ~SSCR0_SSE);
1089 if (!pxa25x_ssp_comp(drv_data))
1090 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1091 /* first set CR1 without interrupt and service enables */
1092 pxa2xx_spi_write(drv_data, SSCR1, cr1 & change_mask);
1093 /* restart the SSP */
1094 pxa2xx_spi_write(drv_data, SSCR0, cr0);
1095
1096 } else {
1097 if (!pxa25x_ssp_comp(drv_data))
1098 pxa2xx_spi_write(drv_data, SSTO, chip->timeout);
1099 }
1100
1101 /*
1102 * Release the data by enabling service requests and interrupts,
1103 * without changing any mode bits
1104 */
1105 pxa2xx_spi_write(drv_data, SSCR1, cr1);
1106
1107 return 1;
1108 }
1109
pxa2xx_spi_handle_err(struct spi_controller * master,struct spi_message * msg)1110 static void pxa2xx_spi_handle_err(struct spi_controller *master,
1111 struct spi_message *msg)
1112 {
1113 struct driver_data *drv_data = spi_controller_get_devdata(master);
1114
1115 /* Disable the SSP */
1116 pxa2xx_spi_write(drv_data, SSCR0,
1117 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
1118 /* Clear and disable interrupts and service requests */
1119 write_SSSR_CS(drv_data, drv_data->clear_sr);
1120 pxa2xx_spi_write(drv_data, SSCR1,
1121 pxa2xx_spi_read(drv_data, SSCR1)
1122 & ~(drv_data->int_cr1 | drv_data->dma_cr1));
1123 if (!pxa25x_ssp_comp(drv_data))
1124 pxa2xx_spi_write(drv_data, SSTO, 0);
1125
1126 /*
1127 * Stop the DMA if running. Note DMA callback handler may have unset
1128 * the dma_running already, which is fine as stopping is not needed
1129 * then but we shouldn't rely this flag for anything else than
1130 * stopping. For instance to differentiate between PIO and DMA
1131 * transfers.
1132 */
1133 if (atomic_read(&drv_data->dma_running))
1134 pxa2xx_spi_dma_stop(drv_data);
1135 }
1136
pxa2xx_spi_unprepare_transfer(struct spi_controller * master)1137 static int pxa2xx_spi_unprepare_transfer(struct spi_controller *master)
1138 {
1139 struct driver_data *drv_data = spi_controller_get_devdata(master);
1140
1141 /* Disable the SSP now */
1142 pxa2xx_spi_write(drv_data, SSCR0,
1143 pxa2xx_spi_read(drv_data, SSCR0) & ~SSCR0_SSE);
1144
1145 return 0;
1146 }
1147
setup_cs(struct spi_device * spi,struct chip_data * chip,struct pxa2xx_spi_chip * chip_info)1148 static int setup_cs(struct spi_device *spi, struct chip_data *chip,
1149 struct pxa2xx_spi_chip *chip_info)
1150 {
1151 struct driver_data *drv_data =
1152 spi_controller_get_devdata(spi->controller);
1153 struct gpio_desc *gpiod;
1154 int err = 0;
1155
1156 if (chip == NULL)
1157 return 0;
1158
1159 if (drv_data->cs_gpiods) {
1160 gpiod = drv_data->cs_gpiods[spi->chip_select];
1161 if (gpiod) {
1162 chip->gpiod_cs = gpiod;
1163 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1164 gpiod_set_value(gpiod, chip->gpio_cs_inverted);
1165 }
1166
1167 return 0;
1168 }
1169
1170 if (chip_info == NULL)
1171 return 0;
1172
1173 /* NOTE: setup() can be called multiple times, possibly with
1174 * different chip_info, release previously requested GPIO
1175 */
1176 if (chip->gpiod_cs) {
1177 gpiod_put(chip->gpiod_cs);
1178 chip->gpiod_cs = NULL;
1179 }
1180
1181 /* If (*cs_control) is provided, ignore GPIO chip select */
1182 if (chip_info->cs_control) {
1183 chip->cs_control = chip_info->cs_control;
1184 return 0;
1185 }
1186
1187 if (gpio_is_valid(chip_info->gpio_cs)) {
1188 err = gpio_request(chip_info->gpio_cs, "SPI_CS");
1189 if (err) {
1190 dev_err(&spi->dev, "failed to request chip select GPIO%d\n",
1191 chip_info->gpio_cs);
1192 return err;
1193 }
1194
1195 gpiod = gpio_to_desc(chip_info->gpio_cs);
1196 chip->gpiod_cs = gpiod;
1197 chip->gpio_cs_inverted = spi->mode & SPI_CS_HIGH;
1198
1199 err = gpiod_direction_output(gpiod, !chip->gpio_cs_inverted);
1200 }
1201
1202 return err;
1203 }
1204
setup(struct spi_device * spi)1205 static int setup(struct spi_device *spi)
1206 {
1207 struct pxa2xx_spi_chip *chip_info;
1208 struct chip_data *chip;
1209 const struct lpss_config *config;
1210 struct driver_data *drv_data =
1211 spi_controller_get_devdata(spi->controller);
1212 uint tx_thres, tx_hi_thres, rx_thres;
1213
1214 switch (drv_data->ssp_type) {
1215 case QUARK_X1000_SSP:
1216 tx_thres = TX_THRESH_QUARK_X1000_DFLT;
1217 tx_hi_thres = 0;
1218 rx_thres = RX_THRESH_QUARK_X1000_DFLT;
1219 break;
1220 case CE4100_SSP:
1221 tx_thres = TX_THRESH_CE4100_DFLT;
1222 tx_hi_thres = 0;
1223 rx_thres = RX_THRESH_CE4100_DFLT;
1224 break;
1225 case LPSS_LPT_SSP:
1226 case LPSS_BYT_SSP:
1227 case LPSS_BSW_SSP:
1228 case LPSS_SPT_SSP:
1229 case LPSS_BXT_SSP:
1230 case LPSS_CNL_SSP:
1231 config = lpss_get_config(drv_data);
1232 tx_thres = config->tx_threshold_lo;
1233 tx_hi_thres = config->tx_threshold_hi;
1234 rx_thres = config->rx_threshold;
1235 break;
1236 default:
1237 tx_thres = TX_THRESH_DFLT;
1238 tx_hi_thres = 0;
1239 rx_thres = RX_THRESH_DFLT;
1240 break;
1241 }
1242
1243 /* Only alloc on first setup */
1244 chip = spi_get_ctldata(spi);
1245 if (!chip) {
1246 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1247 if (!chip)
1248 return -ENOMEM;
1249
1250 if (drv_data->ssp_type == CE4100_SSP) {
1251 if (spi->chip_select > 4) {
1252 dev_err(&spi->dev,
1253 "failed setup: cs number must not be > 4.\n");
1254 kfree(chip);
1255 return -EINVAL;
1256 }
1257
1258 chip->frm = spi->chip_select;
1259 }
1260 chip->enable_dma = drv_data->master_info->enable_dma;
1261 chip->timeout = TIMOUT_DFLT;
1262 }
1263
1264 /* protocol drivers may change the chip settings, so...
1265 * if chip_info exists, use it */
1266 chip_info = spi->controller_data;
1267
1268 /* chip_info isn't always needed */
1269 chip->cr1 = 0;
1270 if (chip_info) {
1271 if (chip_info->timeout)
1272 chip->timeout = chip_info->timeout;
1273 if (chip_info->tx_threshold)
1274 tx_thres = chip_info->tx_threshold;
1275 if (chip_info->tx_hi_threshold)
1276 tx_hi_thres = chip_info->tx_hi_threshold;
1277 if (chip_info->rx_threshold)
1278 rx_thres = chip_info->rx_threshold;
1279 chip->dma_threshold = 0;
1280 if (chip_info->enable_loopback)
1281 chip->cr1 = SSCR1_LBM;
1282 }
1283
1284 chip->lpss_rx_threshold = SSIRF_RxThresh(rx_thres);
1285 chip->lpss_tx_threshold = SSITF_TxLoThresh(tx_thres)
1286 | SSITF_TxHiThresh(tx_hi_thres);
1287
1288 /* set dma burst and threshold outside of chip_info path so that if
1289 * chip_info goes away after setting chip->enable_dma, the
1290 * burst and threshold can still respond to changes in bits_per_word */
1291 if (chip->enable_dma) {
1292 /* set up legal burst and threshold for dma */
1293 if (pxa2xx_spi_set_dma_burst_and_threshold(chip, spi,
1294 spi->bits_per_word,
1295 &chip->dma_burst_size,
1296 &chip->dma_threshold)) {
1297 dev_warn(&spi->dev,
1298 "in setup: DMA burst size reduced to match bits_per_word\n");
1299 }
1300 }
1301
1302 switch (drv_data->ssp_type) {
1303 case QUARK_X1000_SSP:
1304 chip->threshold = (QUARK_X1000_SSCR1_RxTresh(rx_thres)
1305 & QUARK_X1000_SSCR1_RFT)
1306 | (QUARK_X1000_SSCR1_TxTresh(tx_thres)
1307 & QUARK_X1000_SSCR1_TFT);
1308 break;
1309 case CE4100_SSP:
1310 chip->threshold = (CE4100_SSCR1_RxTresh(rx_thres) & CE4100_SSCR1_RFT) |
1311 (CE4100_SSCR1_TxTresh(tx_thres) & CE4100_SSCR1_TFT);
1312 break;
1313 default:
1314 chip->threshold = (SSCR1_RxTresh(rx_thres) & SSCR1_RFT) |
1315 (SSCR1_TxTresh(tx_thres) & SSCR1_TFT);
1316 break;
1317 }
1318
1319 chip->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
1320 chip->cr1 |= (((spi->mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
1321 | (((spi->mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
1322
1323 if (spi->mode & SPI_LOOP)
1324 chip->cr1 |= SSCR1_LBM;
1325
1326 if (spi->bits_per_word <= 8) {
1327 chip->n_bytes = 1;
1328 chip->read = u8_reader;
1329 chip->write = u8_writer;
1330 } else if (spi->bits_per_word <= 16) {
1331 chip->n_bytes = 2;
1332 chip->read = u16_reader;
1333 chip->write = u16_writer;
1334 } else if (spi->bits_per_word <= 32) {
1335 chip->n_bytes = 4;
1336 chip->read = u32_reader;
1337 chip->write = u32_writer;
1338 }
1339
1340 spi_set_ctldata(spi, chip);
1341
1342 if (drv_data->ssp_type == CE4100_SSP)
1343 return 0;
1344
1345 return setup_cs(spi, chip, chip_info);
1346 }
1347
cleanup(struct spi_device * spi)1348 static void cleanup(struct spi_device *spi)
1349 {
1350 struct chip_data *chip = spi_get_ctldata(spi);
1351 struct driver_data *drv_data =
1352 spi_controller_get_devdata(spi->controller);
1353
1354 if (!chip)
1355 return;
1356
1357 if (drv_data->ssp_type != CE4100_SSP && !drv_data->cs_gpiods &&
1358 chip->gpiod_cs)
1359 gpiod_put(chip->gpiod_cs);
1360
1361 kfree(chip);
1362 }
1363
1364 #ifdef CONFIG_PCI
1365 #ifdef CONFIG_ACPI
1366
1367 static const struct acpi_device_id pxa2xx_spi_acpi_match[] = {
1368 { "INT33C0", LPSS_LPT_SSP },
1369 { "INT33C1", LPSS_LPT_SSP },
1370 { "INT3430", LPSS_LPT_SSP },
1371 { "INT3431", LPSS_LPT_SSP },
1372 { "80860F0E", LPSS_BYT_SSP },
1373 { "8086228E", LPSS_BSW_SSP },
1374 { },
1375 };
1376 MODULE_DEVICE_TABLE(acpi, pxa2xx_spi_acpi_match);
1377
pxa2xx_spi_get_port_id(struct acpi_device * adev)1378 static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
1379 {
1380 unsigned int devid;
1381 int port_id = -1;
1382
1383 if (adev && adev->pnp.unique_id &&
1384 !kstrtouint(adev->pnp.unique_id, 0, &devid))
1385 port_id = devid;
1386 return port_id;
1387 }
1388 #else /* !CONFIG_ACPI */
pxa2xx_spi_get_port_id(struct acpi_device * adev)1389 static int pxa2xx_spi_get_port_id(struct acpi_device *adev)
1390 {
1391 return -1;
1392 }
1393 #endif
1394
1395 /*
1396 * PCI IDs of compound devices that integrate both host controller and private
1397 * integrated DMA engine. Please note these are not used in module
1398 * autoloading and probing in this module but matching the LPSS SSP type.
1399 */
1400 static const struct pci_device_id pxa2xx_spi_pci_compound_match[] = {
1401 /* SPT-LP */
1402 { PCI_VDEVICE(INTEL, 0x9d29), LPSS_SPT_SSP },
1403 { PCI_VDEVICE(INTEL, 0x9d2a), LPSS_SPT_SSP },
1404 /* SPT-H */
1405 { PCI_VDEVICE(INTEL, 0xa129), LPSS_SPT_SSP },
1406 { PCI_VDEVICE(INTEL, 0xa12a), LPSS_SPT_SSP },
1407 /* KBL-H */
1408 { PCI_VDEVICE(INTEL, 0xa2a9), LPSS_SPT_SSP },
1409 { PCI_VDEVICE(INTEL, 0xa2aa), LPSS_SPT_SSP },
1410 /* BXT A-Step */
1411 { PCI_VDEVICE(INTEL, 0x0ac2), LPSS_BXT_SSP },
1412 { PCI_VDEVICE(INTEL, 0x0ac4), LPSS_BXT_SSP },
1413 { PCI_VDEVICE(INTEL, 0x0ac6), LPSS_BXT_SSP },
1414 /* BXT B-Step */
1415 { PCI_VDEVICE(INTEL, 0x1ac2), LPSS_BXT_SSP },
1416 { PCI_VDEVICE(INTEL, 0x1ac4), LPSS_BXT_SSP },
1417 { PCI_VDEVICE(INTEL, 0x1ac6), LPSS_BXT_SSP },
1418 /* GLK */
1419 { PCI_VDEVICE(INTEL, 0x31c2), LPSS_BXT_SSP },
1420 { PCI_VDEVICE(INTEL, 0x31c4), LPSS_BXT_SSP },
1421 { PCI_VDEVICE(INTEL, 0x31c6), LPSS_BXT_SSP },
1422 /* ICL-LP */
1423 { PCI_VDEVICE(INTEL, 0x34aa), LPSS_CNL_SSP },
1424 { PCI_VDEVICE(INTEL, 0x34ab), LPSS_CNL_SSP },
1425 { PCI_VDEVICE(INTEL, 0x34fb), LPSS_CNL_SSP },
1426 /* APL */
1427 { PCI_VDEVICE(INTEL, 0x5ac2), LPSS_BXT_SSP },
1428 { PCI_VDEVICE(INTEL, 0x5ac4), LPSS_BXT_SSP },
1429 { PCI_VDEVICE(INTEL, 0x5ac6), LPSS_BXT_SSP },
1430 /* CNL-LP */
1431 { PCI_VDEVICE(INTEL, 0x9daa), LPSS_CNL_SSP },
1432 { PCI_VDEVICE(INTEL, 0x9dab), LPSS_CNL_SSP },
1433 { PCI_VDEVICE(INTEL, 0x9dfb), LPSS_CNL_SSP },
1434 /* CNL-H */
1435 { PCI_VDEVICE(INTEL, 0xa32a), LPSS_CNL_SSP },
1436 { PCI_VDEVICE(INTEL, 0xa32b), LPSS_CNL_SSP },
1437 { PCI_VDEVICE(INTEL, 0xa37b), LPSS_CNL_SSP },
1438 { },
1439 };
1440
pxa2xx_spi_idma_filter(struct dma_chan * chan,void * param)1441 static bool pxa2xx_spi_idma_filter(struct dma_chan *chan, void *param)
1442 {
1443 return param == chan->device->dev;
1444 }
1445
1446 static struct pxa2xx_spi_master *
pxa2xx_spi_init_pdata(struct platform_device * pdev)1447 pxa2xx_spi_init_pdata(struct platform_device *pdev)
1448 {
1449 struct pxa2xx_spi_master *pdata;
1450 struct acpi_device *adev;
1451 struct ssp_device *ssp;
1452 struct resource *res;
1453 const struct acpi_device_id *adev_id = NULL;
1454 const struct pci_device_id *pcidev_id = NULL;
1455 int type;
1456
1457 adev = ACPI_COMPANION(&pdev->dev);
1458
1459 if (dev_is_pci(pdev->dev.parent))
1460 pcidev_id = pci_match_id(pxa2xx_spi_pci_compound_match,
1461 to_pci_dev(pdev->dev.parent));
1462 else if (adev)
1463 adev_id = acpi_match_device(pdev->dev.driver->acpi_match_table,
1464 &pdev->dev);
1465 else
1466 return NULL;
1467
1468 if (adev_id)
1469 type = (int)adev_id->driver_data;
1470 else if (pcidev_id)
1471 type = (int)pcidev_id->driver_data;
1472 else
1473 return NULL;
1474
1475 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1476 if (!pdata)
1477 return NULL;
1478
1479 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1480 if (!res)
1481 return NULL;
1482
1483 ssp = &pdata->ssp;
1484
1485 ssp->phys_base = res->start;
1486 ssp->mmio_base = devm_ioremap_resource(&pdev->dev, res);
1487 if (IS_ERR(ssp->mmio_base))
1488 return NULL;
1489
1490 if (pcidev_id) {
1491 pdata->tx_param = pdev->dev.parent;
1492 pdata->rx_param = pdev->dev.parent;
1493 pdata->dma_filter = pxa2xx_spi_idma_filter;
1494 }
1495
1496 ssp->clk = devm_clk_get(&pdev->dev, NULL);
1497 if (IS_ERR(ssp->clk))
1498 return NULL;
1499
1500 ssp->irq = platform_get_irq(pdev, 0);
1501 if (ssp->irq < 0)
1502 return NULL;
1503
1504 ssp->type = type;
1505 ssp->pdev = pdev;
1506 ssp->port_id = pxa2xx_spi_get_port_id(adev);
1507
1508 pdata->num_chipselect = 1;
1509 pdata->enable_dma = true;
1510
1511 return pdata;
1512 }
1513
1514 #else /* !CONFIG_PCI */
1515 static inline struct pxa2xx_spi_master *
pxa2xx_spi_init_pdata(struct platform_device * pdev)1516 pxa2xx_spi_init_pdata(struct platform_device *pdev)
1517 {
1518 return NULL;
1519 }
1520 #endif
1521
pxa2xx_spi_fw_translate_cs(struct spi_controller * master,unsigned int cs)1522 static int pxa2xx_spi_fw_translate_cs(struct spi_controller *master,
1523 unsigned int cs)
1524 {
1525 struct driver_data *drv_data = spi_controller_get_devdata(master);
1526
1527 if (has_acpi_companion(&drv_data->pdev->dev)) {
1528 switch (drv_data->ssp_type) {
1529 /*
1530 * For Atoms the ACPI DeviceSelection used by the Windows
1531 * driver starts from 1 instead of 0 so translate it here
1532 * to match what Linux expects.
1533 */
1534 case LPSS_BYT_SSP:
1535 case LPSS_BSW_SSP:
1536 return cs - 1;
1537
1538 default:
1539 break;
1540 }
1541 }
1542
1543 return cs;
1544 }
1545
pxa2xx_spi_probe(struct platform_device * pdev)1546 static int pxa2xx_spi_probe(struct platform_device *pdev)
1547 {
1548 struct device *dev = &pdev->dev;
1549 struct pxa2xx_spi_master *platform_info;
1550 struct spi_controller *master;
1551 struct driver_data *drv_data;
1552 struct ssp_device *ssp;
1553 const struct lpss_config *config;
1554 int status, count;
1555 u32 tmp;
1556
1557 platform_info = dev_get_platdata(dev);
1558 if (!platform_info) {
1559 platform_info = pxa2xx_spi_init_pdata(pdev);
1560 if (!platform_info) {
1561 dev_err(&pdev->dev, "missing platform data\n");
1562 return -ENODEV;
1563 }
1564 }
1565
1566 ssp = pxa_ssp_request(pdev->id, pdev->name);
1567 if (!ssp)
1568 ssp = &platform_info->ssp;
1569
1570 if (!ssp->mmio_base) {
1571 dev_err(&pdev->dev, "failed to get ssp\n");
1572 return -ENODEV;
1573 }
1574
1575 master = spi_alloc_master(dev, sizeof(struct driver_data));
1576 if (!master) {
1577 dev_err(&pdev->dev, "cannot alloc spi_master\n");
1578 pxa_ssp_free(ssp);
1579 return -ENOMEM;
1580 }
1581 drv_data = spi_controller_get_devdata(master);
1582 drv_data->master = master;
1583 drv_data->master_info = platform_info;
1584 drv_data->pdev = pdev;
1585 drv_data->ssp = ssp;
1586
1587 master->dev.of_node = pdev->dev.of_node;
1588 /* the spi->mode bits understood by this driver: */
1589 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LOOP;
1590
1591 master->bus_num = ssp->port_id;
1592 master->dma_alignment = DMA_ALIGNMENT;
1593 master->cleanup = cleanup;
1594 master->setup = setup;
1595 master->set_cs = pxa2xx_spi_set_cs;
1596 master->transfer_one = pxa2xx_spi_transfer_one;
1597 master->handle_err = pxa2xx_spi_handle_err;
1598 master->unprepare_transfer_hardware = pxa2xx_spi_unprepare_transfer;
1599 master->fw_translate_cs = pxa2xx_spi_fw_translate_cs;
1600 master->auto_runtime_pm = true;
1601 master->flags = SPI_CONTROLLER_MUST_RX | SPI_CONTROLLER_MUST_TX;
1602
1603 drv_data->ssp_type = ssp->type;
1604
1605 drv_data->ioaddr = ssp->mmio_base;
1606 drv_data->ssdr_physical = ssp->phys_base + SSDR;
1607 if (pxa25x_ssp_comp(drv_data)) {
1608 switch (drv_data->ssp_type) {
1609 case QUARK_X1000_SSP:
1610 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1611 break;
1612 default:
1613 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
1614 break;
1615 }
1616
1617 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE;
1618 drv_data->dma_cr1 = 0;
1619 drv_data->clear_sr = SSSR_ROR;
1620 drv_data->mask_sr = SSSR_RFS | SSSR_TFS | SSSR_ROR;
1621 } else {
1622 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
1623 drv_data->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
1624 drv_data->dma_cr1 = DEFAULT_DMA_CR1;
1625 drv_data->clear_sr = SSSR_ROR | SSSR_TINT;
1626 drv_data->mask_sr = SSSR_TINT | SSSR_RFS | SSSR_TFS | SSSR_ROR;
1627 }
1628
1629 status = request_irq(ssp->irq, ssp_int, IRQF_SHARED, dev_name(dev),
1630 drv_data);
1631 if (status < 0) {
1632 dev_err(&pdev->dev, "cannot get IRQ %d\n", ssp->irq);
1633 goto out_error_master_alloc;
1634 }
1635
1636 /* Setup DMA if requested */
1637 if (platform_info->enable_dma) {
1638 status = pxa2xx_spi_dma_setup(drv_data);
1639 if (status) {
1640 dev_dbg(dev, "no DMA channels available, using PIO\n");
1641 platform_info->enable_dma = false;
1642 } else {
1643 master->can_dma = pxa2xx_spi_can_dma;
1644 master->max_dma_len = MAX_DMA_LEN;
1645 }
1646 }
1647
1648 /* Enable SOC clock */
1649 status = clk_prepare_enable(ssp->clk);
1650 if (status)
1651 goto out_error_dma_irq_alloc;
1652
1653 master->max_speed_hz = clk_get_rate(ssp->clk);
1654
1655 /* Load default SSP configuration */
1656 pxa2xx_spi_write(drv_data, SSCR0, 0);
1657 switch (drv_data->ssp_type) {
1658 case QUARK_X1000_SSP:
1659 tmp = QUARK_X1000_SSCR1_RxTresh(RX_THRESH_QUARK_X1000_DFLT) |
1660 QUARK_X1000_SSCR1_TxTresh(TX_THRESH_QUARK_X1000_DFLT);
1661 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1662
1663 /* using the Motorola SPI protocol and use 8 bit frame */
1664 tmp = QUARK_X1000_SSCR0_Motorola | QUARK_X1000_SSCR0_DataSize(8);
1665 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1666 break;
1667 case CE4100_SSP:
1668 tmp = CE4100_SSCR1_RxTresh(RX_THRESH_CE4100_DFLT) |
1669 CE4100_SSCR1_TxTresh(TX_THRESH_CE4100_DFLT);
1670 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1671 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1672 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1673 break;
1674 default:
1675 tmp = SSCR1_RxTresh(RX_THRESH_DFLT) |
1676 SSCR1_TxTresh(TX_THRESH_DFLT);
1677 pxa2xx_spi_write(drv_data, SSCR1, tmp);
1678 tmp = SSCR0_SCR(2) | SSCR0_Motorola | SSCR0_DataSize(8);
1679 pxa2xx_spi_write(drv_data, SSCR0, tmp);
1680 break;
1681 }
1682
1683 if (!pxa25x_ssp_comp(drv_data))
1684 pxa2xx_spi_write(drv_data, SSTO, 0);
1685
1686 if (!is_quark_x1000_ssp(drv_data))
1687 pxa2xx_spi_write(drv_data, SSPSP, 0);
1688
1689 if (is_lpss_ssp(drv_data)) {
1690 lpss_ssp_setup(drv_data);
1691 config = lpss_get_config(drv_data);
1692 if (config->reg_capabilities >= 0) {
1693 tmp = __lpss_ssp_read_priv(drv_data,
1694 config->reg_capabilities);
1695 tmp &= LPSS_CAPS_CS_EN_MASK;
1696 tmp >>= LPSS_CAPS_CS_EN_SHIFT;
1697 platform_info->num_chipselect = ffz(tmp);
1698 } else if (config->cs_num) {
1699 platform_info->num_chipselect = config->cs_num;
1700 }
1701 }
1702 master->num_chipselect = platform_info->num_chipselect;
1703
1704 count = gpiod_count(&pdev->dev, "cs");
1705 if (count > 0) {
1706 int i;
1707
1708 master->num_chipselect = max_t(int, count,
1709 master->num_chipselect);
1710
1711 drv_data->cs_gpiods = devm_kcalloc(&pdev->dev,
1712 master->num_chipselect, sizeof(struct gpio_desc *),
1713 GFP_KERNEL);
1714 if (!drv_data->cs_gpiods) {
1715 status = -ENOMEM;
1716 goto out_error_clock_enabled;
1717 }
1718
1719 for (i = 0; i < master->num_chipselect; i++) {
1720 struct gpio_desc *gpiod;
1721
1722 gpiod = devm_gpiod_get_index(dev, "cs", i, GPIOD_ASIS);
1723 if (IS_ERR(gpiod)) {
1724 /* Means use native chip select */
1725 if (PTR_ERR(gpiod) == -ENOENT)
1726 continue;
1727
1728 status = (int)PTR_ERR(gpiod);
1729 goto out_error_clock_enabled;
1730 } else {
1731 drv_data->cs_gpiods[i] = gpiod;
1732 }
1733 }
1734 }
1735
1736 pm_runtime_set_autosuspend_delay(&pdev->dev, 50);
1737 pm_runtime_use_autosuspend(&pdev->dev);
1738 pm_runtime_set_active(&pdev->dev);
1739 pm_runtime_enable(&pdev->dev);
1740
1741 /* Register with the SPI framework */
1742 platform_set_drvdata(pdev, drv_data);
1743 status = spi_register_controller(master);
1744 if (status != 0) {
1745 dev_err(&pdev->dev, "problem registering spi master\n");
1746 goto out_error_pm_runtime_enabled;
1747 }
1748
1749 return status;
1750
1751 out_error_pm_runtime_enabled:
1752 pm_runtime_disable(&pdev->dev);
1753
1754 out_error_clock_enabled:
1755 clk_disable_unprepare(ssp->clk);
1756
1757 out_error_dma_irq_alloc:
1758 pxa2xx_spi_dma_release(drv_data);
1759 free_irq(ssp->irq, drv_data);
1760
1761 out_error_master_alloc:
1762 spi_controller_put(master);
1763 pxa_ssp_free(ssp);
1764 return status;
1765 }
1766
pxa2xx_spi_remove(struct platform_device * pdev)1767 static int pxa2xx_spi_remove(struct platform_device *pdev)
1768 {
1769 struct driver_data *drv_data = platform_get_drvdata(pdev);
1770 struct ssp_device *ssp;
1771
1772 if (!drv_data)
1773 return 0;
1774 ssp = drv_data->ssp;
1775
1776 pm_runtime_get_sync(&pdev->dev);
1777
1778 spi_unregister_controller(drv_data->master);
1779
1780 /* Disable the SSP at the peripheral and SOC level */
1781 pxa2xx_spi_write(drv_data, SSCR0, 0);
1782 clk_disable_unprepare(ssp->clk);
1783
1784 /* Release DMA */
1785 if (drv_data->master_info->enable_dma)
1786 pxa2xx_spi_dma_release(drv_data);
1787
1788 pm_runtime_put_noidle(&pdev->dev);
1789 pm_runtime_disable(&pdev->dev);
1790
1791 /* Release IRQ */
1792 free_irq(ssp->irq, drv_data);
1793
1794 /* Release SSP */
1795 pxa_ssp_free(ssp);
1796
1797 return 0;
1798 }
1799
pxa2xx_spi_shutdown(struct platform_device * pdev)1800 static void pxa2xx_spi_shutdown(struct platform_device *pdev)
1801 {
1802 int status = 0;
1803
1804 if ((status = pxa2xx_spi_remove(pdev)) != 0)
1805 dev_err(&pdev->dev, "shutdown failed with %d\n", status);
1806 }
1807
1808 #ifdef CONFIG_PM_SLEEP
pxa2xx_spi_suspend(struct device * dev)1809 static int pxa2xx_spi_suspend(struct device *dev)
1810 {
1811 struct driver_data *drv_data = dev_get_drvdata(dev);
1812 struct ssp_device *ssp = drv_data->ssp;
1813 int status;
1814
1815 status = spi_controller_suspend(drv_data->master);
1816 if (status != 0)
1817 return status;
1818 pxa2xx_spi_write(drv_data, SSCR0, 0);
1819
1820 if (!pm_runtime_suspended(dev))
1821 clk_disable_unprepare(ssp->clk);
1822
1823 return 0;
1824 }
1825
pxa2xx_spi_resume(struct device * dev)1826 static int pxa2xx_spi_resume(struct device *dev)
1827 {
1828 struct driver_data *drv_data = dev_get_drvdata(dev);
1829 struct ssp_device *ssp = drv_data->ssp;
1830 int status;
1831
1832 /* Enable the SSP clock */
1833 if (!pm_runtime_suspended(dev)) {
1834 status = clk_prepare_enable(ssp->clk);
1835 if (status)
1836 return status;
1837 }
1838
1839 /* Restore LPSS private register bits */
1840 if (is_lpss_ssp(drv_data))
1841 lpss_ssp_setup(drv_data);
1842
1843 /* Start the queue running */
1844 status = spi_controller_resume(drv_data->master);
1845 if (status != 0) {
1846 dev_err(dev, "problem starting queue (%d)\n", status);
1847 return status;
1848 }
1849
1850 return 0;
1851 }
1852 #endif
1853
1854 #ifdef CONFIG_PM
pxa2xx_spi_runtime_suspend(struct device * dev)1855 static int pxa2xx_spi_runtime_suspend(struct device *dev)
1856 {
1857 struct driver_data *drv_data = dev_get_drvdata(dev);
1858
1859 clk_disable_unprepare(drv_data->ssp->clk);
1860 return 0;
1861 }
1862
pxa2xx_spi_runtime_resume(struct device * dev)1863 static int pxa2xx_spi_runtime_resume(struct device *dev)
1864 {
1865 struct driver_data *drv_data = dev_get_drvdata(dev);
1866 int status;
1867
1868 status = clk_prepare_enable(drv_data->ssp->clk);
1869 return status;
1870 }
1871 #endif
1872
1873 static const struct dev_pm_ops pxa2xx_spi_pm_ops = {
1874 SET_SYSTEM_SLEEP_PM_OPS(pxa2xx_spi_suspend, pxa2xx_spi_resume)
1875 SET_RUNTIME_PM_OPS(pxa2xx_spi_runtime_suspend,
1876 pxa2xx_spi_runtime_resume, NULL)
1877 };
1878
1879 static struct platform_driver driver = {
1880 .driver = {
1881 .name = "pxa2xx-spi",
1882 .pm = &pxa2xx_spi_pm_ops,
1883 .acpi_match_table = ACPI_PTR(pxa2xx_spi_acpi_match),
1884 },
1885 .probe = pxa2xx_spi_probe,
1886 .remove = pxa2xx_spi_remove,
1887 .shutdown = pxa2xx_spi_shutdown,
1888 };
1889
pxa2xx_spi_init(void)1890 static int __init pxa2xx_spi_init(void)
1891 {
1892 return platform_driver_register(&driver);
1893 }
1894 subsys_initcall(pxa2xx_spi_init);
1895
pxa2xx_spi_exit(void)1896 static void __exit pxa2xx_spi_exit(void)
1897 {
1898 platform_driver_unregister(&driver);
1899 }
1900 module_exit(pxa2xx_spi_exit);
1901