1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * This file is part of wl1251 4 * 5 * Copyright (C) 2008 Nokia Corporation 6 */ 7 8 #include <linux/interrupt.h> 9 #include <linux/irq.h> 10 #include <linux/module.h> 11 #include <linux/slab.h> 12 #include <linux/swab.h> 13 #include <linux/crc7.h> 14 #include <linux/spi/spi.h> 15 #include <linux/gpio.h> 16 #include <linux/of.h> 17 #include <linux/of_gpio.h> 18 #include <linux/regulator/consumer.h> 19 20 #include "wl1251.h" 21 #include "reg.h" 22 #include "spi.h" 23 24 static irqreturn_t wl1251_irq(int irq, void *cookie) 25 { 26 struct wl1251 *wl; 27 28 wl1251_debug(DEBUG_IRQ, "IRQ"); 29 30 wl = cookie; 31 32 ieee80211_queue_work(wl->hw, &wl->irq_work); 33 34 return IRQ_HANDLED; 35 } 36 37 static struct spi_device *wl_to_spi(struct wl1251 *wl) 38 { 39 return wl->if_priv; 40 } 41 42 static void wl1251_spi_reset(struct wl1251 *wl) 43 { 44 u8 *cmd; 45 struct spi_transfer t; 46 struct spi_message m; 47 48 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); 49 if (!cmd) { 50 wl1251_error("could not allocate cmd for spi reset"); 51 return; 52 } 53 54 memset(&t, 0, sizeof(t)); 55 spi_message_init(&m); 56 57 memset(cmd, 0xff, WSPI_INIT_CMD_LEN); 58 59 t.tx_buf = cmd; 60 t.len = WSPI_INIT_CMD_LEN; 61 spi_message_add_tail(&t, &m); 62 63 spi_sync(wl_to_spi(wl), &m); 64 65 wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN); 66 67 kfree(cmd); 68 } 69 70 static void wl1251_spi_wake(struct wl1251 *wl) 71 { 72 struct spi_transfer t; 73 struct spi_message m; 74 u8 *cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); 75 76 if (!cmd) { 77 wl1251_error("could not allocate cmd for spi init"); 78 return; 79 } 80 81 memset(&t, 0, sizeof(t)); 82 spi_message_init(&m); 83 84 /* Set WSPI_INIT_COMMAND 85 * the data is being send from the MSB to LSB 86 */ 87 cmd[0] = 0xff; 88 cmd[1] = 0xff; 89 cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; 90 cmd[3] = 0; 91 cmd[4] = 0; 92 cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3; 93 cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; 94 95 cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS 96 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; 97 98 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) 99 cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; 100 else 101 cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY; 102 103 cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END; 104 /* 105 * The above is the logical order; it must actually be stored 106 * in the buffer byte-swapped. 107 */ 108 __swab32s((u32 *)cmd); 109 __swab32s((u32 *)cmd+1); 110 111 t.tx_buf = cmd; 112 t.len = WSPI_INIT_CMD_LEN; 113 spi_message_add_tail(&t, &m); 114 115 spi_sync(wl_to_spi(wl), &m); 116 117 wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); 118 119 kfree(cmd); 120 } 121 122 static void wl1251_spi_reset_wake(struct wl1251 *wl) 123 { 124 wl1251_spi_reset(wl); 125 wl1251_spi_wake(wl); 126 } 127 128 static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf, 129 size_t len) 130 { 131 struct spi_transfer t[3]; 132 struct spi_message m; 133 u8 *busy_buf; 134 u32 *cmd; 135 136 cmd = &wl->buffer_cmd; 137 busy_buf = wl->buffer_busyword; 138 139 *cmd = 0; 140 *cmd |= WSPI_CMD_READ; 141 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; 142 *cmd |= addr & WSPI_CMD_BYTE_ADDR; 143 144 spi_message_init(&m); 145 memset(t, 0, sizeof(t)); 146 147 t[0].tx_buf = cmd; 148 t[0].len = 4; 149 spi_message_add_tail(&t[0], &m); 150 151 /* Busy and non busy words read */ 152 t[1].rx_buf = busy_buf; 153 t[1].len = WL1251_BUSY_WORD_LEN; 154 spi_message_add_tail(&t[1], &m); 155 156 t[2].rx_buf = buf; 157 t[2].len = len; 158 spi_message_add_tail(&t[2], &m); 159 160 spi_sync(wl_to_spi(wl), &m); 161 162 /* FIXME: check busy words */ 163 164 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); 165 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len); 166 } 167 168 static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf, 169 size_t len) 170 { 171 struct spi_transfer t[2]; 172 struct spi_message m; 173 u32 *cmd; 174 175 cmd = &wl->buffer_cmd; 176 177 *cmd = 0; 178 *cmd |= WSPI_CMD_WRITE; 179 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; 180 *cmd |= addr & WSPI_CMD_BYTE_ADDR; 181 182 spi_message_init(&m); 183 memset(t, 0, sizeof(t)); 184 185 t[0].tx_buf = cmd; 186 t[0].len = sizeof(*cmd); 187 spi_message_add_tail(&t[0], &m); 188 189 t[1].tx_buf = buf; 190 t[1].len = len; 191 spi_message_add_tail(&t[1], &m); 192 193 spi_sync(wl_to_spi(wl), &m); 194 195 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); 196 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len); 197 } 198 199 static void wl1251_spi_enable_irq(struct wl1251 *wl) 200 { 201 return enable_irq(wl->irq); 202 } 203 204 static void wl1251_spi_disable_irq(struct wl1251 *wl) 205 { 206 return disable_irq(wl->irq); 207 } 208 209 static int wl1251_spi_set_power(struct wl1251 *wl, bool enable) 210 { 211 if (gpio_is_valid(wl->power_gpio)) 212 gpio_set_value(wl->power_gpio, enable); 213 214 return 0; 215 } 216 217 static const struct wl1251_if_operations wl1251_spi_ops = { 218 .read = wl1251_spi_read, 219 .write = wl1251_spi_write, 220 .reset = wl1251_spi_reset_wake, 221 .enable_irq = wl1251_spi_enable_irq, 222 .disable_irq = wl1251_spi_disable_irq, 223 .power = wl1251_spi_set_power, 224 }; 225 226 static int wl1251_spi_probe(struct spi_device *spi) 227 { 228 struct device_node *np = spi->dev.of_node; 229 struct ieee80211_hw *hw; 230 struct wl1251 *wl; 231 int ret; 232 233 if (!np) 234 return -ENODEV; 235 236 hw = wl1251_alloc_hw(); 237 if (IS_ERR(hw)) 238 return PTR_ERR(hw); 239 240 wl = hw->priv; 241 242 SET_IEEE80211_DEV(hw, &spi->dev); 243 spi_set_drvdata(spi, wl); 244 wl->if_priv = spi; 245 wl->if_ops = &wl1251_spi_ops; 246 247 /* This is the only SPI value that we need to set here, the rest 248 * comes from the board-peripherals file 249 */ 250 spi->bits_per_word = 32; 251 252 ret = spi_setup(spi); 253 if (ret < 0) { 254 wl1251_error("spi_setup failed"); 255 goto out_free; 256 } 257 258 wl->use_eeprom = of_property_read_bool(np, "ti,wl1251-has-eeprom"); 259 260 wl->power_gpio = of_get_named_gpio(np, "ti,power-gpio", 0); 261 if (wl->power_gpio == -EPROBE_DEFER) { 262 ret = -EPROBE_DEFER; 263 goto out_free; 264 } 265 266 if (gpio_is_valid(wl->power_gpio)) { 267 ret = devm_gpio_request_one(&spi->dev, wl->power_gpio, 268 GPIOF_OUT_INIT_LOW, "wl1251 power"); 269 if (ret) { 270 wl1251_error("Failed to request gpio: %d\n", ret); 271 goto out_free; 272 } 273 } else { 274 wl1251_error("set power gpio missing in platform data"); 275 ret = -ENODEV; 276 goto out_free; 277 } 278 279 wl->irq = spi->irq; 280 if (wl->irq < 0) { 281 wl1251_error("irq missing in platform data"); 282 ret = -ENODEV; 283 goto out_free; 284 } 285 286 irq_set_status_flags(wl->irq, IRQ_NOAUTOEN); 287 ret = devm_request_irq(&spi->dev, wl->irq, wl1251_irq, 0, 288 DRIVER_NAME, wl); 289 if (ret < 0) { 290 wl1251_error("request_irq() failed: %d", ret); 291 goto out_free; 292 } 293 294 irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING); 295 296 wl->vio = devm_regulator_get(&spi->dev, "vio"); 297 if (IS_ERR(wl->vio)) { 298 ret = PTR_ERR(wl->vio); 299 wl1251_error("vio regulator missing: %d", ret); 300 goto out_free; 301 } 302 303 ret = regulator_enable(wl->vio); 304 if (ret) 305 goto out_free; 306 307 ret = wl1251_init_ieee80211(wl); 308 if (ret) 309 goto disable_regulator; 310 311 return 0; 312 313 disable_regulator: 314 regulator_disable(wl->vio); 315 out_free: 316 ieee80211_free_hw(hw); 317 318 return ret; 319 } 320 321 static void wl1251_spi_remove(struct spi_device *spi) 322 { 323 struct wl1251 *wl = spi_get_drvdata(spi); 324 325 wl1251_free_hw(wl); 326 regulator_disable(wl->vio); 327 } 328 329 static struct spi_driver wl1251_spi_driver = { 330 .driver = { 331 .name = DRIVER_NAME, 332 }, 333 334 .probe = wl1251_spi_probe, 335 .remove = wl1251_spi_remove, 336 }; 337 338 module_spi_driver(wl1251_spi_driver); 339 340 MODULE_LICENSE("GPL"); 341 MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>"); 342 MODULE_ALIAS("spi:wl1251"); 343