xref: /openbmc/linux/drivers/net/wireless/ti/wl1251/spi.c (revision 1d207cd30b65fdd60d952cb9e100b6f776564f06)
190921014SLuciano Coelho /*
290921014SLuciano Coelho  * This file is part of wl1251
390921014SLuciano Coelho  *
490921014SLuciano Coelho  * Copyright (C) 2008 Nokia Corporation
590921014SLuciano Coelho  *
690921014SLuciano Coelho  * This program is free software; you can redistribute it and/or
790921014SLuciano Coelho  * modify it under the terms of the GNU General Public License
890921014SLuciano Coelho  * version 2 as published by the Free Software Foundation.
990921014SLuciano Coelho  *
1090921014SLuciano Coelho  * This program is distributed in the hope that it will be useful, but
1190921014SLuciano Coelho  * WITHOUT ANY WARRANTY; without even the implied warranty of
1290921014SLuciano Coelho  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1390921014SLuciano Coelho  * General Public License for more details.
1490921014SLuciano Coelho  *
1590921014SLuciano Coelho  * You should have received a copy of the GNU General Public License
1690921014SLuciano Coelho  * along with this program; if not, write to the Free Software
1790921014SLuciano Coelho  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
1890921014SLuciano Coelho  * 02110-1301 USA
1990921014SLuciano Coelho  *
2090921014SLuciano Coelho  */
2190921014SLuciano Coelho 
2290921014SLuciano Coelho #include <linux/interrupt.h>
2390921014SLuciano Coelho #include <linux/irq.h>
2490921014SLuciano Coelho #include <linux/module.h>
2590921014SLuciano Coelho #include <linux/slab.h>
2690921014SLuciano Coelho #include <linux/crc7.h>
2790921014SLuciano Coelho #include <linux/spi/spi.h>
2890921014SLuciano Coelho #include <linux/wl12xx.h>
29*1d207cd3SSebastian Reichel #include <linux/gpio.h>
3090921014SLuciano Coelho 
3190921014SLuciano Coelho #include "wl1251.h"
3290921014SLuciano Coelho #include "reg.h"
3390921014SLuciano Coelho #include "spi.h"
3490921014SLuciano Coelho 
3590921014SLuciano Coelho static irqreturn_t wl1251_irq(int irq, void *cookie)
3690921014SLuciano Coelho {
3790921014SLuciano Coelho 	struct wl1251 *wl;
3890921014SLuciano Coelho 
3990921014SLuciano Coelho 	wl1251_debug(DEBUG_IRQ, "IRQ");
4090921014SLuciano Coelho 
4190921014SLuciano Coelho 	wl = cookie;
4290921014SLuciano Coelho 
4390921014SLuciano Coelho 	ieee80211_queue_work(wl->hw, &wl->irq_work);
4490921014SLuciano Coelho 
4590921014SLuciano Coelho 	return IRQ_HANDLED;
4690921014SLuciano Coelho }
4790921014SLuciano Coelho 
4890921014SLuciano Coelho static struct spi_device *wl_to_spi(struct wl1251 *wl)
4990921014SLuciano Coelho {
5090921014SLuciano Coelho 	return wl->if_priv;
5190921014SLuciano Coelho }
5290921014SLuciano Coelho 
5390921014SLuciano Coelho static void wl1251_spi_reset(struct wl1251 *wl)
5490921014SLuciano Coelho {
5590921014SLuciano Coelho 	u8 *cmd;
5690921014SLuciano Coelho 	struct spi_transfer t;
5790921014SLuciano Coelho 	struct spi_message m;
5890921014SLuciano Coelho 
5990921014SLuciano Coelho 	cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
6090921014SLuciano Coelho 	if (!cmd) {
6190921014SLuciano Coelho 		wl1251_error("could not allocate cmd for spi reset");
6290921014SLuciano Coelho 		return;
6390921014SLuciano Coelho 	}
6490921014SLuciano Coelho 
6590921014SLuciano Coelho 	memset(&t, 0, sizeof(t));
6690921014SLuciano Coelho 	spi_message_init(&m);
6790921014SLuciano Coelho 
6890921014SLuciano Coelho 	memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
6990921014SLuciano Coelho 
7090921014SLuciano Coelho 	t.tx_buf = cmd;
7190921014SLuciano Coelho 	t.len = WSPI_INIT_CMD_LEN;
7290921014SLuciano Coelho 	spi_message_add_tail(&t, &m);
7390921014SLuciano Coelho 
7490921014SLuciano Coelho 	spi_sync(wl_to_spi(wl), &m);
7590921014SLuciano Coelho 
7690921014SLuciano Coelho 	wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
77a859e4d6SGrazvydas Ignotas 
78a859e4d6SGrazvydas Ignotas 	kfree(cmd);
7990921014SLuciano Coelho }
8090921014SLuciano Coelho 
8190921014SLuciano Coelho static void wl1251_spi_wake(struct wl1251 *wl)
8290921014SLuciano Coelho {
8390921014SLuciano Coelho 	u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd;
8490921014SLuciano Coelho 	struct spi_transfer t;
8590921014SLuciano Coelho 	struct spi_message m;
8690921014SLuciano Coelho 
8790921014SLuciano Coelho 	cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
8890921014SLuciano Coelho 	if (!cmd) {
8990921014SLuciano Coelho 		wl1251_error("could not allocate cmd for spi init");
9090921014SLuciano Coelho 		return;
9190921014SLuciano Coelho 	}
9290921014SLuciano Coelho 
9390921014SLuciano Coelho 	memset(crc, 0, sizeof(crc));
9490921014SLuciano Coelho 	memset(&t, 0, sizeof(t));
9590921014SLuciano Coelho 	spi_message_init(&m);
9690921014SLuciano Coelho 
97789e90e5SSachin Kamat 	/* Set WSPI_INIT_COMMAND
9890921014SLuciano Coelho 	 * the data is being send from the MSB to LSB
9990921014SLuciano Coelho 	 */
10090921014SLuciano Coelho 	cmd[2] = 0xff;
10190921014SLuciano Coelho 	cmd[3] = 0xff;
10290921014SLuciano Coelho 	cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
10390921014SLuciano Coelho 	cmd[0] = 0;
10490921014SLuciano Coelho 	cmd[7] = 0;
10590921014SLuciano Coelho 	cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
10690921014SLuciano Coelho 	cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
10790921014SLuciano Coelho 
10890921014SLuciano Coelho 	if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
10990921014SLuciano Coelho 		cmd[5] |=  WSPI_INIT_CMD_DIS_FIXEDBUSY;
11090921014SLuciano Coelho 	else
11190921014SLuciano Coelho 		cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
11290921014SLuciano Coelho 
11390921014SLuciano Coelho 	cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
11490921014SLuciano Coelho 		| WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
11590921014SLuciano Coelho 
11690921014SLuciano Coelho 	crc[0] = cmd[1];
11790921014SLuciano Coelho 	crc[1] = cmd[0];
11890921014SLuciano Coelho 	crc[2] = cmd[7];
11990921014SLuciano Coelho 	crc[3] = cmd[6];
12090921014SLuciano Coelho 	crc[4] = cmd[5];
12190921014SLuciano Coelho 
12290921014SLuciano Coelho 	cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1;
12390921014SLuciano Coelho 	cmd[4] |= WSPI_INIT_CMD_END;
12490921014SLuciano Coelho 
12590921014SLuciano Coelho 	t.tx_buf = cmd;
12690921014SLuciano Coelho 	t.len = WSPI_INIT_CMD_LEN;
12790921014SLuciano Coelho 	spi_message_add_tail(&t, &m);
12890921014SLuciano Coelho 
12990921014SLuciano Coelho 	spi_sync(wl_to_spi(wl), &m);
13090921014SLuciano Coelho 
13190921014SLuciano Coelho 	wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
132a859e4d6SGrazvydas Ignotas 
133a859e4d6SGrazvydas Ignotas 	kfree(cmd);
13490921014SLuciano Coelho }
13590921014SLuciano Coelho 
13690921014SLuciano Coelho static void wl1251_spi_reset_wake(struct wl1251 *wl)
13790921014SLuciano Coelho {
13890921014SLuciano Coelho 	wl1251_spi_reset(wl);
13990921014SLuciano Coelho 	wl1251_spi_wake(wl);
14090921014SLuciano Coelho }
14190921014SLuciano Coelho 
14290921014SLuciano Coelho static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
14390921014SLuciano Coelho 			    size_t len)
14490921014SLuciano Coelho {
14590921014SLuciano Coelho 	struct spi_transfer t[3];
14690921014SLuciano Coelho 	struct spi_message m;
14790921014SLuciano Coelho 	u8 *busy_buf;
14890921014SLuciano Coelho 	u32 *cmd;
14990921014SLuciano Coelho 
15090921014SLuciano Coelho 	cmd = &wl->buffer_cmd;
15190921014SLuciano Coelho 	busy_buf = wl->buffer_busyword;
15290921014SLuciano Coelho 
15390921014SLuciano Coelho 	*cmd = 0;
15490921014SLuciano Coelho 	*cmd |= WSPI_CMD_READ;
15590921014SLuciano Coelho 	*cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
15690921014SLuciano Coelho 	*cmd |= addr & WSPI_CMD_BYTE_ADDR;
15790921014SLuciano Coelho 
15890921014SLuciano Coelho 	spi_message_init(&m);
15990921014SLuciano Coelho 	memset(t, 0, sizeof(t));
16090921014SLuciano Coelho 
16190921014SLuciano Coelho 	t[0].tx_buf = cmd;
16290921014SLuciano Coelho 	t[0].len = 4;
16390921014SLuciano Coelho 	spi_message_add_tail(&t[0], &m);
16490921014SLuciano Coelho 
16590921014SLuciano Coelho 	/* Busy and non busy words read */
16690921014SLuciano Coelho 	t[1].rx_buf = busy_buf;
16790921014SLuciano Coelho 	t[1].len = WL1251_BUSY_WORD_LEN;
16890921014SLuciano Coelho 	spi_message_add_tail(&t[1], &m);
16990921014SLuciano Coelho 
17090921014SLuciano Coelho 	t[2].rx_buf = buf;
17190921014SLuciano Coelho 	t[2].len = len;
17290921014SLuciano Coelho 	spi_message_add_tail(&t[2], &m);
17390921014SLuciano Coelho 
17490921014SLuciano Coelho 	spi_sync(wl_to_spi(wl), &m);
17590921014SLuciano Coelho 
17690921014SLuciano Coelho 	/* FIXME: check busy words */
17790921014SLuciano Coelho 
17890921014SLuciano Coelho 	wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
17990921014SLuciano Coelho 	wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
18090921014SLuciano Coelho }
18190921014SLuciano Coelho 
18290921014SLuciano Coelho static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
18390921014SLuciano Coelho 			     size_t len)
18490921014SLuciano Coelho {
18590921014SLuciano Coelho 	struct spi_transfer t[2];
18690921014SLuciano Coelho 	struct spi_message m;
18790921014SLuciano Coelho 	u32 *cmd;
18890921014SLuciano Coelho 
18990921014SLuciano Coelho 	cmd = &wl->buffer_cmd;
19090921014SLuciano Coelho 
19190921014SLuciano Coelho 	*cmd = 0;
19290921014SLuciano Coelho 	*cmd |= WSPI_CMD_WRITE;
19390921014SLuciano Coelho 	*cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
19490921014SLuciano Coelho 	*cmd |= addr & WSPI_CMD_BYTE_ADDR;
19590921014SLuciano Coelho 
19690921014SLuciano Coelho 	spi_message_init(&m);
19790921014SLuciano Coelho 	memset(t, 0, sizeof(t));
19890921014SLuciano Coelho 
19990921014SLuciano Coelho 	t[0].tx_buf = cmd;
20090921014SLuciano Coelho 	t[0].len = sizeof(*cmd);
20190921014SLuciano Coelho 	spi_message_add_tail(&t[0], &m);
20290921014SLuciano Coelho 
20390921014SLuciano Coelho 	t[1].tx_buf = buf;
20490921014SLuciano Coelho 	t[1].len = len;
20590921014SLuciano Coelho 	spi_message_add_tail(&t[1], &m);
20690921014SLuciano Coelho 
20790921014SLuciano Coelho 	spi_sync(wl_to_spi(wl), &m);
20890921014SLuciano Coelho 
20990921014SLuciano Coelho 	wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
21090921014SLuciano Coelho 	wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
21190921014SLuciano Coelho }
21290921014SLuciano Coelho 
21390921014SLuciano Coelho static void wl1251_spi_enable_irq(struct wl1251 *wl)
21490921014SLuciano Coelho {
21590921014SLuciano Coelho 	return enable_irq(wl->irq);
21690921014SLuciano Coelho }
21790921014SLuciano Coelho 
21890921014SLuciano Coelho static void wl1251_spi_disable_irq(struct wl1251 *wl)
21990921014SLuciano Coelho {
22090921014SLuciano Coelho 	return disable_irq(wl->irq);
22190921014SLuciano Coelho }
22290921014SLuciano Coelho 
22390921014SLuciano Coelho static int wl1251_spi_set_power(struct wl1251 *wl, bool enable)
22490921014SLuciano Coelho {
225*1d207cd3SSebastian Reichel 	if (gpio_is_valid(wl->power_gpio))
226*1d207cd3SSebastian Reichel 		gpio_set_value(wl->power_gpio, enable);
22790921014SLuciano Coelho 
22890921014SLuciano Coelho 	return 0;
22990921014SLuciano Coelho }
23090921014SLuciano Coelho 
23190921014SLuciano Coelho static const struct wl1251_if_operations wl1251_spi_ops = {
23290921014SLuciano Coelho 	.read = wl1251_spi_read,
23390921014SLuciano Coelho 	.write = wl1251_spi_write,
23490921014SLuciano Coelho 	.reset = wl1251_spi_reset_wake,
23590921014SLuciano Coelho 	.enable_irq = wl1251_spi_enable_irq,
23690921014SLuciano Coelho 	.disable_irq = wl1251_spi_disable_irq,
23790921014SLuciano Coelho 	.power = wl1251_spi_set_power,
23890921014SLuciano Coelho };
23990921014SLuciano Coelho 
240b74324d1SBill Pemberton static int wl1251_spi_probe(struct spi_device *spi)
24190921014SLuciano Coelho {
242946651cbSLuciano Coelho 	struct wl1251_platform_data *pdata;
24390921014SLuciano Coelho 	struct ieee80211_hw *hw;
24490921014SLuciano Coelho 	struct wl1251 *wl;
24590921014SLuciano Coelho 	int ret;
24690921014SLuciano Coelho 
2475f4fe16bSJingoo Han 	pdata = dev_get_platdata(&spi->dev);
24890921014SLuciano Coelho 	if (!pdata) {
24990921014SLuciano Coelho 		wl1251_error("no platform data");
25090921014SLuciano Coelho 		return -ENODEV;
25190921014SLuciano Coelho 	}
25290921014SLuciano Coelho 
25390921014SLuciano Coelho 	hw = wl1251_alloc_hw();
25490921014SLuciano Coelho 	if (IS_ERR(hw))
25590921014SLuciano Coelho 		return PTR_ERR(hw);
25690921014SLuciano Coelho 
25790921014SLuciano Coelho 	wl = hw->priv;
25890921014SLuciano Coelho 
25990921014SLuciano Coelho 	SET_IEEE80211_DEV(hw, &spi->dev);
260c49b05acSJingoo Han 	spi_set_drvdata(spi, wl);
26190921014SLuciano Coelho 	wl->if_priv = spi;
26290921014SLuciano Coelho 	wl->if_ops = &wl1251_spi_ops;
26390921014SLuciano Coelho 
26490921014SLuciano Coelho 	/* This is the only SPI value that we need to set here, the rest
265789e90e5SSachin Kamat 	 * comes from the board-peripherals file
266789e90e5SSachin Kamat 	 */
26790921014SLuciano Coelho 	spi->bits_per_word = 32;
26890921014SLuciano Coelho 
26990921014SLuciano Coelho 	ret = spi_setup(spi);
27090921014SLuciano Coelho 	if (ret < 0) {
27190921014SLuciano Coelho 		wl1251_error("spi_setup failed");
27290921014SLuciano Coelho 		goto out_free;
27390921014SLuciano Coelho 	}
27490921014SLuciano Coelho 
275*1d207cd3SSebastian Reichel 	wl->power_gpio = pdata->power_gpio;
276*1d207cd3SSebastian Reichel 
277*1d207cd3SSebastian Reichel 	if (gpio_is_valid(wl->power_gpio)) {
278*1d207cd3SSebastian Reichel 		ret = devm_gpio_request_one(&spi->dev, wl->power_gpio,
279*1d207cd3SSebastian Reichel 					GPIOF_OUT_INIT_LOW, "wl1251 power");
280*1d207cd3SSebastian Reichel 		if (ret) {
281*1d207cd3SSebastian Reichel 			wl1251_error("Failed to request gpio: %d\n", ret);
282*1d207cd3SSebastian Reichel 			goto out_free;
283*1d207cd3SSebastian Reichel 		}
284*1d207cd3SSebastian Reichel 	} else {
285*1d207cd3SSebastian Reichel 		wl1251_error("set power gpio missing in platform data");
286*1d207cd3SSebastian Reichel 		ret = -ENODEV;
287*1d207cd3SSebastian Reichel 		goto out_free;
28890921014SLuciano Coelho 	}
28990921014SLuciano Coelho 
29090921014SLuciano Coelho 	wl->irq = spi->irq;
29190921014SLuciano Coelho 	if (wl->irq < 0) {
29290921014SLuciano Coelho 		wl1251_error("irq missing in platform data");
293*1d207cd3SSebastian Reichel 		ret = -ENODEV;
294*1d207cd3SSebastian Reichel 		goto out_free;
29590921014SLuciano Coelho 	}
29690921014SLuciano Coelho 
29790921014SLuciano Coelho 	wl->use_eeprom = pdata->use_eeprom;
29890921014SLuciano Coelho 
299f380f2c4SGrazvydas Ignotas 	irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
300*1d207cd3SSebastian Reichel 	ret = devm_request_irq(&spi->dev, wl->irq, wl1251_irq, 0,
301*1d207cd3SSebastian Reichel 							DRIVER_NAME, wl);
30290921014SLuciano Coelho 	if (ret < 0) {
30390921014SLuciano Coelho 		wl1251_error("request_irq() failed: %d", ret);
30490921014SLuciano Coelho 		goto out_free;
30590921014SLuciano Coelho 	}
30690921014SLuciano Coelho 
30790921014SLuciano Coelho 	irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
30890921014SLuciano Coelho 
30990921014SLuciano Coelho 	ret = wl1251_init_ieee80211(wl);
31090921014SLuciano Coelho 	if (ret)
311*1d207cd3SSebastian Reichel 		goto out_free;
31290921014SLuciano Coelho 
31390921014SLuciano Coelho 	return 0;
31490921014SLuciano Coelho 
31590921014SLuciano Coelho  out_free:
31690921014SLuciano Coelho 	ieee80211_free_hw(hw);
31790921014SLuciano Coelho 
31890921014SLuciano Coelho 	return ret;
31990921014SLuciano Coelho }
32090921014SLuciano Coelho 
321b74324d1SBill Pemberton static int wl1251_spi_remove(struct spi_device *spi)
32290921014SLuciano Coelho {
323c49b05acSJingoo Han 	struct wl1251 *wl = spi_get_drvdata(spi);
32490921014SLuciano Coelho 
32590921014SLuciano Coelho 	free_irq(wl->irq, wl);
32690921014SLuciano Coelho 	wl1251_free_hw(wl);
32790921014SLuciano Coelho 
32890921014SLuciano Coelho 	return 0;
32990921014SLuciano Coelho }
33090921014SLuciano Coelho 
33190921014SLuciano Coelho static struct spi_driver wl1251_spi_driver = {
33290921014SLuciano Coelho 	.driver = {
33390921014SLuciano Coelho 		.name		= DRIVER_NAME,
33490921014SLuciano Coelho 		.owner		= THIS_MODULE,
33590921014SLuciano Coelho 	},
33690921014SLuciano Coelho 
33790921014SLuciano Coelho 	.probe		= wl1251_spi_probe,
338b74324d1SBill Pemberton 	.remove		= wl1251_spi_remove,
33990921014SLuciano Coelho };
34090921014SLuciano Coelho 
34119a4e68aSSachin Kamat module_spi_driver(wl1251_spi_driver);
34290921014SLuciano Coelho 
34390921014SLuciano Coelho MODULE_LICENSE("GPL");
34490921014SLuciano Coelho MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
34590921014SLuciano Coelho MODULE_ALIAS("spi:wl1251");
346