blob: 835cdda6f4f586d1eb31fccf6904f4203a82ddcd [file] [log] [blame]
Shadi Ammouri60cadec2008-08-05 13:01:09 -07001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * Marvell Orion SPI controller driver
Shadi Ammouri60cadec2008-08-05 13:01:09 -07003 *
4 * Author: Shadi Ammouri <shadi@marvell.com>
5 * Copyright (C) 2007-2008 Marvell Ltd.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
Shadi Ammouri60cadec2008-08-05 13:01:09 -070012#include <linux/interrupt.h>
13#include <linux/delay.h>
14#include <linux/platform_device.h>
15#include <linux/err.h>
16#include <linux/io.h>
17#include <linux/spi/spi.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040018#include <linux/module.h>
Russell King5c678692014-06-21 12:22:37 +010019#include <linux/pm_runtime.h>
Andrew Lunnf814f9a2012-07-23 12:08:09 +020020#include <linux/of.h>
Greg Ungererdf59fa72014-09-28 23:24:04 +100021#include <linux/of_device.h>
Andrew Lunn4574b882012-04-06 17:17:26 +020022#include <linux/clk.h>
Mark Brown895248f2013-07-29 05:10:21 +010023#include <linux/sizes.h>
Shadi Ammouri60cadec2008-08-05 13:01:09 -070024#include <asm/unaligned.h>
25
26#define DRIVER_NAME "orion_spi"
27
Russell King5c678692014-06-21 12:22:37 +010028/* Runtime PM autosuspend timeout: PM is fairly light on this driver */
29#define SPI_AUTOSUSPEND_TIMEOUT 200
30
Shadi Ammouri60cadec2008-08-05 13:01:09 -070031#define ORION_NUM_CHIPSELECTS 1 /* only one slave is supported*/
32#define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
33
34#define ORION_SPI_IF_CTRL_REG 0x00
35#define ORION_SPI_IF_CONFIG_REG 0x04
36#define ORION_SPI_DATA_OUT_REG 0x08
37#define ORION_SPI_DATA_IN_REG 0x0c
38#define ORION_SPI_INT_CAUSE_REG 0x10
39
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -070040#define ORION_SPI_MODE_CPOL (1 << 11)
41#define ORION_SPI_MODE_CPHA (1 << 12)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070042#define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
43#define ORION_SPI_CLK_PRESCALE_MASK 0x1F
Greg Ungererdf59fa72014-09-28 23:24:04 +100044#define ARMADA_SPI_CLK_PRESCALE_MASK 0xDF
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -070045#define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \
46 ORION_SPI_MODE_CPHA)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070047
Greg Ungererdf59fa72014-09-28 23:24:04 +100048enum orion_spi_type {
49 ORION_SPI,
50 ARMADA_SPI,
51};
52
53struct orion_spi_dev {
54 enum orion_spi_type typ;
55 unsigned int min_divisor;
56 unsigned int max_divisor;
57 u32 prescale_mask;
58};
59
Shadi Ammouri60cadec2008-08-05 13:01:09 -070060struct orion_spi {
Shadi Ammouri60cadec2008-08-05 13:01:09 -070061 struct spi_master *master;
62 void __iomem *base;
Andrew Lunn4574b882012-04-06 17:17:26 +020063 struct clk *clk;
Greg Ungererdf59fa72014-09-28 23:24:04 +100064 const struct orion_spi_dev *devdata;
Shadi Ammouri60cadec2008-08-05 13:01:09 -070065};
66
Shadi Ammouri60cadec2008-08-05 13:01:09 -070067static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
68{
69 return orion_spi->base + reg;
70}
71
72static inline void
73orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
74{
75 void __iomem *reg_addr = spi_reg(orion_spi, reg);
76 u32 val;
77
78 val = readl(reg_addr);
79 val |= mask;
80 writel(val, reg_addr);
81}
82
83static inline void
84orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
85{
86 void __iomem *reg_addr = spi_reg(orion_spi, reg);
87 u32 val;
88
89 val = readl(reg_addr);
90 val &= ~mask;
91 writel(val, reg_addr);
92}
93
Shadi Ammouri60cadec2008-08-05 13:01:09 -070094static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
95{
96 u32 tclk_hz;
97 u32 rate;
98 u32 prescale;
99 u32 reg;
100 struct orion_spi *orion_spi;
Greg Ungererdf59fa72014-09-28 23:24:04 +1000101 const struct orion_spi_dev *devdata;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700102
103 orion_spi = spi_master_get_devdata(spi->master);
Greg Ungererdf59fa72014-09-28 23:24:04 +1000104 devdata = orion_spi->devdata;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700105
Andrew Lunn4574b882012-04-06 17:17:26 +0200106 tclk_hz = clk_get_rate(orion_spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700107
Greg Ungererdf59fa72014-09-28 23:24:04 +1000108 if (devdata->typ == ARMADA_SPI) {
109 unsigned int clk, spr, sppr, sppr2, err;
110 unsigned int best_spr, best_sppr, best_err;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700111
Greg Ungererdf59fa72014-09-28 23:24:04 +1000112 best_err = speed;
113 best_spr = 0;
114 best_sppr = 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700115
Greg Ungererdf59fa72014-09-28 23:24:04 +1000116 /* Iterate over the valid range looking for best fit */
117 for (sppr = 0; sppr < 8; sppr++) {
118 sppr2 = 0x1 << sppr;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700119
Greg Ungererdf59fa72014-09-28 23:24:04 +1000120 spr = tclk_hz / sppr2;
121 spr = DIV_ROUND_UP(spr, speed);
122 if ((spr == 0) || (spr > 15))
123 continue;
124
125 clk = tclk_hz / (spr * sppr2);
126 err = speed - clk;
127
128 if (err < best_err) {
129 best_spr = spr;
130 best_sppr = sppr;
131 best_err = err;
132 }
133 }
134
135 if ((best_sppr == 0) && (best_spr == 0))
136 return -EINVAL;
137
138 prescale = ((best_sppr & 0x6) << 5) |
139 ((best_sppr & 0x1) << 4) | best_spr;
140 } else {
141 /*
142 * the supported rates are: 4,6,8...30
143 * round up as we look for equal or less speed
144 */
145 rate = DIV_ROUND_UP(tclk_hz, speed);
146 rate = roundup(rate, 2);
147
148 /* check if requested speed is too small */
149 if (rate > 30)
150 return -EINVAL;
151
152 if (rate < 4)
153 rate = 4;
154
155 /* Convert the rate to SPI clock divisor value. */
156 prescale = 0x10 + rate/2;
157 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700158
159 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
Greg Ungererdf59fa72014-09-28 23:24:04 +1000160 reg = ((reg & ~devdata->prescale_mask) | prescale);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700161 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
162
163 return 0;
164}
165
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700166static void
167orion_spi_mode_set(struct spi_device *spi)
168{
169 u32 reg;
170 struct orion_spi *orion_spi;
171
172 orion_spi = spi_master_get_devdata(spi->master);
173
174 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
175 reg &= ~ORION_SPI_MODE_MASK;
176 if (spi->mode & SPI_CPOL)
177 reg |= ORION_SPI_MODE_CPOL;
178 if (spi->mode & SPI_CPHA)
179 reg |= ORION_SPI_MODE_CPHA;
180 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
181}
182
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700183/*
184 * called only when no transfer is active on the bus
185 */
186static int
187orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
188{
189 struct orion_spi *orion_spi;
190 unsigned int speed = spi->max_speed_hz;
191 unsigned int bits_per_word = spi->bits_per_word;
192 int rc;
193
194 orion_spi = spi_master_get_devdata(spi->master);
195
196 if ((t != NULL) && t->speed_hz)
197 speed = t->speed_hz;
198
199 if ((t != NULL) && t->bits_per_word)
200 bits_per_word = t->bits_per_word;
201
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700202 orion_spi_mode_set(spi);
203
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700204 rc = orion_spi_baudrate_set(spi, speed);
205 if (rc)
206 return rc;
207
Axel Lin495b3352014-02-11 20:51:36 +0800208 if (bits_per_word == 16)
209 orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
210 ORION_SPI_IF_8_16_BIT_MODE);
211 else
212 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
213 ORION_SPI_IF_8_16_BIT_MODE);
214
215 return 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700216}
217
218static void orion_spi_set_cs(struct orion_spi *orion_spi, int enable)
219{
220 if (enable)
221 orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
222 else
223 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
224}
225
226static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
227{
228 int i;
229
230 for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
231 if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
232 return 1;
Jingoo Hanb8434042014-09-02 11:51:39 +0900233
234 udelay(1);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700235 }
236
237 return -1;
238}
239
240static inline int
241orion_spi_write_read_8bit(struct spi_device *spi,
242 const u8 **tx_buf, u8 **rx_buf)
243{
244 void __iomem *tx_reg, *rx_reg, *int_reg;
245 struct orion_spi *orion_spi;
246
247 orion_spi = spi_master_get_devdata(spi->master);
248 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
249 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
250 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
251
252 /* clear the interrupt cause register */
253 writel(0x0, int_reg);
254
255 if (tx_buf && *tx_buf)
256 writel(*(*tx_buf)++, tx_reg);
257 else
258 writel(0, tx_reg);
259
260 if (orion_spi_wait_till_ready(orion_spi) < 0) {
261 dev_err(&spi->dev, "TXS timed out\n");
262 return -1;
263 }
264
265 if (rx_buf && *rx_buf)
266 *(*rx_buf)++ = readl(rx_reg);
267
268 return 1;
269}
270
271static inline int
272orion_spi_write_read_16bit(struct spi_device *spi,
273 const u16 **tx_buf, u16 **rx_buf)
274{
275 void __iomem *tx_reg, *rx_reg, *int_reg;
276 struct orion_spi *orion_spi;
277
278 orion_spi = spi_master_get_devdata(spi->master);
279 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
280 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
281 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
282
283 /* clear the interrupt cause register */
284 writel(0x0, int_reg);
285
286 if (tx_buf && *tx_buf)
287 writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
288 else
289 writel(0, tx_reg);
290
291 if (orion_spi_wait_till_ready(orion_spi) < 0) {
292 dev_err(&spi->dev, "TXS timed out\n");
293 return -1;
294 }
295
296 if (rx_buf && *rx_buf)
297 put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
298
299 return 1;
300}
301
302static unsigned int
303orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
304{
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700305 unsigned int count;
306 int word_len;
307
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700308 word_len = spi->bits_per_word;
309 count = xfer->len;
310
311 if (word_len == 8) {
312 const u8 *tx = xfer->tx_buf;
313 u8 *rx = xfer->rx_buf;
314
315 do {
316 if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
317 goto out;
318 count--;
319 } while (count);
320 } else if (word_len == 16) {
321 const u16 *tx = xfer->tx_buf;
322 u16 *rx = xfer->rx_buf;
323
324 do {
325 if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
326 goto out;
327 count -= 2;
328 } while (count);
329 }
330
331out:
332 return xfer->len - count;
333}
334
Andrew Lunnba59a802012-07-23 13:16:55 +0200335static int orion_spi_transfer_one_message(struct spi_master *master,
336 struct spi_message *m)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700337{
Andrew Lunnba59a802012-07-23 13:16:55 +0200338 struct orion_spi *orion_spi = spi_master_get_devdata(master);
339 struct spi_device *spi = m->spi;
340 struct spi_transfer *t = NULL;
341 int par_override = 0;
342 int status = 0;
343 int cs_active = 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700344
Andrew Lunnba59a802012-07-23 13:16:55 +0200345 /* Load defaults */
346 status = orion_spi_setup_transfer(spi, NULL);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700347
Andrew Lunnba59a802012-07-23 13:16:55 +0200348 if (status < 0)
349 goto msg_done;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700350
Andrew Lunnba59a802012-07-23 13:16:55 +0200351 list_for_each_entry(t, &m->transfers, transfer_list) {
Andrew Lunnba59a802012-07-23 13:16:55 +0200352 if (par_override || t->speed_hz || t->bits_per_word) {
353 par_override = 1;
354 status = orion_spi_setup_transfer(spi, t);
355 if (status < 0)
356 break;
357 if (!t->speed_hz && !t->bits_per_word)
358 par_override = 0;
359 }
360
361 if (!cs_active) {
362 orion_spi_set_cs(orion_spi, 1);
363 cs_active = 1;
364 }
365
366 if (t->len)
367 m->actual_length += orion_spi_write_read(spi, t);
368
369 if (t->delay_usecs)
370 udelay(t->delay_usecs);
371
372 if (t->cs_change) {
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700373 orion_spi_set_cs(orion_spi, 0);
Andrew Lunnba59a802012-07-23 13:16:55 +0200374 cs_active = 0;
375 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700376 }
377
Andrew Lunnba59a802012-07-23 13:16:55 +0200378msg_done:
379 if (cs_active)
380 orion_spi_set_cs(orion_spi, 0);
381
382 m->status = status;
383 spi_finalize_current_message(master);
384
385 return 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700386}
387
Grant Likely2deff8d2013-02-05 13:27:35 +0000388static int orion_spi_reset(struct orion_spi *orion_spi)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700389{
390 /* Verify that the CS is deasserted */
391 orion_spi_set_cs(orion_spi, 0);
392
393 return 0;
394}
395
Greg Ungererdf59fa72014-09-28 23:24:04 +1000396static const struct orion_spi_dev orion_spi_dev_data = {
397 .typ = ORION_SPI,
398 .min_divisor = 4,
399 .max_divisor = 30,
400 .prescale_mask = ORION_SPI_CLK_PRESCALE_MASK,
401};
402
403static const struct orion_spi_dev armada_spi_dev_data = {
404 .typ = ARMADA_SPI,
405 .min_divisor = 1,
406 .max_divisor = 1920,
407 .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
408};
409
410static const struct of_device_id orion_spi_of_match_table[] = {
411 { .compatible = "marvell,orion-spi", .data = &orion_spi_dev_data, },
412 { .compatible = "marvell,armada-370-spi", .data = &armada_spi_dev_data, },
413 {}
414};
415MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
416
Grant Likely2deff8d2013-02-05 13:27:35 +0000417static int orion_spi_probe(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700418{
Greg Ungererdf59fa72014-09-28 23:24:04 +1000419 const struct of_device_id *of_id;
420 const struct orion_spi_dev *devdata;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700421 struct spi_master *master;
422 struct orion_spi *spi;
423 struct resource *r;
Andrew Lunn4574b882012-04-06 17:17:26 +0200424 unsigned long tclk_hz;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700425 int status = 0;
426
Jingoo Han3fed8062013-10-14 10:35:08 +0900427 master = spi_alloc_master(&pdev->dev, sizeof(*spi));
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700428 if (master == NULL) {
429 dev_dbg(&pdev->dev, "master allocation failed\n");
430 return -ENOMEM;
431 }
432
433 if (pdev->id != -1)
434 master->bus_num = pdev->id;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200435 if (pdev->dev.of_node) {
Thomas Petazzonie06871c2014-07-27 23:53:19 +0200436 u32 cell_index;
Jingoo Hanb8434042014-09-02 11:51:39 +0900437
Thomas Petazzonie06871c2014-07-27 23:53:19 +0200438 if (!of_property_read_u32(pdev->dev.of_node, "cell-index",
439 &cell_index))
440 master->bus_num = cell_index;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200441 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700442
David Brownelle7db06b2009-06-17 16:26:04 -0700443 /* we support only mode 0, and no options */
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700444 master->mode_bits = SPI_CPHA | SPI_CPOL;
David Brownelle7db06b2009-06-17 16:26:04 -0700445
Andrew Lunnba59a802012-07-23 13:16:55 +0200446 master->transfer_one_message = orion_spi_transfer_one_message;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700447 master->num_chipselect = ORION_NUM_CHIPSELECTS;
Axel Lin495b3352014-02-11 20:51:36 +0800448 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
Russell King5c678692014-06-21 12:22:37 +0100449 master->auto_runtime_pm = true;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700450
Jingoo Han24b5a822013-05-23 19:20:40 +0900451 platform_set_drvdata(pdev, master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700452
453 spi = spi_master_get_devdata(master);
454 spi->master = master;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700455
Greg Ungererdf59fa72014-09-28 23:24:04 +1000456 of_id = of_match_device(orion_spi_of_match_table, &pdev->dev);
457 devdata = of_id->data;
458 spi->devdata = devdata;
459
Jingoo Hanbb489842013-12-09 19:21:22 +0900460 spi->clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4574b882012-04-06 17:17:26 +0200461 if (IS_ERR(spi->clk)) {
462 status = PTR_ERR(spi->clk);
463 goto out;
464 }
465
Russell Kingc85012a2014-06-21 11:32:23 +0100466 status = clk_prepare_enable(spi->clk);
467 if (status)
468 goto out;
469
Andrew Lunn4574b882012-04-06 17:17:26 +0200470 tclk_hz = clk_get_rate(spi->clk);
Greg Ungererdf59fa72014-09-28 23:24:04 +1000471 master->max_speed_hz = DIV_ROUND_UP(tclk_hz, devdata->min_divisor);
472 master->min_speed_hz = DIV_ROUND_UP(tclk_hz, devdata->max_divisor);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700473
474 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Mark Brown1729ce32013-07-28 14:38:06 +0100475 spi->base = devm_ioremap_resource(&pdev->dev, r);
476 if (IS_ERR(spi->base)) {
477 status = PTR_ERR(spi->base);
Andrew Lunn4574b882012-04-06 17:17:26 +0200478 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700479 }
480
Russell King5c678692014-06-21 12:22:37 +0100481 pm_runtime_set_active(&pdev->dev);
482 pm_runtime_use_autosuspend(&pdev->dev);
483 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
484 pm_runtime_enable(&pdev->dev);
485
Wei Yongjun14033812014-07-20 22:03:14 +0800486 status = orion_spi_reset(spi);
487 if (status < 0)
Russell King5c678692014-06-21 12:22:37 +0100488 goto out_rel_pm;
489
490 pm_runtime_mark_last_busy(&pdev->dev);
491 pm_runtime_put_autosuspend(&pdev->dev);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700492
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200493 master->dev.of_node = pdev->dev.of_node;
Russell King5c678692014-06-21 12:22:37 +0100494 status = spi_register_master(master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700495 if (status < 0)
Russell King5c678692014-06-21 12:22:37 +0100496 goto out_rel_pm;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700497
498 return status;
499
Russell King5c678692014-06-21 12:22:37 +0100500out_rel_pm:
501 pm_runtime_disable(&pdev->dev);
Andrew Lunn4574b882012-04-06 17:17:26 +0200502out_rel_clk:
503 clk_disable_unprepare(spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700504out:
505 spi_master_put(master);
506 return status;
507}
508
509
Grant Likely2deff8d2013-02-05 13:27:35 +0000510static int orion_spi_remove(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700511{
Russell King5c678692014-06-21 12:22:37 +0100512 struct spi_master *master = platform_get_drvdata(pdev);
513 struct orion_spi *spi = spi_master_get_devdata(master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700514
Russell King5c678692014-06-21 12:22:37 +0100515 pm_runtime_get_sync(&pdev->dev);
Andrew Lunn4574b882012-04-06 17:17:26 +0200516 clk_disable_unprepare(spi->clk);
Andrew Lunn4574b882012-04-06 17:17:26 +0200517
Russell King5c678692014-06-21 12:22:37 +0100518 spi_unregister_master(master);
519 pm_runtime_disable(&pdev->dev);
520
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700521 return 0;
522}
523
524MODULE_ALIAS("platform:" DRIVER_NAME);
525
Russell King5c678692014-06-21 12:22:37 +0100526#ifdef CONFIG_PM_RUNTIME
527static int orion_spi_runtime_suspend(struct device *dev)
528{
529 struct spi_master *master = dev_get_drvdata(dev);
530 struct orion_spi *spi = spi_master_get_devdata(master);
531
532 clk_disable_unprepare(spi->clk);
533 return 0;
534}
535
536static int orion_spi_runtime_resume(struct device *dev)
537{
538 struct spi_master *master = dev_get_drvdata(dev);
539 struct orion_spi *spi = spi_master_get_devdata(master);
540
541 return clk_prepare_enable(spi->clk);
542}
543#endif
544
545static const struct dev_pm_ops orion_spi_pm_ops = {
546 SET_RUNTIME_PM_OPS(orion_spi_runtime_suspend,
547 orion_spi_runtime_resume,
548 NULL)
549};
550
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700551static struct platform_driver orion_spi_driver = {
552 .driver = {
553 .name = DRIVER_NAME,
554 .owner = THIS_MODULE,
Russell King5c678692014-06-21 12:22:37 +0100555 .pm = &orion_spi_pm_ops,
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200556 .of_match_table = of_match_ptr(orion_spi_of_match_table),
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700557 },
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300558 .probe = orion_spi_probe,
Grant Likely2deff8d2013-02-05 13:27:35 +0000559 .remove = orion_spi_remove,
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700560};
561
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300562module_platform_driver(orion_spi_driver);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700563
564MODULE_DESCRIPTION("Orion SPI driver");
565MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
566MODULE_LICENSE("GPL");