1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of wl1251
5 * Copyright (C) 2008 Nokia Corporation
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/swab.h>
14 #include <linux/crc7.h>
15 #include <linux/spi/spi.h>
16 #include <linux/gpio/consumer.h>
18 #include <linux/regulator/consumer.h>
25 struct spi_device *spi;
26 struct gpio_desc *power_gpio;
29 static irqreturn_t wl1251_irq(int irq, void *cookie)
33 wl1251_debug(DEBUG_IRQ, "IRQ");
37 ieee80211_queue_work(wl->hw, &wl->irq_work);
42 static void wl1251_spi_reset(struct wl1251 *wl)
44 struct wl1251_spi *wl_spi = wl->if_priv;
46 struct spi_transfer t;
49 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
51 wl1251_error("could not allocate cmd for spi reset");
55 memset(&t, 0, sizeof(t));
58 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
61 t.len = WSPI_INIT_CMD_LEN;
62 spi_message_add_tail(&t, &m);
64 spi_sync(wl_spi->spi, &m);
66 wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
71 static void wl1251_spi_wake(struct wl1251 *wl)
73 struct wl1251_spi *wl_spi = wl->if_priv;
74 struct spi_transfer t;
76 u8 *cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
79 wl1251_error("could not allocate cmd for spi init");
83 memset(&t, 0, sizeof(t));
86 /* Set WSPI_INIT_COMMAND
87 * the data is being send from the MSB to LSB
91 cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
94 cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
95 cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
97 cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
98 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
100 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
101 cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
103 cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
105 cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END;
107 * The above is the logical order; it must actually be stored
108 * in the buffer byte-swapped.
110 __swab32s((u32 *)cmd);
111 __swab32s((u32 *)cmd+1);
114 t.len = WSPI_INIT_CMD_LEN;
115 spi_message_add_tail(&t, &m);
117 spi_sync(wl_spi->spi, &m);
119 wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
124 static void wl1251_spi_reset_wake(struct wl1251 *wl)
126 wl1251_spi_reset(wl);
130 static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
133 struct wl1251_spi *wl_spi = wl->if_priv;
134 struct spi_transfer t[3];
135 struct spi_message m;
139 cmd = &wl->buffer_cmd;
140 busy_buf = wl->buffer_busyword;
143 *cmd |= WSPI_CMD_READ;
144 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
145 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
147 spi_message_init(&m);
148 memset(t, 0, sizeof(t));
152 spi_message_add_tail(&t[0], &m);
154 /* Busy and non busy words read */
155 t[1].rx_buf = busy_buf;
156 t[1].len = WL1251_BUSY_WORD_LEN;
157 spi_message_add_tail(&t[1], &m);
161 spi_message_add_tail(&t[2], &m);
163 spi_sync(wl_spi->spi, &m);
165 /* FIXME: check busy words */
167 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
168 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
171 static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
174 struct wl1251_spi *wl_spi = wl->if_priv;
175 struct spi_transfer t[2];
176 struct spi_message m;
179 cmd = &wl->buffer_cmd;
182 *cmd |= WSPI_CMD_WRITE;
183 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
184 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
186 spi_message_init(&m);
187 memset(t, 0, sizeof(t));
190 t[0].len = sizeof(*cmd);
191 spi_message_add_tail(&t[0], &m);
195 spi_message_add_tail(&t[1], &m);
197 spi_sync(wl_spi->spi, &m);
199 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
200 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
203 static void wl1251_spi_enable_irq(struct wl1251 *wl)
205 return enable_irq(wl->irq);
208 static void wl1251_spi_disable_irq(struct wl1251 *wl)
210 return disable_irq(wl->irq);
213 static int wl1251_spi_set_power(struct wl1251 *wl, bool enable)
215 struct wl1251_spi *wl_spi = wl->if_priv;
217 if (wl_spi->power_gpio)
218 gpiod_set_value_cansleep(wl_spi->power_gpio, enable);
223 static const struct wl1251_if_operations wl1251_spi_ops = {
224 .read = wl1251_spi_read,
225 .write = wl1251_spi_write,
226 .reset = wl1251_spi_reset_wake,
227 .enable_irq = wl1251_spi_enable_irq,
228 .disable_irq = wl1251_spi_disable_irq,
229 .power = wl1251_spi_set_power,
232 static int wl1251_spi_probe(struct spi_device *spi)
234 struct device_node *np = spi->dev.of_node;
235 struct ieee80211_hw *hw;
236 struct wl1251_spi *wl_spi;
243 wl_spi = devm_kzalloc(&spi->dev, sizeof(*wl_spi), GFP_KERNEL);
249 hw = wl1251_alloc_hw();
255 SET_IEEE80211_DEV(hw, &spi->dev);
256 spi_set_drvdata(spi, wl);
257 wl->if_priv = wl_spi;
258 wl->if_ops = &wl1251_spi_ops;
260 /* This is the only SPI value that we need to set here, the rest
261 * comes from the board-peripherals file
263 spi->bits_per_word = 32;
265 ret = spi_setup(spi);
267 wl1251_error("spi_setup failed");
271 wl->use_eeprom = of_property_read_bool(np, "ti,wl1251-has-eeprom");
273 wl_spi->power_gpio = devm_gpiod_get_optional(&spi->dev, "ti,power",
275 ret = PTR_ERR_OR_ZERO(wl_spi->power_gpio);
277 if (ret != -EPROBE_DEFER)
278 wl1251_error("Failed to request gpio: %d\n", ret);
282 gpiod_set_consumer_name(wl_spi->power_gpio, "wl1251 power");
286 wl1251_error("irq missing in platform data");
291 irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
292 ret = devm_request_irq(&spi->dev, wl->irq, wl1251_irq, 0,
295 wl1251_error("request_irq() failed: %d", ret);
299 irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
301 wl->vio = devm_regulator_get(&spi->dev, "vio");
302 if (IS_ERR(wl->vio)) {
303 ret = PTR_ERR(wl->vio);
304 wl1251_error("vio regulator missing: %d", ret);
308 ret = regulator_enable(wl->vio);
312 ret = wl1251_init_ieee80211(wl);
314 goto disable_regulator;
319 regulator_disable(wl->vio);
321 ieee80211_free_hw(hw);
326 static void wl1251_spi_remove(struct spi_device *spi)
328 struct wl1251 *wl = spi_get_drvdata(spi);
331 regulator_disable(wl->vio);
334 static struct spi_driver wl1251_spi_driver = {
339 .probe = wl1251_spi_probe,
340 .remove = wl1251_spi_remove,
343 module_spi_driver(wl1251_spi_driver);
345 MODULE_LICENSE("GPL");
347 MODULE_ALIAS("spi:wl1251");