12b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
290921014SLuciano Coelho /*
390921014SLuciano Coelho * This file is part of wl1251
490921014SLuciano Coelho *
590921014SLuciano Coelho * Copyright (C) 2008 Nokia Corporation
690921014SLuciano Coelho */
790921014SLuciano Coelho
8*a1d0b953SDmitry Torokhov #include <linux/err.h>
990921014SLuciano Coelho #include <linux/interrupt.h>
1090921014SLuciano Coelho #include <linux/irq.h>
1190921014SLuciano Coelho #include <linux/module.h>
1290921014SLuciano Coelho #include <linux/slab.h>
13e757201bSGeorge Spelvin #include <linux/swab.h>
1490921014SLuciano Coelho #include <linux/crc7.h>
1590921014SLuciano Coelho #include <linux/spi/spi.h>
16*a1d0b953SDmitry Torokhov #include <linux/gpio/consumer.h>
1707bbca6fSSebastian Reichel #include <linux/of.h>
18e4c2e09eSSebastian Reichel #include <linux/regulator/consumer.h>
1990921014SLuciano Coelho
2090921014SLuciano Coelho #include "wl1251.h"
2190921014SLuciano Coelho #include "reg.h"
2290921014SLuciano Coelho #include "spi.h"
2390921014SLuciano Coelho
24*a1d0b953SDmitry Torokhov struct wl1251_spi {
25*a1d0b953SDmitry Torokhov struct spi_device *spi;
26*a1d0b953SDmitry Torokhov struct gpio_desc *power_gpio;
27*a1d0b953SDmitry Torokhov };
28*a1d0b953SDmitry Torokhov
wl1251_irq(int irq,void * cookie)2990921014SLuciano Coelho static irqreturn_t wl1251_irq(int irq, void *cookie)
3090921014SLuciano Coelho {
3190921014SLuciano Coelho struct wl1251 *wl;
3290921014SLuciano Coelho
3390921014SLuciano Coelho wl1251_debug(DEBUG_IRQ, "IRQ");
3490921014SLuciano Coelho
3590921014SLuciano Coelho wl = cookie;
3690921014SLuciano Coelho
3790921014SLuciano Coelho ieee80211_queue_work(wl->hw, &wl->irq_work);
3890921014SLuciano Coelho
3990921014SLuciano Coelho return IRQ_HANDLED;
4090921014SLuciano Coelho }
4190921014SLuciano Coelho
wl1251_spi_reset(struct wl1251 * wl)4290921014SLuciano Coelho static void wl1251_spi_reset(struct wl1251 *wl)
4390921014SLuciano Coelho {
44*a1d0b953SDmitry Torokhov struct wl1251_spi *wl_spi = wl->if_priv;
4590921014SLuciano Coelho u8 *cmd;
4690921014SLuciano Coelho struct spi_transfer t;
4790921014SLuciano Coelho struct spi_message m;
4890921014SLuciano Coelho
4990921014SLuciano Coelho cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
5090921014SLuciano Coelho if (!cmd) {
5190921014SLuciano Coelho wl1251_error("could not allocate cmd for spi reset");
5290921014SLuciano Coelho return;
5390921014SLuciano Coelho }
5490921014SLuciano Coelho
5590921014SLuciano Coelho memset(&t, 0, sizeof(t));
5690921014SLuciano Coelho spi_message_init(&m);
5790921014SLuciano Coelho
5890921014SLuciano Coelho memset(cmd, 0xff, WSPI_INIT_CMD_LEN);
5990921014SLuciano Coelho
6090921014SLuciano Coelho t.tx_buf = cmd;
6190921014SLuciano Coelho t.len = WSPI_INIT_CMD_LEN;
6290921014SLuciano Coelho spi_message_add_tail(&t, &m);
6390921014SLuciano Coelho
64*a1d0b953SDmitry Torokhov spi_sync(wl_spi->spi, &m);
6590921014SLuciano Coelho
6690921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN);
67a859e4d6SGrazvydas Ignotas
68a859e4d6SGrazvydas Ignotas kfree(cmd);
6990921014SLuciano Coelho }
7090921014SLuciano Coelho
wl1251_spi_wake(struct wl1251 * wl)7190921014SLuciano Coelho static void wl1251_spi_wake(struct wl1251 *wl)
7290921014SLuciano Coelho {
73*a1d0b953SDmitry Torokhov struct wl1251_spi *wl_spi = wl->if_priv;
7490921014SLuciano Coelho struct spi_transfer t;
7590921014SLuciano Coelho struct spi_message m;
76e757201bSGeorge Spelvin u8 *cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL);
7790921014SLuciano Coelho
7890921014SLuciano Coelho if (!cmd) {
7990921014SLuciano Coelho wl1251_error("could not allocate cmd for spi init");
8090921014SLuciano Coelho return;
8190921014SLuciano Coelho }
8290921014SLuciano Coelho
8390921014SLuciano Coelho memset(&t, 0, sizeof(t));
8490921014SLuciano Coelho spi_message_init(&m);
8590921014SLuciano Coelho
86789e90e5SSachin Kamat /* Set WSPI_INIT_COMMAND
8790921014SLuciano Coelho * the data is being send from the MSB to LSB
8890921014SLuciano Coelho */
89e757201bSGeorge Spelvin cmd[0] = 0xff;
90e757201bSGeorge Spelvin cmd[1] = 0xff;
91e757201bSGeorge Spelvin cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX;
92e757201bSGeorge Spelvin cmd[3] = 0;
93e757201bSGeorge Spelvin cmd[4] = 0;
94e757201bSGeorge Spelvin cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3;
95e757201bSGeorge Spelvin cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN;
9690921014SLuciano Coelho
97e757201bSGeorge Spelvin cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS
9890921014SLuciano Coelho | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS;
9990921014SLuciano Coelho
100e757201bSGeorge Spelvin if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0)
101e757201bSGeorge Spelvin cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY;
102e757201bSGeorge Spelvin else
103e757201bSGeorge Spelvin cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY;
10490921014SLuciano Coelho
105e757201bSGeorge Spelvin cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END;
106e757201bSGeorge Spelvin /*
107e757201bSGeorge Spelvin * The above is the logical order; it must actually be stored
108e757201bSGeorge Spelvin * in the buffer byte-swapped.
109e757201bSGeorge Spelvin */
110e757201bSGeorge Spelvin __swab32s((u32 *)cmd);
111e757201bSGeorge Spelvin __swab32s((u32 *)cmd+1);
11290921014SLuciano Coelho
11390921014SLuciano Coelho t.tx_buf = cmd;
11490921014SLuciano Coelho t.len = WSPI_INIT_CMD_LEN;
11590921014SLuciano Coelho spi_message_add_tail(&t, &m);
11690921014SLuciano Coelho
117*a1d0b953SDmitry Torokhov spi_sync(wl_spi->spi, &m);
11890921014SLuciano Coelho
11990921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN);
120a859e4d6SGrazvydas Ignotas
121a859e4d6SGrazvydas Ignotas kfree(cmd);
12290921014SLuciano Coelho }
12390921014SLuciano Coelho
wl1251_spi_reset_wake(struct wl1251 * wl)12490921014SLuciano Coelho static void wl1251_spi_reset_wake(struct wl1251 *wl)
12590921014SLuciano Coelho {
12690921014SLuciano Coelho wl1251_spi_reset(wl);
12790921014SLuciano Coelho wl1251_spi_wake(wl);
12890921014SLuciano Coelho }
12990921014SLuciano Coelho
wl1251_spi_read(struct wl1251 * wl,int addr,void * buf,size_t len)13090921014SLuciano Coelho static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf,
13190921014SLuciano Coelho size_t len)
13290921014SLuciano Coelho {
133*a1d0b953SDmitry Torokhov struct wl1251_spi *wl_spi = wl->if_priv;
13490921014SLuciano Coelho struct spi_transfer t[3];
13590921014SLuciano Coelho struct spi_message m;
13690921014SLuciano Coelho u8 *busy_buf;
13790921014SLuciano Coelho u32 *cmd;
13890921014SLuciano Coelho
13990921014SLuciano Coelho cmd = &wl->buffer_cmd;
14090921014SLuciano Coelho busy_buf = wl->buffer_busyword;
14190921014SLuciano Coelho
14290921014SLuciano Coelho *cmd = 0;
14390921014SLuciano Coelho *cmd |= WSPI_CMD_READ;
14490921014SLuciano Coelho *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
14590921014SLuciano Coelho *cmd |= addr & WSPI_CMD_BYTE_ADDR;
14690921014SLuciano Coelho
14790921014SLuciano Coelho spi_message_init(&m);
14890921014SLuciano Coelho memset(t, 0, sizeof(t));
14990921014SLuciano Coelho
15090921014SLuciano Coelho t[0].tx_buf = cmd;
15190921014SLuciano Coelho t[0].len = 4;
15290921014SLuciano Coelho spi_message_add_tail(&t[0], &m);
15390921014SLuciano Coelho
15490921014SLuciano Coelho /* Busy and non busy words read */
15590921014SLuciano Coelho t[1].rx_buf = busy_buf;
15690921014SLuciano Coelho t[1].len = WL1251_BUSY_WORD_LEN;
15790921014SLuciano Coelho spi_message_add_tail(&t[1], &m);
15890921014SLuciano Coelho
15990921014SLuciano Coelho t[2].rx_buf = buf;
16090921014SLuciano Coelho t[2].len = len;
16190921014SLuciano Coelho spi_message_add_tail(&t[2], &m);
16290921014SLuciano Coelho
163*a1d0b953SDmitry Torokhov spi_sync(wl_spi->spi, &m);
16490921014SLuciano Coelho
16590921014SLuciano Coelho /* FIXME: check busy words */
16690921014SLuciano Coelho
16790921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd));
16890921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len);
16990921014SLuciano Coelho }
17090921014SLuciano Coelho
wl1251_spi_write(struct wl1251 * wl,int addr,void * buf,size_t len)17190921014SLuciano Coelho static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf,
17290921014SLuciano Coelho size_t len)
17390921014SLuciano Coelho {
174*a1d0b953SDmitry Torokhov struct wl1251_spi *wl_spi = wl->if_priv;
17590921014SLuciano Coelho struct spi_transfer t[2];
17690921014SLuciano Coelho struct spi_message m;
17790921014SLuciano Coelho u32 *cmd;
17890921014SLuciano Coelho
17990921014SLuciano Coelho cmd = &wl->buffer_cmd;
18090921014SLuciano Coelho
18190921014SLuciano Coelho *cmd = 0;
18290921014SLuciano Coelho *cmd |= WSPI_CMD_WRITE;
18390921014SLuciano Coelho *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH;
18490921014SLuciano Coelho *cmd |= addr & WSPI_CMD_BYTE_ADDR;
18590921014SLuciano Coelho
18690921014SLuciano Coelho spi_message_init(&m);
18790921014SLuciano Coelho memset(t, 0, sizeof(t));
18890921014SLuciano Coelho
18990921014SLuciano Coelho t[0].tx_buf = cmd;
19090921014SLuciano Coelho t[0].len = sizeof(*cmd);
19190921014SLuciano Coelho spi_message_add_tail(&t[0], &m);
19290921014SLuciano Coelho
19390921014SLuciano Coelho t[1].tx_buf = buf;
19490921014SLuciano Coelho t[1].len = len;
19590921014SLuciano Coelho spi_message_add_tail(&t[1], &m);
19690921014SLuciano Coelho
197*a1d0b953SDmitry Torokhov spi_sync(wl_spi->spi, &m);
19890921014SLuciano Coelho
19990921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd));
20090921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len);
20190921014SLuciano Coelho }
20290921014SLuciano Coelho
wl1251_spi_enable_irq(struct wl1251 * wl)20390921014SLuciano Coelho static void wl1251_spi_enable_irq(struct wl1251 *wl)
20490921014SLuciano Coelho {
20590921014SLuciano Coelho return enable_irq(wl->irq);
20690921014SLuciano Coelho }
20790921014SLuciano Coelho
wl1251_spi_disable_irq(struct wl1251 * wl)20890921014SLuciano Coelho static void wl1251_spi_disable_irq(struct wl1251 *wl)
20990921014SLuciano Coelho {
21090921014SLuciano Coelho return disable_irq(wl->irq);
21190921014SLuciano Coelho }
21290921014SLuciano Coelho
wl1251_spi_set_power(struct wl1251 * wl,bool enable)21390921014SLuciano Coelho static int wl1251_spi_set_power(struct wl1251 *wl, bool enable)
21490921014SLuciano Coelho {
215*a1d0b953SDmitry Torokhov struct wl1251_spi *wl_spi = wl->if_priv;
216*a1d0b953SDmitry Torokhov
217*a1d0b953SDmitry Torokhov if (wl_spi->power_gpio)
218*a1d0b953SDmitry Torokhov gpiod_set_value_cansleep(wl_spi->power_gpio, enable);
21990921014SLuciano Coelho
22090921014SLuciano Coelho return 0;
22190921014SLuciano Coelho }
22290921014SLuciano Coelho
22390921014SLuciano Coelho static const struct wl1251_if_operations wl1251_spi_ops = {
22490921014SLuciano Coelho .read = wl1251_spi_read,
22590921014SLuciano Coelho .write = wl1251_spi_write,
22690921014SLuciano Coelho .reset = wl1251_spi_reset_wake,
22790921014SLuciano Coelho .enable_irq = wl1251_spi_enable_irq,
22890921014SLuciano Coelho .disable_irq = wl1251_spi_disable_irq,
22990921014SLuciano Coelho .power = wl1251_spi_set_power,
23090921014SLuciano Coelho };
23190921014SLuciano Coelho
wl1251_spi_probe(struct spi_device * spi)232b74324d1SBill Pemberton static int wl1251_spi_probe(struct spi_device *spi)
23390921014SLuciano Coelho {
23407bbca6fSSebastian Reichel struct device_node *np = spi->dev.of_node;
23590921014SLuciano Coelho struct ieee80211_hw *hw;
236*a1d0b953SDmitry Torokhov struct wl1251_spi *wl_spi;
23790921014SLuciano Coelho struct wl1251 *wl;
23890921014SLuciano Coelho int ret;
23990921014SLuciano Coelho
24006463f6eSDmitry Torokhov if (!np)
24190921014SLuciano Coelho return -ENODEV;
24290921014SLuciano Coelho
243*a1d0b953SDmitry Torokhov wl_spi = devm_kzalloc(&spi->dev, sizeof(*wl_spi), GFP_KERNEL);
244*a1d0b953SDmitry Torokhov if (!wl_spi)
245*a1d0b953SDmitry Torokhov return -ENOMEM;
246*a1d0b953SDmitry Torokhov
247*a1d0b953SDmitry Torokhov wl_spi->spi = spi;
248*a1d0b953SDmitry Torokhov
24990921014SLuciano Coelho hw = wl1251_alloc_hw();
25090921014SLuciano Coelho if (IS_ERR(hw))
25190921014SLuciano Coelho return PTR_ERR(hw);
25290921014SLuciano Coelho
25390921014SLuciano Coelho wl = hw->priv;
25490921014SLuciano Coelho
25590921014SLuciano Coelho SET_IEEE80211_DEV(hw, &spi->dev);
256c49b05acSJingoo Han spi_set_drvdata(spi, wl);
257*a1d0b953SDmitry Torokhov wl->if_priv = wl_spi;
25890921014SLuciano Coelho wl->if_ops = &wl1251_spi_ops;
25990921014SLuciano Coelho
26090921014SLuciano Coelho /* This is the only SPI value that we need to set here, the rest
261789e90e5SSachin Kamat * comes from the board-peripherals file
262789e90e5SSachin Kamat */
26390921014SLuciano Coelho spi->bits_per_word = 32;
26490921014SLuciano Coelho
26590921014SLuciano Coelho ret = spi_setup(spi);
26690921014SLuciano Coelho if (ret < 0) {
26790921014SLuciano Coelho wl1251_error("spi_setup failed");
26890921014SLuciano Coelho goto out_free;
26990921014SLuciano Coelho }
27090921014SLuciano Coelho
27107bbca6fSSebastian Reichel wl->use_eeprom = of_property_read_bool(np, "ti,wl1251-has-eeprom");
27207bbca6fSSebastian Reichel
273*a1d0b953SDmitry Torokhov wl_spi->power_gpio = devm_gpiod_get_optional(&spi->dev, "ti,power",
274*a1d0b953SDmitry Torokhov GPIOD_OUT_LOW);
275*a1d0b953SDmitry Torokhov ret = PTR_ERR_OR_ZERO(wl_spi->power_gpio);
2761d207cd3SSebastian Reichel if (ret) {
277*a1d0b953SDmitry Torokhov if (ret != -EPROBE_DEFER)
2781d207cd3SSebastian Reichel wl1251_error("Failed to request gpio: %d\n", ret);
2791d207cd3SSebastian Reichel goto out_free;
2801d207cd3SSebastian Reichel }
281*a1d0b953SDmitry Torokhov
282*a1d0b953SDmitry Torokhov gpiod_set_consumer_name(wl_spi->power_gpio, "wl1251 power");
28390921014SLuciano Coelho
28490921014SLuciano Coelho wl->irq = spi->irq;
28590921014SLuciano Coelho if (wl->irq < 0) {
28690921014SLuciano Coelho wl1251_error("irq missing in platform data");
2871d207cd3SSebastian Reichel ret = -ENODEV;
2881d207cd3SSebastian Reichel goto out_free;
28990921014SLuciano Coelho }
29090921014SLuciano Coelho
291f380f2c4SGrazvydas Ignotas irq_set_status_flags(wl->irq, IRQ_NOAUTOEN);
2921d207cd3SSebastian Reichel ret = devm_request_irq(&spi->dev, wl->irq, wl1251_irq, 0,
2931d207cd3SSebastian Reichel DRIVER_NAME, wl);
29490921014SLuciano Coelho if (ret < 0) {
29590921014SLuciano Coelho wl1251_error("request_irq() failed: %d", ret);
29690921014SLuciano Coelho goto out_free;
29790921014SLuciano Coelho }
29890921014SLuciano Coelho
29990921014SLuciano Coelho irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING);
30090921014SLuciano Coelho
301e4c2e09eSSebastian Reichel wl->vio = devm_regulator_get(&spi->dev, "vio");
302e4c2e09eSSebastian Reichel if (IS_ERR(wl->vio)) {
303e4c2e09eSSebastian Reichel ret = PTR_ERR(wl->vio);
304e4c2e09eSSebastian Reichel wl1251_error("vio regulator missing: %d", ret);
305e4c2e09eSSebastian Reichel goto out_free;
306e4c2e09eSSebastian Reichel }
307e4c2e09eSSebastian Reichel
308e4c2e09eSSebastian Reichel ret = regulator_enable(wl->vio);
30990921014SLuciano Coelho if (ret)
3101d207cd3SSebastian Reichel goto out_free;
31190921014SLuciano Coelho
312e4c2e09eSSebastian Reichel ret = wl1251_init_ieee80211(wl);
313e4c2e09eSSebastian Reichel if (ret)
314e4c2e09eSSebastian Reichel goto disable_regulator;
315e4c2e09eSSebastian Reichel
31690921014SLuciano Coelho return 0;
31790921014SLuciano Coelho
318e4c2e09eSSebastian Reichel disable_regulator:
319e4c2e09eSSebastian Reichel regulator_disable(wl->vio);
32090921014SLuciano Coelho out_free:
32190921014SLuciano Coelho ieee80211_free_hw(hw);
32290921014SLuciano Coelho
32390921014SLuciano Coelho return ret;
32490921014SLuciano Coelho }
32590921014SLuciano Coelho
wl1251_spi_remove(struct spi_device * spi)326a0386bbaSUwe Kleine-König static void wl1251_spi_remove(struct spi_device *spi)
32790921014SLuciano Coelho {
328c49b05acSJingoo Han struct wl1251 *wl = spi_get_drvdata(spi);
32990921014SLuciano Coelho
33090921014SLuciano Coelho wl1251_free_hw(wl);
331e4c2e09eSSebastian Reichel regulator_disable(wl->vio);
33290921014SLuciano Coelho }
33390921014SLuciano Coelho
33490921014SLuciano Coelho static struct spi_driver wl1251_spi_driver = {
33590921014SLuciano Coelho .driver = {
33690921014SLuciano Coelho .name = DRIVER_NAME,
33790921014SLuciano Coelho },
33890921014SLuciano Coelho
33990921014SLuciano Coelho .probe = wl1251_spi_probe,
340b74324d1SBill Pemberton .remove = wl1251_spi_remove,
34190921014SLuciano Coelho };
34290921014SLuciano Coelho
34319a4e68aSSachin Kamat module_spi_driver(wl1251_spi_driver);
34490921014SLuciano Coelho
34590921014SLuciano Coelho MODULE_LICENSE("GPL");
34690921014SLuciano Coelho MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
34790921014SLuciano Coelho MODULE_ALIAS("spi:wl1251");
348