]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/ti/wl1251/spi.c
Merge remote-tracking branch 'slave-dma/next'
[karo-tx-linux.git] / drivers / net / wireless / ti / wl1251 / spi.c
1 /*
2  * This file is part of wl1251
3  *
4  * Copyright (C) 2008 Nokia Corporation
5  *
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.
9  *
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.
14  *
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
18  * 02110-1301 USA
19  *
20  */
21
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>
29
30 #include "wl1251.h"
31 #include "reg.h"
32 #include "spi.h"
33
34 static 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
42         ieee80211_queue_work(wl->hw, &wl->irq_work);
43
44         return IRQ_HANDLED;
45 }
46
47 static struct spi_device *wl_to_spi(struct wl1251 *wl)
48 {
49         return wl->if_priv;
50 }
51
52 static void wl1251_spi_reset(struct wl1251 *wl)
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) {
60                 wl1251_error("could not allocate cmd for spi reset");
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
73         spi_sync(wl_to_spi(wl), &m);
74
75         wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
76
77         kfree(cmd);
78 }
79
80 static void wl1251_spi_wake(struct wl1251 *wl)
81 {
82         u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
83         struct spi_transfer t;
84         struct spi_message m;
85
86         cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
87         if (!cmd) {
88                 wl1251_error("could not allocate cmd for spi init");
89                 return;
90         }
91
92         memset(crc, 0, sizeof(crc));
93         memset(&t, 0, sizeof(t));
94         spi_message_init(&m);
95
96         /* Set WSPI_INIT_COMMAND
97          * the data is being send from the MSB to LSB
98          */
99         cmd[2] = 0xff;
100         cmd[3] = 0xff;
101         cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
102         cmd[0] = 0;
103         cmd[7] = 0;
104         cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
105         cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
106
107         if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
108                 cmd[5] |=  WSPI_INIT_CMD_DIS_FIXEDBUSY;
109         else
110                 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
111
112         cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
113                 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
114
115         crc[0] = cmd[1];
116         crc[1] = cmd[0];
117         crc[2] = cmd[7];
118         crc[3] = cmd[6];
119         crc[4] = cmd[5];
120
121         cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
122         cmd[4] |= WSPI_INIT_CMD_END;
123
124         t.tx_buf = cmd;
125         t.len = WSPI_INIT_CMD_LEN;
126         spi_message_add_tail(&t, &m);
127
128         spi_sync(wl_to_spi(wl), &m);
129
130         wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
131
132         kfree(cmd);
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 int wl1251_spi_set_power(struct wl1251 *wl, bool enable)
223 {
224         if (wl->set_power)
225                 wl->set_power(enable);
226
227         return 0;
228 }
229
230 static const struct wl1251_if_operations wl1251_spi_ops = {
231         .read = wl1251_spi_read,
232         .write = wl1251_spi_write,
233         .reset = wl1251_spi_reset_wake,
234         .enable_irq = wl1251_spi_enable_irq,
235         .disable_irq = wl1251_spi_disable_irq,
236         .power = wl1251_spi_set_power,
237 };
238
239 static int wl1251_spi_probe(struct spi_device *spi)
240 {
241         struct wl12xx_platform_data *pdata;
242         struct ieee80211_hw *hw;
243         struct wl1251 *wl;
244         int ret;
245
246         pdata = spi->dev.platform_data;
247         if (!pdata) {
248                 wl1251_error("no platform data");
249                 return -ENODEV;
250         }
251
252         hw = wl1251_alloc_hw();
253         if (IS_ERR(hw))
254                 return PTR_ERR(hw);
255
256         wl = hw->priv;
257
258         SET_IEEE80211_DEV(hw, &spi->dev);
259         spi_set_drvdata(spi, wl);
260         wl->if_priv = spi;
261         wl->if_ops = &wl1251_spi_ops;
262
263         /* This is the only SPI value that we need to set here, the rest
264          * comes from the board-peripherals file
265          */
266         spi->bits_per_word = 32;
267
268         ret = spi_setup(spi);
269         if (ret < 0) {
270                 wl1251_error("spi_setup failed");
271                 goto out_free;
272         }
273
274         wl->set_power = pdata->set_power;
275         if (!wl->set_power) {
276                 wl1251_error("set power function missing in platform data");
277                 return -ENODEV;
278         }
279
280         wl->irq = spi->irq;
281         if (wl->irq < 0) {
282                 wl1251_error("irq missing in platform data");
283                 return -ENODEV;
284         }
285
286         wl->use_eeprom = pdata->use_eeprom;
287
288         irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
289         ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl);
290         if (ret < 0) {
291                 wl1251_error("request_irq() failed: %d", ret);
292                 goto out_free;
293         }
294
295         irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
296
297         ret = wl1251_init_ieee80211(wl);
298         if (ret)
299                 goto out_irq;
300
301         return 0;
302
303  out_irq:
304         free_irq(wl->irq, wl);
305
306  out_free:
307         ieee80211_free_hw(hw);
308
309         return ret;
310 }
311
312 static int wl1251_spi_remove(struct spi_device *spi)
313 {
314         struct wl1251 *wl = spi_get_drvdata(spi);
315
316         free_irq(wl->irq, wl);
317         wl1251_free_hw(wl);
318
319         return 0;
320 }
321
322 static struct spi_driver wl1251_spi_driver = {
323         .driver = {
324                 .name           = DRIVER_NAME,
325                 .owner          = THIS_MODULE,
326         },
327
328         .probe          = wl1251_spi_probe,
329         .remove         = wl1251_spi_remove,
330 };
331
332 module_spi_driver(wl1251_spi_driver);
333
334 MODULE_LICENSE("GPL");
335 MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
336 MODULE_ALIAS("spi:wl1251");