1*2b27bdccSThomas 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 890921014SLuciano Coelho #include <linux/interrupt.h> 990921014SLuciano Coelho #include <linux/irq.h> 1090921014SLuciano Coelho #include <linux/module.h> 1190921014SLuciano Coelho #include <linux/slab.h> 12e757201bSGeorge Spelvin #include <linux/swab.h> 1390921014SLuciano Coelho #include <linux/crc7.h> 1490921014SLuciano Coelho #include <linux/spi/spi.h> 1590921014SLuciano Coelho #include <linux/wl12xx.h> 161d207cd3SSebastian Reichel #include <linux/gpio.h> 1707bbca6fSSebastian Reichel #include <linux/of.h> 1807bbca6fSSebastian Reichel #include <linux/of_gpio.h> 19e4c2e09eSSebastian Reichel #include <linux/regulator/consumer.h> 2090921014SLuciano Coelho 2190921014SLuciano Coelho #include "wl1251.h" 2290921014SLuciano Coelho #include "reg.h" 2390921014SLuciano Coelho #include "spi.h" 2490921014SLuciano Coelho 2590921014SLuciano Coelho static irqreturn_t wl1251_irq(int irq, void *cookie) 2690921014SLuciano Coelho { 2790921014SLuciano Coelho struct wl1251 *wl; 2890921014SLuciano Coelho 2990921014SLuciano Coelho wl1251_debug(DEBUG_IRQ, "IRQ"); 3090921014SLuciano Coelho 3190921014SLuciano Coelho wl = cookie; 3290921014SLuciano Coelho 3390921014SLuciano Coelho ieee80211_queue_work(wl->hw, &wl->irq_work); 3490921014SLuciano Coelho 3590921014SLuciano Coelho return IRQ_HANDLED; 3690921014SLuciano Coelho } 3790921014SLuciano Coelho 3890921014SLuciano Coelho static struct spi_device *wl_to_spi(struct wl1251 *wl) 3990921014SLuciano Coelho { 4090921014SLuciano Coelho return wl->if_priv; 4190921014SLuciano Coelho } 4290921014SLuciano Coelho 4390921014SLuciano Coelho static void wl1251_spi_reset(struct wl1251 *wl) 4490921014SLuciano Coelho { 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 6490921014SLuciano Coelho spi_sync(wl_to_spi(wl), &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 7190921014SLuciano Coelho static void wl1251_spi_wake(struct wl1251 *wl) 7290921014SLuciano Coelho { 7390921014SLuciano Coelho struct spi_transfer t; 7490921014SLuciano Coelho struct spi_message m; 75e757201bSGeorge Spelvin u8 *cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); 7690921014SLuciano Coelho 7790921014SLuciano Coelho if (!cmd) { 7890921014SLuciano Coelho wl1251_error("could not allocate cmd for spi init"); 7990921014SLuciano Coelho return; 8090921014SLuciano Coelho } 8190921014SLuciano Coelho 8290921014SLuciano Coelho memset(&t, 0, sizeof(t)); 8390921014SLuciano Coelho spi_message_init(&m); 8490921014SLuciano Coelho 85789e90e5SSachin Kamat /* Set WSPI_INIT_COMMAND 8690921014SLuciano Coelho * the data is being send from the MSB to LSB 8790921014SLuciano Coelho */ 88e757201bSGeorge Spelvin cmd[0] = 0xff; 89e757201bSGeorge Spelvin cmd[1] = 0xff; 90e757201bSGeorge Spelvin cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; 91e757201bSGeorge Spelvin cmd[3] = 0; 92e757201bSGeorge Spelvin cmd[4] = 0; 93e757201bSGeorge Spelvin cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3; 94e757201bSGeorge Spelvin cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; 9590921014SLuciano Coelho 96e757201bSGeorge Spelvin cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS 9790921014SLuciano Coelho | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; 9890921014SLuciano Coelho 99e757201bSGeorge Spelvin if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) 100e757201bSGeorge Spelvin cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; 101e757201bSGeorge Spelvin else 102e757201bSGeorge Spelvin cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY; 10390921014SLuciano Coelho 104e757201bSGeorge Spelvin cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END; 105e757201bSGeorge Spelvin /* 106e757201bSGeorge Spelvin * The above is the logical order; it must actually be stored 107e757201bSGeorge Spelvin * in the buffer byte-swapped. 108e757201bSGeorge Spelvin */ 109e757201bSGeorge Spelvin __swab32s((u32 *)cmd); 110e757201bSGeorge Spelvin __swab32s((u32 *)cmd+1); 11190921014SLuciano Coelho 11290921014SLuciano Coelho t.tx_buf = cmd; 11390921014SLuciano Coelho t.len = WSPI_INIT_CMD_LEN; 11490921014SLuciano Coelho spi_message_add_tail(&t, &m); 11590921014SLuciano Coelho 11690921014SLuciano Coelho spi_sync(wl_to_spi(wl), &m); 11790921014SLuciano Coelho 11890921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); 119a859e4d6SGrazvydas Ignotas 120a859e4d6SGrazvydas Ignotas kfree(cmd); 12190921014SLuciano Coelho } 12290921014SLuciano Coelho 12390921014SLuciano Coelho static void wl1251_spi_reset_wake(struct wl1251 *wl) 12490921014SLuciano Coelho { 12590921014SLuciano Coelho wl1251_spi_reset(wl); 12690921014SLuciano Coelho wl1251_spi_wake(wl); 12790921014SLuciano Coelho } 12890921014SLuciano Coelho 12990921014SLuciano Coelho static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf, 13090921014SLuciano Coelho size_t len) 13190921014SLuciano Coelho { 13290921014SLuciano Coelho struct spi_transfer t[3]; 13390921014SLuciano Coelho struct spi_message m; 13490921014SLuciano Coelho u8 *busy_buf; 13590921014SLuciano Coelho u32 *cmd; 13690921014SLuciano Coelho 13790921014SLuciano Coelho cmd = &wl->buffer_cmd; 13890921014SLuciano Coelho busy_buf = wl->buffer_busyword; 13990921014SLuciano Coelho 14090921014SLuciano Coelho *cmd = 0; 14190921014SLuciano Coelho *cmd |= WSPI_CMD_READ; 14290921014SLuciano Coelho *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; 14390921014SLuciano Coelho *cmd |= addr & WSPI_CMD_BYTE_ADDR; 14490921014SLuciano Coelho 14590921014SLuciano Coelho spi_message_init(&m); 14690921014SLuciano Coelho memset(t, 0, sizeof(t)); 14790921014SLuciano Coelho 14890921014SLuciano Coelho t[0].tx_buf = cmd; 14990921014SLuciano Coelho t[0].len = 4; 15090921014SLuciano Coelho spi_message_add_tail(&t[0], &m); 15190921014SLuciano Coelho 15290921014SLuciano Coelho /* Busy and non busy words read */ 15390921014SLuciano Coelho t[1].rx_buf = busy_buf; 15490921014SLuciano Coelho t[1].len = WL1251_BUSY_WORD_LEN; 15590921014SLuciano Coelho spi_message_add_tail(&t[1], &m); 15690921014SLuciano Coelho 15790921014SLuciano Coelho t[2].rx_buf = buf; 15890921014SLuciano Coelho t[2].len = len; 15990921014SLuciano Coelho spi_message_add_tail(&t[2], &m); 16090921014SLuciano Coelho 16190921014SLuciano Coelho spi_sync(wl_to_spi(wl), &m); 16290921014SLuciano Coelho 16390921014SLuciano Coelho /* FIXME: check busy words */ 16490921014SLuciano Coelho 16590921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); 16690921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len); 16790921014SLuciano Coelho } 16890921014SLuciano Coelho 16990921014SLuciano Coelho static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf, 17090921014SLuciano Coelho size_t len) 17190921014SLuciano Coelho { 17290921014SLuciano Coelho struct spi_transfer t[2]; 17390921014SLuciano Coelho struct spi_message m; 17490921014SLuciano Coelho u32 *cmd; 17590921014SLuciano Coelho 17690921014SLuciano Coelho cmd = &wl->buffer_cmd; 17790921014SLuciano Coelho 17890921014SLuciano Coelho *cmd = 0; 17990921014SLuciano Coelho *cmd |= WSPI_CMD_WRITE; 18090921014SLuciano Coelho *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; 18190921014SLuciano Coelho *cmd |= addr & WSPI_CMD_BYTE_ADDR; 18290921014SLuciano Coelho 18390921014SLuciano Coelho spi_message_init(&m); 18490921014SLuciano Coelho memset(t, 0, sizeof(t)); 18590921014SLuciano Coelho 18690921014SLuciano Coelho t[0].tx_buf = cmd; 18790921014SLuciano Coelho t[0].len = sizeof(*cmd); 18890921014SLuciano Coelho spi_message_add_tail(&t[0], &m); 18990921014SLuciano Coelho 19090921014SLuciano Coelho t[1].tx_buf = buf; 19190921014SLuciano Coelho t[1].len = len; 19290921014SLuciano Coelho spi_message_add_tail(&t[1], &m); 19390921014SLuciano Coelho 19490921014SLuciano Coelho spi_sync(wl_to_spi(wl), &m); 19590921014SLuciano Coelho 19690921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); 19790921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len); 19890921014SLuciano Coelho } 19990921014SLuciano Coelho 20090921014SLuciano Coelho static void wl1251_spi_enable_irq(struct wl1251 *wl) 20190921014SLuciano Coelho { 20290921014SLuciano Coelho return enable_irq(wl->irq); 20390921014SLuciano Coelho } 20490921014SLuciano Coelho 20590921014SLuciano Coelho static void wl1251_spi_disable_irq(struct wl1251 *wl) 20690921014SLuciano Coelho { 20790921014SLuciano Coelho return disable_irq(wl->irq); 20890921014SLuciano Coelho } 20990921014SLuciano Coelho 21090921014SLuciano Coelho static int wl1251_spi_set_power(struct wl1251 *wl, bool enable) 21190921014SLuciano Coelho { 2121d207cd3SSebastian Reichel if (gpio_is_valid(wl->power_gpio)) 2131d207cd3SSebastian Reichel gpio_set_value(wl->power_gpio, enable); 21490921014SLuciano Coelho 21590921014SLuciano Coelho return 0; 21690921014SLuciano Coelho } 21790921014SLuciano Coelho 21890921014SLuciano Coelho static const struct wl1251_if_operations wl1251_spi_ops = { 21990921014SLuciano Coelho .read = wl1251_spi_read, 22090921014SLuciano Coelho .write = wl1251_spi_write, 22190921014SLuciano Coelho .reset = wl1251_spi_reset_wake, 22290921014SLuciano Coelho .enable_irq = wl1251_spi_enable_irq, 22390921014SLuciano Coelho .disable_irq = wl1251_spi_disable_irq, 22490921014SLuciano Coelho .power = wl1251_spi_set_power, 22590921014SLuciano Coelho }; 22690921014SLuciano Coelho 227b74324d1SBill Pemberton static int wl1251_spi_probe(struct spi_device *spi) 22890921014SLuciano Coelho { 22907bbca6fSSebastian Reichel struct wl1251_platform_data *pdata = dev_get_platdata(&spi->dev); 23007bbca6fSSebastian Reichel struct device_node *np = spi->dev.of_node; 23190921014SLuciano Coelho struct ieee80211_hw *hw; 23290921014SLuciano Coelho struct wl1251 *wl; 23390921014SLuciano Coelho int ret; 23490921014SLuciano Coelho 23507bbca6fSSebastian Reichel if (!np && !pdata) { 23690921014SLuciano Coelho wl1251_error("no platform data"); 23790921014SLuciano Coelho return -ENODEV; 23890921014SLuciano Coelho } 23990921014SLuciano Coelho 24090921014SLuciano Coelho hw = wl1251_alloc_hw(); 24190921014SLuciano Coelho if (IS_ERR(hw)) 24290921014SLuciano Coelho return PTR_ERR(hw); 24390921014SLuciano Coelho 24490921014SLuciano Coelho wl = hw->priv; 24590921014SLuciano Coelho 24690921014SLuciano Coelho SET_IEEE80211_DEV(hw, &spi->dev); 247c49b05acSJingoo Han spi_set_drvdata(spi, wl); 24890921014SLuciano Coelho wl->if_priv = spi; 24990921014SLuciano Coelho wl->if_ops = &wl1251_spi_ops; 25090921014SLuciano Coelho 25190921014SLuciano Coelho /* This is the only SPI value that we need to set here, the rest 252789e90e5SSachin Kamat * comes from the board-peripherals file 253789e90e5SSachin Kamat */ 25490921014SLuciano Coelho spi->bits_per_word = 32; 25590921014SLuciano Coelho 25690921014SLuciano Coelho ret = spi_setup(spi); 25790921014SLuciano Coelho if (ret < 0) { 25890921014SLuciano Coelho wl1251_error("spi_setup failed"); 25990921014SLuciano Coelho goto out_free; 26090921014SLuciano Coelho } 26190921014SLuciano Coelho 26207bbca6fSSebastian Reichel if (np) { 26307bbca6fSSebastian Reichel wl->use_eeprom = of_property_read_bool(np, "ti,wl1251-has-eeprom"); 26407bbca6fSSebastian Reichel wl->power_gpio = of_get_named_gpio(np, "ti,power-gpio", 0); 26507bbca6fSSebastian Reichel } else if (pdata) { 2661d207cd3SSebastian Reichel wl->power_gpio = pdata->power_gpio; 26707bbca6fSSebastian Reichel wl->use_eeprom = pdata->use_eeprom; 26807bbca6fSSebastian Reichel } 26907bbca6fSSebastian Reichel 27007bbca6fSSebastian Reichel if (wl->power_gpio == -EPROBE_DEFER) { 27107bbca6fSSebastian Reichel ret = -EPROBE_DEFER; 27207bbca6fSSebastian Reichel goto out_free; 27307bbca6fSSebastian Reichel } 2741d207cd3SSebastian Reichel 2751d207cd3SSebastian Reichel if (gpio_is_valid(wl->power_gpio)) { 2761d207cd3SSebastian Reichel ret = devm_gpio_request_one(&spi->dev, wl->power_gpio, 2771d207cd3SSebastian Reichel GPIOF_OUT_INIT_LOW, "wl1251 power"); 2781d207cd3SSebastian Reichel if (ret) { 2791d207cd3SSebastian Reichel wl1251_error("Failed to request gpio: %d\n", ret); 2801d207cd3SSebastian Reichel goto out_free; 2811d207cd3SSebastian Reichel } 2821d207cd3SSebastian Reichel } else { 2831d207cd3SSebastian Reichel wl1251_error("set power gpio missing in platform data"); 2841d207cd3SSebastian Reichel ret = -ENODEV; 2851d207cd3SSebastian Reichel goto out_free; 28690921014SLuciano Coelho } 28790921014SLuciano Coelho 28890921014SLuciano Coelho wl->irq = spi->irq; 28990921014SLuciano Coelho if (wl->irq < 0) { 29090921014SLuciano Coelho wl1251_error("irq missing in platform data"); 2911d207cd3SSebastian Reichel ret = -ENODEV; 2921d207cd3SSebastian Reichel goto out_free; 29390921014SLuciano Coelho } 29490921014SLuciano Coelho 295f380f2c4SGrazvydas Ignotas irq_set_status_flags(wl->irq, IRQ_NOAUTOEN); 2961d207cd3SSebastian Reichel ret = devm_request_irq(&spi->dev, wl->irq, wl1251_irq, 0, 2971d207cd3SSebastian Reichel DRIVER_NAME, wl); 29890921014SLuciano Coelho if (ret < 0) { 29990921014SLuciano Coelho wl1251_error("request_irq() failed: %d", ret); 30090921014SLuciano Coelho goto out_free; 30190921014SLuciano Coelho } 30290921014SLuciano Coelho 30390921014SLuciano Coelho irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING); 30490921014SLuciano Coelho 305e4c2e09eSSebastian Reichel wl->vio = devm_regulator_get(&spi->dev, "vio"); 306e4c2e09eSSebastian Reichel if (IS_ERR(wl->vio)) { 307e4c2e09eSSebastian Reichel ret = PTR_ERR(wl->vio); 308e4c2e09eSSebastian Reichel wl1251_error("vio regulator missing: %d", ret); 309e4c2e09eSSebastian Reichel goto out_free; 310e4c2e09eSSebastian Reichel } 311e4c2e09eSSebastian Reichel 312e4c2e09eSSebastian Reichel ret = regulator_enable(wl->vio); 31390921014SLuciano Coelho if (ret) 3141d207cd3SSebastian Reichel goto out_free; 31590921014SLuciano Coelho 316e4c2e09eSSebastian Reichel ret = wl1251_init_ieee80211(wl); 317e4c2e09eSSebastian Reichel if (ret) 318e4c2e09eSSebastian Reichel goto disable_regulator; 319e4c2e09eSSebastian Reichel 32090921014SLuciano Coelho return 0; 32190921014SLuciano Coelho 322e4c2e09eSSebastian Reichel disable_regulator: 323e4c2e09eSSebastian Reichel regulator_disable(wl->vio); 32490921014SLuciano Coelho out_free: 32590921014SLuciano Coelho ieee80211_free_hw(hw); 32690921014SLuciano Coelho 32790921014SLuciano Coelho return ret; 32890921014SLuciano Coelho } 32990921014SLuciano Coelho 330b74324d1SBill Pemberton static int wl1251_spi_remove(struct spi_device *spi) 33190921014SLuciano Coelho { 332c49b05acSJingoo Han struct wl1251 *wl = spi_get_drvdata(spi); 33390921014SLuciano Coelho 33490921014SLuciano Coelho wl1251_free_hw(wl); 335e4c2e09eSSebastian Reichel regulator_disable(wl->vio); 33690921014SLuciano Coelho 33790921014SLuciano Coelho return 0; 33890921014SLuciano Coelho } 33990921014SLuciano Coelho 34090921014SLuciano Coelho static struct spi_driver wl1251_spi_driver = { 34190921014SLuciano Coelho .driver = { 34290921014SLuciano Coelho .name = DRIVER_NAME, 34390921014SLuciano Coelho }, 34490921014SLuciano Coelho 34590921014SLuciano Coelho .probe = wl1251_spi_probe, 346b74324d1SBill Pemberton .remove = wl1251_spi_remove, 34790921014SLuciano Coelho }; 34890921014SLuciano Coelho 34919a4e68aSSachin Kamat module_spi_driver(wl1251_spi_driver); 35090921014SLuciano Coelho 35190921014SLuciano Coelho MODULE_LICENSE("GPL"); 35290921014SLuciano Coelho MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>"); 35390921014SLuciano Coelho MODULE_ALIAS("spi:wl1251"); 354