compat-wireless-2010-03-10
[pandora-wifi.git] / drivers / net / wireless / wl12xx / wl1251_spi.c
1 /*
2  * This file is part of wl1251
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
6  * Contact: Kalle Valo <kalle.valo@nokia.com>
7  *
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.
11  *
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.
16  *
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
20  * 02110-1301 USA
21  *
22  */
23
24 #include <linux/irq.h>
25 #include <linux/module.h>
26 #include <linux/crc7.h>
27 #if (LINUX_VERSION_CODE == KERNEL_VERSION(2,6,28))
28 #include <linux/device.h>
29 #endif
30 #include <linux/spi/spi.h>
31 #include <linux/spi/wl12xx.h>
32
33 #include "wl1251.h"
34 #include "wl1251_reg.h"
35 #include "wl1251_spi.h"
36
37 static irqreturn_t wl1251_irq(int irq, void *cookie)
38 {
39         struct wl1251 *wl;
40
41         wl1251_debug(DEBUG_IRQ, "IRQ");
42
43         wl = cookie;
44
45         ieee80211_queue_work(wl->hw, &wl->irq_work);
46
47         return IRQ_HANDLED;
48 }
49
50 static struct spi_device *wl_to_spi(struct wl1251 *wl)
51 {
52         return wl->if_priv;
53 }
54
55 static void wl1251_spi_reset(struct wl1251 *wl)
56 {
57         u8 *cmd;
58         struct spi_transfer t;
59         struct spi_message m;
60
61         cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
62         if (!cmd) {
63                 wl1251_error("could not allocate cmd for spi reset");
64                 return;
65         }
66
67         memset(&t, 0, sizeof(t));
68         spi_message_init(&m);
69
70         memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
71
72         t.tx_buf = cmd;
73         t.len = WSPI_INIT_CMD_LEN;
74         spi_message_add_tail(&t, &m);
75
76         spi_sync(wl_to_spi(wl), &m);
77
78         wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
79 }
80
81 static void wl1251_spi_wake(struct wl1251 *wl)
82 {
83         u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
84         struct spi_transfer t;
85         struct spi_message m;
86
87         cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
88         if (!cmd) {
89                 wl1251_error("could not allocate cmd for spi init");
90                 return;
91         }
92
93         memset(crc, 0, sizeof(crc));
94         memset(&t, 0, sizeof(t));
95         spi_message_init(&m);
96
97         /*
98          * Set WSPI_INIT_COMMAND
99          * the data is being send from the MSB to LSB
100          */
101         cmd[2] = 0xff;
102         cmd[3] = 0xff;
103         cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
104         cmd[0] = 0;
105         cmd[7] = 0;
106         cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
107         cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
108
109         if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
110                 cmd[5] |=  WSPI_INIT_CMD_DIS_FIXEDBUSY;
111         else
112                 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
113
114         cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
115                 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
116
117         crc[0] = cmd[1];
118         crc[1] = cmd[0];
119         crc[2] = cmd[7];
120         crc[3] = cmd[6];
121         crc[4] = cmd[5];
122
123         cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
124         cmd[4] |= WSPI_INIT_CMD_END;
125
126         t.tx_buf = cmd;
127         t.len = WSPI_INIT_CMD_LEN;
128         spi_message_add_tail(&t, &m);
129
130         spi_sync(wl_to_spi(wl), &m);
131
132         wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
133 }
134
135 static void wl1251_spi_reset_wake(struct wl1251 *wl)
136 {
137         wl1251_spi_reset(wl);
138         wl1251_spi_wake(wl);
139 }
140
141 static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
142                             size_t len)
143 {
144         struct spi_transfer t[3];
145         struct spi_message m;
146         u8 *busy_buf;
147         u32 *cmd;
148
149         cmd = &wl->buffer_cmd;
150         busy_buf = wl->buffer_busyword;
151
152         *cmd = 0;
153         *cmd |= WSPI_CMD_READ;
154         *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
155         *cmd |= addr & WSPI_CMD_BYTE_ADDR;
156
157         spi_message_init(&m);
158         memset(t, 0, sizeof(t));
159
160         t[0].tx_buf = cmd;
161         t[0].len = 4;
162         spi_message_add_tail(&t[0], &m);
163
164         /* Busy and non busy words read */
165         t[1].rx_buf = busy_buf;
166         t[1].len = WL1251_BUSY_WORD_LEN;
167         spi_message_add_tail(&t[1], &m);
168
169         t[2].rx_buf = buf;
170         t[2].len = len;
171         spi_message_add_tail(&t[2], &m);
172
173         spi_sync(wl_to_spi(wl), &m);
174
175         /* FIXME: check busy words */
176
177         wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
178         wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
179 }
180
181 static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
182                              size_t len)
183 {
184         struct spi_transfer t[2];
185         struct spi_message m;
186         u32 *cmd;
187
188         cmd = &wl->buffer_cmd;
189
190         *cmd = 0;
191         *cmd |= WSPI_CMD_WRITE;
192         *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
193         *cmd |= addr & WSPI_CMD_BYTE_ADDR;
194
195         spi_message_init(&m);
196         memset(t, 0, sizeof(t));
197
198         t[0].tx_buf = cmd;
199         t[0].len = sizeof(*cmd);
200         spi_message_add_tail(&t[0], &m);
201
202         t[1].tx_buf = buf;
203         t[1].len = len;
204         spi_message_add_tail(&t[1], &m);
205
206         spi_sync(wl_to_spi(wl), &m);
207
208         wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
209         wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
210 }
211
212 static void wl1251_spi_enable_irq(struct wl1251 *wl)
213 {
214         return enable_irq(wl->irq);
215 }
216
217 static void wl1251_spi_disable_irq(struct wl1251 *wl)
218 {
219         return disable_irq(wl->irq);
220 }
221
222 static const struct wl1251_if_operations wl1251_spi_ops = {
223         .read = wl1251_spi_read,
224         .write = wl1251_spi_write,
225         .reset = wl1251_spi_reset_wake,
226         .enable_irq = wl1251_spi_enable_irq,
227         .disable_irq = wl1251_spi_disable_irq,
228 };
229
230 static int __devinit wl1251_spi_probe(struct spi_device *spi)
231 {
232         struct wl12xx_platform_data *pdata;
233         struct ieee80211_hw *hw;
234         struct wl1251 *wl;
235         int ret;
236
237         pdata = spi->dev.platform_data;
238         if (!pdata) {
239                 wl1251_error("no platform data");
240                 return -ENODEV;
241         }
242
243         hw = wl1251_alloc_hw();
244         if (IS_ERR(hw))
245                 return PTR_ERR(hw);
246
247         wl = hw->priv;
248
249         SET_IEEE80211_DEV(hw, &spi->dev);
250         dev_set_drvdata(&spi->dev, wl);
251         wl->if_priv = spi;
252         wl->if_ops = &wl1251_spi_ops;
253
254         /* This is the only SPI value that we need to set here, the rest
255          * comes from the board-peripherals file */
256         spi->bits_per_word = 32;
257
258         ret = spi_setup(spi);
259         if (ret < 0) {
260                 wl1251_error("spi_setup failed");
261                 goto out_free;
262         }
263
264         wl->set_power = pdata->set_power;
265         if (!wl->set_power) {
266                 wl1251_error("set power function missing in platform data");
267                 return -ENODEV;
268         }
269
270         wl->irq = spi->irq;
271         if (wl->irq < 0) {
272                 wl1251_error("irq missing in platform data");
273                 return -ENODEV;
274         }
275
276         wl->use_eeprom = pdata->use_eeprom;
277
278         ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
279         if (ret < 0) {
280                 wl1251_error("request_irq() failed: %d", ret);
281                 goto out_free;
282         }
283
284         set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
285
286         disable_irq(wl->irq);
287
288         ret = wl1251_init_ieee80211(wl);
289         if (ret)
290                 goto out_irq;
291
292         return 0;
293
294  out_irq:
295         free_irq(wl->irq, wl);
296
297  out_free:
298         ieee80211_free_hw(hw);
299
300         return ret;
301 }
302
303 static int __devexit wl1251_spi_remove(struct spi_device *spi)
304 {
305         struct wl1251 *wl = dev_get_drvdata(&spi->dev);
306
307         free_irq(wl->irq, wl);
308         wl1251_free_hw(wl);
309
310         return 0;
311 }
312
313 static struct spi_driver wl1251_spi_driver = {
314         .driver = {
315                 .name           = "wl1251",
316                 .bus            = &spi_bus_type,
317                 .owner          = THIS_MODULE,
318         },
319
320         .probe          = wl1251_spi_probe,
321         .remove         = __devexit_p(wl1251_spi_remove),
322 };
323
324 static int __init wl1251_spi_init(void)
325 {
326         int ret;
327
328         ret = spi_register_driver(&wl1251_spi_driver);
329         if (ret < 0) {
330                 wl1251_error("failed to register spi driver: %d", ret);
331                 goto out;
332         }
333
334 out:
335         return ret;
336 }
337
338 static void __exit wl1251_spi_exit(void)
339 {
340         spi_unregister_driver(&wl1251_spi_driver);
341
342         wl1251_notice("unloaded");
343 }
344
345 module_init(wl1251_spi_init);
346 module_exit(wl1251_spi_exit);
347
348 MODULE_LICENSE("GPL");
349 MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>");