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