blob: 861664776672cfab25200b22dba1227d28af08d6 [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
Ken Wilson23244402015-01-16 13:10:47 +100031/* Some SoCs using this driver support up to 8 chip selects.
32 * It is up to the implementer to only use the chip selects
33 * that are available.
34 */
35#define ORION_NUM_CHIPSELECTS 8
36
Shadi Ammouri60cadec2008-08-05 13:01:09 -070037#define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */
38
39#define ORION_SPI_IF_CTRL_REG 0x00
40#define ORION_SPI_IF_CONFIG_REG 0x04
41#define ORION_SPI_DATA_OUT_REG 0x08
42#define ORION_SPI_DATA_IN_REG 0x0c
43#define ORION_SPI_INT_CAUSE_REG 0x10
44
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -070045#define ORION_SPI_MODE_CPOL (1 << 11)
46#define ORION_SPI_MODE_CPHA (1 << 12)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070047#define ORION_SPI_IF_8_16_BIT_MODE (1 << 5)
48#define ORION_SPI_CLK_PRESCALE_MASK 0x1F
Greg Ungererdf59fa72014-09-28 23:24:04 +100049#define ARMADA_SPI_CLK_PRESCALE_MASK 0xDF
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -070050#define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \
51 ORION_SPI_MODE_CPHA)
Ken Wilson23244402015-01-16 13:10:47 +100052#define ORION_SPI_CS_MASK 0x1C
53#define ORION_SPI_CS_SHIFT 2
54#define ORION_SPI_CS(cs) ((cs << ORION_SPI_CS_SHIFT) & \
55 ORION_SPI_CS_MASK)
Shadi Ammouri60cadec2008-08-05 13:01:09 -070056
Greg Ungererdf59fa72014-09-28 23:24:04 +100057enum orion_spi_type {
58 ORION_SPI,
59 ARMADA_SPI,
60};
61
62struct orion_spi_dev {
63 enum orion_spi_type typ;
64 unsigned int min_divisor;
65 unsigned int max_divisor;
66 u32 prescale_mask;
67};
68
Shadi Ammouri60cadec2008-08-05 13:01:09 -070069struct orion_spi {
Shadi Ammouri60cadec2008-08-05 13:01:09 -070070 struct spi_master *master;
71 void __iomem *base;
Andrew Lunn4574b882012-04-06 17:17:26 +020072 struct clk *clk;
Greg Ungererdf59fa72014-09-28 23:24:04 +100073 const struct orion_spi_dev *devdata;
Shadi Ammouri60cadec2008-08-05 13:01:09 -070074};
75
Shadi Ammouri60cadec2008-08-05 13:01:09 -070076static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg)
77{
78 return orion_spi->base + reg;
79}
80
81static inline void
82orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
83{
84 void __iomem *reg_addr = spi_reg(orion_spi, reg);
85 u32 val;
86
87 val = readl(reg_addr);
88 val |= mask;
89 writel(val, reg_addr);
90}
91
92static inline void
93orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask)
94{
95 void __iomem *reg_addr = spi_reg(orion_spi, reg);
96 u32 val;
97
98 val = readl(reg_addr);
99 val &= ~mask;
100 writel(val, reg_addr);
101}
102
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700103static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed)
104{
105 u32 tclk_hz;
106 u32 rate;
107 u32 prescale;
108 u32 reg;
109 struct orion_spi *orion_spi;
Greg Ungererdf59fa72014-09-28 23:24:04 +1000110 const struct orion_spi_dev *devdata;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700111
112 orion_spi = spi_master_get_devdata(spi->master);
Greg Ungererdf59fa72014-09-28 23:24:04 +1000113 devdata = orion_spi->devdata;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700114
Andrew Lunn4574b882012-04-06 17:17:26 +0200115 tclk_hz = clk_get_rate(orion_spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700116
Greg Ungererdf59fa72014-09-28 23:24:04 +1000117 if (devdata->typ == ARMADA_SPI) {
118 unsigned int clk, spr, sppr, sppr2, err;
119 unsigned int best_spr, best_sppr, best_err;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700120
Greg Ungererdf59fa72014-09-28 23:24:04 +1000121 best_err = speed;
122 best_spr = 0;
123 best_sppr = 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700124
Greg Ungererdf59fa72014-09-28 23:24:04 +1000125 /* Iterate over the valid range looking for best fit */
126 for (sppr = 0; sppr < 8; sppr++) {
127 sppr2 = 0x1 << sppr;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700128
Greg Ungererdf59fa72014-09-28 23:24:04 +1000129 spr = tclk_hz / sppr2;
130 spr = DIV_ROUND_UP(spr, speed);
131 if ((spr == 0) || (spr > 15))
132 continue;
133
134 clk = tclk_hz / (spr * sppr2);
135 err = speed - clk;
136
137 if (err < best_err) {
138 best_spr = spr;
139 best_sppr = sppr;
140 best_err = err;
141 }
142 }
143
144 if ((best_sppr == 0) && (best_spr == 0))
145 return -EINVAL;
146
147 prescale = ((best_sppr & 0x6) << 5) |
148 ((best_sppr & 0x1) << 4) | best_spr;
149 } else {
150 /*
151 * the supported rates are: 4,6,8...30
152 * round up as we look for equal or less speed
153 */
154 rate = DIV_ROUND_UP(tclk_hz, speed);
155 rate = roundup(rate, 2);
156
157 /* check if requested speed is too small */
158 if (rate > 30)
159 return -EINVAL;
160
161 if (rate < 4)
162 rate = 4;
163
164 /* Convert the rate to SPI clock divisor value. */
165 prescale = 0x10 + rate/2;
166 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700167
168 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
Greg Ungererdf59fa72014-09-28 23:24:04 +1000169 reg = ((reg & ~devdata->prescale_mask) | prescale);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700170 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
171
172 return 0;
173}
174
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700175static void
176orion_spi_mode_set(struct spi_device *spi)
177{
178 u32 reg;
179 struct orion_spi *orion_spi;
180
181 orion_spi = spi_master_get_devdata(spi->master);
182
183 reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
184 reg &= ~ORION_SPI_MODE_MASK;
185 if (spi->mode & SPI_CPOL)
186 reg |= ORION_SPI_MODE_CPOL;
187 if (spi->mode & SPI_CPHA)
188 reg |= ORION_SPI_MODE_CPHA;
189 writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG));
190}
191
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700192/*
193 * called only when no transfer is active on the bus
194 */
195static int
196orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
197{
198 struct orion_spi *orion_spi;
199 unsigned int speed = spi->max_speed_hz;
200 unsigned int bits_per_word = spi->bits_per_word;
201 int rc;
202
203 orion_spi = spi_master_get_devdata(spi->master);
204
205 if ((t != NULL) && t->speed_hz)
206 speed = t->speed_hz;
207
208 if ((t != NULL) && t->bits_per_word)
209 bits_per_word = t->bits_per_word;
210
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700211 orion_spi_mode_set(spi);
212
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700213 rc = orion_spi_baudrate_set(spi, speed);
214 if (rc)
215 return rc;
216
Axel Lin495b3352014-02-11 20:51:36 +0800217 if (bits_per_word == 16)
218 orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
219 ORION_SPI_IF_8_16_BIT_MODE);
220 else
221 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG,
222 ORION_SPI_IF_8_16_BIT_MODE);
223
224 return 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700225}
226
Ken Wilson75872eb2015-01-12 13:13:59 +1000227static void orion_spi_set_cs(struct spi_device *spi, bool enable)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700228{
Ken Wilson75872eb2015-01-12 13:13:59 +1000229 struct orion_spi *orion_spi;
230
231 orion_spi = spi_master_get_devdata(spi->master);
232
Ken Wilson23244402015-01-16 13:10:47 +1000233 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, ORION_SPI_CS_MASK);
234 orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG,
235 ORION_SPI_CS(spi->chip_select));
236
Ken Wilson75872eb2015-01-12 13:13:59 +1000237 /* Chip select logic is inverted from spi_set_cs */
238 if (!enable)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700239 orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
240 else
241 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
242}
243
244static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi)
245{
246 int i;
247
248 for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) {
249 if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG)))
250 return 1;
Jingoo Hanb8434042014-09-02 11:51:39 +0900251
252 udelay(1);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700253 }
254
255 return -1;
256}
257
258static inline int
259orion_spi_write_read_8bit(struct spi_device *spi,
260 const u8 **tx_buf, u8 **rx_buf)
261{
262 void __iomem *tx_reg, *rx_reg, *int_reg;
263 struct orion_spi *orion_spi;
264
265 orion_spi = spi_master_get_devdata(spi->master);
266 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
267 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
268 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
269
270 /* clear the interrupt cause register */
271 writel(0x0, int_reg);
272
273 if (tx_buf && *tx_buf)
274 writel(*(*tx_buf)++, tx_reg);
275 else
276 writel(0, tx_reg);
277
278 if (orion_spi_wait_till_ready(orion_spi) < 0) {
279 dev_err(&spi->dev, "TXS timed out\n");
280 return -1;
281 }
282
283 if (rx_buf && *rx_buf)
284 *(*rx_buf)++ = readl(rx_reg);
285
286 return 1;
287}
288
289static inline int
290orion_spi_write_read_16bit(struct spi_device *spi,
291 const u16 **tx_buf, u16 **rx_buf)
292{
293 void __iomem *tx_reg, *rx_reg, *int_reg;
294 struct orion_spi *orion_spi;
295
296 orion_spi = spi_master_get_devdata(spi->master);
297 tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG);
298 rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG);
299 int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG);
300
301 /* clear the interrupt cause register */
302 writel(0x0, int_reg);
303
304 if (tx_buf && *tx_buf)
305 writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg);
306 else
307 writel(0, tx_reg);
308
309 if (orion_spi_wait_till_ready(orion_spi) < 0) {
310 dev_err(&spi->dev, "TXS timed out\n");
311 return -1;
312 }
313
314 if (rx_buf && *rx_buf)
315 put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++);
316
317 return 1;
318}
319
320static unsigned int
321orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer)
322{
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700323 unsigned int count;
324 int word_len;
325
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700326 word_len = spi->bits_per_word;
327 count = xfer->len;
328
329 if (word_len == 8) {
330 const u8 *tx = xfer->tx_buf;
331 u8 *rx = xfer->rx_buf;
332
333 do {
334 if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0)
335 goto out;
336 count--;
337 } while (count);
338 } else if (word_len == 16) {
339 const u16 *tx = xfer->tx_buf;
340 u16 *rx = xfer->rx_buf;
341
342 do {
343 if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0)
344 goto out;
345 count -= 2;
346 } while (count);
347 }
348
349out:
350 return xfer->len - count;
351}
352
Ken Wilson75872eb2015-01-12 13:13:59 +1000353static int orion_spi_transfer_one(struct spi_master *master,
354 struct spi_device *spi,
355 struct spi_transfer *t)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700356{
Andrew Lunnba59a802012-07-23 13:16:55 +0200357 int status = 0;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700358
Ken Wilson75872eb2015-01-12 13:13:59 +1000359 status = orion_spi_setup_transfer(spi, t);
Andrew Lunnba59a802012-07-23 13:16:55 +0200360 if (status < 0)
Ken Wilson75872eb2015-01-12 13:13:59 +1000361 return status;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700362
Ken Wilson75872eb2015-01-12 13:13:59 +1000363 if (t->len)
364 orion_spi_write_read(spi, t);
Andrew Lunnba59a802012-07-23 13:16:55 +0200365
Ken Wilson75872eb2015-01-12 13:13:59 +1000366 return status;
367}
Andrew Lunnba59a802012-07-23 13:16:55 +0200368
Ken Wilson75872eb2015-01-12 13:13:59 +1000369static int orion_spi_setup(struct spi_device *spi)
370{
371 return orion_spi_setup_transfer(spi, NULL);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700372}
373
Grant Likely2deff8d2013-02-05 13:27:35 +0000374static int orion_spi_reset(struct orion_spi *orion_spi)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700375{
376 /* Verify that the CS is deasserted */
Ken Wilson75872eb2015-01-12 13:13:59 +1000377 orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700378 return 0;
379}
380
Greg Ungererdf59fa72014-09-28 23:24:04 +1000381static const struct orion_spi_dev orion_spi_dev_data = {
382 .typ = ORION_SPI,
383 .min_divisor = 4,
384 .max_divisor = 30,
385 .prescale_mask = ORION_SPI_CLK_PRESCALE_MASK,
386};
387
388static const struct orion_spi_dev armada_spi_dev_data = {
389 .typ = ARMADA_SPI,
390 .min_divisor = 1,
391 .max_divisor = 1920,
392 .prescale_mask = ARMADA_SPI_CLK_PRESCALE_MASK,
393};
394
395static const struct of_device_id orion_spi_of_match_table[] = {
396 { .compatible = "marvell,orion-spi", .data = &orion_spi_dev_data, },
397 { .compatible = "marvell,armada-370-spi", .data = &armada_spi_dev_data, },
398 {}
399};
400MODULE_DEVICE_TABLE(of, orion_spi_of_match_table);
401
Grant Likely2deff8d2013-02-05 13:27:35 +0000402static int orion_spi_probe(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700403{
Greg Ungererdf59fa72014-09-28 23:24:04 +1000404 const struct of_device_id *of_id;
405 const struct orion_spi_dev *devdata;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700406 struct spi_master *master;
407 struct orion_spi *spi;
408 struct resource *r;
Andrew Lunn4574b882012-04-06 17:17:26 +0200409 unsigned long tclk_hz;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700410 int status = 0;
411
Jingoo Han3fed8062013-10-14 10:35:08 +0900412 master = spi_alloc_master(&pdev->dev, sizeof(*spi));
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700413 if (master == NULL) {
414 dev_dbg(&pdev->dev, "master allocation failed\n");
415 return -ENOMEM;
416 }
417
418 if (pdev->id != -1)
419 master->bus_num = pdev->id;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200420 if (pdev->dev.of_node) {
Thomas Petazzonie06871c2014-07-27 23:53:19 +0200421 u32 cell_index;
Jingoo Hanb8434042014-09-02 11:51:39 +0900422
Thomas Petazzonie06871c2014-07-27 23:53:19 +0200423 if (!of_property_read_u32(pdev->dev.of_node, "cell-index",
424 &cell_index))
425 master->bus_num = cell_index;
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200426 }
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700427
David Brownelle7db06b2009-06-17 16:26:04 -0700428 /* we support only mode 0, and no options */
Jason Gunthorpeb15d5d72012-11-21 12:23:35 -0700429 master->mode_bits = SPI_CPHA | SPI_CPOL;
Ken Wilson75872eb2015-01-12 13:13:59 +1000430 master->set_cs = orion_spi_set_cs;
431 master->transfer_one = orion_spi_transfer_one;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700432 master->num_chipselect = ORION_NUM_CHIPSELECTS;
Ken Wilson75872eb2015-01-12 13:13:59 +1000433 master->setup = orion_spi_setup;
Axel Lin495b3352014-02-11 20:51:36 +0800434 master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
Russell King5c678692014-06-21 12:22:37 +0100435 master->auto_runtime_pm = true;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700436
Jingoo Han24b5a822013-05-23 19:20:40 +0900437 platform_set_drvdata(pdev, master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700438
439 spi = spi_master_get_devdata(master);
440 spi->master = master;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700441
Greg Ungererdf59fa72014-09-28 23:24:04 +1000442 of_id = of_match_device(orion_spi_of_match_table, &pdev->dev);
Greg Ungerer9a2d3632014-10-21 15:57:48 +1000443 devdata = (of_id) ? of_id->data : &orion_spi_dev_data;
Greg Ungererdf59fa72014-09-28 23:24:04 +1000444 spi->devdata = devdata;
445
Jingoo Hanbb489842013-12-09 19:21:22 +0900446 spi->clk = devm_clk_get(&pdev->dev, NULL);
Andrew Lunn4574b882012-04-06 17:17:26 +0200447 if (IS_ERR(spi->clk)) {
448 status = PTR_ERR(spi->clk);
449 goto out;
450 }
451
Russell Kingc85012a2014-06-21 11:32:23 +0100452 status = clk_prepare_enable(spi->clk);
453 if (status)
454 goto out;
455
Andrew Lunn4574b882012-04-06 17:17:26 +0200456 tclk_hz = clk_get_rate(spi->clk);
Greg Ungererdf59fa72014-09-28 23:24:04 +1000457 master->max_speed_hz = DIV_ROUND_UP(tclk_hz, devdata->min_divisor);
458 master->min_speed_hz = DIV_ROUND_UP(tclk_hz, devdata->max_divisor);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700459
460 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Mark Brown1729ce32013-07-28 14:38:06 +0100461 spi->base = devm_ioremap_resource(&pdev->dev, r);
462 if (IS_ERR(spi->base)) {
463 status = PTR_ERR(spi->base);
Andrew Lunn4574b882012-04-06 17:17:26 +0200464 goto out_rel_clk;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700465 }
466
Russell King5c678692014-06-21 12:22:37 +0100467 pm_runtime_set_active(&pdev->dev);
468 pm_runtime_use_autosuspend(&pdev->dev);
469 pm_runtime_set_autosuspend_delay(&pdev->dev, SPI_AUTOSUSPEND_TIMEOUT);
470 pm_runtime_enable(&pdev->dev);
471
Wei Yongjun14033812014-07-20 22:03:14 +0800472 status = orion_spi_reset(spi);
473 if (status < 0)
Russell King5c678692014-06-21 12:22:37 +0100474 goto out_rel_pm;
475
476 pm_runtime_mark_last_busy(&pdev->dev);
477 pm_runtime_put_autosuspend(&pdev->dev);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700478
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200479 master->dev.of_node = pdev->dev.of_node;
Russell King5c678692014-06-21 12:22:37 +0100480 status = spi_register_master(master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700481 if (status < 0)
Russell King5c678692014-06-21 12:22:37 +0100482 goto out_rel_pm;
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700483
484 return status;
485
Russell King5c678692014-06-21 12:22:37 +0100486out_rel_pm:
487 pm_runtime_disable(&pdev->dev);
Andrew Lunn4574b882012-04-06 17:17:26 +0200488out_rel_clk:
489 clk_disable_unprepare(spi->clk);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700490out:
491 spi_master_put(master);
492 return status;
493}
494
495
Grant Likely2deff8d2013-02-05 13:27:35 +0000496static int orion_spi_remove(struct platform_device *pdev)
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700497{
Russell King5c678692014-06-21 12:22:37 +0100498 struct spi_master *master = platform_get_drvdata(pdev);
499 struct orion_spi *spi = spi_master_get_devdata(master);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700500
Russell King5c678692014-06-21 12:22:37 +0100501 pm_runtime_get_sync(&pdev->dev);
Andrew Lunn4574b882012-04-06 17:17:26 +0200502 clk_disable_unprepare(spi->clk);
Andrew Lunn4574b882012-04-06 17:17:26 +0200503
Russell King5c678692014-06-21 12:22:37 +0100504 spi_unregister_master(master);
505 pm_runtime_disable(&pdev->dev);
506
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700507 return 0;
508}
509
510MODULE_ALIAS("platform:" DRIVER_NAME);
511
Rafael J. Wysockiec833052014-12-13 00:41:15 +0100512#ifdef CONFIG_PM
Russell King5c678692014-06-21 12:22:37 +0100513static int orion_spi_runtime_suspend(struct device *dev)
514{
515 struct spi_master *master = dev_get_drvdata(dev);
516 struct orion_spi *spi = spi_master_get_devdata(master);
517
518 clk_disable_unprepare(spi->clk);
519 return 0;
520}
521
522static int orion_spi_runtime_resume(struct device *dev)
523{
524 struct spi_master *master = dev_get_drvdata(dev);
525 struct orion_spi *spi = spi_master_get_devdata(master);
526
527 return clk_prepare_enable(spi->clk);
528}
529#endif
530
531static const struct dev_pm_ops orion_spi_pm_ops = {
532 SET_RUNTIME_PM_OPS(orion_spi_runtime_suspend,
533 orion_spi_runtime_resume,
534 NULL)
535};
536
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700537static struct platform_driver orion_spi_driver = {
538 .driver = {
539 .name = DRIVER_NAME,
Russell King5c678692014-06-21 12:22:37 +0100540 .pm = &orion_spi_pm_ops,
Andrew Lunnf814f9a2012-07-23 12:08:09 +0200541 .of_match_table = of_match_ptr(orion_spi_of_match_table),
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700542 },
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300543 .probe = orion_spi_probe,
Grant Likely2deff8d2013-02-05 13:27:35 +0000544 .remove = orion_spi_remove,
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700545};
546
Ezequiel Garcia41ab7242013-02-04 09:26:26 -0300547module_platform_driver(orion_spi_driver);
Shadi Ammouri60cadec2008-08-05 13:01:09 -0700548
549MODULE_DESCRIPTION("Orion SPI driver");
550MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>");
551MODULE_LICENSE("GPL");