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> 26*e757201bSGeorge Spelvin #include <linux/swab.h> 2790921014SLuciano Coelho #include <linux/crc7.h> 2890921014SLuciano Coelho #include <linux/spi/spi.h> 2990921014SLuciano Coelho #include <linux/wl12xx.h> 301d207cd3SSebastian Reichel #include <linux/gpio.h> 3107bbca6fSSebastian Reichel #include <linux/of.h> 3207bbca6fSSebastian Reichel #include <linux/of_gpio.h> 33e4c2e09eSSebastian Reichel #include <linux/regulator/consumer.h> 3490921014SLuciano Coelho 3590921014SLuciano Coelho #include "wl1251.h" 3690921014SLuciano Coelho #include "reg.h" 3790921014SLuciano Coelho #include "spi.h" 3890921014SLuciano Coelho 3990921014SLuciano Coelho static irqreturn_t wl1251_irq(int irq, void *cookie) 4090921014SLuciano Coelho { 4190921014SLuciano Coelho struct wl1251 *wl; 4290921014SLuciano Coelho 4390921014SLuciano Coelho wl1251_debug(DEBUG_IRQ, "IRQ"); 4490921014SLuciano Coelho 4590921014SLuciano Coelho wl = cookie; 4690921014SLuciano Coelho 4790921014SLuciano Coelho ieee80211_queue_work(wl->hw, &wl->irq_work); 4890921014SLuciano Coelho 4990921014SLuciano Coelho return IRQ_HANDLED; 5090921014SLuciano Coelho } 5190921014SLuciano Coelho 5290921014SLuciano Coelho static struct spi_device *wl_to_spi(struct wl1251 *wl) 5390921014SLuciano Coelho { 5490921014SLuciano Coelho return wl->if_priv; 5590921014SLuciano Coelho } 5690921014SLuciano Coelho 5790921014SLuciano Coelho static void wl1251_spi_reset(struct wl1251 *wl) 5890921014SLuciano Coelho { 5990921014SLuciano Coelho u8 *cmd; 6090921014SLuciano Coelho struct spi_transfer t; 6190921014SLuciano Coelho struct spi_message m; 6290921014SLuciano Coelho 6390921014SLuciano Coelho cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); 6490921014SLuciano Coelho if (!cmd) { 6590921014SLuciano Coelho wl1251_error("could not allocate cmd for spi reset"); 6690921014SLuciano Coelho return; 6790921014SLuciano Coelho } 6890921014SLuciano Coelho 6990921014SLuciano Coelho memset(&t, 0, sizeof(t)); 7090921014SLuciano Coelho spi_message_init(&m); 7190921014SLuciano Coelho 7290921014SLuciano Coelho memset(cmd, 0xff, WSPI_INIT_CMD_LEN); 7390921014SLuciano Coelho 7490921014SLuciano Coelho t.tx_buf = cmd; 7590921014SLuciano Coelho t.len = WSPI_INIT_CMD_LEN; 7690921014SLuciano Coelho spi_message_add_tail(&t, &m); 7790921014SLuciano Coelho 7890921014SLuciano Coelho spi_sync(wl_to_spi(wl), &m); 7990921014SLuciano Coelho 8090921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN); 81a859e4d6SGrazvydas Ignotas 82a859e4d6SGrazvydas Ignotas kfree(cmd); 8390921014SLuciano Coelho } 8490921014SLuciano Coelho 8590921014SLuciano Coelho static void wl1251_spi_wake(struct wl1251 *wl) 8690921014SLuciano Coelho { 8790921014SLuciano Coelho struct spi_transfer t; 8890921014SLuciano Coelho struct spi_message m; 89*e757201bSGeorge Spelvin u8 *cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); 9090921014SLuciano Coelho 9190921014SLuciano Coelho if (!cmd) { 9290921014SLuciano Coelho wl1251_error("could not allocate cmd for spi init"); 9390921014SLuciano Coelho return; 9490921014SLuciano Coelho } 9590921014SLuciano Coelho 9690921014SLuciano Coelho memset(&t, 0, sizeof(t)); 9790921014SLuciano Coelho spi_message_init(&m); 9890921014SLuciano Coelho 99789e90e5SSachin Kamat /* Set WSPI_INIT_COMMAND 10090921014SLuciano Coelho * the data is being send from the MSB to LSB 10190921014SLuciano Coelho */ 102*e757201bSGeorge Spelvin cmd[0] = 0xff; 103*e757201bSGeorge Spelvin cmd[1] = 0xff; 104*e757201bSGeorge Spelvin cmd[2] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; 105*e757201bSGeorge Spelvin cmd[3] = 0; 106*e757201bSGeorge Spelvin cmd[4] = 0; 107*e757201bSGeorge Spelvin cmd[5] = HW_ACCESS_WSPI_INIT_CMD_MASK << 3; 108*e757201bSGeorge Spelvin cmd[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; 10990921014SLuciano Coelho 110*e757201bSGeorge Spelvin cmd[6] = WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS 11190921014SLuciano Coelho | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; 11290921014SLuciano Coelho 113*e757201bSGeorge Spelvin if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) 114*e757201bSGeorge Spelvin cmd[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; 115*e757201bSGeorge Spelvin else 116*e757201bSGeorge Spelvin cmd[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY; 11790921014SLuciano Coelho 118*e757201bSGeorge Spelvin cmd[7] = crc7_be(0, cmd+2, WSPI_INIT_CMD_CRC_LEN) | WSPI_INIT_CMD_END; 119*e757201bSGeorge Spelvin /* 120*e757201bSGeorge Spelvin * The above is the logical order; it must actually be stored 121*e757201bSGeorge Spelvin * in the buffer byte-swapped. 122*e757201bSGeorge Spelvin */ 123*e757201bSGeorge Spelvin __swab32s((u32 *)cmd); 124*e757201bSGeorge Spelvin __swab32s((u32 *)cmd+1); 12590921014SLuciano Coelho 12690921014SLuciano Coelho t.tx_buf = cmd; 12790921014SLuciano Coelho t.len = WSPI_INIT_CMD_LEN; 12890921014SLuciano Coelho spi_message_add_tail(&t, &m); 12990921014SLuciano Coelho 13090921014SLuciano Coelho spi_sync(wl_to_spi(wl), &m); 13190921014SLuciano Coelho 13290921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); 133a859e4d6SGrazvydas Ignotas 134a859e4d6SGrazvydas Ignotas kfree(cmd); 13590921014SLuciano Coelho } 13690921014SLuciano Coelho 13790921014SLuciano Coelho static void wl1251_spi_reset_wake(struct wl1251 *wl) 13890921014SLuciano Coelho { 13990921014SLuciano Coelho wl1251_spi_reset(wl); 14090921014SLuciano Coelho wl1251_spi_wake(wl); 14190921014SLuciano Coelho } 14290921014SLuciano Coelho 14390921014SLuciano Coelho static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf, 14490921014SLuciano Coelho size_t len) 14590921014SLuciano Coelho { 14690921014SLuciano Coelho struct spi_transfer t[3]; 14790921014SLuciano Coelho struct spi_message m; 14890921014SLuciano Coelho u8 *busy_buf; 14990921014SLuciano Coelho u32 *cmd; 15090921014SLuciano Coelho 15190921014SLuciano Coelho cmd = &wl->buffer_cmd; 15290921014SLuciano Coelho busy_buf = wl->buffer_busyword; 15390921014SLuciano Coelho 15490921014SLuciano Coelho *cmd = 0; 15590921014SLuciano Coelho *cmd |= WSPI_CMD_READ; 15690921014SLuciano Coelho *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; 15790921014SLuciano Coelho *cmd |= addr & WSPI_CMD_BYTE_ADDR; 15890921014SLuciano Coelho 15990921014SLuciano Coelho spi_message_init(&m); 16090921014SLuciano Coelho memset(t, 0, sizeof(t)); 16190921014SLuciano Coelho 16290921014SLuciano Coelho t[0].tx_buf = cmd; 16390921014SLuciano Coelho t[0].len = 4; 16490921014SLuciano Coelho spi_message_add_tail(&t[0], &m); 16590921014SLuciano Coelho 16690921014SLuciano Coelho /* Busy and non busy words read */ 16790921014SLuciano Coelho t[1].rx_buf = busy_buf; 16890921014SLuciano Coelho t[1].len = WL1251_BUSY_WORD_LEN; 16990921014SLuciano Coelho spi_message_add_tail(&t[1], &m); 17090921014SLuciano Coelho 17190921014SLuciano Coelho t[2].rx_buf = buf; 17290921014SLuciano Coelho t[2].len = len; 17390921014SLuciano Coelho spi_message_add_tail(&t[2], &m); 17490921014SLuciano Coelho 17590921014SLuciano Coelho spi_sync(wl_to_spi(wl), &m); 17690921014SLuciano Coelho 17790921014SLuciano Coelho /* FIXME: check busy words */ 17890921014SLuciano Coelho 17990921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); 18090921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len); 18190921014SLuciano Coelho } 18290921014SLuciano Coelho 18390921014SLuciano Coelho static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf, 18490921014SLuciano Coelho size_t len) 18590921014SLuciano Coelho { 18690921014SLuciano Coelho struct spi_transfer t[2]; 18790921014SLuciano Coelho struct spi_message m; 18890921014SLuciano Coelho u32 *cmd; 18990921014SLuciano Coelho 19090921014SLuciano Coelho cmd = &wl->buffer_cmd; 19190921014SLuciano Coelho 19290921014SLuciano Coelho *cmd = 0; 19390921014SLuciano Coelho *cmd |= WSPI_CMD_WRITE; 19490921014SLuciano Coelho *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; 19590921014SLuciano Coelho *cmd |= addr & WSPI_CMD_BYTE_ADDR; 19690921014SLuciano Coelho 19790921014SLuciano Coelho spi_message_init(&m); 19890921014SLuciano Coelho memset(t, 0, sizeof(t)); 19990921014SLuciano Coelho 20090921014SLuciano Coelho t[0].tx_buf = cmd; 20190921014SLuciano Coelho t[0].len = sizeof(*cmd); 20290921014SLuciano Coelho spi_message_add_tail(&t[0], &m); 20390921014SLuciano Coelho 20490921014SLuciano Coelho t[1].tx_buf = buf; 20590921014SLuciano Coelho t[1].len = len; 20690921014SLuciano Coelho spi_message_add_tail(&t[1], &m); 20790921014SLuciano Coelho 20890921014SLuciano Coelho spi_sync(wl_to_spi(wl), &m); 20990921014SLuciano Coelho 21090921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); 21190921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len); 21290921014SLuciano Coelho } 21390921014SLuciano Coelho 21490921014SLuciano Coelho static void wl1251_spi_enable_irq(struct wl1251 *wl) 21590921014SLuciano Coelho { 21690921014SLuciano Coelho return enable_irq(wl->irq); 21790921014SLuciano Coelho } 21890921014SLuciano Coelho 21990921014SLuciano Coelho static void wl1251_spi_disable_irq(struct wl1251 *wl) 22090921014SLuciano Coelho { 22190921014SLuciano Coelho return disable_irq(wl->irq); 22290921014SLuciano Coelho } 22390921014SLuciano Coelho 22490921014SLuciano Coelho static int wl1251_spi_set_power(struct wl1251 *wl, bool enable) 22590921014SLuciano Coelho { 2261d207cd3SSebastian Reichel if (gpio_is_valid(wl->power_gpio)) 2271d207cd3SSebastian Reichel gpio_set_value(wl->power_gpio, enable); 22890921014SLuciano Coelho 22990921014SLuciano Coelho return 0; 23090921014SLuciano Coelho } 23190921014SLuciano Coelho 23290921014SLuciano Coelho static const struct wl1251_if_operations wl1251_spi_ops = { 23390921014SLuciano Coelho .read = wl1251_spi_read, 23490921014SLuciano Coelho .write = wl1251_spi_write, 23590921014SLuciano Coelho .reset = wl1251_spi_reset_wake, 23690921014SLuciano Coelho .enable_irq = wl1251_spi_enable_irq, 23790921014SLuciano Coelho .disable_irq = wl1251_spi_disable_irq, 23890921014SLuciano Coelho .power = wl1251_spi_set_power, 23990921014SLuciano Coelho }; 24090921014SLuciano Coelho 241b74324d1SBill Pemberton static int wl1251_spi_probe(struct spi_device *spi) 24290921014SLuciano Coelho { 24307bbca6fSSebastian Reichel struct wl1251_platform_data *pdata = dev_get_platdata(&spi->dev); 24407bbca6fSSebastian Reichel struct device_node *np = spi->dev.of_node; 24590921014SLuciano Coelho struct ieee80211_hw *hw; 24690921014SLuciano Coelho struct wl1251 *wl; 24790921014SLuciano Coelho int ret; 24890921014SLuciano Coelho 24907bbca6fSSebastian Reichel if (!np && !pdata) { 25090921014SLuciano Coelho wl1251_error("no platform data"); 25190921014SLuciano Coelho return -ENODEV; 25290921014SLuciano Coelho } 25390921014SLuciano Coelho 25490921014SLuciano Coelho hw = wl1251_alloc_hw(); 25590921014SLuciano Coelho if (IS_ERR(hw)) 25690921014SLuciano Coelho return PTR_ERR(hw); 25790921014SLuciano Coelho 25890921014SLuciano Coelho wl = hw->priv; 25990921014SLuciano Coelho 26090921014SLuciano Coelho SET_IEEE80211_DEV(hw, &spi->dev); 261c49b05acSJingoo Han spi_set_drvdata(spi, wl); 26290921014SLuciano Coelho wl->if_priv = spi; 26390921014SLuciano Coelho wl->if_ops = &wl1251_spi_ops; 26490921014SLuciano Coelho 26590921014SLuciano Coelho /* This is the only SPI value that we need to set here, the rest 266789e90e5SSachin Kamat * comes from the board-peripherals file 267789e90e5SSachin Kamat */ 26890921014SLuciano Coelho spi->bits_per_word = 32; 26990921014SLuciano Coelho 27090921014SLuciano Coelho ret = spi_setup(spi); 27190921014SLuciano Coelho if (ret < 0) { 27290921014SLuciano Coelho wl1251_error("spi_setup failed"); 27390921014SLuciano Coelho goto out_free; 27490921014SLuciano Coelho } 27590921014SLuciano Coelho 27607bbca6fSSebastian Reichel if (np) { 27707bbca6fSSebastian Reichel wl->use_eeprom = of_property_read_bool(np, "ti,wl1251-has-eeprom"); 27807bbca6fSSebastian Reichel wl->power_gpio = of_get_named_gpio(np, "ti,power-gpio", 0); 27907bbca6fSSebastian Reichel } else if (pdata) { 2801d207cd3SSebastian Reichel wl->power_gpio = pdata->power_gpio; 28107bbca6fSSebastian Reichel wl->use_eeprom = pdata->use_eeprom; 28207bbca6fSSebastian Reichel } 28307bbca6fSSebastian Reichel 28407bbca6fSSebastian Reichel if (wl->power_gpio == -EPROBE_DEFER) { 28507bbca6fSSebastian Reichel ret = -EPROBE_DEFER; 28607bbca6fSSebastian Reichel goto out_free; 28707bbca6fSSebastian Reichel } 2881d207cd3SSebastian Reichel 2891d207cd3SSebastian Reichel if (gpio_is_valid(wl->power_gpio)) { 2901d207cd3SSebastian Reichel ret = devm_gpio_request_one(&spi->dev, wl->power_gpio, 2911d207cd3SSebastian Reichel GPIOF_OUT_INIT_LOW, "wl1251 power"); 2921d207cd3SSebastian Reichel if (ret) { 2931d207cd3SSebastian Reichel wl1251_error("Failed to request gpio: %d\n", ret); 2941d207cd3SSebastian Reichel goto out_free; 2951d207cd3SSebastian Reichel } 2961d207cd3SSebastian Reichel } else { 2971d207cd3SSebastian Reichel wl1251_error("set power gpio missing in platform data"); 2981d207cd3SSebastian Reichel ret = -ENODEV; 2991d207cd3SSebastian Reichel goto out_free; 30090921014SLuciano Coelho } 30190921014SLuciano Coelho 30290921014SLuciano Coelho wl->irq = spi->irq; 30390921014SLuciano Coelho if (wl->irq < 0) { 30490921014SLuciano Coelho wl1251_error("irq missing in platform data"); 3051d207cd3SSebastian Reichel ret = -ENODEV; 3061d207cd3SSebastian Reichel goto out_free; 30790921014SLuciano Coelho } 30890921014SLuciano Coelho 309f380f2c4SGrazvydas Ignotas irq_set_status_flags(wl->irq, IRQ_NOAUTOEN); 3101d207cd3SSebastian Reichel ret = devm_request_irq(&spi->dev, wl->irq, wl1251_irq, 0, 3111d207cd3SSebastian Reichel DRIVER_NAME, wl); 31290921014SLuciano Coelho if (ret < 0) { 31390921014SLuciano Coelho wl1251_error("request_irq() failed: %d", ret); 31490921014SLuciano Coelho goto out_free; 31590921014SLuciano Coelho } 31690921014SLuciano Coelho 31790921014SLuciano Coelho irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING); 31890921014SLuciano Coelho 319e4c2e09eSSebastian Reichel wl->vio = devm_regulator_get(&spi->dev, "vio"); 320e4c2e09eSSebastian Reichel if (IS_ERR(wl->vio)) { 321e4c2e09eSSebastian Reichel ret = PTR_ERR(wl->vio); 322e4c2e09eSSebastian Reichel wl1251_error("vio regulator missing: %d", ret); 323e4c2e09eSSebastian Reichel goto out_free; 324e4c2e09eSSebastian Reichel } 325e4c2e09eSSebastian Reichel 326e4c2e09eSSebastian Reichel ret = regulator_enable(wl->vio); 32790921014SLuciano Coelho if (ret) 3281d207cd3SSebastian Reichel goto out_free; 32990921014SLuciano Coelho 330e4c2e09eSSebastian Reichel ret = wl1251_init_ieee80211(wl); 331e4c2e09eSSebastian Reichel if (ret) 332e4c2e09eSSebastian Reichel goto disable_regulator; 333e4c2e09eSSebastian Reichel 33490921014SLuciano Coelho return 0; 33590921014SLuciano Coelho 336e4c2e09eSSebastian Reichel disable_regulator: 337e4c2e09eSSebastian Reichel regulator_disable(wl->vio); 33890921014SLuciano Coelho out_free: 33990921014SLuciano Coelho ieee80211_free_hw(hw); 34090921014SLuciano Coelho 34190921014SLuciano Coelho return ret; 34290921014SLuciano Coelho } 34390921014SLuciano Coelho 344b74324d1SBill Pemberton static int wl1251_spi_remove(struct spi_device *spi) 34590921014SLuciano Coelho { 346c49b05acSJingoo Han struct wl1251 *wl = spi_get_drvdata(spi); 34790921014SLuciano Coelho 34890921014SLuciano Coelho free_irq(wl->irq, wl); 34990921014SLuciano Coelho wl1251_free_hw(wl); 350e4c2e09eSSebastian Reichel regulator_disable(wl->vio); 35190921014SLuciano Coelho 35290921014SLuciano Coelho return 0; 35390921014SLuciano Coelho } 35490921014SLuciano Coelho 35590921014SLuciano Coelho static struct spi_driver wl1251_spi_driver = { 35690921014SLuciano Coelho .driver = { 35790921014SLuciano Coelho .name = DRIVER_NAME, 35890921014SLuciano Coelho .owner = THIS_MODULE, 35990921014SLuciano Coelho }, 36090921014SLuciano Coelho 36190921014SLuciano Coelho .probe = wl1251_spi_probe, 362b74324d1SBill Pemberton .remove = wl1251_spi_remove, 36390921014SLuciano Coelho }; 36490921014SLuciano Coelho 36519a4e68aSSachin Kamat module_spi_driver(wl1251_spi_driver); 36690921014SLuciano Coelho 36790921014SLuciano Coelho MODULE_LICENSE("GPL"); 36890921014SLuciano Coelho MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>"); 36990921014SLuciano Coelho MODULE_ALIAS("spi:wl1251"); 370