mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / drivers / net / wireless / ti / wlcore / spi.c
blob5b287b7f96e641eb0bbc988c718fa2fe699a38f8
1 /*
2 * This file is part of wl1271
4 * Copyright (C) 2008-2009 Nokia Corporation
6 * Contact: Luciano Coelho <luciano.coelho@nokia.com>
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.
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.
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
24 #include <linux/interrupt.h>
25 #include <linux/irq.h>
26 #include <linux/module.h>
27 #include <linux/crc7.h>
28 #include <linux/spi/spi.h>
29 #include <linux/wl12xx.h>
30 #include <linux/platform_device.h>
31 #include <linux/slab.h>
33 #include "wlcore.h"
34 #include "wl12xx_80211.h"
35 #include "io.h"
37 #define WSPI_CMD_READ 0x40000000
38 #define WSPI_CMD_WRITE 0x00000000
39 #define WSPI_CMD_FIXED 0x20000000
40 #define WSPI_CMD_BYTE_LENGTH 0x1FFE0000
41 #define WSPI_CMD_BYTE_LENGTH_OFFSET 17
42 #define WSPI_CMD_BYTE_ADDR 0x0001FFFF
44 #define WSPI_INIT_CMD_CRC_LEN 5
46 #define WSPI_INIT_CMD_START 0x00
47 #define WSPI_INIT_CMD_TX 0x40
48 /* the extra bypass bit is sampled by the TNET as '1' */
49 #define WSPI_INIT_CMD_BYPASS_BIT 0x80
50 #define WSPI_INIT_CMD_FIXEDBUSY_LEN 0x07
51 #define WSPI_INIT_CMD_EN_FIXEDBUSY 0x80
52 #define WSPI_INIT_CMD_DIS_FIXEDBUSY 0x00
53 #define WSPI_INIT_CMD_IOD 0x40
54 #define WSPI_INIT_CMD_IP 0x20
55 #define WSPI_INIT_CMD_CS 0x10
56 #define WSPI_INIT_CMD_WS 0x08
57 #define WSPI_INIT_CMD_WSPI 0x01
58 #define WSPI_INIT_CMD_END 0x01
60 #define WSPI_INIT_CMD_LEN 8
62 #define HW_ACCESS_WSPI_FIXED_BUSY_LEN \
63 ((WL1271_BUSY_WORD_LEN - 4) / sizeof(u32))
64 #define HW_ACCESS_WSPI_INIT_CMD_MASK 0
66 /* HW limitation: maximum possible chunk size is 4095 bytes */
67 #define WSPI_MAX_CHUNK_SIZE 4092
70 * only support SPI for 12xx - this code should be reworked when 18xx
71 * support is introduced
73 #define SPI_AGGR_BUFFER_SIZE (4 * PAGE_SIZE)
75 /* Maximum number of SPI write chunks */
76 #define WSPI_MAX_NUM_OF_CHUNKS \
77 ((SPI_AGGR_BUFFER_SIZE / WSPI_MAX_CHUNK_SIZE) + 1)
80 struct wl12xx_spi_glue {
81 struct device *dev;
82 struct platform_device *core;
85 static void wl12xx_spi_reset(struct device *child)
87 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
88 u8 *cmd;
89 struct spi_transfer t;
90 struct spi_message m;
92 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
93 if (!cmd) {
94 dev_err(child->parent,
95 "could not allocate cmd for spi reset\n");
96 return;
99 memset(&t, 0, sizeof(t));
100 spi_message_init(&m);
102 memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
104 t.tx_buf = cmd;
105 t.len = WSPI_INIT_CMD_LEN;
106 spi_message_add_tail(&t, &m);
108 spi_sync(to_spi_device(glue->dev), &m);
110 kfree(cmd);
113 static void wl12xx_spi_init(struct device *child)
115 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
116 u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
117 struct spi_transfer t;
118 struct spi_message m;
120 cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
121 if (!cmd) {
122 dev_err(child->parent,
123 "could not allocate cmd for spi init\n");
124 return;
127 memset(crc, 0, sizeof(crc));
128 memset(&t, 0, sizeof(t));
129 spi_message_init(&m);
132 * Set WSPI_INIT_COMMAND
133 * the data is being send from the MSB to LSB
135 cmd[2] = 0xff;
136 cmd[3] = 0xff;
137 cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
138 cmd[0] = 0;
139 cmd[7] = 0;
140 cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
141 cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
143 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
144 cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
145 else
146 cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
148 cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
149 | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
151 crc[0] = cmd[1];
152 crc[1] = cmd[0];
153 crc[2] = cmd[7];
154 crc[3] = cmd[6];
155 crc[4] = cmd[5];
157 cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
158 cmd[4] |= WSPI_INIT_CMD_END;
160 t.tx_buf = cmd;
161 t.len = WSPI_INIT_CMD_LEN;
162 spi_message_add_tail(&t, &m);
164 spi_sync(to_spi_device(glue->dev), &m);
165 kfree(cmd);
168 #define WL1271_BUSY_WORD_TIMEOUT 1000
170 static int wl12xx_spi_read_busy(struct device *child)
172 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
173 struct wl1271 *wl = dev_get_drvdata(child);
174 struct spi_transfer t[1];
175 struct spi_message m;
176 u32 *busy_buf;
177 int num_busy_bytes = 0;
180 * Read further busy words from SPI until a non-busy word is
181 * encountered, then read the data itself into the buffer.
184 num_busy_bytes = WL1271_BUSY_WORD_TIMEOUT;
185 busy_buf = wl->buffer_busyword;
186 while (num_busy_bytes) {
187 num_busy_bytes--;
188 spi_message_init(&m);
189 memset(t, 0, sizeof(t));
190 t[0].rx_buf = busy_buf;
191 t[0].len = sizeof(u32);
192 t[0].cs_change = true;
193 spi_message_add_tail(&t[0], &m);
194 spi_sync(to_spi_device(glue->dev), &m);
196 if (*busy_buf & 0x1)
197 return 0;
200 /* The SPI bus is unresponsive, the read failed. */
201 dev_err(child->parent, "SPI read busy-word timeout!\n");
202 return -ETIMEDOUT;
205 static int __must_check wl12xx_spi_raw_read(struct device *child, int addr,
206 void *buf, size_t len, bool fixed)
208 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
209 struct wl1271 *wl = dev_get_drvdata(child);
210 struct spi_transfer t[2];
211 struct spi_message m;
212 u32 *busy_buf;
213 u32 *cmd;
214 u32 chunk_len;
216 while (len > 0) {
217 chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
219 cmd = &wl->buffer_cmd;
220 busy_buf = wl->buffer_busyword;
222 *cmd = 0;
223 *cmd |= WSPI_CMD_READ;
224 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
225 WSPI_CMD_BYTE_LENGTH;
226 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
228 if (fixed)
229 *cmd |= WSPI_CMD_FIXED;
231 spi_message_init(&m);
232 memset(t, 0, sizeof(t));
234 t[0].tx_buf = cmd;
235 t[0].len = 4;
236 t[0].cs_change = true;
237 spi_message_add_tail(&t[0], &m);
239 /* Busy and non busy words read */
240 t[1].rx_buf = busy_buf;
241 t[1].len = WL1271_BUSY_WORD_LEN;
242 t[1].cs_change = true;
243 spi_message_add_tail(&t[1], &m);
245 spi_sync(to_spi_device(glue->dev), &m);
247 if (!(busy_buf[WL1271_BUSY_WORD_CNT - 1] & 0x1) &&
248 wl12xx_spi_read_busy(child)) {
249 memset(buf, 0, chunk_len);
250 return 0;
253 spi_message_init(&m);
254 memset(t, 0, sizeof(t));
256 t[0].rx_buf = buf;
257 t[0].len = chunk_len;
258 t[0].cs_change = true;
259 spi_message_add_tail(&t[0], &m);
261 spi_sync(to_spi_device(glue->dev), &m);
263 if (!fixed)
264 addr += chunk_len;
265 buf += chunk_len;
266 len -= chunk_len;
269 return 0;
272 static int __must_check wl12xx_spi_raw_write(struct device *child, int addr,
273 void *buf, size_t len, bool fixed)
275 struct wl12xx_spi_glue *glue = dev_get_drvdata(child->parent);
276 /* SPI write buffers - 2 for each chunk */
277 struct spi_transfer t[2 * WSPI_MAX_NUM_OF_CHUNKS];
278 struct spi_message m;
279 u32 commands[WSPI_MAX_NUM_OF_CHUNKS]; /* 1 command per chunk */
280 u32 *cmd;
281 u32 chunk_len;
282 int i;
284 WARN_ON(len > SPI_AGGR_BUFFER_SIZE);
286 spi_message_init(&m);
287 memset(t, 0, sizeof(t));
289 cmd = &commands[0];
290 i = 0;
291 while (len > 0) {
292 chunk_len = min((size_t)WSPI_MAX_CHUNK_SIZE, len);
294 *cmd = 0;
295 *cmd |= WSPI_CMD_WRITE;
296 *cmd |= (chunk_len << WSPI_CMD_BYTE_LENGTH_OFFSET) &
297 WSPI_CMD_BYTE_LENGTH;
298 *cmd |= addr & WSPI_CMD_BYTE_ADDR;
300 if (fixed)
301 *cmd |= WSPI_CMD_FIXED;
303 t[i].tx_buf = cmd;
304 t[i].len = sizeof(*cmd);
305 spi_message_add_tail(&t[i++], &m);
307 t[i].tx_buf = buf;
308 t[i].len = chunk_len;
309 spi_message_add_tail(&t[i++], &m);
311 if (!fixed)
312 addr += chunk_len;
313 buf += chunk_len;
314 len -= chunk_len;
315 cmd++;
318 spi_sync(to_spi_device(glue->dev), &m);
320 return 0;
323 static struct wl1271_if_operations spi_ops = {
324 .read = wl12xx_spi_raw_read,
325 .write = wl12xx_spi_raw_write,
326 .reset = wl12xx_spi_reset,
327 .init = wl12xx_spi_init,
328 .set_block_size = NULL,
331 static int wl1271_probe(struct spi_device *spi)
333 struct wl12xx_spi_glue *glue;
334 struct wlcore_platdev_data *pdev_data;
335 struct resource res[1];
336 int ret = -ENOMEM;
338 pdev_data = kzalloc(sizeof(*pdev_data), GFP_KERNEL);
339 if (!pdev_data)
340 goto out;
342 pdev_data->pdata = spi->dev.platform_data;
343 if (!pdev_data->pdata) {
344 dev_err(&spi->dev, "no platform data\n");
345 ret = -ENODEV;
346 goto out_free_pdev_data;
349 pdev_data->if_ops = &spi_ops;
351 glue = kzalloc(sizeof(*glue), GFP_KERNEL);
352 if (!glue) {
353 dev_err(&spi->dev, "can't allocate glue\n");
354 goto out_free_pdev_data;
357 glue->dev = &spi->dev;
359 spi_set_drvdata(spi, glue);
361 /* This is the only SPI value that we need to set here, the rest
362 * comes from the board-peripherals file */
363 spi->bits_per_word = 32;
365 ret = spi_setup(spi);
366 if (ret < 0) {
367 dev_err(glue->dev, "spi_setup failed\n");
368 goto out_free_glue;
371 glue->core = platform_device_alloc("wl12xx", PLATFORM_DEVID_AUTO);
372 if (!glue->core) {
373 dev_err(glue->dev, "can't allocate platform_device\n");
374 ret = -ENOMEM;
375 goto out_free_glue;
378 glue->core->dev.parent = &spi->dev;
380 memset(res, 0x00, sizeof(res));
382 res[0].start = spi->irq;
383 res[0].flags = IORESOURCE_IRQ;
384 res[0].name = "irq";
386 ret = platform_device_add_resources(glue->core, res, ARRAY_SIZE(res));
387 if (ret) {
388 dev_err(glue->dev, "can't add resources\n");
389 goto out_dev_put;
392 ret = platform_device_add_data(glue->core, pdev_data,
393 sizeof(*pdev_data));
394 if (ret) {
395 dev_err(glue->dev, "can't add platform data\n");
396 goto out_dev_put;
399 ret = platform_device_add(glue->core);
400 if (ret) {
401 dev_err(glue->dev, "can't register platform device\n");
402 goto out_dev_put;
405 return 0;
407 out_dev_put:
408 platform_device_put(glue->core);
410 out_free_glue:
411 kfree(glue);
413 out_free_pdev_data:
414 kfree(pdev_data);
416 out:
417 return ret;
420 static int wl1271_remove(struct spi_device *spi)
422 struct wl12xx_spi_glue *glue = spi_get_drvdata(spi);
424 platform_device_unregister(glue->core);
425 kfree(glue);
427 return 0;
431 static struct spi_driver wl1271_spi_driver = {
432 .driver = {
433 .name = "wl1271_spi",
434 .owner = THIS_MODULE,
437 .probe = wl1271_probe,
438 .remove = wl1271_remove,
441 module_spi_driver(wl1271_spi_driver);
442 MODULE_LICENSE("GPL");
443 MODULE_AUTHOR("Luciano Coelho <coelho@ti.com>");
444 MODULE_AUTHOR("Juuso Oikarinen <juuso.oikarinen@nokia.com>");
445 MODULE_ALIAS("spi:wl1271");