Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 2 | /* |
| 3 | * OMAP7xx SPI 100k controller driver |
| 4 | * Author: Fabrice Crohas <fcrohas@gmail.com> |
| 5 | * from original omap1_mcspi driver |
| 6 | * |
| 7 | * Copyright (C) 2005, 2006 Nokia Corporation |
| 8 | * Author: Samuel Ortiz <samuel.ortiz@nokia.com> and |
Mauro Carvalho Chehab | 6328caf | 2021-05-19 10:15:36 +0200 | [diff] [blame] | 9 | * Juha Yrjola <juha.yrjola@nokia.com> |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 10 | */ |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/init.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/device.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/platform_device.h> |
Mark Brown | db91841 | 2014-12-10 18:16:25 +0000 | [diff] [blame] | 18 | #include <linux/pm_runtime.h> |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 19 | #include <linux/err.h> |
| 20 | #include <linux/clk.h> |
| 21 | #include <linux/io.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 23 | |
| 24 | #include <linux/spi/spi.h> |
| 25 | |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 26 | #define OMAP1_SPI100K_MAX_FREQ 48000000 |
| 27 | |
| 28 | #define ICR_SPITAS (OMAP7XX_ICR_BASE + 0x12) |
| 29 | |
| 30 | #define SPI_SETUP1 0x00 |
| 31 | #define SPI_SETUP2 0x02 |
| 32 | #define SPI_CTRL 0x04 |
| 33 | #define SPI_STATUS 0x06 |
| 34 | #define SPI_TX_LSB 0x08 |
| 35 | #define SPI_TX_MSB 0x0a |
| 36 | #define SPI_RX_LSB 0x0c |
| 37 | #define SPI_RX_MSB 0x0e |
| 38 | |
| 39 | #define SPI_SETUP1_INT_READ_ENABLE (1UL << 5) |
| 40 | #define SPI_SETUP1_INT_WRITE_ENABLE (1UL << 4) |
| 41 | #define SPI_SETUP1_CLOCK_DIVISOR(x) ((x) << 1) |
| 42 | #define SPI_SETUP1_CLOCK_ENABLE (1UL << 0) |
| 43 | |
| 44 | #define SPI_SETUP2_ACTIVE_EDGE_FALLING (0UL << 0) |
| 45 | #define SPI_SETUP2_ACTIVE_EDGE_RISING (1UL << 0) |
| 46 | #define SPI_SETUP2_NEGATIVE_LEVEL (0UL << 5) |
| 47 | #define SPI_SETUP2_POSITIVE_LEVEL (1UL << 5) |
| 48 | #define SPI_SETUP2_LEVEL_TRIGGER (0UL << 10) |
| 49 | #define SPI_SETUP2_EDGE_TRIGGER (1UL << 10) |
| 50 | |
| 51 | #define SPI_CTRL_SEN(x) ((x) << 7) |
| 52 | #define SPI_CTRL_WORD_SIZE(x) (((x) - 1) << 2) |
| 53 | #define SPI_CTRL_WR (1UL << 1) |
| 54 | #define SPI_CTRL_RD (1UL << 0) |
| 55 | |
| 56 | #define SPI_STATUS_WE (1UL << 1) |
| 57 | #define SPI_STATUS_RD (1UL << 0) |
| 58 | |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 59 | /* use PIO for small transfers, avoiding DMA setup/teardown overhead and |
| 60 | * cache operations; better heuristics consider wordsize and bitrate. |
| 61 | */ |
| 62 | #define DMA_MIN_BYTES 8 |
| 63 | |
| 64 | #define SPI_RUNNING 0 |
| 65 | #define SPI_SHUTDOWN 1 |
| 66 | |
| 67 | struct omap1_spi100k { |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 68 | struct clk *ick; |
| 69 | struct clk *fck; |
| 70 | |
| 71 | /* Virtual base address of the controller */ |
| 72 | void __iomem *base; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | struct omap1_spi100k_cs { |
| 76 | void __iomem *base; |
| 77 | int word_len; |
| 78 | }; |
| 79 | |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 80 | static void spi100k_enable_clock(struct spi_master *master) |
| 81 | { |
| 82 | unsigned int val; |
| 83 | struct omap1_spi100k *spi100k = spi_master_get_devdata(master); |
| 84 | |
| 85 | /* enable SPI */ |
| 86 | val = readw(spi100k->base + SPI_SETUP1); |
| 87 | val |= SPI_SETUP1_CLOCK_ENABLE; |
| 88 | writew(val, spi100k->base + SPI_SETUP1); |
| 89 | } |
| 90 | |
| 91 | static void spi100k_disable_clock(struct spi_master *master) |
| 92 | { |
| 93 | unsigned int val; |
| 94 | struct omap1_spi100k *spi100k = spi_master_get_devdata(master); |
| 95 | |
| 96 | /* disable SPI */ |
| 97 | val = readw(spi100k->base + SPI_SETUP1); |
| 98 | val &= ~SPI_SETUP1_CLOCK_ENABLE; |
| 99 | writew(val, spi100k->base + SPI_SETUP1); |
| 100 | } |
| 101 | |
| 102 | static void spi100k_write_data(struct spi_master *master, int len, int data) |
| 103 | { |
| 104 | struct omap1_spi100k *spi100k = spi_master_get_devdata(master); |
| 105 | |
Cory Maccarrone | 5c2818c | 2010-05-30 00:12:23 +0000 | [diff] [blame] | 106 | /* write 16-bit word, shifting 8-bit data if necessary */ |
| 107 | if (len <= 8) { |
| 108 | data <<= 8; |
| 109 | len = 16; |
| 110 | } |
| 111 | |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 112 | spi100k_enable_clock(master); |
Jay Fang | f2edb98 | 2021-03-24 14:16:35 +0800 | [diff] [blame] | 113 | writew(data, spi100k->base + SPI_TX_MSB); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 114 | |
| 115 | writew(SPI_CTRL_SEN(0) | |
| 116 | SPI_CTRL_WORD_SIZE(len) | |
| 117 | SPI_CTRL_WR, |
| 118 | spi100k->base + SPI_CTRL); |
| 119 | |
| 120 | /* Wait for bit ack send change */ |
Jingoo Han | 31804f6 | 2014-02-26 10:27:10 +0900 | [diff] [blame] | 121 | while ((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_WE) != SPI_STATUS_WE) |
| 122 | ; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 123 | udelay(1000); |
| 124 | |
| 125 | spi100k_disable_clock(master); |
| 126 | } |
| 127 | |
| 128 | static int spi100k_read_data(struct spi_master *master, int len) |
| 129 | { |
zhengbin | 944be39 | 2019-10-09 08:37:16 +0800 | [diff] [blame] | 130 | int dataL; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 131 | struct omap1_spi100k *spi100k = spi_master_get_devdata(master); |
| 132 | |
Cory Maccarrone | 5c2818c | 2010-05-30 00:12:23 +0000 | [diff] [blame] | 133 | /* Always do at least 16 bits */ |
| 134 | if (len <= 8) |
| 135 | len = 16; |
| 136 | |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 137 | spi100k_enable_clock(master); |
| 138 | writew(SPI_CTRL_SEN(0) | |
| 139 | SPI_CTRL_WORD_SIZE(len) | |
| 140 | SPI_CTRL_RD, |
| 141 | spi100k->base + SPI_CTRL); |
| 142 | |
Jingoo Han | 31804f6 | 2014-02-26 10:27:10 +0900 | [diff] [blame] | 143 | while ((readw(spi100k->base + SPI_STATUS) & SPI_STATUS_RD) != SPI_STATUS_RD) |
| 144 | ; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 145 | udelay(1000); |
| 146 | |
| 147 | dataL = readw(spi100k->base + SPI_RX_LSB); |
zhengbin | 944be39 | 2019-10-09 08:37:16 +0800 | [diff] [blame] | 148 | readw(spi100k->base + SPI_RX_MSB); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 149 | spi100k_disable_clock(master); |
| 150 | |
| 151 | return dataL; |
| 152 | } |
| 153 | |
| 154 | static void spi100k_open(struct spi_master *master) |
| 155 | { |
| 156 | /* get control of SPI */ |
| 157 | struct omap1_spi100k *spi100k = spi_master_get_devdata(master); |
| 158 | |
| 159 | writew(SPI_SETUP1_INT_READ_ENABLE | |
| 160 | SPI_SETUP1_INT_WRITE_ENABLE | |
| 161 | SPI_SETUP1_CLOCK_DIVISOR(0), spi100k->base + SPI_SETUP1); |
| 162 | |
| 163 | /* configure clock and interrupts */ |
| 164 | writew(SPI_SETUP2_ACTIVE_EDGE_FALLING | |
| 165 | SPI_SETUP2_NEGATIVE_LEVEL | |
| 166 | SPI_SETUP2_LEVEL_TRIGGER, spi100k->base + SPI_SETUP2); |
| 167 | } |
| 168 | |
| 169 | static void omap1_spi100k_force_cs(struct omap1_spi100k *spi100k, int enable) |
| 170 | { |
| 171 | if (enable) |
| 172 | writew(0x05fc, spi100k->base + SPI_CTRL); |
| 173 | else |
| 174 | writew(0x05fd, spi100k->base + SPI_CTRL); |
| 175 | } |
| 176 | |
| 177 | static unsigned |
| 178 | omap1_spi100k_txrx_pio(struct spi_device *spi, struct spi_transfer *xfer) |
| 179 | { |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 180 | struct omap1_spi100k_cs *cs = spi->controller_state; |
| 181 | unsigned int count, c; |
| 182 | int word_len; |
| 183 | |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 184 | count = xfer->len; |
| 185 | c = count; |
| 186 | word_len = cs->word_len; |
| 187 | |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 188 | if (word_len <= 8) { |
| 189 | u8 *rx; |
| 190 | const u8 *tx; |
| 191 | |
| 192 | rx = xfer->rx_buf; |
| 193 | tx = xfer->tx_buf; |
| 194 | do { |
Jingoo Han | 31804f6 | 2014-02-26 10:27:10 +0900 | [diff] [blame] | 195 | c -= 1; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 196 | if (xfer->tx_buf != NULL) |
Cory Maccarrone | 5c2818c | 2010-05-30 00:12:23 +0000 | [diff] [blame] | 197 | spi100k_write_data(spi->master, word_len, *tx++); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 198 | if (xfer->rx_buf != NULL) |
Cory Maccarrone | 5c2818c | 2010-05-30 00:12:23 +0000 | [diff] [blame] | 199 | *rx++ = spi100k_read_data(spi->master, word_len); |
Jingoo Han | 31804f6 | 2014-02-26 10:27:10 +0900 | [diff] [blame] | 200 | } while (c); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 201 | } else if (word_len <= 16) { |
| 202 | u16 *rx; |
| 203 | const u16 *tx; |
| 204 | |
| 205 | rx = xfer->rx_buf; |
| 206 | tx = xfer->tx_buf; |
| 207 | do { |
Jingoo Han | 31804f6 | 2014-02-26 10:27:10 +0900 | [diff] [blame] | 208 | c -= 2; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 209 | if (xfer->tx_buf != NULL) |
Jingoo Han | 31804f6 | 2014-02-26 10:27:10 +0900 | [diff] [blame] | 210 | spi100k_write_data(spi->master, word_len, *tx++); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 211 | if (xfer->rx_buf != NULL) |
Jingoo Han | 31804f6 | 2014-02-26 10:27:10 +0900 | [diff] [blame] | 212 | *rx++ = spi100k_read_data(spi->master, word_len); |
| 213 | } while (c); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 214 | } else if (word_len <= 32) { |
| 215 | u32 *rx; |
| 216 | const u32 *tx; |
| 217 | |
| 218 | rx = xfer->rx_buf; |
| 219 | tx = xfer->tx_buf; |
| 220 | do { |
Jingoo Han | 31804f6 | 2014-02-26 10:27:10 +0900 | [diff] [blame] | 221 | c -= 4; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 222 | if (xfer->tx_buf != NULL) |
Jingoo Han | 31804f6 | 2014-02-26 10:27:10 +0900 | [diff] [blame] | 223 | spi100k_write_data(spi->master, word_len, *tx); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 224 | if (xfer->rx_buf != NULL) |
Jingoo Han | 31804f6 | 2014-02-26 10:27:10 +0900 | [diff] [blame] | 225 | *rx = spi100k_read_data(spi->master, word_len); |
| 226 | } while (c); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 227 | } |
| 228 | return count - c; |
| 229 | } |
| 230 | |
| 231 | /* called only when no transfer is active to this device */ |
| 232 | static int omap1_spi100k_setup_transfer(struct spi_device *spi, |
| 233 | struct spi_transfer *t) |
| 234 | { |
| 235 | struct omap1_spi100k *spi100k = spi_master_get_devdata(spi->master); |
| 236 | struct omap1_spi100k_cs *cs = spi->controller_state; |
Jarkko Nikula | 76f67ea | 2015-09-15 16:26:21 +0300 | [diff] [blame] | 237 | u8 word_len; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 238 | |
Jarkko Nikula | 76f67ea | 2015-09-15 16:26:21 +0300 | [diff] [blame] | 239 | if (t != NULL) |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 240 | word_len = t->bits_per_word; |
Jarkko Nikula | 76f67ea | 2015-09-15 16:26:21 +0300 | [diff] [blame] | 241 | else |
| 242 | word_len = spi->bits_per_word; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 243 | |
Tian Tao | e7a1a3a | 2021-04-29 19:20:48 +0800 | [diff] [blame] | 244 | if (word_len > 32) |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 245 | return -EINVAL; |
| 246 | cs->word_len = word_len; |
| 247 | |
| 248 | /* SPI init before transfer */ |
Jay Fang | f2edb98 | 2021-03-24 14:16:35 +0800 | [diff] [blame] | 249 | writew(0x3e, spi100k->base + SPI_SETUP1); |
| 250 | writew(0x00, spi100k->base + SPI_STATUS); |
| 251 | writew(0x3e, spi100k->base + SPI_CTRL); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /* the spi->mode bits understood by this driver: */ |
| 257 | #define MODEBITS (SPI_CPOL | SPI_CPHA | SPI_CS_HIGH) |
| 258 | |
| 259 | static int omap1_spi100k_setup(struct spi_device *spi) |
| 260 | { |
| 261 | int ret; |
| 262 | struct omap1_spi100k *spi100k; |
| 263 | struct omap1_spi100k_cs *cs = spi->controller_state; |
| 264 | |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 265 | spi100k = spi_master_get_devdata(spi->master); |
| 266 | |
| 267 | if (!cs) { |
Axel Lin | d1c18ca | 2014-03-29 15:03:37 +0800 | [diff] [blame] | 268 | cs = devm_kzalloc(&spi->dev, sizeof(*cs), GFP_KERNEL); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 269 | if (!cs) |
| 270 | return -ENOMEM; |
| 271 | cs->base = spi100k->base + spi->chip_select * 0x14; |
| 272 | spi->controller_state = cs; |
| 273 | } |
| 274 | |
| 275 | spi100k_open(spi->master); |
| 276 | |
Mark Brown | 13cd19e | 2013-07-10 16:09:22 +0100 | [diff] [blame] | 277 | clk_prepare_enable(spi100k->ick); |
| 278 | clk_prepare_enable(spi100k->fck); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 279 | |
| 280 | ret = omap1_spi100k_setup_transfer(spi, NULL); |
| 281 | |
Mark Brown | 13cd19e | 2013-07-10 16:09:22 +0100 | [diff] [blame] | 282 | clk_disable_unprepare(spi100k->ick); |
| 283 | clk_disable_unprepare(spi100k->fck); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | |
Mark Brown | e8153ab | 2013-07-10 15:40:19 +0100 | [diff] [blame] | 288 | static int omap1_spi100k_transfer_one_message(struct spi_master *master, |
| 289 | struct spi_message *m) |
| 290 | { |
| 291 | struct omap1_spi100k *spi100k = spi_master_get_devdata(master); |
| 292 | struct spi_device *spi = m->spi; |
| 293 | struct spi_transfer *t = NULL; |
| 294 | int cs_active = 0; |
Mark Brown | e8153ab | 2013-07-10 15:40:19 +0100 | [diff] [blame] | 295 | int status = 0; |
| 296 | |
| 297 | list_for_each_entry(t, &m->transfers, transfer_list) { |
| 298 | if (t->tx_buf == NULL && t->rx_buf == NULL && t->len) { |
Mark Brown | e8153ab | 2013-07-10 15:40:19 +0100 | [diff] [blame] | 299 | break; |
| 300 | } |
Jarkko Nikula | 76f67ea | 2015-09-15 16:26:21 +0300 | [diff] [blame] | 301 | status = omap1_spi100k_setup_transfer(spi, t); |
| 302 | if (status < 0) |
| 303 | break; |
Mark Brown | e8153ab | 2013-07-10 15:40:19 +0100 | [diff] [blame] | 304 | |
| 305 | if (!cs_active) { |
| 306 | omap1_spi100k_force_cs(spi100k, 1); |
| 307 | cs_active = 1; |
| 308 | } |
| 309 | |
| 310 | if (t->len) { |
| 311 | unsigned count; |
| 312 | |
| 313 | count = omap1_spi100k_txrx_pio(spi, t); |
| 314 | m->actual_length += count; |
| 315 | |
| 316 | if (count != t->len) { |
Mark Brown | e8153ab | 2013-07-10 15:40:19 +0100 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | } |
| 320 | |
Alexandru Ardelean | e74dc5c | 2019-09-26 13:51:37 +0300 | [diff] [blame] | 321 | spi_transfer_delay_exec(t); |
Mark Brown | e8153ab | 2013-07-10 15:40:19 +0100 | [diff] [blame] | 322 | |
| 323 | /* ignore the "leave it on after last xfer" hint */ |
| 324 | |
| 325 | if (t->cs_change) { |
| 326 | omap1_spi100k_force_cs(spi100k, 0); |
| 327 | cs_active = 0; |
| 328 | } |
| 329 | } |
| 330 | |
Jarkko Nikula | 76f67ea | 2015-09-15 16:26:21 +0300 | [diff] [blame] | 331 | status = omap1_spi100k_setup_transfer(spi, NULL); |
Mark Brown | e8153ab | 2013-07-10 15:40:19 +0100 | [diff] [blame] | 332 | |
| 333 | if (cs_active) |
| 334 | omap1_spi100k_force_cs(spi100k, 0); |
| 335 | |
| 336 | m->status = status; |
Mark Brown | da60b85 | 2013-07-10 15:52:11 +0100 | [diff] [blame] | 337 | |
| 338 | spi_finalize_current_message(master); |
Mark Brown | e8153ab | 2013-07-10 15:40:19 +0100 | [diff] [blame] | 339 | |
| 340 | return status; |
| 341 | } |
| 342 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 343 | static int omap1_spi100k_probe(struct platform_device *pdev) |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 344 | { |
| 345 | struct spi_master *master; |
| 346 | struct omap1_spi100k *spi100k; |
| 347 | int status = 0; |
| 348 | |
| 349 | if (!pdev->id) |
| 350 | return -EINVAL; |
| 351 | |
Jingoo Han | 31804f6 | 2014-02-26 10:27:10 +0900 | [diff] [blame] | 352 | master = spi_alloc_master(&pdev->dev, sizeof(*spi100k)); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 353 | if (master == NULL) { |
| 354 | dev_dbg(&pdev->dev, "master allocation failed\n"); |
| 355 | return -ENOMEM; |
| 356 | } |
| 357 | |
| 358 | if (pdev->id != -1) |
Jingoo Han | 31804f6 | 2014-02-26 10:27:10 +0900 | [diff] [blame] | 359 | master->bus_num = pdev->id; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 360 | |
| 361 | master->setup = omap1_spi100k_setup; |
Mark Brown | da60b85 | 2013-07-10 15:52:11 +0100 | [diff] [blame] | 362 | master->transfer_one_message = omap1_spi100k_transfer_one_message; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 363 | master->num_chipselect = 2; |
| 364 | master->mode_bits = MODEBITS; |
Stephen Warren | 24778be | 2013-05-21 20:36:35 -0600 | [diff] [blame] | 365 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32); |
Mark Brown | 69ea672 | 2013-07-10 15:06:46 +0100 | [diff] [blame] | 366 | master->min_speed_hz = OMAP1_SPI100K_MAX_FREQ/(1<<16); |
| 367 | master->max_speed_hz = OMAP1_SPI100K_MAX_FREQ; |
Mark Brown | db91841 | 2014-12-10 18:16:25 +0000 | [diff] [blame] | 368 | master->auto_runtime_pm = true; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 369 | |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 370 | spi100k = spi_master_get_devdata(master); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 371 | |
| 372 | /* |
| 373 | * The memory region base address is taken as the platform_data. |
| 374 | * You should allocate this with ioremap() before initializing |
| 375 | * the SPI. |
| 376 | */ |
Jingoo Han | 8074cf0 | 2013-07-30 16:58:59 +0900 | [diff] [blame] | 377 | spi100k->base = (void __iomem *)dev_get_platdata(&pdev->dev); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 378 | |
Mark Brown | 022a941 | 2013-07-10 16:07:51 +0100 | [diff] [blame] | 379 | spi100k->ick = devm_clk_get(&pdev->dev, "ick"); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 380 | if (IS_ERR(spi100k->ick)) { |
| 381 | dev_dbg(&pdev->dev, "can't get spi100k_ick\n"); |
| 382 | status = PTR_ERR(spi100k->ick); |
Mark Brown | 022a941 | 2013-07-10 16:07:51 +0100 | [diff] [blame] | 383 | goto err; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Mark Brown | 022a941 | 2013-07-10 16:07:51 +0100 | [diff] [blame] | 386 | spi100k->fck = devm_clk_get(&pdev->dev, "fck"); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 387 | if (IS_ERR(spi100k->fck)) { |
| 388 | dev_dbg(&pdev->dev, "can't get spi100k_fck\n"); |
| 389 | status = PTR_ERR(spi100k->fck); |
Mark Brown | 022a941 | 2013-07-10 16:07:51 +0100 | [diff] [blame] | 390 | goto err; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Mark Brown | db91841 | 2014-12-10 18:16:25 +0000 | [diff] [blame] | 393 | status = clk_prepare_enable(spi100k->ick); |
| 394 | if (status != 0) { |
| 395 | dev_err(&pdev->dev, "failed to enable ick: %d\n", status); |
| 396 | goto err; |
| 397 | } |
| 398 | |
| 399 | status = clk_prepare_enable(spi100k->fck); |
| 400 | if (status != 0) { |
| 401 | dev_err(&pdev->dev, "failed to enable fck: %d\n", status); |
| 402 | goto err_ick; |
| 403 | } |
| 404 | |
| 405 | pm_runtime_enable(&pdev->dev); |
| 406 | pm_runtime_set_active(&pdev->dev); |
| 407 | |
Jingoo Han | 5c4c5c7 | 2013-09-24 13:37:23 +0900 | [diff] [blame] | 408 | status = devm_spi_register_master(&pdev->dev, master); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 409 | if (status < 0) |
Mark Brown | db91841 | 2014-12-10 18:16:25 +0000 | [diff] [blame] | 410 | goto err_fck; |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 411 | |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 412 | return status; |
| 413 | |
Mark Brown | db91841 | 2014-12-10 18:16:25 +0000 | [diff] [blame] | 414 | err_fck: |
| 415 | clk_disable_unprepare(spi100k->fck); |
| 416 | err_ick: |
| 417 | clk_disable_unprepare(spi100k->ick); |
Mark Brown | 022a941 | 2013-07-10 16:07:51 +0100 | [diff] [blame] | 418 | err: |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 419 | spi_master_put(master); |
| 420 | return status; |
| 421 | } |
| 422 | |
Mark Brown | db91841 | 2014-12-10 18:16:25 +0000 | [diff] [blame] | 423 | static int omap1_spi100k_remove(struct platform_device *pdev) |
| 424 | { |
Wei Yongjun | a23faea | 2021-04-09 08:29:54 +0000 | [diff] [blame] | 425 | struct spi_master *master = platform_get_drvdata(pdev); |
Mark Brown | db91841 | 2014-12-10 18:16:25 +0000 | [diff] [blame] | 426 | struct omap1_spi100k *spi100k = spi_master_get_devdata(master); |
| 427 | |
| 428 | pm_runtime_disable(&pdev->dev); |
| 429 | |
| 430 | clk_disable_unprepare(spi100k->fck); |
| 431 | clk_disable_unprepare(spi100k->ick); |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | #ifdef CONFIG_PM |
| 437 | static int omap1_spi100k_runtime_suspend(struct device *dev) |
| 438 | { |
Wei Yongjun | a23faea | 2021-04-09 08:29:54 +0000 | [diff] [blame] | 439 | struct spi_master *master = dev_get_drvdata(dev); |
Mark Brown | db91841 | 2014-12-10 18:16:25 +0000 | [diff] [blame] | 440 | struct omap1_spi100k *spi100k = spi_master_get_devdata(master); |
| 441 | |
| 442 | clk_disable_unprepare(spi100k->ick); |
| 443 | clk_disable_unprepare(spi100k->fck); |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static int omap1_spi100k_runtime_resume(struct device *dev) |
| 449 | { |
Wei Yongjun | a23faea | 2021-04-09 08:29:54 +0000 | [diff] [blame] | 450 | struct spi_master *master = dev_get_drvdata(dev); |
Mark Brown | db91841 | 2014-12-10 18:16:25 +0000 | [diff] [blame] | 451 | struct omap1_spi100k *spi100k = spi_master_get_devdata(master); |
| 452 | int ret; |
| 453 | |
| 454 | ret = clk_prepare_enable(spi100k->ick); |
| 455 | if (ret != 0) { |
| 456 | dev_err(dev, "Failed to enable ick: %d\n", ret); |
| 457 | return ret; |
| 458 | } |
| 459 | |
| 460 | ret = clk_prepare_enable(spi100k->fck); |
| 461 | if (ret != 0) { |
| 462 | dev_err(dev, "Failed to enable fck: %d\n", ret); |
| 463 | clk_disable_unprepare(spi100k->ick); |
| 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | #endif |
| 470 | |
| 471 | static const struct dev_pm_ops omap1_spi100k_pm = { |
| 472 | SET_RUNTIME_PM_OPS(omap1_spi100k_runtime_suspend, |
| 473 | omap1_spi100k_runtime_resume, NULL) |
| 474 | }; |
| 475 | |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 476 | static struct platform_driver omap1_spi100k_driver = { |
| 477 | .driver = { |
| 478 | .name = "omap1_spi100k", |
Mark Brown | db91841 | 2014-12-10 18:16:25 +0000 | [diff] [blame] | 479 | .pm = &omap1_spi100k_pm, |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 480 | }, |
Mark Brown | 2d0c614 | 2013-07-10 16:10:33 +0100 | [diff] [blame] | 481 | .probe = omap1_spi100k_probe, |
Mark Brown | db91841 | 2014-12-10 18:16:25 +0000 | [diff] [blame] | 482 | .remove = omap1_spi100k_remove, |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 483 | }; |
| 484 | |
Mark Brown | 2d0c614 | 2013-07-10 16:10:33 +0100 | [diff] [blame] | 485 | module_platform_driver(omap1_spi100k_driver); |
Cory Maccarrone | 35c9049 | 2009-12-13 01:02:11 -0700 | [diff] [blame] | 486 | |
| 487 | MODULE_DESCRIPTION("OMAP7xx SPI 100k controller driver"); |
| 488 | MODULE_AUTHOR("Fabrice Crohas <fcrohas@gmail.com>"); |
| 489 | MODULE_LICENSE("GPL"); |