2 * This file is part of wl1251
4 * Copyright (C) 2008 Nokia Corporation
6 * Contact: Kalle Valo <kalle.valo@nokia.com>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/irq.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/crc7.h>
28 #include <linux/spi/spi.h>
29 #include <linux/spi/wl12xx.h>
32 #include "wl1251_reg.h"
33 #include "wl1251_spi.h"
35 static irqreturn_t wl1251_irq(int irq, void *cookie)
39 wl1251_debug(DEBUG_IRQ, "IRQ");
43 ieee80211_queue_work(wl->hw, &wl->irq_work);
48 static struct spi_device *wl_to_spi(struct wl1251 *wl)
53 static void wl1251_spi_reset(struct wl1251 *wl)
56 struct spi_transfer t;
59 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
61 wl1251_error("could not allocate cmd for spi reset");
65 memset(&t, 0, sizeof(t));
68 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
71 t.len = WSPI_INIT_CMD_LEN;
72 spi_message_add_tail(&t, &m);
74 spi_sync(wl_to_spi(wl), &m);
76 wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
79 static void wl1251_spi_wake(struct wl1251 *wl)
81 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
82 struct spi_transfer t;
85 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
87 wl1251_error("could not allocate cmd for spi init");
91 memset(crc, 0, sizeof(crc));
92 memset(&t, 0, sizeof(t));
96 * Set WSPI_INIT_COMMAND
97 * the data is being send from the MSB to LSB
101 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
104 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
105 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
107 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
108 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
110 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
112 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
113 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
121 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
122 cmd[4] |= WSPI_INIT_CMD_END;
125 t.len = WSPI_INIT_CMD_LEN;
126 spi_message_add_tail(&t, &m);
128 spi_sync(wl_to_spi(wl), &m);
130 wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
133 static void wl1251_spi_reset_wake(struct wl1251 *wl)
135 wl1251_spi_reset(wl);
139 static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
142 struct spi_transfer t[3];
143 struct spi_message m;
147 cmd = &wl->buffer_cmd;
148 busy_buf = wl->buffer_busyword;
151 *cmd |= WSPI_CMD_READ;
152 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
153 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
155 spi_message_init(&m);
156 memset(t, 0, sizeof(t));
160 spi_message_add_tail(&t[0], &m);
162 /* Busy and non busy words read */
163 t[1].rx_buf = busy_buf;
164 t[1].len = WL1251_BUSY_WORD_LEN;
165 spi_message_add_tail(&t[1], &m);
169 spi_message_add_tail(&t[2], &m);
171 spi_sync(wl_to_spi(wl), &m);
173 /* FIXME: check busy words */
175 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
176 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
179 static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
182 struct spi_transfer t[2];
183 struct spi_message m;
186 cmd = &wl->buffer_cmd;
189 *cmd |= WSPI_CMD_WRITE;
190 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
191 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
193 spi_message_init(&m);
194 memset(t, 0, sizeof(t));
197 t[0].len = sizeof(*cmd);
198 spi_message_add_tail(&t[0], &m);
202 spi_message_add_tail(&t[1], &m);
204 spi_sync(wl_to_spi(wl), &m);
206 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
207 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
210 static void wl1251_spi_enable_irq(struct wl1251 *wl)
212 return enable_irq(wl->irq);
215 static void wl1251_spi_disable_irq(struct wl1251 *wl)
217 return disable_irq(wl->irq);
220 static const struct wl1251_if_operations wl1251_spi_ops = {
221 .read = wl1251_spi_read,
222 .write = wl1251_spi_write,
223 .reset = wl1251_spi_reset_wake,
224 .enable_irq = wl1251_spi_enable_irq,
225 .disable_irq = wl1251_spi_disable_irq,
228 static int __devinit wl1251_spi_probe(struct spi_device *spi)
230 struct wl12xx_platform_data *pdata;
231 struct ieee80211_hw *hw;
235 pdata = spi->dev.platform_data;
237 wl1251_error("no platform data");
241 hw = wl1251_alloc_hw();
247 SET_IEEE80211_DEV(hw, &spi->dev);
248 dev_set_drvdata(&spi->dev, wl);
250 wl->if_ops = &wl1251_spi_ops;
252 /* This is the only SPI value that we need to set here, the rest
253 * comes from the board-peripherals file */
254 spi->bits_per_word = 32;
256 ret = spi_setup(spi);
258 wl1251_error("spi_setup failed");
262 wl->set_power = pdata->set_power;
263 if (!wl->set_power) {
264 wl1251_error("set power function missing in platform data");
270 wl1251_error("irq missing in platform data");
274 wl->use_eeprom = pdata->use_eeprom;
276 ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
278 wl1251_error("request_irq() failed: %d", ret);
282 set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
284 disable_irq(wl->irq);
286 ret = wl1251_init_ieee80211(wl);
293 free_irq(wl->irq, wl);
296 ieee80211_free_hw(hw);
301 static int __devexit wl1251_spi_remove(struct spi_device *spi)
303 struct wl1251 *wl = dev_get_drvdata(&spi->dev);
305 free_irq(wl->irq, wl);
311 static struct spi_driver wl1251_spi_driver = {
314 .bus = &spi_bus_type,
315 .owner = THIS_MODULE,
318 .probe = wl1251_spi_probe,
319 .remove = __devexit_p(wl1251_spi_remove),
322 static int __init wl1251_spi_init(void)
326 ret = spi_register_driver(&wl1251_spi_driver);
328 wl1251_error("failed to register spi driver: %d", ret);
336 static void __exit wl1251_spi_exit(void)
338 spi_unregister_driver(&wl1251_spi_driver);
340 wl1251_notice("unloaded");
343 module_init(wl1251_spi_init);
344 module_exit(wl1251_spi_exit);
346 MODULE_LICENSE("GPL");
347 MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");