Fabio Estevam | 6126fd8 | 2018-05-02 16:18:29 -0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // Freescale i.MX7ULP LPSPI driver |
| 4 | // |
| 5 | // Copyright 2016 Freescale Semiconductor, Inc. |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 6 | // Copyright 2018 NXP Semiconductors |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 7 | |
| 8 | #include <linux/clk.h> |
| 9 | #include <linux/completion.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/err.h> |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 12 | #include <linux/gpio.h> |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/io.h> |
| 15 | #include <linux/irq.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/of.h> |
| 19 | #include <linux/of_device.h> |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 20 | #include <linux/of_gpio.h> |
Han Xu | 944c01a | 2019-03-06 06:30:39 +0000 | [diff] [blame] | 21 | #include <linux/pinctrl/consumer.h> |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 23 | #include <linux/platform_data/spi-imx.h> |
Han Xu | 944c01a | 2019-03-06 06:30:39 +0000 | [diff] [blame] | 24 | #include <linux/pm_runtime.h> |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 25 | #include <linux/slab.h> |
| 26 | #include <linux/spi/spi.h> |
| 27 | #include <linux/spi/spi_bitbang.h> |
| 28 | #include <linux/types.h> |
| 29 | |
| 30 | #define DRIVER_NAME "fsl_lpspi" |
| 31 | |
Han Xu | 944c01a | 2019-03-06 06:30:39 +0000 | [diff] [blame] | 32 | #define FSL_LPSPI_RPM_TIMEOUT 50 /* 50ms */ |
| 33 | |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 34 | /* i.MX7ULP LPSPI registers */ |
| 35 | #define IMX7ULP_VERID 0x0 |
| 36 | #define IMX7ULP_PARAM 0x4 |
| 37 | #define IMX7ULP_CR 0x10 |
| 38 | #define IMX7ULP_SR 0x14 |
| 39 | #define IMX7ULP_IER 0x18 |
| 40 | #define IMX7ULP_DER 0x1c |
| 41 | #define IMX7ULP_CFGR0 0x20 |
| 42 | #define IMX7ULP_CFGR1 0x24 |
| 43 | #define IMX7ULP_DMR0 0x30 |
| 44 | #define IMX7ULP_DMR1 0x34 |
| 45 | #define IMX7ULP_CCR 0x40 |
| 46 | #define IMX7ULP_FCR 0x58 |
| 47 | #define IMX7ULP_FSR 0x5c |
| 48 | #define IMX7ULP_TCR 0x60 |
| 49 | #define IMX7ULP_TDR 0x64 |
| 50 | #define IMX7ULP_RSR 0x70 |
| 51 | #define IMX7ULP_RDR 0x74 |
| 52 | |
| 53 | /* General control register field define */ |
| 54 | #define CR_RRF BIT(9) |
| 55 | #define CR_RTF BIT(8) |
| 56 | #define CR_RST BIT(1) |
| 57 | #define CR_MEN BIT(0) |
Clark Wang | 6a13044 | 2019-01-07 07:47:41 +0000 | [diff] [blame] | 58 | #define SR_MBF BIT(24) |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 59 | #define SR_TCF BIT(10) |
Clark Wang | c23fdef | 2019-01-07 07:47:38 +0000 | [diff] [blame] | 60 | #define SR_FCF BIT(9) |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 61 | #define SR_RDF BIT(1) |
| 62 | #define SR_TDF BIT(0) |
| 63 | #define IER_TCIE BIT(10) |
Clark Wang | c23fdef | 2019-01-07 07:47:38 +0000 | [diff] [blame] | 64 | #define IER_FCIE BIT(9) |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 65 | #define IER_RDIE BIT(1) |
| 66 | #define IER_TDIE BIT(0) |
| 67 | #define CFGR1_PCSCFG BIT(27) |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 68 | #define CFGR1_PINCFG (BIT(24)|BIT(25)) |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 69 | #define CFGR1_PCSPOL BIT(8) |
| 70 | #define CFGR1_NOSTALL BIT(3) |
| 71 | #define CFGR1_MASTER BIT(0) |
Clark Wang | 6a13044 | 2019-01-07 07:47:41 +0000 | [diff] [blame] | 72 | #define FSR_RXCOUNT (BIT(16)|BIT(17)|BIT(18)) |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 73 | #define RSR_RXEMPTY BIT(1) |
| 74 | #define TCR_CPOL BIT(31) |
| 75 | #define TCR_CPHA BIT(30) |
| 76 | #define TCR_CONT BIT(21) |
| 77 | #define TCR_CONTC BIT(20) |
| 78 | #define TCR_RXMSK BIT(19) |
| 79 | #define TCR_TXMSK BIT(18) |
| 80 | |
| 81 | static int clkdivs[] = {1, 2, 4, 8, 16, 32, 64, 128}; |
| 82 | |
| 83 | struct lpspi_config { |
| 84 | u8 bpw; |
| 85 | u8 chip_select; |
| 86 | u8 prescale; |
| 87 | u16 mode; |
| 88 | u32 speed_hz; |
| 89 | }; |
| 90 | |
| 91 | struct fsl_lpspi_data { |
| 92 | struct device *dev; |
| 93 | void __iomem *base; |
Clark Wang | f5e5afd | 2019-03-06 06:30:34 +0000 | [diff] [blame] | 94 | struct clk *clk_ipg; |
| 95 | struct clk *clk_per; |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 96 | bool is_slave; |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 97 | bool is_first_byte; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 98 | |
| 99 | void *rx_buf; |
| 100 | const void *tx_buf; |
| 101 | void (*tx)(struct fsl_lpspi_data *); |
| 102 | void (*rx)(struct fsl_lpspi_data *); |
| 103 | |
| 104 | u32 remain; |
Clark Wang | cf86874 | 2018-12-07 02:50:38 +0000 | [diff] [blame] | 105 | u8 watermark; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 106 | u8 txfifosize; |
| 107 | u8 rxfifosize; |
| 108 | |
| 109 | struct lpspi_config config; |
| 110 | struct completion xfer_done; |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 111 | |
| 112 | bool slave_aborted; |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 113 | |
| 114 | int chipselect[0]; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static const struct of_device_id fsl_lpspi_dt_ids[] = { |
| 118 | { .compatible = "fsl,imx7ulp-spi", }, |
| 119 | { /* sentinel */ } |
| 120 | }; |
| 121 | MODULE_DEVICE_TABLE(of, fsl_lpspi_dt_ids); |
| 122 | |
| 123 | #define LPSPI_BUF_RX(type) \ |
| 124 | static void fsl_lpspi_buf_rx_##type(struct fsl_lpspi_data *fsl_lpspi) \ |
| 125 | { \ |
| 126 | unsigned int val = readl(fsl_lpspi->base + IMX7ULP_RDR); \ |
| 127 | \ |
| 128 | if (fsl_lpspi->rx_buf) { \ |
| 129 | *(type *)fsl_lpspi->rx_buf = val; \ |
| 130 | fsl_lpspi->rx_buf += sizeof(type); \ |
| 131 | } \ |
| 132 | } |
| 133 | |
| 134 | #define LPSPI_BUF_TX(type) \ |
| 135 | static void fsl_lpspi_buf_tx_##type(struct fsl_lpspi_data *fsl_lpspi) \ |
| 136 | { \ |
| 137 | type val = 0; \ |
| 138 | \ |
| 139 | if (fsl_lpspi->tx_buf) { \ |
| 140 | val = *(type *)fsl_lpspi->tx_buf; \ |
| 141 | fsl_lpspi->tx_buf += sizeof(type); \ |
| 142 | } \ |
| 143 | \ |
| 144 | fsl_lpspi->remain -= sizeof(type); \ |
| 145 | writel(val, fsl_lpspi->base + IMX7ULP_TDR); \ |
| 146 | } |
| 147 | |
| 148 | LPSPI_BUF_RX(u8) |
| 149 | LPSPI_BUF_TX(u8) |
| 150 | LPSPI_BUF_RX(u16) |
| 151 | LPSPI_BUF_TX(u16) |
| 152 | LPSPI_BUF_RX(u32) |
| 153 | LPSPI_BUF_TX(u32) |
| 154 | |
| 155 | static void fsl_lpspi_intctrl(struct fsl_lpspi_data *fsl_lpspi, |
| 156 | unsigned int enable) |
| 157 | { |
| 158 | writel(enable, fsl_lpspi->base + IMX7ULP_IER); |
| 159 | } |
| 160 | |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 161 | static int lpspi_prepare_xfer_hardware(struct spi_controller *controller) |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 162 | { |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 163 | struct fsl_lpspi_data *fsl_lpspi = |
| 164 | spi_controller_get_devdata(controller); |
Clark Wang | f5e5afd | 2019-03-06 06:30:34 +0000 | [diff] [blame] | 165 | int ret; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 166 | |
Han Xu | 944c01a | 2019-03-06 06:30:39 +0000 | [diff] [blame] | 167 | ret = pm_runtime_get_sync(fsl_lpspi->dev); |
| 168 | if (ret < 0) { |
| 169 | dev_err(fsl_lpspi->dev, "failed to enable clock\n"); |
Clark Wang | f5e5afd | 2019-03-06 06:30:34 +0000 | [diff] [blame] | 170 | return ret; |
| 171 | } |
| 172 | |
| 173 | return 0; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 174 | } |
| 175 | |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 176 | static int lpspi_unprepare_xfer_hardware(struct spi_controller *controller) |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 177 | { |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 178 | struct fsl_lpspi_data *fsl_lpspi = |
| 179 | spi_controller_get_devdata(controller); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 180 | |
Han Xu | 944c01a | 2019-03-06 06:30:39 +0000 | [diff] [blame] | 181 | pm_runtime_mark_last_busy(fsl_lpspi->dev); |
| 182 | pm_runtime_put_autosuspend(fsl_lpspi->dev); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 187 | static int fsl_lpspi_prepare_message(struct spi_controller *controller, |
| 188 | struct spi_message *msg) |
| 189 | { |
| 190 | struct fsl_lpspi_data *fsl_lpspi = |
| 191 | spi_controller_get_devdata(controller); |
| 192 | struct spi_device *spi = msg->spi; |
| 193 | int gpio = fsl_lpspi->chipselect[spi->chip_select]; |
| 194 | |
| 195 | if (gpio_is_valid(gpio)) |
| 196 | gpio_direction_output(gpio, spi->mode & SPI_CS_HIGH ? 0 : 1); |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 201 | static void fsl_lpspi_write_tx_fifo(struct fsl_lpspi_data *fsl_lpspi) |
| 202 | { |
| 203 | u8 txfifo_cnt; |
Clark Wang | c23fdef | 2019-01-07 07:47:38 +0000 | [diff] [blame] | 204 | u32 temp; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 205 | |
| 206 | txfifo_cnt = readl(fsl_lpspi->base + IMX7ULP_FSR) & 0xff; |
| 207 | |
| 208 | while (txfifo_cnt < fsl_lpspi->txfifosize) { |
| 209 | if (!fsl_lpspi->remain) |
| 210 | break; |
| 211 | fsl_lpspi->tx(fsl_lpspi); |
| 212 | txfifo_cnt++; |
| 213 | } |
| 214 | |
Clark Wang | c23fdef | 2019-01-07 07:47:38 +0000 | [diff] [blame] | 215 | if (txfifo_cnt < fsl_lpspi->txfifosize) { |
| 216 | if (!fsl_lpspi->is_slave) { |
| 217 | temp = readl(fsl_lpspi->base + IMX7ULP_TCR); |
| 218 | temp &= ~TCR_CONTC; |
| 219 | writel(temp, fsl_lpspi->base + IMX7ULP_TCR); |
| 220 | } |
| 221 | |
| 222 | fsl_lpspi_intctrl(fsl_lpspi, IER_FCIE); |
| 223 | } else |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 224 | fsl_lpspi_intctrl(fsl_lpspi, IER_TDIE); |
| 225 | } |
| 226 | |
| 227 | static void fsl_lpspi_read_rx_fifo(struct fsl_lpspi_data *fsl_lpspi) |
| 228 | { |
| 229 | while (!(readl(fsl_lpspi->base + IMX7ULP_RSR) & RSR_RXEMPTY)) |
| 230 | fsl_lpspi->rx(fsl_lpspi); |
| 231 | } |
| 232 | |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 233 | static void fsl_lpspi_set_cmd(struct fsl_lpspi_data *fsl_lpspi) |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 234 | { |
| 235 | u32 temp = 0; |
| 236 | |
| 237 | temp |= fsl_lpspi->config.bpw - 1; |
Gao Pan | e3a4939 | 2016-11-24 19:04:43 +0800 | [diff] [blame] | 238 | temp |= (fsl_lpspi->config.mode & 0x3) << 30; |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 239 | if (!fsl_lpspi->is_slave) { |
| 240 | temp |= fsl_lpspi->config.prescale << 27; |
| 241 | temp |= (fsl_lpspi->config.chip_select & 0x3) << 24; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 242 | |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 243 | /* |
| 244 | * Set TCR_CONT will keep SS asserted after current transfer. |
| 245 | * For the first transfer, clear TCR_CONTC to assert SS. |
| 246 | * For subsequent transfer, set TCR_CONTC to keep SS asserted. |
| 247 | */ |
| 248 | temp |= TCR_CONT; |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 249 | if (fsl_lpspi->is_first_byte) |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 250 | temp &= ~TCR_CONTC; |
| 251 | else |
| 252 | temp |= TCR_CONTC; |
| 253 | } |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 254 | writel(temp, fsl_lpspi->base + IMX7ULP_TCR); |
| 255 | |
| 256 | dev_dbg(fsl_lpspi->dev, "TCR=0x%x\n", temp); |
| 257 | } |
| 258 | |
| 259 | static void fsl_lpspi_set_watermark(struct fsl_lpspi_data *fsl_lpspi) |
| 260 | { |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 261 | u32 temp; |
| 262 | |
Clark Wang | cf86874 | 2018-12-07 02:50:38 +0000 | [diff] [blame] | 263 | temp = fsl_lpspi->watermark >> 1 | (fsl_lpspi->watermark >> 1) << 16; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 264 | |
| 265 | writel(temp, fsl_lpspi->base + IMX7ULP_FCR); |
| 266 | |
| 267 | dev_dbg(fsl_lpspi->dev, "FCR=0x%x\n", temp); |
| 268 | } |
| 269 | |
| 270 | static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi) |
| 271 | { |
| 272 | struct lpspi_config config = fsl_lpspi->config; |
| 273 | unsigned int perclk_rate, scldiv; |
| 274 | u8 prescale; |
| 275 | |
Clark Wang | f5e5afd | 2019-03-06 06:30:34 +0000 | [diff] [blame] | 276 | perclk_rate = clk_get_rate(fsl_lpspi->clk_per); |
Clark Wang | 77736a9 | 2019-03-06 06:30:41 +0000 | [diff] [blame] | 277 | |
| 278 | if (config.speed_hz > perclk_rate / 2) { |
| 279 | dev_err(fsl_lpspi->dev, |
| 280 | "per-clk should be at least two times of transfer speed"); |
| 281 | return -EINVAL; |
| 282 | } |
| 283 | |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 284 | for (prescale = 0; prescale < 8; prescale++) { |
| 285 | scldiv = perclk_rate / |
| 286 | (clkdivs[prescale] * config.speed_hz) - 2; |
| 287 | if (scldiv < 256) { |
| 288 | fsl_lpspi->config.prescale = prescale; |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | if (prescale == 8 && scldiv >= 256) |
| 294 | return -EINVAL; |
| 295 | |
Clark Wang | cf86874 | 2018-12-07 02:50:38 +0000 | [diff] [blame] | 296 | writel(scldiv | (scldiv << 8) | ((scldiv >> 1) << 16), |
| 297 | fsl_lpspi->base + IMX7ULP_CCR); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 298 | |
| 299 | dev_dbg(fsl_lpspi->dev, "perclk=%d, speed=%d, prescale =%d, scldiv=%d\n", |
| 300 | perclk_rate, config.speed_hz, prescale, scldiv); |
| 301 | |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | static int fsl_lpspi_config(struct fsl_lpspi_data *fsl_lpspi) |
| 306 | { |
| 307 | u32 temp; |
| 308 | int ret; |
| 309 | |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 310 | if (!fsl_lpspi->is_slave) { |
| 311 | ret = fsl_lpspi_set_bitrate(fsl_lpspi); |
| 312 | if (ret) |
| 313 | return ret; |
| 314 | } |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 315 | |
| 316 | fsl_lpspi_set_watermark(fsl_lpspi); |
| 317 | |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 318 | if (!fsl_lpspi->is_slave) |
| 319 | temp = CFGR1_MASTER; |
| 320 | else |
| 321 | temp = CFGR1_PINCFG; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 322 | if (fsl_lpspi->config.mode & SPI_CS_HIGH) |
| 323 | temp |= CFGR1_PCSPOL; |
| 324 | writel(temp, fsl_lpspi->base + IMX7ULP_CFGR1); |
| 325 | |
| 326 | temp = readl(fsl_lpspi->base + IMX7ULP_CR); |
| 327 | temp |= CR_RRF | CR_RTF | CR_MEN; |
| 328 | writel(temp, fsl_lpspi->base + IMX7ULP_CR); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
Clark Wang | 77736a9 | 2019-03-06 06:30:41 +0000 | [diff] [blame] | 333 | static int fsl_lpspi_setup_transfer(struct spi_device *spi, |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 334 | struct spi_transfer *t) |
| 335 | { |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 336 | struct fsl_lpspi_data *fsl_lpspi = |
| 337 | spi_controller_get_devdata(spi->controller); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 338 | |
| 339 | fsl_lpspi->config.mode = spi->mode; |
| 340 | fsl_lpspi->config.bpw = t ? t->bits_per_word : spi->bits_per_word; |
| 341 | fsl_lpspi->config.speed_hz = t ? t->speed_hz : spi->max_speed_hz; |
| 342 | fsl_lpspi->config.chip_select = spi->chip_select; |
| 343 | |
| 344 | if (!fsl_lpspi->config.speed_hz) |
| 345 | fsl_lpspi->config.speed_hz = spi->max_speed_hz; |
| 346 | if (!fsl_lpspi->config.bpw) |
| 347 | fsl_lpspi->config.bpw = spi->bits_per_word; |
| 348 | |
| 349 | /* Initialize the functions for transfer */ |
| 350 | if (fsl_lpspi->config.bpw <= 8) { |
| 351 | fsl_lpspi->rx = fsl_lpspi_buf_rx_u8; |
| 352 | fsl_lpspi->tx = fsl_lpspi_buf_tx_u8; |
| 353 | } else if (fsl_lpspi->config.bpw <= 16) { |
| 354 | fsl_lpspi->rx = fsl_lpspi_buf_rx_u16; |
| 355 | fsl_lpspi->tx = fsl_lpspi_buf_tx_u16; |
| 356 | } else { |
| 357 | fsl_lpspi->rx = fsl_lpspi_buf_rx_u32; |
| 358 | fsl_lpspi->tx = fsl_lpspi_buf_tx_u32; |
| 359 | } |
| 360 | |
Clark Wang | cf86874 | 2018-12-07 02:50:38 +0000 | [diff] [blame] | 361 | if (t->len <= fsl_lpspi->txfifosize) |
| 362 | fsl_lpspi->watermark = t->len; |
| 363 | else |
| 364 | fsl_lpspi->watermark = fsl_lpspi->txfifosize; |
| 365 | |
Clark Wang | 77736a9 | 2019-03-06 06:30:41 +0000 | [diff] [blame] | 366 | return fsl_lpspi_config(fsl_lpspi); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 367 | } |
| 368 | |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 369 | static int fsl_lpspi_slave_abort(struct spi_controller *controller) |
| 370 | { |
| 371 | struct fsl_lpspi_data *fsl_lpspi = |
| 372 | spi_controller_get_devdata(controller); |
| 373 | |
| 374 | fsl_lpspi->slave_aborted = true; |
| 375 | complete(&fsl_lpspi->xfer_done); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static int fsl_lpspi_wait_for_completion(struct spi_controller *controller) |
| 380 | { |
| 381 | struct fsl_lpspi_data *fsl_lpspi = |
| 382 | spi_controller_get_devdata(controller); |
| 383 | |
| 384 | if (fsl_lpspi->is_slave) { |
| 385 | if (wait_for_completion_interruptible(&fsl_lpspi->xfer_done) || |
| 386 | fsl_lpspi->slave_aborted) { |
| 387 | dev_dbg(fsl_lpspi->dev, "interrupted\n"); |
| 388 | return -EINTR; |
| 389 | } |
| 390 | } else { |
| 391 | if (!wait_for_completion_timeout(&fsl_lpspi->xfer_done, HZ)) { |
| 392 | dev_dbg(fsl_lpspi->dev, "wait for completion timeout\n"); |
| 393 | return -ETIMEDOUT; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
Clark Wang | a15dc3d | 2019-01-07 07:47:43 +0000 | [diff] [blame] | 400 | static int fsl_lpspi_reset(struct fsl_lpspi_data *fsl_lpspi) |
| 401 | { |
| 402 | u32 temp; |
| 403 | |
| 404 | /* Disable all interrupt */ |
| 405 | fsl_lpspi_intctrl(fsl_lpspi, 0); |
| 406 | |
| 407 | /* W1C for all flags in SR */ |
| 408 | temp = 0x3F << 8; |
| 409 | writel(temp, fsl_lpspi->base + IMX7ULP_SR); |
| 410 | |
| 411 | /* Clear FIFO and disable module */ |
| 412 | temp = CR_RRF | CR_RTF; |
| 413 | writel(temp, fsl_lpspi->base + IMX7ULP_CR); |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 418 | static int fsl_lpspi_pio_transfer(struct spi_controller *controller, |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 419 | struct spi_transfer *t) |
| 420 | { |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 421 | struct fsl_lpspi_data *fsl_lpspi = |
| 422 | spi_controller_get_devdata(controller); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 423 | int ret; |
| 424 | |
| 425 | fsl_lpspi->tx_buf = t->tx_buf; |
| 426 | fsl_lpspi->rx_buf = t->rx_buf; |
| 427 | fsl_lpspi->remain = t->len; |
| 428 | |
| 429 | reinit_completion(&fsl_lpspi->xfer_done); |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 430 | fsl_lpspi->slave_aborted = false; |
| 431 | |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 432 | fsl_lpspi_write_tx_fifo(fsl_lpspi); |
Gao Pan | d2ad0a62 | 2016-11-28 11:02:59 +0800 | [diff] [blame] | 433 | |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 434 | ret = fsl_lpspi_wait_for_completion(controller); |
| 435 | if (ret) |
| 436 | return ret; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 437 | |
Clark Wang | a15dc3d | 2019-01-07 07:47:43 +0000 | [diff] [blame] | 438 | fsl_lpspi_reset(fsl_lpspi); |
| 439 | |
Gao Pan | d989eed | 2016-12-02 11:50:01 +0800 | [diff] [blame] | 440 | return 0; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 441 | } |
| 442 | |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 443 | static int fsl_lpspi_transfer_one(struct spi_controller *controller, |
| 444 | struct spi_device *spi, |
| 445 | struct spi_transfer *t) |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 446 | { |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 447 | struct fsl_lpspi_data *fsl_lpspi = |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 448 | spi_controller_get_devdata(controller); |
| 449 | int ret; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 450 | |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 451 | fsl_lpspi->is_first_byte = true; |
| 452 | ret = fsl_lpspi_setup_transfer(spi, t); |
| 453 | if (ret < 0) |
| 454 | return ret; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 455 | |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 456 | fsl_lpspi_set_cmd(fsl_lpspi); |
| 457 | fsl_lpspi->is_first_byte = false; |
Clark Wang | 77736a9 | 2019-03-06 06:30:41 +0000 | [diff] [blame] | 458 | |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 459 | ret = fsl_lpspi_pio_transfer(controller, t); |
| 460 | if (ret < 0) |
| 461 | return ret; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 462 | |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 463 | return 0; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | static irqreturn_t fsl_lpspi_isr(int irq, void *dev_id) |
| 467 | { |
Clark Wang | c23fdef | 2019-01-07 07:47:38 +0000 | [diff] [blame] | 468 | u32 temp_SR, temp_IER; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 469 | struct fsl_lpspi_data *fsl_lpspi = dev_id; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 470 | |
Clark Wang | c23fdef | 2019-01-07 07:47:38 +0000 | [diff] [blame] | 471 | temp_IER = readl(fsl_lpspi->base + IMX7ULP_IER); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 472 | fsl_lpspi_intctrl(fsl_lpspi, 0); |
Clark Wang | c23fdef | 2019-01-07 07:47:38 +0000 | [diff] [blame] | 473 | temp_SR = readl(fsl_lpspi->base + IMX7ULP_SR); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 474 | |
| 475 | fsl_lpspi_read_rx_fifo(fsl_lpspi); |
| 476 | |
Clark Wang | c23fdef | 2019-01-07 07:47:38 +0000 | [diff] [blame] | 477 | if ((temp_SR & SR_TDF) && (temp_IER & IER_TDIE)) { |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 478 | fsl_lpspi_write_tx_fifo(fsl_lpspi); |
Clark Wang | c23fdef | 2019-01-07 07:47:38 +0000 | [diff] [blame] | 479 | return IRQ_HANDLED; |
| 480 | } |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 481 | |
Clark Wang | 6a13044 | 2019-01-07 07:47:41 +0000 | [diff] [blame] | 482 | if (temp_SR & SR_MBF || |
| 483 | readl(fsl_lpspi->base + IMX7ULP_FSR) & FSR_RXCOUNT) { |
| 484 | writel(SR_FCF, fsl_lpspi->base + IMX7ULP_SR); |
| 485 | fsl_lpspi_intctrl(fsl_lpspi, IER_FCIE); |
| 486 | return IRQ_HANDLED; |
| 487 | } |
| 488 | |
Clark Wang | c23fdef | 2019-01-07 07:47:38 +0000 | [diff] [blame] | 489 | if (temp_SR & SR_FCF && (temp_IER & IER_FCIE)) { |
| 490 | writel(SR_FCF, fsl_lpspi->base + IMX7ULP_SR); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 491 | complete(&fsl_lpspi->xfer_done); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 492 | return IRQ_HANDLED; |
| 493 | } |
| 494 | |
| 495 | return IRQ_NONE; |
| 496 | } |
| 497 | |
Han Xu | 944c01a | 2019-03-06 06:30:39 +0000 | [diff] [blame] | 498 | int fsl_lpspi_runtime_resume(struct device *dev) |
| 499 | { |
| 500 | struct fsl_lpspi_data *fsl_lpspi = dev_get_drvdata(dev); |
| 501 | int ret; |
| 502 | |
| 503 | ret = clk_prepare_enable(fsl_lpspi->clk_per); |
| 504 | if (ret) |
| 505 | return ret; |
| 506 | |
| 507 | ret = clk_prepare_enable(fsl_lpspi->clk_ipg); |
| 508 | if (ret) { |
| 509 | clk_disable_unprepare(fsl_lpspi->clk_per); |
| 510 | return ret; |
| 511 | } |
| 512 | |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | int fsl_lpspi_runtime_suspend(struct device *dev) |
| 517 | { |
| 518 | struct fsl_lpspi_data *fsl_lpspi = dev_get_drvdata(dev); |
| 519 | |
| 520 | clk_disable_unprepare(fsl_lpspi->clk_per); |
| 521 | clk_disable_unprepare(fsl_lpspi->clk_ipg); |
| 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
| 526 | static int fsl_lpspi_init_rpm(struct fsl_lpspi_data *fsl_lpspi) |
| 527 | { |
| 528 | struct device *dev = fsl_lpspi->dev; |
| 529 | |
| 530 | pm_runtime_enable(dev); |
| 531 | pm_runtime_set_autosuspend_delay(dev, FSL_LPSPI_RPM_TIMEOUT); |
| 532 | pm_runtime_use_autosuspend(dev); |
| 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 537 | static int fsl_lpspi_probe(struct platform_device *pdev) |
| 538 | { |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 539 | struct device_node *np = pdev->dev.of_node; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 540 | struct fsl_lpspi_data *fsl_lpspi; |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 541 | struct spi_controller *controller; |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 542 | struct spi_imx_master *lpspi_platform_info = |
| 543 | dev_get_platdata(&pdev->dev); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 544 | struct resource *res; |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 545 | int i, ret, irq; |
Gao Pan | b88a0de | 2016-11-28 11:03:00 +0800 | [diff] [blame] | 546 | u32 temp; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 547 | |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 548 | if (of_property_read_bool((&pdev->dev)->of_node, "spi-slave")) |
| 549 | controller = spi_alloc_slave(&pdev->dev, |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 550 | sizeof(struct fsl_lpspi_data)); |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 551 | else |
| 552 | controller = spi_alloc_master(&pdev->dev, |
| 553 | sizeof(struct fsl_lpspi_data)); |
| 554 | |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 555 | if (!controller) |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 556 | return -ENOMEM; |
| 557 | |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 558 | platform_set_drvdata(pdev, controller); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 559 | |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 560 | controller->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32); |
| 561 | controller->bus_num = pdev->id; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 562 | |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 563 | fsl_lpspi = spi_controller_get_devdata(controller); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 564 | fsl_lpspi->dev = &pdev->dev; |
Han Xu | 944c01a | 2019-03-06 06:30:39 +0000 | [diff] [blame] | 565 | dev_set_drvdata(&pdev->dev, fsl_lpspi); |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 566 | fsl_lpspi->is_slave = of_property_read_bool((&pdev->dev)->of_node, |
| 567 | "spi-slave"); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 568 | |
Clark Wang | c7a4025 | 2019-03-06 06:30:43 +0000 | [diff] [blame^] | 569 | if (!fsl_lpspi->is_slave) { |
| 570 | for (i = 0; i < controller->num_chipselect; i++) { |
| 571 | int cs_gpio = of_get_named_gpio(np, "cs-gpios", i); |
| 572 | |
| 573 | if (!gpio_is_valid(cs_gpio) && lpspi_platform_info) |
| 574 | cs_gpio = lpspi_platform_info->chipselect[i]; |
| 575 | |
| 576 | fsl_lpspi->chipselect[i] = cs_gpio; |
| 577 | if (!gpio_is_valid(cs_gpio)) |
| 578 | continue; |
| 579 | |
| 580 | ret = devm_gpio_request(&pdev->dev, |
| 581 | fsl_lpspi->chipselect[i], |
| 582 | DRIVER_NAME); |
| 583 | if (ret) { |
| 584 | dev_err(&pdev->dev, "can't get cs gpios\n"); |
| 585 | goto out_controller_put; |
| 586 | } |
| 587 | } |
| 588 | controller->cs_gpios = fsl_lpspi->chipselect; |
| 589 | controller->prepare_message = fsl_lpspi_prepare_message; |
| 590 | } |
| 591 | |
| 592 | controller->transfer_one = fsl_lpspi_transfer_one; |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 593 | controller->prepare_transfer_hardware = lpspi_prepare_xfer_hardware; |
| 594 | controller->unprepare_transfer_hardware = lpspi_unprepare_xfer_hardware; |
| 595 | controller->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; |
| 596 | controller->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX; |
| 597 | controller->dev.of_node = pdev->dev.of_node; |
| 598 | controller->bus_num = pdev->id; |
Clark Wang | bcd8731 | 2018-12-07 02:50:36 +0000 | [diff] [blame] | 599 | controller->slave_abort = fsl_lpspi_slave_abort; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 600 | |
| 601 | init_completion(&fsl_lpspi->xfer_done); |
| 602 | |
| 603 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 604 | fsl_lpspi->base = devm_ioremap_resource(&pdev->dev, res); |
| 605 | if (IS_ERR(fsl_lpspi->base)) { |
| 606 | ret = PTR_ERR(fsl_lpspi->base); |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 607 | goto out_controller_put; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | irq = platform_get_irq(pdev, 0); |
| 611 | if (irq < 0) { |
| 612 | ret = irq; |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 613 | goto out_controller_put; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | ret = devm_request_irq(&pdev->dev, irq, fsl_lpspi_isr, 0, |
| 617 | dev_name(&pdev->dev), fsl_lpspi); |
| 618 | if (ret) { |
| 619 | dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret); |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 620 | goto out_controller_put; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 621 | } |
| 622 | |
Clark Wang | f5e5afd | 2019-03-06 06:30:34 +0000 | [diff] [blame] | 623 | fsl_lpspi->clk_per = devm_clk_get(&pdev->dev, "per"); |
| 624 | if (IS_ERR(fsl_lpspi->clk_per)) { |
| 625 | ret = PTR_ERR(fsl_lpspi->clk_per); |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 626 | goto out_controller_put; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 627 | } |
| 628 | |
Clark Wang | f5e5afd | 2019-03-06 06:30:34 +0000 | [diff] [blame] | 629 | fsl_lpspi->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); |
| 630 | if (IS_ERR(fsl_lpspi->clk_ipg)) { |
| 631 | ret = PTR_ERR(fsl_lpspi->clk_ipg); |
| 632 | goto out_controller_put; |
| 633 | } |
| 634 | |
Han Xu | 944c01a | 2019-03-06 06:30:39 +0000 | [diff] [blame] | 635 | /* enable the clock */ |
| 636 | ret = fsl_lpspi_init_rpm(fsl_lpspi); |
| 637 | if (ret) |
Clark Wang | f5e5afd | 2019-03-06 06:30:34 +0000 | [diff] [blame] | 638 | goto out_controller_put; |
Clark Wang | f5e5afd | 2019-03-06 06:30:34 +0000 | [diff] [blame] | 639 | |
Han Xu | 944c01a | 2019-03-06 06:30:39 +0000 | [diff] [blame] | 640 | ret = pm_runtime_get_sync(fsl_lpspi->dev); |
| 641 | if (ret < 0) { |
| 642 | dev_err(fsl_lpspi->dev, "failed to enable clock\n"); |
| 643 | return ret; |
Gao Pan | b88a0de | 2016-11-28 11:03:00 +0800 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | temp = readl(fsl_lpspi->base + IMX7ULP_PARAM); |
| 647 | fsl_lpspi->txfifosize = 1 << (temp & 0x0f); |
| 648 | fsl_lpspi->rxfifosize = 1 << ((temp >> 8) & 0x0f); |
| 649 | |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 650 | ret = devm_spi_register_controller(&pdev->dev, controller); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 651 | if (ret < 0) { |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 652 | dev_err(&pdev->dev, "spi_register_controller error.\n"); |
| 653 | goto out_controller_put; |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | return 0; |
| 657 | |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 658 | out_controller_put: |
| 659 | spi_controller_put(controller); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 660 | |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | static int fsl_lpspi_remove(struct platform_device *pdev) |
| 665 | { |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 666 | struct spi_controller *controller = platform_get_drvdata(pdev); |
| 667 | struct fsl_lpspi_data *fsl_lpspi = |
| 668 | spi_controller_get_devdata(controller); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 669 | |
Han Xu | 944c01a | 2019-03-06 06:30:39 +0000 | [diff] [blame] | 670 | pm_runtime_disable(fsl_lpspi->dev); |
| 671 | |
| 672 | spi_master_put(controller); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 673 | |
| 674 | return 0; |
| 675 | } |
| 676 | |
Han Xu | 944c01a | 2019-03-06 06:30:39 +0000 | [diff] [blame] | 677 | #ifdef CONFIG_PM_SLEEP |
| 678 | static int fsl_lpspi_suspend(struct device *dev) |
| 679 | { |
| 680 | int ret; |
| 681 | |
| 682 | pinctrl_pm_select_sleep_state(dev); |
| 683 | ret = pm_runtime_force_suspend(dev); |
| 684 | return ret; |
| 685 | } |
| 686 | |
| 687 | static int fsl_lpspi_resume(struct device *dev) |
| 688 | { |
| 689 | int ret; |
| 690 | |
| 691 | ret = pm_runtime_force_resume(dev); |
| 692 | if (ret) { |
| 693 | dev_err(dev, "Error in resume: %d\n", ret); |
| 694 | return ret; |
| 695 | } |
| 696 | |
| 697 | pinctrl_pm_select_default_state(dev); |
| 698 | |
| 699 | return 0; |
| 700 | } |
| 701 | #endif /* CONFIG_PM_SLEEP */ |
| 702 | |
| 703 | static const struct dev_pm_ops fsl_lpspi_pm_ops = { |
| 704 | SET_RUNTIME_PM_OPS(fsl_lpspi_runtime_suspend, |
| 705 | fsl_lpspi_runtime_resume, NULL) |
| 706 | SET_SYSTEM_SLEEP_PM_OPS(fsl_lpspi_suspend, fsl_lpspi_resume) |
| 707 | }; |
| 708 | |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 709 | static struct platform_driver fsl_lpspi_driver = { |
| 710 | .driver = { |
Gao Pan | 102ecc47 | 2017-01-04 17:38:16 +0800 | [diff] [blame] | 711 | .name = DRIVER_NAME, |
| 712 | .of_match_table = fsl_lpspi_dt_ids, |
Han Xu | 944c01a | 2019-03-06 06:30:39 +0000 | [diff] [blame] | 713 | .pm = &fsl_lpspi_pm_ops, |
Gao Pan | 102ecc47 | 2017-01-04 17:38:16 +0800 | [diff] [blame] | 714 | }, |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 715 | .probe = fsl_lpspi_probe, |
| 716 | .remove = fsl_lpspi_remove, |
| 717 | }; |
| 718 | module_platform_driver(fsl_lpspi_driver); |
| 719 | |
Clark Wang | 07d7155 | 2018-12-07 02:50:34 +0000 | [diff] [blame] | 720 | MODULE_DESCRIPTION("LPSPI Controller driver"); |
Gao Pan | 5314987 | 2016-11-22 21:52:17 +0800 | [diff] [blame] | 721 | MODULE_AUTHOR("Gao Pan <pandy.gao@nxp.com>"); |
Gao Pan | b6787b6 | 2016-12-02 11:50:00 +0800 | [diff] [blame] | 722 | MODULE_LICENSE("GPL"); |