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>
34 static irqreturn_t wl1251_irq(int irq, void *cookie)
38 wl1251_debug(DEBUG_IRQ, "IRQ");
42 ieee80211_queue_work(wl->hw, &wl->irq_work);
47 static struct spi_device *wl_to_spi(struct wl1251 *wl)
52 static void wl1251_spi_reset(struct wl1251 *wl)
55 struct spi_transfer t;
58 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
60 wl1251_error("could not allocate cmd for spi reset");
64 memset(&t, 0, sizeof(t));
67 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
70 t.len = WSPI_INIT_CMD_LEN;
71 spi_message_add_tail(&t, &m);
73 spi_sync(wl_to_spi(wl), &m);
75 wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
80 static void wl1251_spi_wake(struct wl1251 *wl)
82 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
83 struct spi_transfer t;
86 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
88 wl1251_error("could not allocate cmd for spi init");
92 memset(crc, 0, sizeof(crc));
93 memset(&t, 0, sizeof(t));
97 * Set WSPI_INIT_COMMAND
98 * the data is being send from the MSB to LSB
102 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
105 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
106 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
108 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
109 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
111 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
113 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
114 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
122 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
123 cmd[4] |= WSPI_INIT_CMD_END;
126 t.len = WSPI_INIT_CMD_LEN;
127 spi_message_add_tail(&t, &m);
129 spi_sync(wl_to_spi(wl), &m);
131 wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
136 static void wl1251_spi_reset_wake(struct wl1251 *wl)
138 wl1251_spi_reset(wl);
142 static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
145 struct spi_transfer t[3];
146 struct spi_message m;
150 cmd = &wl->buffer_cmd;
151 busy_buf = wl->buffer_busyword;
154 *cmd |= WSPI_CMD_READ;
155 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
156 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
158 spi_message_init(&m);
159 memset(t, 0, sizeof(t));
163 spi_message_add_tail(&t[0], &m);
165 /* Busy and non busy words read */
166 t[1].rx_buf = busy_buf;
167 t[1].len = WL1251_BUSY_WORD_LEN;
168 spi_message_add_tail(&t[1], &m);
172 spi_message_add_tail(&t[2], &m);
174 spi_sync(wl_to_spi(wl), &m);
176 /* FIXME: check busy words */
178 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
179 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
182 static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
185 struct spi_transfer t[2];
186 struct spi_message m;
189 cmd = &wl->buffer_cmd;
192 *cmd |= WSPI_CMD_WRITE;
193 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
194 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
196 spi_message_init(&m);
197 memset(t, 0, sizeof(t));
200 t[0].len = sizeof(*cmd);
201 spi_message_add_tail(&t[0], &m);
205 spi_message_add_tail(&t[1], &m);
207 spi_sync(wl_to_spi(wl), &m);
209 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
210 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
213 static void wl1251_spi_enable_irq(struct wl1251 *wl)
215 return enable_irq(wl->irq);
218 static void wl1251_spi_disable_irq(struct wl1251 *wl)
220 return disable_irq(wl->irq);
223 static int wl1251_spi_set_power(struct wl1251 *wl, bool enable)
226 wl->set_power(enable);
231 static const struct wl1251_if_operations wl1251_spi_ops = {
232 .read = wl1251_spi_read,
233 .write = wl1251_spi_write,
234 .reset = wl1251_spi_reset_wake,
235 .enable_irq = wl1251_spi_enable_irq,
236 .disable_irq = wl1251_spi_disable_irq,
237 .power = wl1251_spi_set_power,
240 static int wl1251_spi_probe(struct spi_device *spi)
242 struct wl12xx_platform_data *pdata;
243 struct ieee80211_hw *hw;
247 pdata = spi->dev.platform_data;
249 wl1251_error("no platform data");
253 hw = wl1251_alloc_hw();
259 SET_IEEE80211_DEV(hw, &spi->dev);
260 spi_set_drvdata(spi, wl);
262 wl->if_ops = &wl1251_spi_ops;
264 /* This is the only SPI value that we need to set here, the rest
265 * comes from the board-peripherals file */
266 spi->bits_per_word = 32;
268 ret = spi_setup(spi);
270 wl1251_error("spi_setup failed");
274 wl->set_power = pdata->set_power;
275 if (!wl->set_power) {
276 wl1251_error("set power function missing in platform data");
282 wl1251_error("irq missing in platform data");
286 wl->use_eeprom = pdata->use_eeprom;
288 irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
289 ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
291 wl1251_error("request_irq() failed: %d", ret);
295 irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
297 ret = wl1251_init_ieee80211(wl);
304 free_irq(wl->irq, wl);
307 ieee80211_free_hw(hw);
312 static int wl1251_spi_remove(struct spi_device *spi)
314 struct wl1251 *wl = spi_get_drvdata(spi);
316 free_irq(wl->irq, wl);
322 static struct spi_driver wl1251_spi_driver = {
325 .owner = THIS_MODULE,
328 .probe = wl1251_spi_probe,
329 .remove = wl1251_spi_remove,
332 static int __init wl1251_spi_init(void)
336 ret = spi_register_driver(&wl1251_spi_driver);
338 wl1251_error("failed to register spi driver: %d", ret);
346 static void __exit wl1251_spi_exit(void)
348 spi_unregister_driver(&wl1251_spi_driver);
350 wl1251_notice("unloaded");
353 module_init(wl1251_spi_init);
354 module_exit(wl1251_spi_exit);
356 MODULE_LICENSE("GPL");
357 MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
358 MODULE_ALIAS("spi:wl1251");