2 * This file is part of wl1251
4 * Copyright (C) 2008 Nokia Corporation
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 #include <linux/interrupt.h>
23 #include <linux/irq.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
26 #include <linux/crc7.h>
27 #include <linux/spi/spi.h>
28 #include <linux/wl12xx.h>
29 #include <linux/gpio.h>
31 #include <linux/of_gpio.h>
32 #include <linux/regulator/consumer.h>
38 static irqreturn_t wl1251_irq(int irq, void *cookie)
42 wl1251_debug(DEBUG_IRQ, "IRQ");
46 ieee80211_queue_work(wl->hw, &wl->irq_work);
51 static struct spi_device *wl_to_spi(struct wl1251 *wl)
56 static void wl1251_spi_reset(struct wl1251 *wl)
59 struct spi_transfer t;
62 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
64 wl1251_error("could not allocate cmd for spi reset");
68 memset(&t, 0, sizeof(t));
71 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
74 t.len = WSPI_INIT_CMD_LEN;
75 spi_message_add_tail(&t, &m);
77 spi_sync(wl_to_spi(wl), &m);
79 wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
84 static void wl1251_spi_wake(struct wl1251 *wl)
86 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
87 struct spi_transfer t;
90 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
92 wl1251_error("could not allocate cmd for spi init");
96 memset(crc, 0, sizeof(crc));
97 memset(&t, 0, sizeof(t));
100 /* Set WSPI_INIT_COMMAND
101 * the data is being send from the MSB to LSB
105 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
108 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
109 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
111 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
112 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
114 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
116 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
117 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
125 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
126 cmd[4] |= WSPI_INIT_CMD_END;
129 t.len = WSPI_INIT_CMD_LEN;
130 spi_message_add_tail(&t, &m);
132 spi_sync(wl_to_spi(wl), &m);
134 wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
139 static void wl1251_spi_reset_wake(struct wl1251 *wl)
141 wl1251_spi_reset(wl);
145 static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
148 struct spi_transfer t[3];
149 struct spi_message m;
153 cmd = &wl->buffer_cmd;
154 busy_buf = wl->buffer_busyword;
157 *cmd |= WSPI_CMD_READ;
158 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
159 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
161 spi_message_init(&m);
162 memset(t, 0, sizeof(t));
166 spi_message_add_tail(&t[0], &m);
168 /* Busy and non busy words read */
169 t[1].rx_buf = busy_buf;
170 t[1].len = WL1251_BUSY_WORD_LEN;
171 spi_message_add_tail(&t[1], &m);
175 spi_message_add_tail(&t[2], &m);
177 spi_sync(wl_to_spi(wl), &m);
179 /* FIXME: check busy words */
181 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
182 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
185 static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
188 struct spi_transfer t[2];
189 struct spi_message m;
192 cmd = &wl->buffer_cmd;
195 *cmd |= WSPI_CMD_WRITE;
196 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
197 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
199 spi_message_init(&m);
200 memset(t, 0, sizeof(t));
203 t[0].len = sizeof(*cmd);
204 spi_message_add_tail(&t[0], &m);
208 spi_message_add_tail(&t[1], &m);
210 spi_sync(wl_to_spi(wl), &m);
212 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
213 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
216 static void wl1251_spi_enable_irq(struct wl1251 *wl)
218 return enable_irq(wl->irq);
221 static void wl1251_spi_disable_irq(struct wl1251 *wl)
223 return disable_irq(wl->irq);
226 static int wl1251_spi_set_power(struct wl1251 *wl, bool enable)
228 if (gpio_is_valid(wl->power_gpio))
229 gpio_set_value(wl->power_gpio, enable);
234 static const struct wl1251_if_operations wl1251_spi_ops = {
235 .read = wl1251_spi_read,
236 .write = wl1251_spi_write,
237 .reset = wl1251_spi_reset_wake,
238 .enable_irq = wl1251_spi_enable_irq,
239 .disable_irq = wl1251_spi_disable_irq,
240 .power = wl1251_spi_set_power,
243 static int wl1251_spi_probe(struct spi_device *spi)
245 struct wl1251_platform_data *pdata = dev_get_platdata(&spi->dev);
246 struct device_node *np = spi->dev.of_node;
247 struct ieee80211_hw *hw;
252 wl1251_error("no platform data");
256 hw = wl1251_alloc_hw();
262 SET_IEEE80211_DEV(hw, &spi->dev);
263 spi_set_drvdata(spi, wl);
265 wl->if_ops = &wl1251_spi_ops;
267 /* This is the only SPI value that we need to set here, the rest
268 * comes from the board-peripherals file
270 spi->bits_per_word = 32;
272 ret = spi_setup(spi);
274 wl1251_error("spi_setup failed");
279 wl->use_eeprom = of_property_read_bool(np, "ti,wl1251-has-eeprom");
280 wl->power_gpio = of_get_named_gpio(np, "ti,power-gpio", 0);
282 wl->power_gpio = pdata->power_gpio;
283 wl->use_eeprom = pdata->use_eeprom;
286 if (wl->power_gpio == -EPROBE_DEFER) {
291 if (gpio_is_valid(wl->power_gpio)) {
292 ret = devm_gpio_request_one(&spi->dev, wl->power_gpio,
293 GPIOF_OUT_INIT_LOW, "wl1251 power");
295 wl1251_error("Failed to request gpio: %d\n", ret);
299 wl1251_error("set power gpio missing in platform data");
306 wl1251_error("irq missing in platform data");
311 irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
312 ret = devm_request_irq(&spi->dev, wl->irq, wl1251_irq, 0,
315 wl1251_error("request_irq() failed: %d", ret);
319 irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
321 wl->vio = devm_regulator_get(&spi->dev, "vio");
322 if (IS_ERR(wl->vio)) {
323 ret = PTR_ERR(wl->vio);
324 wl1251_error("vio regulator missing: %d", ret);
328 ret = regulator_enable(wl->vio);
332 ret = wl1251_init_ieee80211(wl);
334 goto disable_regulator;
339 regulator_disable(wl->vio);
341 ieee80211_free_hw(hw);
346 static int wl1251_spi_remove(struct spi_device *spi)
348 struct wl1251 *wl = spi_get_drvdata(spi);
350 free_irq(wl->irq, wl);
352 regulator_disable(wl->vio);
357 static struct spi_driver wl1251_spi_driver = {
360 .owner = THIS_MODULE,
363 .probe = wl1251_spi_probe,
364 .remove = wl1251_spi_remove,
367 module_spi_driver(wl1251_spi_driver);
369 MODULE_LICENSE("GPL");
370 MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
371 MODULE_ALIAS("spi:wl1251");