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/irq.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/crc7.h>
26 #include <linux/spi/spi.h>
27 #include <linux/wl12xx.h>
33 static irqreturn_t wl1251_irq(int irq, void *cookie)
37 wl1251_debug(DEBUG_IRQ, "IRQ");
41 ieee80211_queue_work(wl->hw, &wl->irq_work);
46 static struct spi_device *wl_to_spi(struct wl1251 *wl)
51 static void wl1251_spi_reset(struct wl1251 *wl)
54 struct spi_transfer t;
57 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
59 wl1251_error("could not allocate cmd for spi reset");
63 memset(&t, 0, sizeof(t));
66 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
69 t.len = WSPI_INIT_CMD_LEN;
70 spi_message_add_tail(&t, &m);
72 spi_sync(wl_to_spi(wl), &m);
74 wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
77 static void wl1251_spi_wake(struct wl1251 *wl)
79 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
80 struct spi_transfer t;
83 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
85 wl1251_error("could not allocate cmd for spi init");
89 memset(crc, 0, sizeof(crc));
90 memset(&t, 0, sizeof(t));
94 * Set WSPI_INIT_COMMAND
95 * the data is being send from the MSB to LSB
99 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
102 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
103 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
105 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
106 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
108 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
110 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
111 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
119 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
120 cmd[4] |= WSPI_INIT_CMD_END;
123 t.len = WSPI_INIT_CMD_LEN;
124 spi_message_add_tail(&t, &m);
126 spi_sync(wl_to_spi(wl), &m);
128 wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
131 static void wl1251_spi_reset_wake(struct wl1251 *wl)
133 wl1251_spi_reset(wl);
137 static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
140 struct spi_transfer t[3];
141 struct spi_message m;
145 cmd = &wl->buffer_cmd;
146 busy_buf = wl->buffer_busyword;
149 *cmd |= WSPI_CMD_READ;
150 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
151 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
153 spi_message_init(&m);
154 memset(t, 0, sizeof(t));
158 spi_message_add_tail(&t[0], &m);
160 /* Busy and non busy words read */
161 t[1].rx_buf = busy_buf;
162 t[1].len = WL1251_BUSY_WORD_LEN;
163 spi_message_add_tail(&t[1], &m);
167 spi_message_add_tail(&t[2], &m);
169 spi_sync(wl_to_spi(wl), &m);
171 /* FIXME: check busy words */
173 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
174 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
177 static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
180 struct spi_transfer t[2];
181 struct spi_message m;
184 cmd = &wl->buffer_cmd;
187 *cmd |= WSPI_CMD_WRITE;
188 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
189 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
191 spi_message_init(&m);
192 memset(t, 0, sizeof(t));
195 t[0].len = sizeof(*cmd);
196 spi_message_add_tail(&t[0], &m);
200 spi_message_add_tail(&t[1], &m);
202 spi_sync(wl_to_spi(wl), &m);
204 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
205 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
208 static void wl1251_spi_enable_irq(struct wl1251 *wl)
210 return enable_irq(wl->irq);
213 static void wl1251_spi_disable_irq(struct wl1251 *wl)
215 return disable_irq(wl->irq);
218 static int wl1251_spi_set_power(struct wl1251 *wl, bool enable)
221 wl->set_power(enable);
226 static const struct wl1251_if_operations wl1251_spi_ops = {
227 .read = wl1251_spi_read,
228 .write = wl1251_spi_write,
229 .reset = wl1251_spi_reset_wake,
230 .enable_irq = wl1251_spi_enable_irq,
231 .disable_irq = wl1251_spi_disable_irq,
232 .power = wl1251_spi_set_power,
235 static int __devinit wl1251_spi_probe(struct spi_device *spi)
237 struct wl12xx_platform_data *pdata;
238 struct ieee80211_hw *hw;
242 pdata = spi->dev.platform_data;
244 wl1251_error("no platform data");
248 hw = wl1251_alloc_hw();
254 SET_IEEE80211_DEV(hw, &spi->dev);
255 dev_set_drvdata(&spi->dev, wl);
257 wl->if_ops = &wl1251_spi_ops;
259 /* This is the only SPI value that we need to set here, the rest
260 * comes from the board-peripherals file */
261 spi->bits_per_word = 32;
263 ret = spi_setup(spi);
265 wl1251_error("spi_setup failed");
269 wl->set_power = pdata->set_power;
270 if (!wl->set_power) {
271 wl1251_error("set power function missing in platform data");
277 wl1251_error("irq missing in platform data");
281 wl->use_eeprom = pdata->use_eeprom;
283 ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
285 wl1251_error("request_irq() failed: %d", ret);
289 irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
291 disable_irq(wl->irq);
293 ret = wl1251_init_ieee80211(wl);
300 free_irq(wl->irq, wl);
303 ieee80211_free_hw(hw);
308 static int __devexit wl1251_spi_remove(struct spi_device *spi)
310 struct wl1251 *wl = dev_get_drvdata(&spi->dev);
312 free_irq(wl->irq, wl);
318 static struct spi_driver wl1251_spi_driver = {
321 .bus = &spi_bus_type,
322 .owner = THIS_MODULE,
325 .probe = wl1251_spi_probe,
326 .remove = __devexit_p(wl1251_spi_remove),
329 static int __init wl1251_spi_init(void)
333 ret = spi_register_driver(&wl1251_spi_driver);
335 wl1251_error("failed to register spi driver: %d", ret);
343 static void __exit wl1251_spi_exit(void)
345 spi_unregister_driver(&wl1251_spi_driver);
347 wl1251_notice("unloaded");
350 module_init(wl1251_spi_init);
351 module_exit(wl1251_spi_exit);
353 MODULE_LICENSE("GPL");
354 MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
355 MODULE_ALIAS("spi:wl1251");