blob: 6abcbc3f7fc7d6c207c79b3f4fc5e0c0bc23d206 [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>
Sebastian Reichel1d207cd2014-02-15 00:05:53 +010029#include <linux/gpio.h>
Kalle Valo2f01a1f2009-04-29 23:33:31 +030030
Kalle Valo13674112009-06-12 14:17:25 +030031#include "wl1251.h"
Kalle Valo9bc67722010-10-10 11:28:32 +030032#include "reg.h"
33#include "spi.h"
Kalle Valo2f01a1f2009-04-29 23:33:31 +030034
Bob Copelandb5ed9c12009-08-07 13:33:49 +030035static irqreturn_t wl1251_irq(int irq, void *cookie)
36{
37 struct wl1251 *wl;
38
39 wl1251_debug(DEBUG_IRQ, "IRQ");
40
41 wl = cookie;
42
Kalle Valo16e711f2009-08-07 13:35:04 +030043 ieee80211_queue_work(wl->hw, &wl->irq_work);
Bob Copelandb5ed9c12009-08-07 13:33:49 +030044
45 return IRQ_HANDLED;
46}
47
Bob Copelandaf8c78e2009-08-07 13:33:34 +030048static struct spi_device *wl_to_spi(struct wl1251 *wl)
49{
50 return wl->if_priv;
51}
52
Bob Copeland08d9f5722009-08-07 13:33:11 +030053static void wl1251_spi_reset(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030054{
55 u8 *cmd;
56 struct spi_transfer t;
57 struct spi_message m;
58
59 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
60 if (!cmd) {
Kalle Valo80301cd2009-06-12 14:17:39 +030061 wl1251_error("could not allocate cmd for spi reset");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030062 return;
63 }
64
65 memset(&t, 0, sizeof(t));
66 spi_message_init(&m);
67
68 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
69
70 t.tx_buf = cmd;
71 t.len = WSPI_INIT_CMD_LEN;
72 spi_message_add_tail(&t, &m);
73
Bob Copelandaf8c78e2009-08-07 13:33:34 +030074 spi_sync(wl_to_spi(wl), &m);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030075
Kalle Valo80301cd2009-06-12 14:17:39 +030076 wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
Grazvydas Ignotasa859e4d2012-06-16 22:26:48 +030077
78 kfree(cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +030079}
80
Bob Copeland8e639c02009-08-07 13:33:26 +030081static void wl1251_spi_wake(struct wl1251 *wl)
Kalle Valo2f01a1f2009-04-29 23:33:31 +030082{
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) {
Kalle Valo80301cd2009-06-12 14:17:39 +030089 wl1251_error("could not allocate cmd for spi init");
Kalle Valo2f01a1f2009-04-29 23:33:31 +030090 return;
91 }
92
93 memset(crc, 0, sizeof(crc));
94 memset(&t, 0, sizeof(t));
95 spi_message_init(&m);
96
Sachin Kamat789e90e2013-05-29 15:48:23 +053097 /* Set WSPI_INIT_COMMAND
Kalle Valo2f01a1f2009-04-29 23:33:31 +030098 * the data is being send from the MSB to LSB
99 */
100 cmd[2] = 0xff;
101 cmd[3] = 0xff;
102 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
103 cmd[0] = 0;
104 cmd[7] = 0;
105 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
106 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
107
108 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
109 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
110 else
111 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
112
113 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
114 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
115
116 crc[0] = cmd[1];
117 crc[1] = cmd[0];
118 crc[2] = cmd[7];
119 crc[3] = cmd[6];
120 crc[4] = cmd[5];
121
122 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
123 cmd[4] |= WSPI_INIT_CMD_END;
124
125 t.tx_buf = cmd;
126 t.len = WSPI_INIT_CMD_LEN;
127 spi_message_add_tail(&t, &m);
128
Bob Copelandaf8c78e2009-08-07 13:33:34 +0300129 spi_sync(wl_to_spi(wl), &m);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300130
Kalle Valo80301cd2009-06-12 14:17:39 +0300131 wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
Grazvydas Ignotasa859e4d2012-06-16 22:26:48 +0300132
133 kfree(cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300134}
135
Bob Copeland08d9f5722009-08-07 13:33:11 +0300136static void wl1251_spi_reset_wake(struct wl1251 *wl)
137{
138 wl1251_spi_reset(wl);
Bob Copeland8e639c02009-08-07 13:33:26 +0300139 wl1251_spi_wake(wl);
Bob Copeland08d9f5722009-08-07 13:33:11 +0300140}
141
Bob Copeland08d9f5722009-08-07 13:33:11 +0300142static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
143 size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300144{
145 struct spi_transfer t[3];
146 struct spi_message m;
Kalle Valo5262c122009-06-12 14:14:55 +0300147 u8 *busy_buf;
Kalle Valo56343a32009-06-12 14:14:47 +0300148 u32 *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300149
Kalle Valo56343a32009-06-12 14:14:47 +0300150 cmd = &wl->buffer_cmd;
Kalle Valo5262c122009-06-12 14:14:55 +0300151 busy_buf = wl->buffer_busyword;
Kalle Valo56343a32009-06-12 14:14:47 +0300152
153 *cmd = 0;
154 *cmd |= WSPI_CMD_READ;
155 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
156 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300157
158 spi_message_init(&m);
159 memset(t, 0, sizeof(t));
160
Kalle Valo56343a32009-06-12 14:14:47 +0300161 t[0].tx_buf = cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300162 t[0].len = 4;
163 spi_message_add_tail(&t[0], &m);
164
165 /* Busy and non busy words read */
166 t[1].rx_buf = busy_buf;
Kalle Valo80301cd2009-06-12 14:17:39 +0300167 t[1].len = WL1251_BUSY_WORD_LEN;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300168 spi_message_add_tail(&t[1], &m);
169
170 t[2].rx_buf = buf;
171 t[2].len = len;
172 spi_message_add_tail(&t[2], &m);
173
Bob Copelandaf8c78e2009-08-07 13:33:34 +0300174 spi_sync(wl_to_spi(wl), &m);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300175
176 /* FIXME: check busy words */
177
Kalle Valo80301cd2009-06-12 14:17:39 +0300178 wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
179 wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300180}
181
Bob Copeland08d9f5722009-08-07 13:33:11 +0300182static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
183 size_t len)
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300184{
185 struct spi_transfer t[2];
186 struct spi_message m;
Kalle Valo56343a32009-06-12 14:14:47 +0300187 u32 *cmd;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300188
Kalle Valo56343a32009-06-12 14:14:47 +0300189 cmd = &wl->buffer_cmd;
190
191 *cmd = 0;
192 *cmd |= WSPI_CMD_WRITE;
193 *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
194 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300195
196 spi_message_init(&m);
197 memset(t, 0, sizeof(t));
198
Kalle Valo56343a32009-06-12 14:14:47 +0300199 t[0].tx_buf = cmd;
200 t[0].len = sizeof(*cmd);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300201 spi_message_add_tail(&t[0], &m);
202
203 t[1].tx_buf = buf;
204 t[1].len = len;
205 spi_message_add_tail(&t[1], &m);
206
Bob Copelandaf8c78e2009-08-07 13:33:34 +0300207 spi_sync(wl_to_spi(wl), &m);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300208
Kalle Valo80301cd2009-06-12 14:17:39 +0300209 wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
210 wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
Kalle Valo2f01a1f2009-04-29 23:33:31 +0300211}
Bob Copeland08d9f5722009-08-07 13:33:11 +0300212
Bob Copelandb5ed9c12009-08-07 13:33:49 +0300213static void wl1251_spi_enable_irq(struct wl1251 *wl)
214{
215 return enable_irq(wl->irq);
216}
217
218static void wl1251_spi_disable_irq(struct wl1251 *wl)
219{
220 return disable_irq(wl->irq);
221}
222
Grazvydas Ignotascb7bbc72010-11-04 00:13:47 +0200223static int wl1251_spi_set_power(struct wl1251 *wl, bool enable)
224{
Sebastian Reichel1d207cd2014-02-15 00:05:53 +0100225 if (gpio_is_valid(wl->power_gpio))
226 gpio_set_value(wl->power_gpio, enable);
Grazvydas Ignotascb7bbc72010-11-04 00:13:47 +0200227
228 return 0;
229}
230
Bob Copelandaf8c78e2009-08-07 13:33:34 +0300231static const struct wl1251_if_operations wl1251_spi_ops = {
Bob Copeland08d9f5722009-08-07 13:33:11 +0300232 .read = wl1251_spi_read,
233 .write = wl1251_spi_write,
234 .reset = wl1251_spi_reset_wake,
Bob Copelandb5ed9c12009-08-07 13:33:49 +0300235 .enable_irq = wl1251_spi_enable_irq,
236 .disable_irq = wl1251_spi_disable_irq,
Grazvydas Ignotascb7bbc72010-11-04 00:13:47 +0200237 .power = wl1251_spi_set_power,
Bob Copeland08d9f5722009-08-07 13:33:11 +0300238};
Bob Copeland8e639c02009-08-07 13:33:26 +0300239
Bill Pembertonb74324d2012-12-03 09:56:42 -0500240static int wl1251_spi_probe(struct spi_device *spi)
Bob Copeland8e639c02009-08-07 13:33:26 +0300241{
Luciano Coelho946651c2014-02-15 00:05:52 +0100242 struct wl1251_platform_data *pdata;
Bob Copeland8e639c02009-08-07 13:33:26 +0300243 struct ieee80211_hw *hw;
244 struct wl1251 *wl;
245 int ret;
246
Jingoo Han5f4fe162013-09-10 17:57:05 +0900247 pdata = dev_get_platdata(&spi->dev);
Bob Copeland8e639c02009-08-07 13:33:26 +0300248 if (!pdata) {
249 wl1251_error("no platform data");
250 return -ENODEV;
251 }
252
253 hw = wl1251_alloc_hw();
254 if (IS_ERR(hw))
255 return PTR_ERR(hw);
256
257 wl = hw->priv;
258
259 SET_IEEE80211_DEV(hw, &spi->dev);
Jingoo Hanc49b05a2013-04-05 20:36:25 +0000260 spi_set_drvdata(spi, wl);
Bob Copelandaf8c78e2009-08-07 13:33:34 +0300261 wl->if_priv = spi;
Bob Copeland8e639c02009-08-07 13:33:26 +0300262 wl->if_ops = &wl1251_spi_ops;
263
264 /* This is the only SPI value that we need to set here, the rest
Sachin Kamat789e90e2013-05-29 15:48:23 +0530265 * comes from the board-peripherals file
266 */
Bob Copeland8e639c02009-08-07 13:33:26 +0300267 spi->bits_per_word = 32;
268
269 ret = spi_setup(spi);
270 if (ret < 0) {
271 wl1251_error("spi_setup failed");
272 goto out_free;
273 }
274
Sebastian Reichel1d207cd2014-02-15 00:05:53 +0100275 wl->power_gpio = pdata->power_gpio;
276
277 if (gpio_is_valid(wl->power_gpio)) {
278 ret = devm_gpio_request_one(&spi->dev, wl->power_gpio,
279 GPIOF_OUT_INIT_LOW, "wl1251 power");
280 if (ret) {
281 wl1251_error("Failed to request gpio: %d\n", ret);
282 goto out_free;
283 }
284 } else {
285 wl1251_error("set power gpio missing in platform data");
286 ret = -ENODEV;
287 goto out_free;
Bob Copeland8e639c02009-08-07 13:33:26 +0300288 }
289
290 wl->irq = spi->irq;
291 if (wl->irq < 0) {
292 wl1251_error("irq missing in platform data");
Sebastian Reichel1d207cd2014-02-15 00:05:53 +0100293 ret = -ENODEV;
294 goto out_free;
Bob Copeland8e639c02009-08-07 13:33:26 +0300295 }
296
David-John Willisc95cf3d02009-11-17 18:50:09 +0200297 wl->use_eeprom = pdata->use_eeprom;
298
Grazvydas Ignotasf380f2c2012-05-18 03:04:08 +0300299 irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
Sebastian Reichel1d207cd2014-02-15 00:05:53 +0100300 ret = devm_request_irq(&spi->dev, wl->irq, wl1251_irq, 0,
301 DRIVER_NAME, wl);
Bob Copeland8e639c02009-08-07 13:33:26 +0300302 if (ret < 0) {
303 wl1251_error("request_irq() failed: %d", ret);
304 goto out_free;
305 }
306
Thomas Gleixnerdced35a2011-03-28 17:49:12 +0200307 irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
Bob Copeland8e639c02009-08-07 13:33:26 +0300308
Bob Copeland8e639c02009-08-07 13:33:26 +0300309 ret = wl1251_init_ieee80211(wl);
310 if (ret)
Sebastian Reichel1d207cd2014-02-15 00:05:53 +0100311 goto out_free;
Bob Copeland8e639c02009-08-07 13:33:26 +0300312
313 return 0;
314
Bob Copeland8e639c02009-08-07 13:33:26 +0300315 out_free:
316 ieee80211_free_hw(hw);
317
318 return ret;
319}
320
Bill Pembertonb74324d2012-12-03 09:56:42 -0500321static int wl1251_spi_remove(struct spi_device *spi)
Bob Copeland8e639c02009-08-07 13:33:26 +0300322{
Jingoo Hanc49b05a2013-04-05 20:36:25 +0000323 struct wl1251 *wl = spi_get_drvdata(spi);
Bob Copeland8e639c02009-08-07 13:33:26 +0300324
Bob Copelandb5ed9c12009-08-07 13:33:49 +0300325 free_irq(wl->irq, wl);
Bob Copeland8e639c02009-08-07 13:33:26 +0300326 wl1251_free_hw(wl);
327
328 return 0;
329}
330
331static struct spi_driver wl1251_spi_driver = {
332 .driver = {
Kalle Valo7590a552010-04-02 15:31:46 +0300333 .name = DRIVER_NAME,
Bob Copeland8e639c02009-08-07 13:33:26 +0300334 .owner = THIS_MODULE,
335 },
336
337 .probe = wl1251_spi_probe,
Bill Pembertonb74324d2012-12-03 09:56:42 -0500338 .remove = wl1251_spi_remove,
Bob Copeland8e639c02009-08-07 13:33:26 +0300339};
340
Sachin Kamat19a4e682013-05-29 15:48:22 +0530341module_spi_driver(wl1251_spi_driver);
Bob Copeland8e639c02009-08-07 13:33:26 +0300342
343MODULE_LICENSE("GPL");
Kalle Valo31c726f2010-08-22 22:46:15 +0300344MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
Ameya Palandef148cfd2010-07-05 17:12:38 +0300345MODULE_ALIAS("spi:wl1251");