blob: 16f0c71f6d4cb84dbd42a274570d153f6e28ccd8 [file] [log] [blame]
Luciano Coelhof5fc0f82009-08-06 16:25:28 +03001/*
2 * This file is part of wl1271
3 *
4 * Copyright (C) 2008-2009 Nokia Corporation
5 *
6 * Contact: Luciano Coelho <luciano.coelho@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
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000024#include <linux/interrupt.h>
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +020025#include <linux/irq.h>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030026#include <linux/module.h>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030027#include <linux/crc7.h>
28#include <linux/spi/spi.h>
Ohad Ben-Cohenc1f9a092010-09-16 13:16:02 +020029#include <linux/wl12xx.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030031
Shahar Levi00d20102010-11-08 11:20:10 +000032#include "wl12xx.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030033#include "wl12xx_80211.h"
Shahar Levi00d20102010-11-08 11:20:10 +000034#include "io.h"
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030035
Shahar Levi00d20102010-11-08 11:20:10 +000036#include "reg.h"
Teemu Paasikivi760d9692010-02-22 08:38:25 +020037
38#define WSPI_CMD_READ 0x40000000
39#define WSPI_CMD_WRITE 0x00000000
40#define WSPI_CMD_FIXED 0x20000000
41#define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
42#define WSPI_CMD_BYTE_LENGTH_OFFSET 17
43#define WSPI_CMD_BYTE_ADDR 0x0001FFFF
44
45#define WSPI_INIT_CMD_CRC_LEN 5
46
47#define WSPI_INIT_CMD_START 0x00
48#define WSPI_INIT_CMD_TX 0x40
49/* the extra bypass bit is sampled by the TNET as '1' */
50#define WSPI_INIT_CMD_BYPASS_BIT 0x80
51#define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
52#define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
53#define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
54#define WSPI_INIT_CMD_IOD 0x40
55#define WSPI_INIT_CMD_IP 0x20
56#define WSPI_INIT_CMD_CS 0x10
57#define WSPI_INIT_CMD_WS 0x08
58#define WSPI_INIT_CMD_WSPI 0x01
59#define WSPI_INIT_CMD_END 0x01
60
61#define WSPI_INIT_CMD_LEN 8
62
63#define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
64 ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
65#define HW_ACCESS_WSPI_INIT_CMD_MASK 0
66
Ido Yariv5c57a902010-09-30 13:28:26 +020067/* HW limitation: maximum possible chunk size is 4095 bytes */
68#define WSPI_MAX_CHUNK_SIZE 4092
69
70#define WSPI_MAX_NUM_OF_CHUNKS (WL1271_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE)
71
Felipe Balbib65019f2011-10-04 23:36:47 +030072struct wl12xx_spi_glue {
73 struct device *dev;
74 struct wl1271 *wl;
75};
76
77static inline struct wl12xx_spi_glue *wl_to_glue(struct wl1271 *wl)
Teemu Paasikivi8197b712010-02-22 08:38:23 +020078{
79 return wl->if_priv;
80}
81
82static struct device *wl1271_spi_wl_to_dev(struct wl1271 *wl)
83{
Felipe Balbib65019f2011-10-04 23:36:47 +030084 return wl_to_glue(wl)->dev;
Teemu Paasikivi8197b712010-02-22 08:38:23 +020085}
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030086
Teemu Paasikivi760d9692010-02-22 08:38:25 +020087static void wl1271_spi_disable_interrupts(struct wl1271 *wl)
Teemu Paasikivi54f7e502010-02-22 08:38:22 +020088{
89 disable_irq(wl->irq);
90}
91
Teemu Paasikivi760d9692010-02-22 08:38:25 +020092static void wl1271_spi_enable_interrupts(struct wl1271 *wl)
Teemu Paasikivi54f7e502010-02-22 08:38:22 +020093{
94 enable_irq(wl->irq);
95}
96
Teemu Paasikivi760d9692010-02-22 08:38:25 +020097static void wl1271_spi_reset(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +030098{
Felipe Balbib65019f2011-10-04 23:36:47 +030099 struct wl12xx_spi_glue *glue = wl_to_glue(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300100 u8 *cmd;
101 struct spi_transfer t;
102 struct spi_message m;
103
104 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
105 if (!cmd) {
106 wl1271_error("could not allocate cmd for spi reset");
107 return;
108 }
109
110 memset(&t, 0, sizeof(t));
111 spi_message_init(&m);
112
113 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
114
115 t.tx_buf = cmd;
116 t.len = WSPI_INIT_CMD_LEN;
117 spi_message_add_tail(&t, &m);
118
Felipe Balbib65019f2011-10-04 23:36:47 +0300119 spi_sync(to_spi_device(glue->dev), &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300120
121 wl1271_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
Dan Carpenter0dd38662010-12-21 07:00:13 +0300122 kfree(cmd);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300123}
124
Teemu Paasikivi760d9692010-02-22 08:38:25 +0200125static void wl1271_spi_init(struct wl1271 *wl)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300126{
Felipe Balbib65019f2011-10-04 23:36:47 +0300127 struct wl12xx_spi_glue *glue = wl_to_glue(wl);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300128 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
129 struct spi_transfer t;
130 struct spi_message m;
131
132 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
133 if (!cmd) {
134 wl1271_error("could not allocate cmd for spi init");
135 return;
136 }
137
138 memset(crc, 0, sizeof(crc));
139 memset(&t, 0, sizeof(t));
140 spi_message_init(&m);
141
142 /*
143 * Set WSPI_INIT_COMMAND
144 * the data is being send from the MSB to LSB
145 */
146 cmd[2] = 0xff;
147 cmd[3] = 0xff;
148 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
149 cmd[0] = 0;
150 cmd[7] = 0;
151 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
152 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
153
154 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
155 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
156 else
157 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
158
159 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
160 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
161
162 crc[0] = cmd[1];
163 crc[1] = cmd[0];
164 crc[2] = cmd[7];
165 crc[3] = cmd[6];
166 crc[4] = cmd[5];
167
168 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
169 cmd[4] |= WSPI_INIT_CMD_END;
170
171 t.tx_buf = cmd;
172 t.len = WSPI_INIT_CMD_LEN;
173 spi_message_add_tail(&t, &m);
174
Felipe Balbib65019f2011-10-04 23:36:47 +0300175 spi_sync(to_spi_device(glue->dev), &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300176 wl1271_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
Kulikov Vasiliybb123612010-07-31 20:33:59 +0400177 kfree(cmd);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300178}
179
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300180#define WL1271_BUSY_WORD_TIMEOUT 1000
181
Juuso Oikarinen259da432010-03-26 12:53:18 +0200182static int wl1271_spi_read_busy(struct wl1271 *wl)
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300183{
Felipe Balbib65019f2011-10-04 23:36:47 +0300184 struct wl12xx_spi_glue *glue = wl_to_glue(wl);
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300185 struct spi_transfer t[1];
186 struct spi_message m;
187 u32 *busy_buf;
188 int num_busy_bytes = 0;
189
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300190 /*
191 * Read further busy words from SPI until a non-busy word is
192 * encountered, then read the data itself into the buffer.
193 */
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300194
195 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
196 busy_buf = wl->buffer_busyword;
197 while (num_busy_bytes) {
198 num_busy_bytes--;
199 spi_message_init(&m);
200 memset(t, 0, sizeof(t));
201 t[0].rx_buf = busy_buf;
202 t[0].len = sizeof(u32);
Juuso Oikarinen259da432010-03-26 12:53:18 +0200203 t[0].cs_change = true;
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300204 spi_message_add_tail(&t[0], &m);
Felipe Balbib65019f2011-10-04 23:36:47 +0300205 spi_sync(to_spi_device(glue->dev), &m);
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300206
Juuso Oikarinen259da432010-03-26 12:53:18 +0200207 if (*busy_buf & 0x1)
208 return 0;
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300209 }
210
211 /* The SPI bus is unresponsive, the read failed. */
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300212 wl1271_error("SPI read busy-word timeout!\n");
Juuso Oikarinen259da432010-03-26 12:53:18 +0200213 return -ETIMEDOUT;
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300214}
215
Teemu Paasikivi760d9692010-02-22 08:38:25 +0200216static void wl1271_spi_raw_read(struct wl1271 *wl, int addr, void *buf,
Juuso Oikarinen259da432010-03-26 12:53:18 +0200217 size_t len, bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300218{
Felipe Balbib65019f2011-10-04 23:36:47 +0300219 struct wl12xx_spi_glue *glue = wl_to_glue(wl);
Ido Yariv5c57a902010-09-30 13:28:26 +0200220 struct spi_transfer t[2];
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300221 struct spi_message m;
Juuso Oikarinen545f1da2009-10-08 21:56:23 +0300222 u32 *busy_buf;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300223 u32 *cmd;
Ido Yariv5c57a902010-09-30 13:28:26 +0200224 u32 chunk_len;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300225
Ido Yariv5c57a902010-09-30 13:28:26 +0200226 while (len > 0) {
227 chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300228
Ido Yariv5c57a902010-09-30 13:28:26 +0200229 cmd = &wl->buffer_cmd;
230 busy_buf = wl->buffer_busyword;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300231
Ido Yariv5c57a902010-09-30 13:28:26 +0200232 *cmd = 0;
233 *cmd |= WSPI_CMD_READ;
234 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
235 WSPI_CMD_BYTE_LENGTH;
236 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300237
Ido Yariv5c57a902010-09-30 13:28:26 +0200238 if (fixed)
239 *cmd |= WSPI_CMD_FIXED;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300240
Ido Yariv5c57a902010-09-30 13:28:26 +0200241 spi_message_init(&m);
242 memset(t, 0, sizeof(t));
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300243
Ido Yariv5c57a902010-09-30 13:28:26 +0200244 t[0].tx_buf = cmd;
245 t[0].len = 4;
246 t[0].cs_change = true;
247 spi_message_add_tail(&t[0], &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300248
Ido Yariv5c57a902010-09-30 13:28:26 +0200249 /* Busy and non busy words read */
250 t[1].rx_buf = busy_buf;
251 t[1].len = WL1271_BUSY_WORD_LEN;
252 t[1].cs_change = true;
253 spi_message_add_tail(&t[1], &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300254
Felipe Balbib65019f2011-10-04 23:36:47 +0300255 spi_sync(to_spi_device(glue->dev), &m);
Ido Yariv5c57a902010-09-30 13:28:26 +0200256
257 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
258 wl1271_spi_read_busy(wl)) {
259 memset(buf, 0, chunk_len);
260 return;
261 }
262
263 spi_message_init(&m);
264 memset(t, 0, sizeof(t));
265
266 t[0].rx_buf = buf;
267 t[0].len = chunk_len;
268 t[0].cs_change = true;
269 spi_message_add_tail(&t[0], &m);
270
Felipe Balbib65019f2011-10-04 23:36:47 +0300271 spi_sync(to_spi_device(glue->dev), &m);
Ido Yariv5c57a902010-09-30 13:28:26 +0200272
273 wl1271_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
274 wl1271_dump(DEBUG_SPI, "spi_read buf <- ", buf, chunk_len);
275
276 if (!fixed)
277 addr += chunk_len;
278 buf += chunk_len;
279 len -= chunk_len;
Juuso Oikarinen259da432010-03-26 12:53:18 +0200280 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300281}
282
Teemu Paasikivi760d9692010-02-22 08:38:25 +0200283static void wl1271_spi_raw_write(struct wl1271 *wl, int addr, void *buf,
Juuso Oikarinen74621412009-10-12 15:08:54 +0300284 size_t len, bool fixed)
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300285{
Felipe Balbib65019f2011-10-04 23:36:47 +0300286 struct wl12xx_spi_glue *glue = wl_to_glue(wl);
Ido Yariv5c57a902010-09-30 13:28:26 +0200287 struct spi_transfer t[2 * WSPI_MAX_NUM_OF_CHUNKS];
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300288 struct spi_message m;
Ido Yariv5c57a902010-09-30 13:28:26 +0200289 u32 commands[WSPI_MAX_NUM_OF_CHUNKS];
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300290 u32 *cmd;
Ido Yariv5c57a902010-09-30 13:28:26 +0200291 u32 chunk_len;
292 int i;
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300293
Ido Yariv5c57a902010-09-30 13:28:26 +0200294 WARN_ON(len > WL1271_AGGR_BUFFER_SIZE);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300295
296 spi_message_init(&m);
297 memset(t, 0, sizeof(t));
298
Ido Yariv5c57a902010-09-30 13:28:26 +0200299 cmd = &commands[0];
300 i = 0;
301 while (len > 0) {
302 chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300303
Ido Yariv5c57a902010-09-30 13:28:26 +0200304 *cmd = 0;
305 *cmd |= WSPI_CMD_WRITE;
306 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
307 WSPI_CMD_BYTE_LENGTH;
308 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
309
310 if (fixed)
311 *cmd |= WSPI_CMD_FIXED;
312
313 t[i].tx_buf = cmd;
314 t[i].len = sizeof(*cmd);
315 spi_message_add_tail(&t[i++], &m);
316
317 t[i].tx_buf = buf;
318 t[i].len = chunk_len;
319 spi_message_add_tail(&t[i++], &m);
320
321 wl1271_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
322 wl1271_dump(DEBUG_SPI, "spi_write buf -> ", buf, chunk_len);
323
324 if (!fixed)
325 addr += chunk_len;
326 buf += chunk_len;
327 len -= chunk_len;
328 cmd++;
329 }
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300330
Felipe Balbib65019f2011-10-04 23:36:47 +0300331 spi_sync(to_spi_device(glue->dev), &m);
Luciano Coelhof5fc0f82009-08-06 16:25:28 +0300332}
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200333
Ido Yariva6208652011-03-01 15:14:41 +0200334static irqreturn_t wl1271_hardirq(int irq, void *cookie)
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200335{
Ido Yariva6208652011-03-01 15:14:41 +0200336 struct wl1271 *wl = cookie;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200337 unsigned long flags;
338
339 wl1271_debug(DEBUG_IRQ, "IRQ");
340
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200341 /* complete the ELP completion */
342 spin_lock_irqsave(&wl->wl_lock, flags);
Ido Yariva6208652011-03-01 15:14:41 +0200343 set_bit(WL1271_FLAG_IRQ_RUNNING, &wl->flags);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200344 if (wl->elp_compl) {
345 complete(wl->elp_compl);
346 wl->elp_compl = NULL;
347 }
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200348 spin_unlock_irqrestore(&wl->wl_lock, flags);
349
Ido Yariva6208652011-03-01 15:14:41 +0200350 return IRQ_WAKE_THREAD;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200351}
352
Ohad Ben-Cohen2cc78ff2010-09-16 01:22:04 +0200353static int wl1271_spi_set_power(struct wl1271 *wl, bool enable)
Teemu Paasikivibecd5512010-03-18 12:26:27 +0200354{
355 if (wl->set_power)
356 wl->set_power(enable);
Ohad Ben-Cohen2cc78ff2010-09-16 01:22:04 +0200357
358 return 0;
Teemu Paasikivibecd5512010-03-18 12:26:27 +0200359}
360
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200361static struct wl1271_if_operations spi_ops = {
362 .read = wl1271_spi_raw_read,
363 .write = wl1271_spi_raw_write,
364 .reset = wl1271_spi_reset,
365 .init = wl1271_spi_init,
Teemu Paasikivibecd5512010-03-18 12:26:27 +0200366 .power = wl1271_spi_set_power,
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200367 .dev = wl1271_spi_wl_to_dev,
368 .enable_irq = wl1271_spi_enable_interrupts,
Luciano Coelhoa81159e2011-03-14 14:05:13 +0200369 .disable_irq = wl1271_spi_disable_interrupts,
370 .set_block_size = NULL,
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200371};
372
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200373static int __devinit wl1271_probe(struct spi_device *spi)
374{
Felipe Balbib65019f2011-10-04 23:36:47 +0300375 struct wl12xx_spi_glue *glue;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200376 struct wl12xx_platform_data *pdata;
377 struct ieee80211_hw *hw;
378 struct wl1271 *wl;
Ido Yariv341b7cd2011-03-31 10:07:01 +0200379 unsigned long irqflags;
Felipe Balbib65019f2011-10-04 23:36:47 +0300380 int ret = -ENOMEM;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200381
382 pdata = spi->dev.platform_data;
383 if (!pdata) {
384 wl1271_error("no platform data");
385 return -ENODEV;
386 }
387
Felipe Balbib65019f2011-10-04 23:36:47 +0300388 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
389 if (!glue) {
390 wl1271_error("can't allocate glue");
391 goto out;
392 }
393
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200394 hw = wl1271_alloc_hw();
Felipe Balbib65019f2011-10-04 23:36:47 +0300395 if (IS_ERR(hw)) {
396 ret = PTR_ERR(hw);
397 goto out_free_glue;
398 }
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200399
400 wl = hw->priv;
401
Felipe Balbib65019f2011-10-04 23:36:47 +0300402 glue->dev = &spi->dev;
403 glue->wl = wl;
404
405 spi_set_drvdata(spi, glue);
406 wl->if_priv = glue;
Teemu Paasikivi8197b712010-02-22 08:38:23 +0200407
408 wl->if_ops = &spi_ops;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200409
410 /* This is the only SPI value that we need to set here, the rest
411 * comes from the board-peripherals file */
412 spi->bits_per_word = 32;
413
414 ret = spi_setup(spi);
415 if (ret < 0) {
416 wl1271_error("spi_setup failed");
Felipe Balbib65019f2011-10-04 23:36:47 +0300417 goto out_free_hw;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200418 }
419
420 wl->set_power = pdata->set_power;
421 if (!wl->set_power) {
422 wl1271_error("set power function missing in platform data");
423 ret = -ENODEV;
Felipe Balbib65019f2011-10-04 23:36:47 +0300424 goto out_free_hw;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200425 }
426
Ohad Ben-Cohen15cea992010-09-16 01:31:51 +0200427 wl->ref_clock = pdata->board_ref_clock;
Shahar Levi5ea417a2011-03-06 16:32:11 +0200428 wl->tcxo_clock = pdata->board_tcxo_clock;
Ido Yariv341b7cd2011-03-31 10:07:01 +0200429 wl->platform_quirks = pdata->platform_quirks;
430
431 if (wl->platform_quirks & WL12XX_PLATFORM_QUIRK_EDGE_IRQ)
432 irqflags = IRQF_TRIGGER_RISING;
433 else
434 irqflags = IRQF_TRIGGER_HIGH | IRQF_ONESHOT;
Ohad Ben-Cohen15cea992010-09-16 01:31:51 +0200435
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200436 wl->irq = spi->irq;
437 if (wl->irq < 0) {
438 wl1271_error("irq missing in platform data");
439 ret = -ENODEV;
Felipe Balbib65019f2011-10-04 23:36:47 +0300440 goto out_free_hw;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200441 }
442
Ido Yariva6208652011-03-01 15:14:41 +0200443 ret = request_threaded_irq(wl->irq, wl1271_hardirq, wl1271_irq,
Ido Yariv341b7cd2011-03-31 10:07:01 +0200444 irqflags,
Ido Yariva6208652011-03-01 15:14:41 +0200445 DRIVER_NAME, wl);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200446 if (ret < 0) {
447 wl1271_error("request_irq() failed: %d", ret);
Felipe Balbib65019f2011-10-04 23:36:47 +0300448 goto out_free_hw;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200449 }
450
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200451 disable_irq(wl->irq);
452
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200453 ret = wl1271_init_ieee80211(wl);
454 if (ret)
Juuso Oikarinena1dd8182010-03-18 12:26:31 +0200455 goto out_irq;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200456
457 ret = wl1271_register_hw(wl);
458 if (ret)
Juuso Oikarinena1dd8182010-03-18 12:26:31 +0200459 goto out_irq;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200460
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200461 return 0;
462
Felipe Balbib65019f2011-10-04 23:36:47 +0300463out_irq:
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200464 free_irq(wl->irq, wl);
465
Felipe Balbib65019f2011-10-04 23:36:47 +0300466out_free_hw:
Teemu Paasikivi3b56dd62010-03-18 12:26:46 +0200467 wl1271_free_hw(wl);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200468
Felipe Balbib65019f2011-10-04 23:36:47 +0300469out_free_glue:
470 kfree(glue);
471out:
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200472 return ret;
473}
474
475static int __devexit wl1271_remove(struct spi_device *spi)
476{
Felipe Balbib65019f2011-10-04 23:36:47 +0300477 struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
478 struct wl1271 *wl = glue->wl;
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200479
Teemu Paasikivi3b56dd62010-03-18 12:26:46 +0200480 wl1271_unregister_hw(wl);
Juuso Oikarinen4e23b112010-08-13 04:46:48 +0200481 free_irq(wl->irq, wl);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200482 wl1271_free_hw(wl);
Felipe Balbib65019f2011-10-04 23:36:47 +0300483 kfree(glue);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200484
485 return 0;
486}
487
488
489static struct spi_driver wl1271_spi_driver = {
490 .driver = {
Luciano Coelho7fdd50d2010-03-26 12:53:10 +0200491 .name = "wl1271_spi",
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200492 .bus = &spi_bus_type,
493 .owner = THIS_MODULE,
494 },
495
496 .probe = wl1271_probe,
497 .remove = __devexit_p(wl1271_remove),
498};
499
500static int __init wl1271_init(void)
501{
Felipe Balbiba2274c2011-05-14 00:26:23 +0300502 return spi_register_driver(&wl1271_spi_driver);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200503}
504
505static void __exit wl1271_exit(void)
506{
507 spi_unregister_driver(&wl1271_spi_driver);
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200508}
509
510module_init(wl1271_init);
511module_exit(wl1271_exit);
512
513MODULE_LICENSE("GPL");
Luciano Coelho5245e3a2011-03-30 21:31:39 +0300514MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
Teemu Paasikivi2d5e82b2010-02-22 08:38:21 +0200515MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
Arik Nemtsovc302b2c2011-08-17 10:45:48 +0300516MODULE_FIRMWARE(WL127X_FW_NAME);
Shahar Levi5aa42342011-03-06 16:32:07 +0200517MODULE_FIRMWARE(WL128X_FW_NAME);
Ameya Palandef148cfd2010-07-05 17:12:38 +0300518MODULE_ALIAS("spi:wl1271");