blob: 1342f81e683d1498bb16d5904d032009244bed58 [file] [log] [blame]
Kalle Valo2f01a1f2009-04-29 23:33:31 +03001/*
Kalle Valo80301cd2009-06-12 14:17:39 +03002 * This file is part of wl1251
Kalle Valo2f01a1f2009-04-29 23:33:31 +03003 *
4 * Copyright (C) 2008 Nokia Corporation
5 *
Kalle Valo2f01a1f2009-04-29 23:33:31 +03006 * 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
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000022#include <linux/interrupt.h>
Bob Copeland8e639c02009-08-07 13:33:26 +030023#include <linux/irq.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030024#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030026#include <linux/crc7.h>
27#include <linux/spi/spi.h>
Ohad Ben-Cohenc1f9a092010-09-16 13:16:02 +020028#include <linux/wl12xx.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030029
Kalle Valo13674112009-06-12 14:17:25 +030030#include "wl1251.h"
Kalle Valo9bc67722010-10-10 11:28:32 +030031#include "reg.h"
32#include "spi.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030033
Bob Copelandb5ed9c12009-08-07 13:33:49 +030034static 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
Kalle Valo16e711f2009-08-07 13:35:04 +030042 ieee80211_queue_work(wl->hw, &wl->irq_work);
Bob Copelandb5ed9c12009-08-07 13:33:49 +030043
44 return IRQ_HANDLED;
45}
46
Bob Copelandaf8c78e2009-08-07 13:33:34 +030047static struct spi_device *wl_to_spi(struct wl1251 *wl)
48{
49 return wl->if_priv;
50}
51
Bob Copeland08d9f5722009-08-07 13:33:11 +030052static void wl1251_spi_reset(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030053{
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) {
Kalle Valo80301cd2009-06-12 14:17:39 +030060 wl1251_error("could not allocate cmd for spi reset");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030061 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
Bob Copelandaf8c78e2009-08-07 13:33:34 +030073 spi_sync(wl_to_spi(wl), &m);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030074
Kalle Valo80301cd2009-06-12 14:17:39 +030075 wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
Grazvydas Ignotasa859e4d2012-06-16 22:26:48 +030076
77 kfree(cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030078}
79
Bob Copeland8e639c02009-08-07 13:33:26 +030080static void wl1251_spi_wake(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030081{
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) {
Kalle Valo80301cd2009-06-12 14:17:39 +030088 wl1251_error("could not allocate cmd for spi init");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030089 return;
90 }
91
92 memset(crc, 0, sizeof(crc));
93 memset(&t, 0, sizeof(t));
94 spi_message_init(&m);
95
Sachin Kamat789e90e2013-05-29 15:48:23 +053096 /* Set WSPI_INIT_COMMAND
Kalle Valo2f01a1f2009-04-29 23:33:31 +030097 * 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
Bob Copelandaf8c78e2009-08-07 13:33:34 +0300128 spi_sync(wl_to_spi(wl), &m);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300129
Kalle Valo80301cd2009-06-12 14:17:39 +0300130 wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
Grazvydas Ignotasa859e4d2012-06-16 22:26:48 +0300131
132 kfree(cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300133}
134
Bob Copeland08d9f5722009-08-07 13:33:11 +0300135static void wl1251_spi_reset_wake(struct wl1251 *wl)
136{
137 wl1251_spi_reset(wl);
Bob Copeland8e639c02009-08-07 13:33:26 +0300138 wl1251_spi_wake(wl);
Bob Copeland08d9f5722009-08-07 13:33:11 +0300139}
140
Bob Copeland08d9f5722009-08-07 13:33:11 +0300141static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
142 size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300143{
144 struct spi_transfer t[3];
145 struct spi_message m;
Kalle Valo5262c122009-06-12 14:14:55 +0300146 u8 *busy_buf;
Kalle Valo56343a32009-06-12 14:14:47 +0300147 u32 *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300148
Kalle Valo56343a32009-06-12 14:14:47 +0300149 cmd = &wl->buffer_cmd;
Kalle Valo5262c122009-06-12 14:14:55 +0300150 busy_buf = wl->buffer_busyword;
Kalle Valo56343a32009-06-12 14:14:47 +0300151
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;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300156
157 spi_message_init(&m);
158 memset(t, 0, sizeof(t));
159
Kalle Valo56343a32009-06-12 14:14:47 +0300160 t[0].tx_buf = cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300161 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;
Kalle Valo80301cd2009-06-12 14:17:39 +0300166 t[1].len = WL1251_BUSY_WORD_LEN;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300167 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
Bob Copelandaf8c78e2009-08-07 13:33:34 +0300173 spi_sync(wl_to_spi(wl), &m);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300174
175 /* FIXME: check busy words */
176
Kalle Valo80301cd2009-06-12 14:17:39 +0300177 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
178 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300179}
180
Bob Copeland08d9f5722009-08-07 13:33:11 +0300181static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
182 size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300183{
184 struct spi_transfer t[2];
185 struct spi_message m;
Kalle Valo56343a32009-06-12 14:14:47 +0300186 u32 *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300187
Kalle Valo56343a32009-06-12 14:14:47 +0300188 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;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300194
195 spi_message_init(&m);
196 memset(t, 0, sizeof(t));
197
Kalle Valo56343a32009-06-12 14:14:47 +0300198 t[0].tx_buf = cmd;
199 t[0].len = sizeof(*cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300200 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
Bob Copelandaf8c78e2009-08-07 13:33:34 +0300206 spi_sync(wl_to_spi(wl), &m);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300207
Kalle Valo80301cd2009-06-12 14:17:39 +0300208 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
209 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300210}
Bob Copeland08d9f5722009-08-07 13:33:11 +0300211
Bob Copelandb5ed9c12009-08-07 13:33:49 +0300212static void wl1251_spi_enable_irq(struct wl1251 *wl)
213{
214 return enable_irq(wl->irq);
215}
216
217static void wl1251_spi_disable_irq(struct wl1251 *wl)
218{
219 return disable_irq(wl->irq);
220}
221
Grazvydas Ignotascb7bbc72010-11-04 00:13:47 +0200222static 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
Bob Copelandaf8c78e2009-08-07 13:33:34 +0300230static const struct wl1251_if_operations wl1251_spi_ops = {
Bob Copeland08d9f5722009-08-07 13:33:11 +0300231 .read = wl1251_spi_read,
232 .write = wl1251_spi_write,
233 .reset = wl1251_spi_reset_wake,
Bob Copelandb5ed9c12009-08-07 13:33:49 +0300234 .enable_irq = wl1251_spi_enable_irq,
235 .disable_irq = wl1251_spi_disable_irq,
Grazvydas Ignotascb7bbc72010-11-04 00:13:47 +0200236 .power = wl1251_spi_set_power,
Bob Copeland08d9f5722009-08-07 13:33:11 +0300237};
Bob Copeland8e639c02009-08-07 13:33:26 +0300238
Bill Pembertonb74324d2012-12-03 09:56:42 -0500239static int wl1251_spi_probe(struct spi_device *spi)
Bob Copeland8e639c02009-08-07 13:33:26 +0300240{
241 struct wl12xx_platform_data *pdata;
242 struct ieee80211_hw *hw;
243 struct wl1251 *wl;
244 int ret;
245
Jingoo Han5f4fe162013-09-10 17:57:05 +0900246 pdata = dev_get_platdata(&spi->dev);
Bob Copeland8e639c02009-08-07 13:33:26 +0300247 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);
Jingoo Hanc49b05a2013-04-05 20:36:25 +0000259 spi_set_drvdata(spi, wl);
Bob Copelandaf8c78e2009-08-07 13:33:34 +0300260 wl->if_priv = spi;
Bob Copeland8e639c02009-08-07 13:33:26 +0300261 wl->if_ops = &wl1251_spi_ops;
262
263 /* This is the only SPI value that we need to set here, the rest
Sachin Kamat789e90e2013-05-29 15:48:23 +0530264 * comes from the board-peripherals file
265 */
Bob Copeland8e639c02009-08-07 13:33:26 +0300266 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
David-John Willisc95cf3d02009-11-17 18:50:09 +0200286 wl->use_eeprom = pdata->use_eeprom;
287
Grazvydas Ignotasf380f2c2012-05-18 03:04:08 +0300288 irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
Bob Copeland8e639c02009-08-07 13:33:26 +0300289 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
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200295 irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
Bob Copeland8e639c02009-08-07 13:33:26 +0300296
Bob Copeland8e639c02009-08-07 13:33:26 +0300297 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
Bill Pembertonb74324d2012-12-03 09:56:42 -0500312static int wl1251_spi_remove(struct spi_device *spi)
Bob Copeland8e639c02009-08-07 13:33:26 +0300313{
Jingoo Hanc49b05a2013-04-05 20:36:25 +0000314 struct wl1251 *wl = spi_get_drvdata(spi);
Bob Copeland8e639c02009-08-07 13:33:26 +0300315
Bob Copelandb5ed9c12009-08-07 13:33:49 +0300316 free_irq(wl->irq, wl);
Bob Copeland8e639c02009-08-07 13:33:26 +0300317 wl1251_free_hw(wl);
318
319 return 0;
320}
321
322static struct spi_driver wl1251_spi_driver = {
323 .driver = {
Kalle Valo7590a552010-04-02 15:31:46 +0300324 .name = DRIVER_NAME,
Bob Copeland8e639c02009-08-07 13:33:26 +0300325 .owner = THIS_MODULE,
326 },
327
328 .probe = wl1251_spi_probe,
Bill Pembertonb74324d2012-12-03 09:56:42 -0500329 .remove = wl1251_spi_remove,
Bob Copeland8e639c02009-08-07 13:33:26 +0300330};
331
Sachin Kamat19a4e682013-05-29 15:48:22 +0530332module_spi_driver(wl1251_spi_driver);
Bob Copeland8e639c02009-08-07 13:33:26 +0300333
334MODULE_LICENSE("GPL");
Kalle Valo31c726f2010-08-22 22:46:15 +0300335MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
Ameya Palandef148cfd2010-07-05 17:12:38 +0300336MODULE_ALIAS("spi:wl1251");