Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 1 | /* |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 2 | * Marvell Orion SPI controller driver |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 3 | * |
| 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 | |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/io.h> |
| 18 | #include <linux/spi/spi.h> |
Paul Gortmaker | d7614de | 2011-07-03 15:44:29 -0400 | [diff] [blame] | 19 | #include <linux/module.h> |
Andrew Lunn | f814f9a | 2012-07-23 12:08:09 +0200 | [diff] [blame] | 20 | #include <linux/of.h> |
Andrew Lunn | 4574b88 | 2012-04-06 17:17:26 +0200 | [diff] [blame] | 21 | #include <linux/clk.h> |
Mark Brown | 895248f | 2013-07-29 05:10:21 +0100 | [diff] [blame] | 22 | #include <linux/sizes.h> |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 23 | #include <asm/unaligned.h> |
| 24 | |
| 25 | #define DRIVER_NAME "orion_spi" |
| 26 | |
| 27 | #define ORION_NUM_CHIPSELECTS 1 /* only one slave is supported*/ |
| 28 | #define ORION_SPI_WAIT_RDY_MAX_LOOP 2000 /* in usec */ |
| 29 | |
| 30 | #define ORION_SPI_IF_CTRL_REG 0x00 |
| 31 | #define ORION_SPI_IF_CONFIG_REG 0x04 |
| 32 | #define ORION_SPI_DATA_OUT_REG 0x08 |
| 33 | #define ORION_SPI_DATA_IN_REG 0x0c |
| 34 | #define ORION_SPI_INT_CAUSE_REG 0x10 |
| 35 | |
Jason Gunthorpe | b15d5d7 | 2012-11-21 12:23:35 -0700 | [diff] [blame] | 36 | #define ORION_SPI_MODE_CPOL (1 << 11) |
| 37 | #define ORION_SPI_MODE_CPHA (1 << 12) |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 38 | #define ORION_SPI_IF_8_16_BIT_MODE (1 << 5) |
| 39 | #define ORION_SPI_CLK_PRESCALE_MASK 0x1F |
Jason Gunthorpe | b15d5d7 | 2012-11-21 12:23:35 -0700 | [diff] [blame] | 40 | #define ORION_SPI_MODE_MASK (ORION_SPI_MODE_CPOL | \ |
| 41 | ORION_SPI_MODE_CPHA) |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 42 | |
| 43 | struct orion_spi { |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 44 | struct spi_master *master; |
| 45 | void __iomem *base; |
Andrew Lunn | 4574b88 | 2012-04-06 17:17:26 +0200 | [diff] [blame] | 46 | struct clk *clk; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 49 | static inline void __iomem *spi_reg(struct orion_spi *orion_spi, u32 reg) |
| 50 | { |
| 51 | return orion_spi->base + reg; |
| 52 | } |
| 53 | |
| 54 | static inline void |
| 55 | orion_spi_setbits(struct orion_spi *orion_spi, u32 reg, u32 mask) |
| 56 | { |
| 57 | void __iomem *reg_addr = spi_reg(orion_spi, reg); |
| 58 | u32 val; |
| 59 | |
| 60 | val = readl(reg_addr); |
| 61 | val |= mask; |
| 62 | writel(val, reg_addr); |
| 63 | } |
| 64 | |
| 65 | static inline void |
| 66 | orion_spi_clrbits(struct orion_spi *orion_spi, u32 reg, u32 mask) |
| 67 | { |
| 68 | void __iomem *reg_addr = spi_reg(orion_spi, reg); |
| 69 | u32 val; |
| 70 | |
| 71 | val = readl(reg_addr); |
| 72 | val &= ~mask; |
| 73 | writel(val, reg_addr); |
| 74 | } |
| 75 | |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 76 | static int orion_spi_baudrate_set(struct spi_device *spi, unsigned int speed) |
| 77 | { |
| 78 | u32 tclk_hz; |
| 79 | u32 rate; |
| 80 | u32 prescale; |
| 81 | u32 reg; |
| 82 | struct orion_spi *orion_spi; |
| 83 | |
| 84 | orion_spi = spi_master_get_devdata(spi->master); |
| 85 | |
Andrew Lunn | 4574b88 | 2012-04-06 17:17:26 +0200 | [diff] [blame] | 86 | tclk_hz = clk_get_rate(orion_spi->clk); |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * the supported rates are: 4,6,8...30 |
| 90 | * round up as we look for equal or less speed |
| 91 | */ |
| 92 | rate = DIV_ROUND_UP(tclk_hz, speed); |
| 93 | rate = roundup(rate, 2); |
| 94 | |
| 95 | /* check if requested speed is too small */ |
| 96 | if (rate > 30) |
| 97 | return -EINVAL; |
| 98 | |
| 99 | if (rate < 4) |
| 100 | rate = 4; |
| 101 | |
| 102 | /* Convert the rate to SPI clock divisor value. */ |
| 103 | prescale = 0x10 + rate/2; |
| 104 | |
| 105 | reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG)); |
| 106 | reg = ((reg & ~ORION_SPI_CLK_PRESCALE_MASK) | prescale); |
| 107 | writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG)); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Jason Gunthorpe | b15d5d7 | 2012-11-21 12:23:35 -0700 | [diff] [blame] | 112 | static void |
| 113 | orion_spi_mode_set(struct spi_device *spi) |
| 114 | { |
| 115 | u32 reg; |
| 116 | struct orion_spi *orion_spi; |
| 117 | |
| 118 | orion_spi = spi_master_get_devdata(spi->master); |
| 119 | |
| 120 | reg = readl(spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG)); |
| 121 | reg &= ~ORION_SPI_MODE_MASK; |
| 122 | if (spi->mode & SPI_CPOL) |
| 123 | reg |= ORION_SPI_MODE_CPOL; |
| 124 | if (spi->mode & SPI_CPHA) |
| 125 | reg |= ORION_SPI_MODE_CPHA; |
| 126 | writel(reg, spi_reg(orion_spi, ORION_SPI_IF_CONFIG_REG)); |
| 127 | } |
| 128 | |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 129 | /* |
| 130 | * called only when no transfer is active on the bus |
| 131 | */ |
| 132 | static int |
| 133 | orion_spi_setup_transfer(struct spi_device *spi, struct spi_transfer *t) |
| 134 | { |
| 135 | struct orion_spi *orion_spi; |
| 136 | unsigned int speed = spi->max_speed_hz; |
| 137 | unsigned int bits_per_word = spi->bits_per_word; |
| 138 | int rc; |
| 139 | |
| 140 | orion_spi = spi_master_get_devdata(spi->master); |
| 141 | |
| 142 | if ((t != NULL) && t->speed_hz) |
| 143 | speed = t->speed_hz; |
| 144 | |
| 145 | if ((t != NULL) && t->bits_per_word) |
| 146 | bits_per_word = t->bits_per_word; |
| 147 | |
Jason Gunthorpe | b15d5d7 | 2012-11-21 12:23:35 -0700 | [diff] [blame] | 148 | orion_spi_mode_set(spi); |
| 149 | |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 150 | rc = orion_spi_baudrate_set(spi, speed); |
| 151 | if (rc) |
| 152 | return rc; |
| 153 | |
Axel Lin | 495b335 | 2014-02-11 20:51:36 +0800 | [diff] [blame^] | 154 | if (bits_per_word == 16) |
| 155 | orion_spi_setbits(orion_spi, ORION_SPI_IF_CONFIG_REG, |
| 156 | ORION_SPI_IF_8_16_BIT_MODE); |
| 157 | else |
| 158 | orion_spi_clrbits(orion_spi, ORION_SPI_IF_CONFIG_REG, |
| 159 | ORION_SPI_IF_8_16_BIT_MODE); |
| 160 | |
| 161 | return 0; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static void orion_spi_set_cs(struct orion_spi *orion_spi, int enable) |
| 165 | { |
| 166 | if (enable) |
| 167 | orion_spi_setbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1); |
| 168 | else |
| 169 | orion_spi_clrbits(orion_spi, ORION_SPI_IF_CTRL_REG, 0x1); |
| 170 | } |
| 171 | |
| 172 | static inline int orion_spi_wait_till_ready(struct orion_spi *orion_spi) |
| 173 | { |
| 174 | int i; |
| 175 | |
| 176 | for (i = 0; i < ORION_SPI_WAIT_RDY_MAX_LOOP; i++) { |
| 177 | if (readl(spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG))) |
| 178 | return 1; |
| 179 | else |
| 180 | udelay(1); |
| 181 | } |
| 182 | |
| 183 | return -1; |
| 184 | } |
| 185 | |
| 186 | static inline int |
| 187 | orion_spi_write_read_8bit(struct spi_device *spi, |
| 188 | const u8 **tx_buf, u8 **rx_buf) |
| 189 | { |
| 190 | void __iomem *tx_reg, *rx_reg, *int_reg; |
| 191 | struct orion_spi *orion_spi; |
| 192 | |
| 193 | orion_spi = spi_master_get_devdata(spi->master); |
| 194 | tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG); |
| 195 | rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG); |
| 196 | int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG); |
| 197 | |
| 198 | /* clear the interrupt cause register */ |
| 199 | writel(0x0, int_reg); |
| 200 | |
| 201 | if (tx_buf && *tx_buf) |
| 202 | writel(*(*tx_buf)++, tx_reg); |
| 203 | else |
| 204 | writel(0, tx_reg); |
| 205 | |
| 206 | if (orion_spi_wait_till_ready(orion_spi) < 0) { |
| 207 | dev_err(&spi->dev, "TXS timed out\n"); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | if (rx_buf && *rx_buf) |
| 212 | *(*rx_buf)++ = readl(rx_reg); |
| 213 | |
| 214 | return 1; |
| 215 | } |
| 216 | |
| 217 | static inline int |
| 218 | orion_spi_write_read_16bit(struct spi_device *spi, |
| 219 | const u16 **tx_buf, u16 **rx_buf) |
| 220 | { |
| 221 | void __iomem *tx_reg, *rx_reg, *int_reg; |
| 222 | struct orion_spi *orion_spi; |
| 223 | |
| 224 | orion_spi = spi_master_get_devdata(spi->master); |
| 225 | tx_reg = spi_reg(orion_spi, ORION_SPI_DATA_OUT_REG); |
| 226 | rx_reg = spi_reg(orion_spi, ORION_SPI_DATA_IN_REG); |
| 227 | int_reg = spi_reg(orion_spi, ORION_SPI_INT_CAUSE_REG); |
| 228 | |
| 229 | /* clear the interrupt cause register */ |
| 230 | writel(0x0, int_reg); |
| 231 | |
| 232 | if (tx_buf && *tx_buf) |
| 233 | writel(__cpu_to_le16(get_unaligned((*tx_buf)++)), tx_reg); |
| 234 | else |
| 235 | writel(0, tx_reg); |
| 236 | |
| 237 | if (orion_spi_wait_till_ready(orion_spi) < 0) { |
| 238 | dev_err(&spi->dev, "TXS timed out\n"); |
| 239 | return -1; |
| 240 | } |
| 241 | |
| 242 | if (rx_buf && *rx_buf) |
| 243 | put_unaligned(__le16_to_cpu(readl(rx_reg)), (*rx_buf)++); |
| 244 | |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | static unsigned int |
| 249 | orion_spi_write_read(struct spi_device *spi, struct spi_transfer *xfer) |
| 250 | { |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 251 | unsigned int count; |
| 252 | int word_len; |
| 253 | |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 254 | word_len = spi->bits_per_word; |
| 255 | count = xfer->len; |
| 256 | |
| 257 | if (word_len == 8) { |
| 258 | const u8 *tx = xfer->tx_buf; |
| 259 | u8 *rx = xfer->rx_buf; |
| 260 | |
| 261 | do { |
| 262 | if (orion_spi_write_read_8bit(spi, &tx, &rx) < 0) |
| 263 | goto out; |
| 264 | count--; |
| 265 | } while (count); |
| 266 | } else if (word_len == 16) { |
| 267 | const u16 *tx = xfer->tx_buf; |
| 268 | u16 *rx = xfer->rx_buf; |
| 269 | |
| 270 | do { |
| 271 | if (orion_spi_write_read_16bit(spi, &tx, &rx) < 0) |
| 272 | goto out; |
| 273 | count -= 2; |
| 274 | } while (count); |
| 275 | } |
| 276 | |
| 277 | out: |
| 278 | return xfer->len - count; |
| 279 | } |
| 280 | |
| 281 | |
Andrew Lunn | ba59a80 | 2012-07-23 13:16:55 +0200 | [diff] [blame] | 282 | static int orion_spi_transfer_one_message(struct spi_master *master, |
| 283 | struct spi_message *m) |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 284 | { |
Andrew Lunn | ba59a80 | 2012-07-23 13:16:55 +0200 | [diff] [blame] | 285 | struct orion_spi *orion_spi = spi_master_get_devdata(master); |
| 286 | struct spi_device *spi = m->spi; |
| 287 | struct spi_transfer *t = NULL; |
| 288 | int par_override = 0; |
| 289 | int status = 0; |
| 290 | int cs_active = 0; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 291 | |
Andrew Lunn | ba59a80 | 2012-07-23 13:16:55 +0200 | [diff] [blame] | 292 | /* Load defaults */ |
| 293 | status = orion_spi_setup_transfer(spi, NULL); |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 294 | |
Andrew Lunn | ba59a80 | 2012-07-23 13:16:55 +0200 | [diff] [blame] | 295 | if (status < 0) |
| 296 | goto msg_done; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 297 | |
Andrew Lunn | ba59a80 | 2012-07-23 13:16:55 +0200 | [diff] [blame] | 298 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 299 | /* make sure buffer length is even when working in 16 |
| 300 | * bit mode*/ |
| 301 | if ((t->bits_per_word == 16) && (t->len & 1)) { |
| 302 | dev_err(&spi->dev, |
| 303 | "message rejected : " |
| 304 | "odd data length %d while in 16 bit mode\n", |
| 305 | t->len); |
| 306 | status = -EIO; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 307 | goto msg_done; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 308 | } |
| 309 | |
Andrew Lunn | ba59a80 | 2012-07-23 13:16:55 +0200 | [diff] [blame] | 310 | if (par_override || t->speed_hz || t->bits_per_word) { |
| 311 | par_override = 1; |
| 312 | status = orion_spi_setup_transfer(spi, t); |
| 313 | if (status < 0) |
| 314 | break; |
| 315 | if (!t->speed_hz && !t->bits_per_word) |
| 316 | par_override = 0; |
| 317 | } |
| 318 | |
| 319 | if (!cs_active) { |
| 320 | orion_spi_set_cs(orion_spi, 1); |
| 321 | cs_active = 1; |
| 322 | } |
| 323 | |
| 324 | if (t->len) |
| 325 | m->actual_length += orion_spi_write_read(spi, t); |
| 326 | |
| 327 | if (t->delay_usecs) |
| 328 | udelay(t->delay_usecs); |
| 329 | |
| 330 | if (t->cs_change) { |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 331 | orion_spi_set_cs(orion_spi, 0); |
Andrew Lunn | ba59a80 | 2012-07-23 13:16:55 +0200 | [diff] [blame] | 332 | cs_active = 0; |
| 333 | } |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Andrew Lunn | ba59a80 | 2012-07-23 13:16:55 +0200 | [diff] [blame] | 336 | msg_done: |
| 337 | if (cs_active) |
| 338 | orion_spi_set_cs(orion_spi, 0); |
| 339 | |
| 340 | m->status = status; |
| 341 | spi_finalize_current_message(master); |
| 342 | |
| 343 | return 0; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Grant Likely | 2deff8d | 2013-02-05 13:27:35 +0000 | [diff] [blame] | 346 | static int orion_spi_reset(struct orion_spi *orion_spi) |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 347 | { |
| 348 | /* Verify that the CS is deasserted */ |
| 349 | orion_spi_set_cs(orion_spi, 0); |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Grant Likely | 2deff8d | 2013-02-05 13:27:35 +0000 | [diff] [blame] | 354 | static int orion_spi_probe(struct platform_device *pdev) |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 355 | { |
| 356 | struct spi_master *master; |
| 357 | struct orion_spi *spi; |
| 358 | struct resource *r; |
Andrew Lunn | 4574b88 | 2012-04-06 17:17:26 +0200 | [diff] [blame] | 359 | unsigned long tclk_hz; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 360 | int status = 0; |
Andrew Lunn | f814f9a | 2012-07-23 12:08:09 +0200 | [diff] [blame] | 361 | const u32 *iprop; |
| 362 | int size; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 363 | |
Jingoo Han | 3fed806 | 2013-10-14 10:35:08 +0900 | [diff] [blame] | 364 | master = spi_alloc_master(&pdev->dev, sizeof(*spi)); |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 365 | if (master == NULL) { |
| 366 | dev_dbg(&pdev->dev, "master allocation failed\n"); |
| 367 | return -ENOMEM; |
| 368 | } |
| 369 | |
| 370 | if (pdev->id != -1) |
| 371 | master->bus_num = pdev->id; |
Andrew Lunn | f814f9a | 2012-07-23 12:08:09 +0200 | [diff] [blame] | 372 | if (pdev->dev.of_node) { |
| 373 | iprop = of_get_property(pdev->dev.of_node, "cell-index", |
| 374 | &size); |
| 375 | if (iprop && size == sizeof(*iprop)) |
| 376 | master->bus_num = *iprop; |
| 377 | } |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 378 | |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 379 | /* we support only mode 0, and no options */ |
Jason Gunthorpe | b15d5d7 | 2012-11-21 12:23:35 -0700 | [diff] [blame] | 380 | master->mode_bits = SPI_CPHA | SPI_CPOL; |
David Brownell | e7db06b | 2009-06-17 16:26:04 -0700 | [diff] [blame] | 381 | |
Andrew Lunn | ba59a80 | 2012-07-23 13:16:55 +0200 | [diff] [blame] | 382 | master->transfer_one_message = orion_spi_transfer_one_message; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 383 | master->num_chipselect = ORION_NUM_CHIPSELECTS; |
Axel Lin | 495b335 | 2014-02-11 20:51:36 +0800 | [diff] [blame^] | 384 | master->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16); |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 385 | |
Jingoo Han | 24b5a82 | 2013-05-23 19:20:40 +0900 | [diff] [blame] | 386 | platform_set_drvdata(pdev, master); |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 387 | |
| 388 | spi = spi_master_get_devdata(master); |
| 389 | spi->master = master; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 390 | |
Jingoo Han | bb48984 | 2013-12-09 19:21:22 +0900 | [diff] [blame] | 391 | spi->clk = devm_clk_get(&pdev->dev, NULL); |
Andrew Lunn | 4574b88 | 2012-04-06 17:17:26 +0200 | [diff] [blame] | 392 | if (IS_ERR(spi->clk)) { |
| 393 | status = PTR_ERR(spi->clk); |
| 394 | goto out; |
| 395 | } |
| 396 | |
| 397 | clk_prepare(spi->clk); |
| 398 | clk_enable(spi->clk); |
| 399 | tclk_hz = clk_get_rate(spi->clk); |
Axel Lin | b52a37f | 2014-02-04 11:05:50 +0800 | [diff] [blame] | 400 | master->max_speed_hz = DIV_ROUND_UP(tclk_hz, 4); |
| 401 | master->min_speed_hz = DIV_ROUND_UP(tclk_hz, 30); |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 402 | |
| 403 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Mark Brown | 1729ce3 | 2013-07-28 14:38:06 +0100 | [diff] [blame] | 404 | spi->base = devm_ioremap_resource(&pdev->dev, r); |
| 405 | if (IS_ERR(spi->base)) { |
| 406 | status = PTR_ERR(spi->base); |
Andrew Lunn | 4574b88 | 2012-04-06 17:17:26 +0200 | [diff] [blame] | 407 | goto out_rel_clk; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 410 | if (orion_spi_reset(spi) < 0) |
Mark Brown | 1729ce3 | 2013-07-28 14:38:06 +0100 | [diff] [blame] | 411 | goto out_rel_clk; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 412 | |
Andrew Lunn | f814f9a | 2012-07-23 12:08:09 +0200 | [diff] [blame] | 413 | master->dev.of_node = pdev->dev.of_node; |
Jingoo Han | 4bd3d8e | 2013-09-24 13:43:09 +0900 | [diff] [blame] | 414 | status = devm_spi_register_master(&pdev->dev, master); |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 415 | if (status < 0) |
Mark Brown | 1729ce3 | 2013-07-28 14:38:06 +0100 | [diff] [blame] | 416 | goto out_rel_clk; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 417 | |
| 418 | return status; |
| 419 | |
Andrew Lunn | 4574b88 | 2012-04-06 17:17:26 +0200 | [diff] [blame] | 420 | out_rel_clk: |
| 421 | clk_disable_unprepare(spi->clk); |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 422 | out: |
| 423 | spi_master_put(master); |
| 424 | return status; |
| 425 | } |
| 426 | |
| 427 | |
Grant Likely | 2deff8d | 2013-02-05 13:27:35 +0000 | [diff] [blame] | 428 | static int orion_spi_remove(struct platform_device *pdev) |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 429 | { |
| 430 | struct spi_master *master; |
Andrew Lunn | ba59a80 | 2012-07-23 13:16:55 +0200 | [diff] [blame] | 431 | struct orion_spi *spi; |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 432 | |
Jingoo Han | 24b5a82 | 2013-05-23 19:20:40 +0900 | [diff] [blame] | 433 | master = platform_get_drvdata(pdev); |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 434 | spi = spi_master_get_devdata(master); |
| 435 | |
Andrew Lunn | 4574b88 | 2012-04-06 17:17:26 +0200 | [diff] [blame] | 436 | clk_disable_unprepare(spi->clk); |
Andrew Lunn | 4574b88 | 2012-04-06 17:17:26 +0200 | [diff] [blame] | 437 | |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | MODULE_ALIAS("platform:" DRIVER_NAME); |
| 442 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 443 | static const struct of_device_id orion_spi_of_match_table[] = { |
Andrew Lunn | f814f9a | 2012-07-23 12:08:09 +0200 | [diff] [blame] | 444 | { .compatible = "marvell,orion-spi", }, |
| 445 | {} |
| 446 | }; |
| 447 | MODULE_DEVICE_TABLE(of, orion_spi_of_match_table); |
| 448 | |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 449 | static struct platform_driver orion_spi_driver = { |
| 450 | .driver = { |
| 451 | .name = DRIVER_NAME, |
| 452 | .owner = THIS_MODULE, |
Andrew Lunn | f814f9a | 2012-07-23 12:08:09 +0200 | [diff] [blame] | 453 | .of_match_table = of_match_ptr(orion_spi_of_match_table), |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 454 | }, |
Ezequiel Garcia | 41ab724 | 2013-02-04 09:26:26 -0300 | [diff] [blame] | 455 | .probe = orion_spi_probe, |
Grant Likely | 2deff8d | 2013-02-05 13:27:35 +0000 | [diff] [blame] | 456 | .remove = orion_spi_remove, |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 457 | }; |
| 458 | |
Ezequiel Garcia | 41ab724 | 2013-02-04 09:26:26 -0300 | [diff] [blame] | 459 | module_platform_driver(orion_spi_driver); |
Shadi Ammouri | 60cadec | 2008-08-05 13:01:09 -0700 | [diff] [blame] | 460 | |
| 461 | MODULE_DESCRIPTION("Orion SPI driver"); |
| 462 | MODULE_AUTHOR("Shadi Ammouri <shadi@marvell.com>"); |
| 463 | MODULE_LICENSE("GPL"); |