Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Texas Instruments. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | |
| 19 | #include <linux/interrupt.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/gpio.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/platform_device.h> |
| 25 | #include <linux/err.h> |
| 26 | #include <linux/clk.h> |
| 27 | #include <linux/dma-mapping.h> |
| 28 | #include <linux/spi/spi.h> |
| 29 | #include <linux/spi/spi_bitbang.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/slab.h> |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 31 | |
| 32 | #include <mach/spi.h> |
| 33 | #include <mach/edma.h> |
| 34 | |
| 35 | #define SPI_NO_RESOURCE ((resource_size_t)-1) |
| 36 | |
| 37 | #define SPI_MAX_CHIPSELECT 2 |
| 38 | |
| 39 | #define CS_DEFAULT 0xFF |
| 40 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 41 | #define SPIFMT_PHASE_MASK BIT(16) |
| 42 | #define SPIFMT_POLARITY_MASK BIT(17) |
| 43 | #define SPIFMT_DISTIMER_MASK BIT(18) |
| 44 | #define SPIFMT_SHIFTDIR_MASK BIT(20) |
| 45 | #define SPIFMT_WAITENA_MASK BIT(21) |
| 46 | #define SPIFMT_PARITYENA_MASK BIT(22) |
| 47 | #define SPIFMT_ODD_PARITY_MASK BIT(23) |
| 48 | #define SPIFMT_WDELAY_MASK 0x3f000000u |
| 49 | #define SPIFMT_WDELAY_SHIFT 24 |
Brian Niebuhr | 7fe0092 | 2010-08-13 13:27:23 +0530 | [diff] [blame] | 50 | #define SPIFMT_PRESCALE_SHIFT 8 |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 51 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 52 | |
| 53 | /* SPIPC0 */ |
| 54 | #define SPIPC0_DIFUN_MASK BIT(11) /* MISO */ |
| 55 | #define SPIPC0_DOFUN_MASK BIT(10) /* MOSI */ |
| 56 | #define SPIPC0_CLKFUN_MASK BIT(9) /* CLK */ |
| 57 | #define SPIPC0_SPIENA_MASK BIT(8) /* nREADY */ |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 58 | |
| 59 | #define SPIINT_MASKALL 0x0101035F |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 60 | #define SPIINT_MASKINT 0x0000015F |
| 61 | #define SPI_INTLVL_1 0x000001FF |
| 62 | #define SPI_INTLVL_0 0x00000000 |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 63 | |
Brian Niebuhr | cfbc5d1 | 2010-08-12 12:27:33 +0530 | [diff] [blame] | 64 | /* SPIDAT1 (upper 16 bit defines) */ |
| 65 | #define SPIDAT1_CSHOLD_MASK BIT(12) |
| 66 | |
| 67 | /* SPIGCR1 */ |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 68 | #define SPIGCR1_CLKMOD_MASK BIT(1) |
| 69 | #define SPIGCR1_MASTER_MASK BIT(0) |
| 70 | #define SPIGCR1_LOOPBACK_MASK BIT(16) |
Sekhar Nori | 8e206f1 | 2010-08-20 16:20:49 +0530 | [diff] [blame] | 71 | #define SPIGCR1_SPIENA_MASK BIT(24) |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 72 | |
| 73 | /* SPIBUF */ |
| 74 | #define SPIBUF_TXFULL_MASK BIT(29) |
| 75 | #define SPIBUF_RXEMPTY_MASK BIT(31) |
| 76 | |
Brian Niebuhr | 7abbf23 | 2010-08-19 15:07:38 +0530 | [diff] [blame] | 77 | /* SPIDELAY */ |
| 78 | #define SPIDELAY_C2TDELAY_SHIFT 24 |
| 79 | #define SPIDELAY_C2TDELAY_MASK (0xFF << SPIDELAY_C2TDELAY_SHIFT) |
| 80 | #define SPIDELAY_T2CDELAY_SHIFT 16 |
| 81 | #define SPIDELAY_T2CDELAY_MASK (0xFF << SPIDELAY_T2CDELAY_SHIFT) |
| 82 | #define SPIDELAY_T2EDELAY_SHIFT 8 |
| 83 | #define SPIDELAY_T2EDELAY_MASK (0xFF << SPIDELAY_T2EDELAY_SHIFT) |
| 84 | #define SPIDELAY_C2EDELAY_SHIFT 0 |
| 85 | #define SPIDELAY_C2EDELAY_MASK 0xFF |
| 86 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 87 | /* Error Masks */ |
| 88 | #define SPIFLG_DLEN_ERR_MASK BIT(0) |
| 89 | #define SPIFLG_TIMEOUT_MASK BIT(1) |
| 90 | #define SPIFLG_PARERR_MASK BIT(2) |
| 91 | #define SPIFLG_DESYNC_MASK BIT(3) |
| 92 | #define SPIFLG_BITERR_MASK BIT(4) |
| 93 | #define SPIFLG_OVRRUN_MASK BIT(6) |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 94 | #define SPIFLG_BUF_INIT_ACTIVE_MASK BIT(24) |
Brian Niebuhr | 839c996 | 2010-08-23 16:39:19 +0530 | [diff] [blame] | 95 | #define SPIFLG_ERROR_MASK (SPIFLG_DLEN_ERR_MASK \ |
| 96 | | SPIFLG_TIMEOUT_MASK | SPIFLG_PARERR_MASK \ |
| 97 | | SPIFLG_DESYNC_MASK | SPIFLG_BITERR_MASK \ |
| 98 | | SPIFLG_OVRRUN_MASK) |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 99 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 100 | #define SPIINT_DMA_REQ_EN BIT(16) |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 101 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 102 | /* SPI Controller registers */ |
| 103 | #define SPIGCR0 0x00 |
| 104 | #define SPIGCR1 0x04 |
| 105 | #define SPIINT 0x08 |
| 106 | #define SPILVL 0x0c |
| 107 | #define SPIFLG 0x10 |
| 108 | #define SPIPC0 0x14 |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 109 | #define SPIDAT1 0x3c |
| 110 | #define SPIBUF 0x40 |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 111 | #define SPIDELAY 0x48 |
| 112 | #define SPIDEF 0x4c |
| 113 | #define SPIFMT0 0x50 |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 114 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 115 | /* We have 2 DMA channels per CS, one for RX and one for TX */ |
| 116 | struct davinci_spi_dma { |
| 117 | int dma_tx_channel; |
| 118 | int dma_rx_channel; |
Brian Niebuhr | 523c37e | 2010-10-04 17:35:34 +0530 | [diff] [blame^] | 119 | int dummy_param_slot; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 120 | enum dma_event_q eventq; |
| 121 | |
| 122 | struct completion dma_tx_completion; |
| 123 | struct completion dma_rx_completion; |
| 124 | }; |
| 125 | |
| 126 | /* SPI Controller driver's private data. */ |
| 127 | struct davinci_spi { |
| 128 | struct spi_bitbang bitbang; |
| 129 | struct clk *clk; |
| 130 | |
| 131 | u8 version; |
| 132 | resource_size_t pbase; |
| 133 | void __iomem *base; |
| 134 | size_t region_size; |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 135 | u32 irq; |
| 136 | struct completion done; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 137 | |
| 138 | const void *tx; |
| 139 | void *rx; |
Brian Niebuhr | e91c659 | 2010-10-01 10:29:29 +0530 | [diff] [blame] | 140 | #define SPI_TMP_BUFSZ (SMP_CACHE_BYTES + 1) |
| 141 | u8 rx_tmp_buf[SPI_TMP_BUFSZ]; |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 142 | int rcount; |
| 143 | int wcount; |
Brian Niebuhr | 96fd881 | 2010-09-27 22:23:23 +0530 | [diff] [blame] | 144 | struct davinci_spi_dma dma_channels; |
Brian Niebuhr | 778e261 | 2010-09-03 15:15:06 +0530 | [diff] [blame] | 145 | struct davinci_spi_platform_data *pdata; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 146 | |
| 147 | void (*get_rx)(u32 rx_data, struct davinci_spi *); |
| 148 | u32 (*get_tx)(struct davinci_spi *); |
| 149 | |
Brian Niebuhr | cda987e | 2010-08-19 16:16:28 +0530 | [diff] [blame] | 150 | u8 bytes_per_word[SPI_MAX_CHIPSELECT]; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
Brian Niebuhr | 53a31b0 | 2010-08-16 15:05:51 +0530 | [diff] [blame] | 153 | static struct davinci_spi_config davinci_spi_default_cfg; |
| 154 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 155 | static void davinci_spi_rx_buf_u8(u32 data, struct davinci_spi *davinci_spi) |
| 156 | { |
Brian Niebuhr | 53d454a1 | 2010-08-19 17:04:25 +0530 | [diff] [blame] | 157 | if (davinci_spi->rx) { |
| 158 | u8 *rx = davinci_spi->rx; |
| 159 | *rx++ = (u8)data; |
| 160 | davinci_spi->rx = rx; |
| 161 | } |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | static void davinci_spi_rx_buf_u16(u32 data, struct davinci_spi *davinci_spi) |
| 165 | { |
Brian Niebuhr | 53d454a1 | 2010-08-19 17:04:25 +0530 | [diff] [blame] | 166 | if (davinci_spi->rx) { |
| 167 | u16 *rx = davinci_spi->rx; |
| 168 | *rx++ = (u16)data; |
| 169 | davinci_spi->rx = rx; |
| 170 | } |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static u32 davinci_spi_tx_buf_u8(struct davinci_spi *davinci_spi) |
| 174 | { |
Brian Niebuhr | 53d454a1 | 2010-08-19 17:04:25 +0530 | [diff] [blame] | 175 | u32 data = 0; |
| 176 | if (davinci_spi->tx) { |
| 177 | const u8 *tx = davinci_spi->tx; |
| 178 | data = *tx++; |
| 179 | davinci_spi->tx = tx; |
| 180 | } |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 181 | return data; |
| 182 | } |
| 183 | |
| 184 | static u32 davinci_spi_tx_buf_u16(struct davinci_spi *davinci_spi) |
| 185 | { |
Brian Niebuhr | 53d454a1 | 2010-08-19 17:04:25 +0530 | [diff] [blame] | 186 | u32 data = 0; |
| 187 | if (davinci_spi->tx) { |
| 188 | const u16 *tx = davinci_spi->tx; |
| 189 | data = *tx++; |
| 190 | davinci_spi->tx = tx; |
| 191 | } |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 192 | return data; |
| 193 | } |
| 194 | |
| 195 | static inline void set_io_bits(void __iomem *addr, u32 bits) |
| 196 | { |
| 197 | u32 v = ioread32(addr); |
| 198 | |
| 199 | v |= bits; |
| 200 | iowrite32(v, addr); |
| 201 | } |
| 202 | |
| 203 | static inline void clear_io_bits(void __iomem *addr, u32 bits) |
| 204 | { |
| 205 | u32 v = ioread32(addr); |
| 206 | |
| 207 | v &= ~bits; |
| 208 | iowrite32(v, addr); |
| 209 | } |
| 210 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 211 | /* |
| 212 | * Interface to control the chip select signal |
| 213 | */ |
| 214 | static void davinci_spi_chipselect(struct spi_device *spi, int value) |
| 215 | { |
| 216 | struct davinci_spi *davinci_spi; |
| 217 | struct davinci_spi_platform_data *pdata; |
Brian Niebuhr | 7978b8c | 2010-08-13 10:11:03 +0530 | [diff] [blame] | 218 | u8 chip_sel = spi->chip_select; |
Brian Niebuhr | cfbc5d1 | 2010-08-12 12:27:33 +0530 | [diff] [blame] | 219 | u16 spidat1_cfg = CS_DEFAULT; |
Brian Niebuhr | 2385397 | 2010-08-13 10:57:44 +0530 | [diff] [blame] | 220 | bool gpio_chipsel = false; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 221 | |
| 222 | davinci_spi = spi_master_get_devdata(spi->master); |
| 223 | pdata = davinci_spi->pdata; |
| 224 | |
Brian Niebuhr | 2385397 | 2010-08-13 10:57:44 +0530 | [diff] [blame] | 225 | if (pdata->chip_sel && chip_sel < pdata->num_chipselect && |
| 226 | pdata->chip_sel[chip_sel] != SPI_INTERN_CS) |
| 227 | gpio_chipsel = true; |
| 228 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 229 | /* |
| 230 | * Board specific chip select logic decides the polarity and cs |
| 231 | * line for the controller |
| 232 | */ |
Brian Niebuhr | 2385397 | 2010-08-13 10:57:44 +0530 | [diff] [blame] | 233 | if (gpio_chipsel) { |
| 234 | if (value == BITBANG_CS_ACTIVE) |
| 235 | gpio_set_value(pdata->chip_sel[chip_sel], 0); |
| 236 | else |
| 237 | gpio_set_value(pdata->chip_sel[chip_sel], 1); |
| 238 | } else { |
| 239 | if (value == BITBANG_CS_ACTIVE) { |
| 240 | spidat1_cfg |= SPIDAT1_CSHOLD_MASK; |
| 241 | spidat1_cfg &= ~(0x1 << chip_sel); |
| 242 | } |
Brian Niebuhr | 7978b8c | 2010-08-13 10:11:03 +0530 | [diff] [blame] | 243 | |
Brian Niebuhr | 2385397 | 2010-08-13 10:57:44 +0530 | [diff] [blame] | 244 | iowrite16(spidat1_cfg, davinci_spi->base + SPIDAT1 + 2); |
| 245 | } |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
Brian Niebuhr | 7fe0092 | 2010-08-13 13:27:23 +0530 | [diff] [blame] | 249 | * davinci_spi_get_prescale - Calculates the correct prescale value |
| 250 | * @maxspeed_hz: the maximum rate the SPI clock can run at |
| 251 | * |
| 252 | * This function calculates the prescale value that generates a clock rate |
| 253 | * less than or equal to the specified maximum. |
| 254 | * |
| 255 | * Returns: calculated prescale - 1 for easy programming into SPI registers |
| 256 | * or negative error number if valid prescalar cannot be updated. |
| 257 | */ |
| 258 | static inline int davinci_spi_get_prescale(struct davinci_spi *davinci_spi, |
| 259 | u32 max_speed_hz) |
| 260 | { |
| 261 | int ret; |
| 262 | |
| 263 | ret = DIV_ROUND_UP(clk_get_rate(davinci_spi->clk), max_speed_hz); |
| 264 | |
| 265 | if (ret < 3 || ret > 256) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | return ret - 1; |
| 269 | } |
| 270 | |
| 271 | /** |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 272 | * davinci_spi_setup_transfer - This functions will determine transfer method |
| 273 | * @spi: spi device on which data transfer to be done |
| 274 | * @t: spi transfer in which transfer info is filled |
| 275 | * |
| 276 | * This function determines data transfer method (8/16/32 bit transfer). |
| 277 | * It will also set the SPI Clock Control register according to |
| 278 | * SPI slave device freq. |
| 279 | */ |
| 280 | static int davinci_spi_setup_transfer(struct spi_device *spi, |
| 281 | struct spi_transfer *t) |
| 282 | { |
| 283 | |
| 284 | struct davinci_spi *davinci_spi; |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 285 | struct davinci_spi_config *spicfg; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 286 | u8 bits_per_word = 0; |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 287 | u32 hz = 0, spifmt = 0, prescale = 0; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 288 | |
| 289 | davinci_spi = spi_master_get_devdata(spi->master); |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 290 | spicfg = (struct davinci_spi_config *)spi->controller_data; |
| 291 | if (!spicfg) |
| 292 | spicfg = &davinci_spi_default_cfg; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 293 | |
| 294 | if (t) { |
| 295 | bits_per_word = t->bits_per_word; |
| 296 | hz = t->speed_hz; |
| 297 | } |
| 298 | |
| 299 | /* if bits_per_word is not set then set it default */ |
| 300 | if (!bits_per_word) |
| 301 | bits_per_word = spi->bits_per_word; |
| 302 | |
| 303 | /* |
| 304 | * Assign function pointer to appropriate transfer method |
| 305 | * 8bit, 16bit or 32bit transfer |
| 306 | */ |
| 307 | if (bits_per_word <= 8 && bits_per_word >= 2) { |
| 308 | davinci_spi->get_rx = davinci_spi_rx_buf_u8; |
| 309 | davinci_spi->get_tx = davinci_spi_tx_buf_u8; |
Brian Niebuhr | cda987e | 2010-08-19 16:16:28 +0530 | [diff] [blame] | 310 | davinci_spi->bytes_per_word[spi->chip_select] = 1; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 311 | } else if (bits_per_word <= 16 && bits_per_word >= 2) { |
| 312 | davinci_spi->get_rx = davinci_spi_rx_buf_u16; |
| 313 | davinci_spi->get_tx = davinci_spi_tx_buf_u16; |
Brian Niebuhr | cda987e | 2010-08-19 16:16:28 +0530 | [diff] [blame] | 314 | davinci_spi->bytes_per_word[spi->chip_select] = 2; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 315 | } else |
| 316 | return -EINVAL; |
| 317 | |
| 318 | if (!hz) |
| 319 | hz = spi->max_speed_hz; |
| 320 | |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 321 | /* Set up SPIFMTn register, unique to this chipselect. */ |
| 322 | |
Brian Niebuhr | 7fe0092 | 2010-08-13 13:27:23 +0530 | [diff] [blame] | 323 | prescale = davinci_spi_get_prescale(davinci_spi, hz); |
| 324 | if (prescale < 0) |
| 325 | return prescale; |
| 326 | |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 327 | spifmt = (prescale << SPIFMT_PRESCALE_SHIFT) | (bits_per_word & 0x1f); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 328 | |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 329 | if (spi->mode & SPI_LSB_FIRST) |
| 330 | spifmt |= SPIFMT_SHIFTDIR_MASK; |
| 331 | |
| 332 | if (spi->mode & SPI_CPOL) |
| 333 | spifmt |= SPIFMT_POLARITY_MASK; |
| 334 | |
| 335 | if (!(spi->mode & SPI_CPHA)) |
| 336 | spifmt |= SPIFMT_PHASE_MASK; |
| 337 | |
| 338 | /* |
| 339 | * Version 1 hardware supports two basic SPI modes: |
| 340 | * - Standard SPI mode uses 4 pins, with chipselect |
| 341 | * - 3 pin SPI is a 4 pin variant without CS (SPI_NO_CS) |
| 342 | * (distinct from SPI_3WIRE, with just one data wire; |
| 343 | * or similar variants without MOSI or without MISO) |
| 344 | * |
| 345 | * Version 2 hardware supports an optional handshaking signal, |
| 346 | * so it can support two more modes: |
| 347 | * - 5 pin SPI variant is standard SPI plus SPI_READY |
| 348 | * - 4 pin with enable is (SPI_READY | SPI_NO_CS) |
| 349 | */ |
| 350 | |
| 351 | if (davinci_spi->version == SPI_VERSION_2) { |
| 352 | |
Brian Niebuhr | 7abbf23 | 2010-08-19 15:07:38 +0530 | [diff] [blame] | 353 | u32 delay = 0; |
| 354 | |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 355 | spifmt |= ((spicfg->wdelay << SPIFMT_WDELAY_SHIFT) |
| 356 | & SPIFMT_WDELAY_MASK); |
| 357 | |
| 358 | if (spicfg->odd_parity) |
| 359 | spifmt |= SPIFMT_ODD_PARITY_MASK; |
| 360 | |
| 361 | if (spicfg->parity_enable) |
| 362 | spifmt |= SPIFMT_PARITYENA_MASK; |
| 363 | |
Brian Niebuhr | 7abbf23 | 2010-08-19 15:07:38 +0530 | [diff] [blame] | 364 | if (spicfg->timer_disable) { |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 365 | spifmt |= SPIFMT_DISTIMER_MASK; |
Brian Niebuhr | 7abbf23 | 2010-08-19 15:07:38 +0530 | [diff] [blame] | 366 | } else { |
| 367 | delay |= (spicfg->c2tdelay << SPIDELAY_C2TDELAY_SHIFT) |
| 368 | & SPIDELAY_C2TDELAY_MASK; |
| 369 | delay |= (spicfg->t2cdelay << SPIDELAY_T2CDELAY_SHIFT) |
| 370 | & SPIDELAY_T2CDELAY_MASK; |
| 371 | } |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 372 | |
Brian Niebuhr | 7abbf23 | 2010-08-19 15:07:38 +0530 | [diff] [blame] | 373 | if (spi->mode & SPI_READY) { |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 374 | spifmt |= SPIFMT_WAITENA_MASK; |
Brian Niebuhr | 7abbf23 | 2010-08-19 15:07:38 +0530 | [diff] [blame] | 375 | delay |= (spicfg->t2edelay << SPIDELAY_T2EDELAY_SHIFT) |
| 376 | & SPIDELAY_T2EDELAY_MASK; |
| 377 | delay |= (spicfg->c2edelay << SPIDELAY_C2EDELAY_SHIFT) |
| 378 | & SPIDELAY_C2EDELAY_MASK; |
| 379 | } |
| 380 | |
| 381 | iowrite32(delay, davinci_spi->base + SPIDELAY); |
Brian Niebuhr | 25f3351 | 2010-08-19 12:15:22 +0530 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | iowrite32(spifmt, davinci_spi->base + SPIFMT0); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 385 | |
| 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | static void davinci_spi_dma_rx_callback(unsigned lch, u16 ch_status, void *data) |
| 390 | { |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 391 | struct davinci_spi_dma *davinci_spi_dma = data; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 392 | |
| 393 | if (ch_status == DMA_COMPLETE) |
| 394 | edma_stop(davinci_spi_dma->dma_rx_channel); |
| 395 | else |
| 396 | edma_clean_channel(davinci_spi_dma->dma_rx_channel); |
| 397 | |
| 398 | complete(&davinci_spi_dma->dma_rx_completion); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static void davinci_spi_dma_tx_callback(unsigned lch, u16 ch_status, void *data) |
| 402 | { |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 403 | struct davinci_spi_dma *davinci_spi_dma = data; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 404 | |
| 405 | if (ch_status == DMA_COMPLETE) |
| 406 | edma_stop(davinci_spi_dma->dma_tx_channel); |
| 407 | else |
| 408 | edma_clean_channel(davinci_spi_dma->dma_tx_channel); |
| 409 | |
| 410 | complete(&davinci_spi_dma->dma_tx_completion); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 413 | /** |
| 414 | * davinci_spi_setup - This functions will set default transfer method |
| 415 | * @spi: spi device on which data transfer to be done |
| 416 | * |
| 417 | * This functions sets the default transfer method. |
| 418 | */ |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 419 | static int davinci_spi_setup(struct spi_device *spi) |
| 420 | { |
Brian Niebuhr | b23a5d4 | 2010-09-24 18:53:32 +0530 | [diff] [blame] | 421 | int retval = 0; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 422 | struct davinci_spi *davinci_spi; |
Brian Niebuhr | be88471 | 2010-09-03 12:15:28 +0530 | [diff] [blame] | 423 | struct davinci_spi_platform_data *pdata; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 424 | |
| 425 | davinci_spi = spi_master_get_devdata(spi->master); |
Brian Niebuhr | be88471 | 2010-09-03 12:15:28 +0530 | [diff] [blame] | 426 | pdata = davinci_spi->pdata; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 427 | |
| 428 | /* if bits per word length is zero then set it default 8 */ |
| 429 | if (!spi->bits_per_word) |
| 430 | spi->bits_per_word = 8; |
| 431 | |
Brian Niebuhr | be88471 | 2010-09-03 12:15:28 +0530 | [diff] [blame] | 432 | if (!(spi->mode & SPI_NO_CS)) { |
| 433 | if ((pdata->chip_sel == NULL) || |
| 434 | (pdata->chip_sel[spi->chip_select] == SPI_INTERN_CS)) |
| 435 | set_io_bits(davinci_spi->base + SPIPC0, |
| 436 | 1 << spi->chip_select); |
| 437 | |
| 438 | } |
| 439 | |
| 440 | if (spi->mode & SPI_READY) |
| 441 | set_io_bits(davinci_spi->base + SPIPC0, SPIPC0_SPIENA_MASK); |
| 442 | |
| 443 | if (spi->mode & SPI_LOOP) |
| 444 | set_io_bits(davinci_spi->base + SPIGCR1, |
| 445 | SPIGCR1_LOOPBACK_MASK); |
| 446 | else |
| 447 | clear_io_bits(davinci_spi->base + SPIGCR1, |
| 448 | SPIGCR1_LOOPBACK_MASK); |
| 449 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 450 | return retval; |
| 451 | } |
| 452 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 453 | static int davinci_spi_check_error(struct davinci_spi *davinci_spi, |
| 454 | int int_status) |
| 455 | { |
| 456 | struct device *sdev = davinci_spi->bitbang.master->dev.parent; |
| 457 | |
| 458 | if (int_status & SPIFLG_TIMEOUT_MASK) { |
| 459 | dev_dbg(sdev, "SPI Time-out Error\n"); |
| 460 | return -ETIMEDOUT; |
| 461 | } |
| 462 | if (int_status & SPIFLG_DESYNC_MASK) { |
| 463 | dev_dbg(sdev, "SPI Desynchronization Error\n"); |
| 464 | return -EIO; |
| 465 | } |
| 466 | if (int_status & SPIFLG_BITERR_MASK) { |
| 467 | dev_dbg(sdev, "SPI Bit error\n"); |
| 468 | return -EIO; |
| 469 | } |
| 470 | |
| 471 | if (davinci_spi->version == SPI_VERSION_2) { |
| 472 | if (int_status & SPIFLG_DLEN_ERR_MASK) { |
| 473 | dev_dbg(sdev, "SPI Data Length Error\n"); |
| 474 | return -EIO; |
| 475 | } |
| 476 | if (int_status & SPIFLG_PARERR_MASK) { |
| 477 | dev_dbg(sdev, "SPI Parity Error\n"); |
| 478 | return -EIO; |
| 479 | } |
| 480 | if (int_status & SPIFLG_OVRRUN_MASK) { |
| 481 | dev_dbg(sdev, "SPI Data Overrun error\n"); |
| 482 | return -EIO; |
| 483 | } |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 484 | if (int_status & SPIFLG_BUF_INIT_ACTIVE_MASK) { |
| 485 | dev_dbg(sdev, "SPI Buffer Init Active\n"); |
| 486 | return -EBUSY; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | /** |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 494 | * davinci_spi_process_events - check for and handle any SPI controller events |
| 495 | * @davinci_spi: the controller data |
| 496 | * |
| 497 | * This function will check the SPIFLG register and handle any events that are |
| 498 | * detected there |
| 499 | */ |
| 500 | static int davinci_spi_process_events(struct davinci_spi *davinci_spi) |
| 501 | { |
| 502 | u32 buf, status, errors = 0, data1_reg_val; |
| 503 | |
| 504 | buf = ioread32(davinci_spi->base + SPIBUF); |
| 505 | |
| 506 | if (davinci_spi->rcount > 0 && !(buf & SPIBUF_RXEMPTY_MASK)) { |
| 507 | davinci_spi->get_rx(buf & 0xFFFF, davinci_spi); |
| 508 | davinci_spi->rcount--; |
| 509 | } |
| 510 | |
| 511 | status = ioread32(davinci_spi->base + SPIFLG); |
| 512 | |
| 513 | if (unlikely(status & SPIFLG_ERROR_MASK)) { |
| 514 | errors = status & SPIFLG_ERROR_MASK; |
| 515 | goto out; |
| 516 | } |
| 517 | |
| 518 | if (davinci_spi->wcount > 0 && !(buf & SPIBUF_TXFULL_MASK)) { |
| 519 | data1_reg_val = ioread32(davinci_spi->base + SPIDAT1); |
| 520 | davinci_spi->wcount--; |
| 521 | data1_reg_val &= ~0xFFFF; |
| 522 | data1_reg_val |= 0xFFFF & davinci_spi->get_tx(davinci_spi); |
| 523 | iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1); |
| 524 | } |
| 525 | |
| 526 | out: |
| 527 | return errors; |
| 528 | } |
| 529 | |
| 530 | /** |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 531 | * davinci_spi_bufs - functions which will handle transfer data |
| 532 | * @spi: spi device on which data transfer to be done |
| 533 | * @t: spi transfer in which transfer info is filled |
| 534 | * |
| 535 | * This function will put data to be transferred into data register |
| 536 | * of SPI controller and then wait until the completion will be marked |
| 537 | * by the IRQ Handler. |
| 538 | */ |
| 539 | static int davinci_spi_bufs_pio(struct spi_device *spi, struct spi_transfer *t) |
| 540 | { |
| 541 | struct davinci_spi *davinci_spi; |
Brian Niebuhr | 839c996 | 2010-08-23 16:39:19 +0530 | [diff] [blame] | 542 | int ret; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 543 | u32 tx_data, data1_reg_val; |
Brian Niebuhr | 839c996 | 2010-08-23 16:39:19 +0530 | [diff] [blame] | 544 | u32 errors = 0; |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 545 | struct davinci_spi_config *spicfg; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 546 | struct davinci_spi_platform_data *pdata; |
| 547 | |
| 548 | davinci_spi = spi_master_get_devdata(spi->master); |
| 549 | pdata = davinci_spi->pdata; |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 550 | spicfg = (struct davinci_spi_config *)spi->controller_data; |
| 551 | if (!spicfg) |
| 552 | spicfg = &davinci_spi_default_cfg; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 553 | |
| 554 | davinci_spi->tx = t->tx_buf; |
| 555 | davinci_spi->rx = t->rx_buf; |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 556 | davinci_spi->wcount = t->len / |
| 557 | davinci_spi->bytes_per_word[spi->chip_select]; |
| 558 | davinci_spi->rcount = davinci_spi->wcount; |
Brian Niebuhr | 7978b8c | 2010-08-13 10:11:03 +0530 | [diff] [blame] | 559 | |
Brian Niebuhr | 839c996 | 2010-08-23 16:39:19 +0530 | [diff] [blame] | 560 | data1_reg_val = ioread32(davinci_spi->base + SPIDAT1); |
| 561 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 562 | /* Enable SPI */ |
| 563 | set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK); |
| 564 | |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 565 | if (spicfg->io_type == SPI_IO_TYPE_INTR) { |
| 566 | set_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT); |
| 567 | INIT_COMPLETION(davinci_spi->done); |
| 568 | } |
Brian Niebuhr | cf90fe7 | 2010-08-20 17:02:49 +0530 | [diff] [blame] | 569 | |
Brian Niebuhr | 839c996 | 2010-08-23 16:39:19 +0530 | [diff] [blame] | 570 | /* start the transfer */ |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 571 | davinci_spi->wcount--; |
Brian Niebuhr | 839c996 | 2010-08-23 16:39:19 +0530 | [diff] [blame] | 572 | tx_data = davinci_spi->get_tx(davinci_spi); |
| 573 | data1_reg_val &= 0xFFFF0000; |
| 574 | data1_reg_val |= tx_data & 0xFFFF; |
| 575 | iowrite32(data1_reg_val, davinci_spi->base + SPIDAT1); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 576 | |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 577 | /* Wait for the transfer to complete */ |
| 578 | if (spicfg->io_type == SPI_IO_TYPE_INTR) { |
| 579 | wait_for_completion_interruptible(&(davinci_spi->done)); |
| 580 | } else { |
| 581 | while (davinci_spi->rcount > 0 || davinci_spi->wcount > 0) { |
| 582 | errors = davinci_spi_process_events(davinci_spi); |
| 583 | if (errors) |
| 584 | break; |
| 585 | cpu_relax(); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 586 | } |
| 587 | } |
| 588 | |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 589 | clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL); |
| 590 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 591 | /* |
| 592 | * Check for bit error, desync error,parity error,timeout error and |
| 593 | * receive overflow errors |
| 594 | */ |
Brian Niebuhr | 839c996 | 2010-08-23 16:39:19 +0530 | [diff] [blame] | 595 | if (errors) { |
| 596 | ret = davinci_spi_check_error(davinci_spi, errors); |
| 597 | WARN(!ret, "%s: error reported but no error found!\n", |
| 598 | dev_name(&spi->dev)); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 599 | return ret; |
Brian Niebuhr | 839c996 | 2010-08-23 16:39:19 +0530 | [diff] [blame] | 600 | } |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 601 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 602 | return t->len; |
| 603 | } |
| 604 | |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 605 | /** |
| 606 | * davinci_spi_irq - Interrupt handler for SPI Master Controller |
| 607 | * @irq: IRQ number for this SPI Master |
| 608 | * @context_data: structure for SPI Master controller davinci_spi |
| 609 | * |
| 610 | * ISR will determine that interrupt arrives either for READ or WRITE command. |
| 611 | * According to command it will do the appropriate action. It will check |
| 612 | * transfer length and if it is not zero then dispatch transfer command again. |
| 613 | * If transfer length is zero then it will indicate the COMPLETION so that |
| 614 | * davinci_spi_bufs function can go ahead. |
| 615 | */ |
| 616 | static irqreturn_t davinci_spi_irq(s32 irq, void *context_data) |
| 617 | { |
| 618 | struct davinci_spi *davinci_spi = context_data; |
| 619 | int status; |
| 620 | |
| 621 | status = davinci_spi_process_events(davinci_spi); |
| 622 | if (unlikely(status != 0)) |
| 623 | clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKINT); |
| 624 | |
| 625 | if ((!davinci_spi->rcount && !davinci_spi->wcount) || status) |
| 626 | complete(&davinci_spi->done); |
| 627 | |
| 628 | return IRQ_HANDLED; |
| 629 | } |
| 630 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 631 | static int davinci_spi_bufs_dma(struct spi_device *spi, struct spi_transfer *t) |
| 632 | { |
| 633 | struct davinci_spi *davinci_spi; |
| 634 | int int_status = 0; |
Brian Niebuhr | d3f7141 | 2010-09-29 12:31:54 +0530 | [diff] [blame] | 635 | int count; |
Brian Niebuhr | e91c659 | 2010-10-01 10:29:29 +0530 | [diff] [blame] | 636 | unsigned rx_buf_count; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 637 | struct davinci_spi_dma *davinci_spi_dma; |
Brian Niebuhr | b7ab24a | 2010-08-19 16:42:42 +0530 | [diff] [blame] | 638 | int data_type, ret; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 639 | unsigned long tx_reg, rx_reg; |
Brian Niebuhr | c29e3c6 | 2010-09-28 13:59:26 +0530 | [diff] [blame] | 640 | struct davinci_spi_platform_data *pdata; |
Brian Niebuhr | e91c659 | 2010-10-01 10:29:29 +0530 | [diff] [blame] | 641 | void *rx_buf; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 642 | struct device *sdev; |
Brian Niebuhr | 49fc3f4 | 2010-10-01 11:22:23 +0530 | [diff] [blame] | 643 | struct edmacc_param param; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 644 | |
| 645 | davinci_spi = spi_master_get_devdata(spi->master); |
Brian Niebuhr | c29e3c6 | 2010-09-28 13:59:26 +0530 | [diff] [blame] | 646 | pdata = davinci_spi->pdata; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 647 | sdev = davinci_spi->bitbang.master->dev.parent; |
| 648 | |
Brian Niebuhr | 96fd881 | 2010-09-27 22:23:23 +0530 | [diff] [blame] | 649 | davinci_spi_dma = &davinci_spi->dma_channels; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 650 | |
| 651 | tx_reg = (unsigned long)davinci_spi->pbase + SPIDAT1; |
| 652 | rx_reg = (unsigned long)davinci_spi->pbase + SPIBUF; |
| 653 | |
| 654 | davinci_spi->tx = t->tx_buf; |
| 655 | davinci_spi->rx = t->rx_buf; |
| 656 | |
| 657 | /* convert len to words based on bits_per_word */ |
Brian Niebuhr | b7ab24a | 2010-08-19 16:42:42 +0530 | [diff] [blame] | 658 | data_type = davinci_spi->bytes_per_word[spi->chip_select]; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 659 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 660 | init_completion(&davinci_spi_dma->dma_rx_completion); |
| 661 | init_completion(&davinci_spi_dma->dma_tx_completion); |
| 662 | |
Brian Niebuhr | f2bf4e8 | 2010-08-20 15:28:23 +0530 | [diff] [blame] | 663 | count = t->len / data_type; /* the number of elements */ |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 664 | |
| 665 | /* disable all interrupts for dma transfers */ |
| 666 | clear_io_bits(davinci_spi->base + SPIINT, SPIINT_MASKALL); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 667 | /* Enable SPI */ |
| 668 | set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_SPIENA_MASK); |
| 669 | |
Brian Niebuhr | d3f7141 | 2010-09-29 12:31:54 +0530 | [diff] [blame] | 670 | /* |
| 671 | * Transmit DMA setup |
| 672 | * |
| 673 | * If there is transmit data, map the transmit buffer, set it as the |
| 674 | * source of data and set the source B index to data size. |
| 675 | * If there is no transmit data, set the transmit register as the |
| 676 | * source of data, and set the source B index to zero. |
| 677 | * |
| 678 | * The destination is always the transmit register itself. And the |
| 679 | * destination never increments. |
| 680 | */ |
| 681 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 682 | if (t->tx_buf) { |
| 683 | t->tx_dma = dma_map_single(&spi->dev, (void *)t->tx_buf, count, |
| 684 | DMA_TO_DEVICE); |
| 685 | if (dma_mapping_error(&spi->dev, t->tx_dma)) { |
| 686 | dev_dbg(sdev, "Unable to DMA map a %d bytes" |
| 687 | " TX buffer\n", count); |
| 688 | return -ENOMEM; |
| 689 | } |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 690 | } |
| 691 | |
Brian Niebuhr | 49fc3f4 | 2010-10-01 11:22:23 +0530 | [diff] [blame] | 692 | param.opt = TCINTEN | EDMA_TCC(davinci_spi_dma->dma_tx_channel); |
| 693 | param.src = t->tx_buf ? t->tx_dma : tx_reg; |
| 694 | param.a_b_cnt = count << 16 | data_type; |
| 695 | param.dst = tx_reg; |
| 696 | param.src_dst_bidx = t->tx_buf ? data_type : 0; |
| 697 | param.link_bcntrld = 0xffff; |
| 698 | param.src_dst_cidx = 0; |
| 699 | param.ccnt = 1; |
| 700 | edma_write_slot(davinci_spi_dma->dma_tx_channel, ¶m); |
Brian Niebuhr | 523c37e | 2010-10-04 17:35:34 +0530 | [diff] [blame^] | 701 | edma_link(davinci_spi_dma->dma_tx_channel, |
| 702 | davinci_spi_dma->dummy_param_slot); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 703 | |
Brian Niebuhr | e91c659 | 2010-10-01 10:29:29 +0530 | [diff] [blame] | 704 | /* |
| 705 | * Receive DMA setup |
| 706 | * |
| 707 | * If there is receive buffer, use it to receive data. If there |
| 708 | * is none provided, use a temporary receive buffer. Set the |
| 709 | * destination B index to 0 so effectively only one byte is used |
| 710 | * in the temporary buffer (address does not increment). |
| 711 | * |
| 712 | * The source of receive data is the receive data register. The |
| 713 | * source address never increments. |
| 714 | */ |
| 715 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 716 | if (t->rx_buf) { |
Brian Niebuhr | e91c659 | 2010-10-01 10:29:29 +0530 | [diff] [blame] | 717 | rx_buf = t->rx_buf; |
| 718 | rx_buf_count = count; |
| 719 | } else { |
| 720 | rx_buf = davinci_spi->rx_tmp_buf; |
| 721 | rx_buf_count = sizeof(davinci_spi->rx_tmp_buf); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Brian Niebuhr | e91c659 | 2010-10-01 10:29:29 +0530 | [diff] [blame] | 724 | t->rx_dma = dma_map_single(&spi->dev, rx_buf, rx_buf_count, |
| 725 | DMA_FROM_DEVICE); |
| 726 | if (dma_mapping_error(&spi->dev, t->rx_dma)) { |
| 727 | dev_dbg(sdev, "Couldn't DMA map a %d bytes RX buffer\n", |
| 728 | rx_buf_count); |
| 729 | if (t->tx_buf) |
| 730 | dma_unmap_single(NULL, t->tx_dma, count, DMA_TO_DEVICE); |
| 731 | return -ENOMEM; |
| 732 | } |
| 733 | |
Brian Niebuhr | 49fc3f4 | 2010-10-01 11:22:23 +0530 | [diff] [blame] | 734 | param.opt = TCINTEN | EDMA_TCC(davinci_spi_dma->dma_rx_channel); |
| 735 | param.src = rx_reg; |
| 736 | param.a_b_cnt = count << 16 | data_type; |
| 737 | param.dst = t->rx_dma; |
| 738 | param.src_dst_bidx = (t->rx_buf ? data_type : 0) << 16; |
| 739 | param.link_bcntrld = 0xffff; |
| 740 | param.src_dst_cidx = 0; |
| 741 | param.ccnt = 1; |
| 742 | edma_write_slot(davinci_spi_dma->dma_rx_channel, ¶m); |
Brian Niebuhr | e91c659 | 2010-10-01 10:29:29 +0530 | [diff] [blame] | 743 | |
Brian Niebuhr | c29e3c6 | 2010-09-28 13:59:26 +0530 | [diff] [blame] | 744 | if (pdata->cshold_bug) { |
| 745 | u16 spidat1 = ioread16(davinci_spi->base + SPIDAT1 + 2); |
| 746 | iowrite16(spidat1, davinci_spi->base + SPIDAT1 + 2); |
| 747 | } |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 748 | |
Brian Niebuhr | e91c659 | 2010-10-01 10:29:29 +0530 | [diff] [blame] | 749 | edma_start(davinci_spi_dma->dma_rx_channel); |
Brian Niebuhr | c29e3c6 | 2010-09-28 13:59:26 +0530 | [diff] [blame] | 750 | edma_start(davinci_spi_dma->dma_tx_channel); |
Brian Niebuhr | a4f4497 | 2010-10-01 14:00:48 +0530 | [diff] [blame] | 751 | set_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 752 | |
Brian Niebuhr | c29e3c6 | 2010-09-28 13:59:26 +0530 | [diff] [blame] | 753 | wait_for_completion_interruptible(&davinci_spi_dma->dma_tx_completion); |
Brian Niebuhr | e91c659 | 2010-10-01 10:29:29 +0530 | [diff] [blame] | 754 | wait_for_completion_interruptible(&davinci_spi_dma->dma_rx_completion); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 755 | |
Brian Niebuhr | d3f7141 | 2010-09-29 12:31:54 +0530 | [diff] [blame] | 756 | if (t->tx_buf) |
| 757 | dma_unmap_single(NULL, t->tx_dma, count, DMA_TO_DEVICE); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 758 | |
Brian Niebuhr | e91c659 | 2010-10-01 10:29:29 +0530 | [diff] [blame] | 759 | dma_unmap_single(NULL, t->rx_dma, rx_buf_count, DMA_FROM_DEVICE); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 760 | |
Brian Niebuhr | a4f4497 | 2010-10-01 14:00:48 +0530 | [diff] [blame] | 761 | clear_io_bits(davinci_spi->base + SPIINT, SPIINT_DMA_REQ_EN); |
| 762 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 763 | /* |
| 764 | * Check for bit error, desync error,parity error,timeout error and |
| 765 | * receive overflow errors |
| 766 | */ |
| 767 | int_status = ioread32(davinci_spi->base + SPIFLG); |
| 768 | |
| 769 | ret = davinci_spi_check_error(davinci_spi, int_status); |
| 770 | if (ret != 0) |
| 771 | return ret; |
| 772 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 773 | return t->len; |
| 774 | } |
| 775 | |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 776 | static int davinci_spi_request_dma(struct davinci_spi_dma *davinci_spi_dma) |
| 777 | { |
| 778 | int r; |
| 779 | |
| 780 | r = edma_alloc_channel(davinci_spi_dma->dma_rx_channel, |
| 781 | davinci_spi_dma_rx_callback, davinci_spi_dma, |
| 782 | davinci_spi_dma->eventq); |
| 783 | if (r < 0) { |
| 784 | pr_err("Unable to request DMA channel for SPI RX\n"); |
Brian Niebuhr | 523c37e | 2010-10-04 17:35:34 +0530 | [diff] [blame^] | 785 | r = -EAGAIN; |
| 786 | goto rx_dma_failed; |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | r = edma_alloc_channel(davinci_spi_dma->dma_tx_channel, |
| 790 | davinci_spi_dma_tx_callback, davinci_spi_dma, |
| 791 | davinci_spi_dma->eventq); |
| 792 | if (r < 0) { |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 793 | pr_err("Unable to request DMA channel for SPI TX\n"); |
Brian Niebuhr | 523c37e | 2010-10-04 17:35:34 +0530 | [diff] [blame^] | 794 | r = -EAGAIN; |
| 795 | goto tx_dma_failed; |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 796 | } |
| 797 | |
Brian Niebuhr | 523c37e | 2010-10-04 17:35:34 +0530 | [diff] [blame^] | 798 | r = edma_alloc_slot(EDMA_CTLR(davinci_spi_dma->dma_tx_channel), |
| 799 | EDMA_SLOT_ANY); |
| 800 | if (r < 0) { |
| 801 | pr_err("Unable to request SPI TX DMA param slot\n"); |
| 802 | r = -EAGAIN; |
| 803 | goto param_failed; |
| 804 | } |
| 805 | davinci_spi_dma->dummy_param_slot = r; |
| 806 | edma_link(davinci_spi_dma->dummy_param_slot, |
| 807 | davinci_spi_dma->dummy_param_slot); |
| 808 | |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 809 | return 0; |
Brian Niebuhr | 523c37e | 2010-10-04 17:35:34 +0530 | [diff] [blame^] | 810 | param_failed: |
| 811 | edma_free_channel(davinci_spi_dma->dma_tx_channel); |
| 812 | tx_dma_failed: |
| 813 | edma_free_channel(davinci_spi_dma->dma_rx_channel); |
| 814 | rx_dma_failed: |
| 815 | return r; |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 816 | } |
| 817 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 818 | /** |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 819 | * davinci_spi_probe - probe function for SPI Master Controller |
| 820 | * @pdev: platform_device structure which contains plateform specific data |
| 821 | */ |
| 822 | static int davinci_spi_probe(struct platform_device *pdev) |
| 823 | { |
| 824 | struct spi_master *master; |
| 825 | struct davinci_spi *davinci_spi; |
| 826 | struct davinci_spi_platform_data *pdata; |
| 827 | struct resource *r, *mem; |
| 828 | resource_size_t dma_rx_chan = SPI_NO_RESOURCE; |
| 829 | resource_size_t dma_tx_chan = SPI_NO_RESOURCE; |
| 830 | resource_size_t dma_eventq = SPI_NO_RESOURCE; |
| 831 | int i = 0, ret = 0; |
Brian Niebuhr | f34bd4c | 2010-09-03 11:56:35 +0530 | [diff] [blame] | 832 | u32 spipc0; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 833 | |
| 834 | pdata = pdev->dev.platform_data; |
| 835 | if (pdata == NULL) { |
| 836 | ret = -ENODEV; |
| 837 | goto err; |
| 838 | } |
| 839 | |
| 840 | master = spi_alloc_master(&pdev->dev, sizeof(struct davinci_spi)); |
| 841 | if (master == NULL) { |
| 842 | ret = -ENOMEM; |
| 843 | goto err; |
| 844 | } |
| 845 | |
| 846 | dev_set_drvdata(&pdev->dev, master); |
| 847 | |
| 848 | davinci_spi = spi_master_get_devdata(master); |
| 849 | if (davinci_spi == NULL) { |
| 850 | ret = -ENOENT; |
| 851 | goto free_master; |
| 852 | } |
| 853 | |
| 854 | r = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 855 | if (r == NULL) { |
| 856 | ret = -ENOENT; |
| 857 | goto free_master; |
| 858 | } |
| 859 | |
| 860 | davinci_spi->pbase = r->start; |
| 861 | davinci_spi->region_size = resource_size(r); |
| 862 | davinci_spi->pdata = pdata; |
| 863 | |
| 864 | mem = request_mem_region(r->start, davinci_spi->region_size, |
| 865 | pdev->name); |
| 866 | if (mem == NULL) { |
| 867 | ret = -EBUSY; |
| 868 | goto free_master; |
| 869 | } |
| 870 | |
Sekhar Nori | 50356dd | 2010-10-08 15:27:26 +0530 | [diff] [blame] | 871 | davinci_spi->base = ioremap(r->start, davinci_spi->region_size); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 872 | if (davinci_spi->base == NULL) { |
| 873 | ret = -ENOMEM; |
| 874 | goto release_region; |
| 875 | } |
| 876 | |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 877 | davinci_spi->irq = platform_get_irq(pdev, 0); |
| 878 | if (davinci_spi->irq <= 0) { |
| 879 | ret = -EINVAL; |
| 880 | goto unmap_io; |
| 881 | } |
| 882 | |
| 883 | ret = request_irq(davinci_spi->irq, davinci_spi_irq, 0, |
| 884 | dev_name(&pdev->dev), davinci_spi); |
| 885 | if (ret) |
| 886 | goto unmap_io; |
| 887 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 888 | davinci_spi->bitbang.master = spi_master_get(master); |
| 889 | if (davinci_spi->bitbang.master == NULL) { |
| 890 | ret = -ENODEV; |
Brian Niebuhr | d3f7141 | 2010-09-29 12:31:54 +0530 | [diff] [blame] | 891 | goto irq_free; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | davinci_spi->clk = clk_get(&pdev->dev, NULL); |
| 895 | if (IS_ERR(davinci_spi->clk)) { |
| 896 | ret = -ENODEV; |
| 897 | goto put_master; |
| 898 | } |
| 899 | clk_enable(davinci_spi->clk); |
| 900 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 901 | master->bus_num = pdev->id; |
| 902 | master->num_chipselect = pdata->num_chipselect; |
| 903 | master->setup = davinci_spi_setup; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 904 | |
| 905 | davinci_spi->bitbang.chipselect = davinci_spi_chipselect; |
| 906 | davinci_spi->bitbang.setup_transfer = davinci_spi_setup_transfer; |
| 907 | |
| 908 | davinci_spi->version = pdata->version; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 909 | |
| 910 | davinci_spi->bitbang.flags = SPI_NO_CS | SPI_LSB_FIRST | SPI_LOOP; |
| 911 | if (davinci_spi->version == SPI_VERSION_2) |
| 912 | davinci_spi->bitbang.flags |= SPI_READY; |
| 913 | |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 914 | r = platform_get_resource(pdev, IORESOURCE_DMA, 0); |
| 915 | if (r) |
| 916 | dma_rx_chan = r->start; |
| 917 | r = platform_get_resource(pdev, IORESOURCE_DMA, 1); |
| 918 | if (r) |
| 919 | dma_tx_chan = r->start; |
| 920 | r = platform_get_resource(pdev, IORESOURCE_DMA, 2); |
| 921 | if (r) |
| 922 | dma_eventq = r->start; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 923 | |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 924 | davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_pio; |
| 925 | if (dma_rx_chan != SPI_NO_RESOURCE && |
| 926 | dma_tx_chan != SPI_NO_RESOURCE && |
| 927 | dma_eventq != SPI_NO_RESOURCE) { |
| 928 | davinci_spi->dma_channels.dma_rx_channel = dma_rx_chan; |
| 929 | davinci_spi->dma_channels.dma_tx_channel = dma_tx_chan; |
Brian Niebuhr | 96fd881 | 2010-09-27 22:23:23 +0530 | [diff] [blame] | 930 | davinci_spi->dma_channels.eventq = dma_eventq; |
| 931 | |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 932 | ret = davinci_spi_request_dma(&davinci_spi->dma_channels); |
| 933 | if (ret) |
| 934 | goto free_clk; |
| 935 | |
| 936 | davinci_spi->bitbang.txrx_bufs = davinci_spi_bufs_dma; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 937 | dev_info(&pdev->dev, "DaVinci SPI driver in EDMA mode\n" |
| 938 | "Using RX channel = %d , TX channel = %d and " |
| 939 | "event queue = %d", dma_rx_chan, dma_tx_chan, |
| 940 | dma_eventq); |
| 941 | } |
| 942 | |
| 943 | davinci_spi->get_rx = davinci_spi_rx_buf_u8; |
| 944 | davinci_spi->get_tx = davinci_spi_tx_buf_u8; |
| 945 | |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 946 | init_completion(&davinci_spi->done); |
| 947 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 948 | /* Reset In/OUT SPI module */ |
| 949 | iowrite32(0, davinci_spi->base + SPIGCR0); |
| 950 | udelay(100); |
| 951 | iowrite32(1, davinci_spi->base + SPIGCR0); |
| 952 | |
Brian Niebuhr | be88471 | 2010-09-03 12:15:28 +0530 | [diff] [blame] | 953 | /* Set up SPIPC0. CS and ENA init is done in davinci_spi_setup */ |
Brian Niebuhr | f34bd4c | 2010-09-03 11:56:35 +0530 | [diff] [blame] | 954 | spipc0 = SPIPC0_DIFUN_MASK | SPIPC0_DOFUN_MASK | SPIPC0_CLKFUN_MASK; |
| 955 | iowrite32(spipc0, davinci_spi->base + SPIPC0); |
| 956 | |
Brian Niebuhr | 2385397 | 2010-08-13 10:57:44 +0530 | [diff] [blame] | 957 | /* initialize chip selects */ |
| 958 | if (pdata->chip_sel) { |
| 959 | for (i = 0; i < pdata->num_chipselect; i++) { |
| 960 | if (pdata->chip_sel[i] != SPI_INTERN_CS) |
| 961 | gpio_direction_output(pdata->chip_sel[i], 1); |
| 962 | } |
| 963 | } |
| 964 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 965 | /* Clock internal */ |
| 966 | if (davinci_spi->pdata->clk_internal) |
| 967 | set_io_bits(davinci_spi->base + SPIGCR1, |
| 968 | SPIGCR1_CLKMOD_MASK); |
| 969 | else |
| 970 | clear_io_bits(davinci_spi->base + SPIGCR1, |
| 971 | SPIGCR1_CLKMOD_MASK); |
| 972 | |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 973 | if (pdata->intr_line) |
| 974 | iowrite32(SPI_INTLVL_1, davinci_spi->base + SPILVL); |
| 975 | else |
| 976 | iowrite32(SPI_INTLVL_0, davinci_spi->base + SPILVL); |
| 977 | |
Brian Niebuhr | 843a713 | 2010-08-12 12:49:05 +0530 | [diff] [blame] | 978 | iowrite32(CS_DEFAULT, davinci_spi->base + SPIDEF); |
| 979 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 980 | /* master mode default */ |
| 981 | set_io_bits(davinci_spi->base + SPIGCR1, SPIGCR1_MASTER_MASK); |
| 982 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 983 | ret = spi_bitbang_start(&davinci_spi->bitbang); |
| 984 | if (ret) |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 985 | goto free_dma; |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 986 | |
Brian Niebuhr | 3b740b1 | 2010-09-03 14:50:07 +0530 | [diff] [blame] | 987 | dev_info(&pdev->dev, "Controller at 0x%p\n", davinci_spi->base); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 988 | |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 989 | return ret; |
| 990 | |
Sekhar Nori | 903ca25 | 2010-10-01 14:51:40 +0530 | [diff] [blame] | 991 | free_dma: |
| 992 | edma_free_channel(davinci_spi->dma_channels.dma_tx_channel); |
| 993 | edma_free_channel(davinci_spi->dma_channels.dma_rx_channel); |
Brian Niebuhr | 523c37e | 2010-10-04 17:35:34 +0530 | [diff] [blame^] | 994 | edma_free_slot(davinci_spi->dma_channels.dummy_param_slot); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 995 | free_clk: |
| 996 | clk_disable(davinci_spi->clk); |
| 997 | clk_put(davinci_spi->clk); |
| 998 | put_master: |
| 999 | spi_master_put(master); |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 1000 | irq_free: |
| 1001 | free_irq(davinci_spi->irq, davinci_spi); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 1002 | unmap_io: |
| 1003 | iounmap(davinci_spi->base); |
| 1004 | release_region: |
| 1005 | release_mem_region(davinci_spi->pbase, davinci_spi->region_size); |
| 1006 | free_master: |
| 1007 | kfree(master); |
| 1008 | err: |
| 1009 | return ret; |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * davinci_spi_remove - remove function for SPI Master Controller |
| 1014 | * @pdev: platform_device structure which contains plateform specific data |
| 1015 | * |
| 1016 | * This function will do the reverse action of davinci_spi_probe function |
| 1017 | * It will free the IRQ and SPI controller's memory region. |
| 1018 | * It will also call spi_bitbang_stop to destroy the work queue which was |
| 1019 | * created by spi_bitbang_start. |
| 1020 | */ |
| 1021 | static int __exit davinci_spi_remove(struct platform_device *pdev) |
| 1022 | { |
| 1023 | struct davinci_spi *davinci_spi; |
| 1024 | struct spi_master *master; |
| 1025 | |
| 1026 | master = dev_get_drvdata(&pdev->dev); |
| 1027 | davinci_spi = spi_master_get_devdata(master); |
| 1028 | |
| 1029 | spi_bitbang_stop(&davinci_spi->bitbang); |
| 1030 | |
| 1031 | clk_disable(davinci_spi->clk); |
| 1032 | clk_put(davinci_spi->clk); |
| 1033 | spi_master_put(master); |
Brian Niebuhr | e0d205e | 2010-09-02 16:52:06 +0530 | [diff] [blame] | 1034 | free_irq(davinci_spi->irq, davinci_spi); |
Sandeep Paulraj | 358934a | 2009-12-16 22:02:18 +0000 | [diff] [blame] | 1035 | iounmap(davinci_spi->base); |
| 1036 | release_mem_region(davinci_spi->pbase, davinci_spi->region_size); |
| 1037 | |
| 1038 | return 0; |
| 1039 | } |
| 1040 | |
| 1041 | static struct platform_driver davinci_spi_driver = { |
| 1042 | .driver.name = "spi_davinci", |
| 1043 | .remove = __exit_p(davinci_spi_remove), |
| 1044 | }; |
| 1045 | |
| 1046 | static int __init davinci_spi_init(void) |
| 1047 | { |
| 1048 | return platform_driver_probe(&davinci_spi_driver, davinci_spi_probe); |
| 1049 | } |
| 1050 | module_init(davinci_spi_init); |
| 1051 | |
| 1052 | static void __exit davinci_spi_exit(void) |
| 1053 | { |
| 1054 | platform_driver_unregister(&davinci_spi_driver); |
| 1055 | } |
| 1056 | module_exit(davinci_spi_exit); |
| 1057 | |
| 1058 | MODULE_DESCRIPTION("TI DaVinci SPI Master Controller Driver"); |
| 1059 | MODULE_LICENSE("GPL"); |