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> 291d207cd3SSebastian Reichel #include <linux/gpio.h> 30*07bbca6fSSebastian Reichel #include <linux/of.h> 31*07bbca6fSSebastian Reichel #include <linux/of_gpio.h> 32e4c2e09eSSebastian Reichel #include <linux/regulator/consumer.h> 3390921014SLuciano Coelho 3490921014SLuciano Coelho #include "wl1251.h" 3590921014SLuciano Coelho #include "reg.h" 3690921014SLuciano Coelho #include "spi.h" 3790921014SLuciano Coelho 3890921014SLuciano Coelho static irqreturn_t wl1251_irq(int irq, void *cookie) 3990921014SLuciano Coelho { 4090921014SLuciano Coelho struct wl1251 *wl; 4190921014SLuciano Coelho 4290921014SLuciano Coelho wl1251_debug(DEBUG_IRQ, "IRQ"); 4390921014SLuciano Coelho 4490921014SLuciano Coelho wl = cookie; 4590921014SLuciano Coelho 4690921014SLuciano Coelho ieee80211_queue_work(wl->hw, &wl->irq_work); 4790921014SLuciano Coelho 4890921014SLuciano Coelho return IRQ_HANDLED; 4990921014SLuciano Coelho } 5090921014SLuciano Coelho 5190921014SLuciano Coelho static struct spi_device *wl_to_spi(struct wl1251 *wl) 5290921014SLuciano Coelho { 5390921014SLuciano Coelho return wl->if_priv; 5490921014SLuciano Coelho } 5590921014SLuciano Coelho 5690921014SLuciano Coelho static void wl1251_spi_reset(struct wl1251 *wl) 5790921014SLuciano Coelho { 5890921014SLuciano Coelho u8 *cmd; 5990921014SLuciano Coelho struct spi_transfer t; 6090921014SLuciano Coelho struct spi_message m; 6190921014SLuciano Coelho 6290921014SLuciano Coelho cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); 6390921014SLuciano Coelho if (!cmd) { 6490921014SLuciano Coelho wl1251_error("could not allocate cmd for spi reset"); 6590921014SLuciano Coelho return; 6690921014SLuciano Coelho } 6790921014SLuciano Coelho 6890921014SLuciano Coelho memset(&t, 0, sizeof(t)); 6990921014SLuciano Coelho spi_message_init(&m); 7090921014SLuciano Coelho 7190921014SLuciano Coelho memset(cmd, 0xff, WSPI_INIT_CMD_LEN); 7290921014SLuciano Coelho 7390921014SLuciano Coelho t.tx_buf = cmd; 7490921014SLuciano Coelho t.len = WSPI_INIT_CMD_LEN; 7590921014SLuciano Coelho spi_message_add_tail(&t, &m); 7690921014SLuciano Coelho 7790921014SLuciano Coelho spi_sync(wl_to_spi(wl), &m); 7890921014SLuciano Coelho 7990921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN); 80a859e4d6SGrazvydas Ignotas 81a859e4d6SGrazvydas Ignotas kfree(cmd); 8290921014SLuciano Coelho } 8390921014SLuciano Coelho 8490921014SLuciano Coelho static void wl1251_spi_wake(struct wl1251 *wl) 8590921014SLuciano Coelho { 8690921014SLuciano Coelho u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd; 8790921014SLuciano Coelho struct spi_transfer t; 8890921014SLuciano Coelho struct spi_message m; 8990921014SLuciano Coelho 9090921014SLuciano Coelho cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); 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(crc, 0, sizeof(crc)); 9790921014SLuciano Coelho memset(&t, 0, sizeof(t)); 9890921014SLuciano Coelho spi_message_init(&m); 9990921014SLuciano Coelho 100789e90e5SSachin Kamat /* Set WSPI_INIT_COMMAND 10190921014SLuciano Coelho * the data is being send from the MSB to LSB 10290921014SLuciano Coelho */ 10390921014SLuciano Coelho cmd[2] = 0xff; 10490921014SLuciano Coelho cmd[3] = 0xff; 10590921014SLuciano Coelho cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; 10690921014SLuciano Coelho cmd[0] = 0; 10790921014SLuciano Coelho cmd[7] = 0; 10890921014SLuciano Coelho cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3; 10990921014SLuciano Coelho cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; 11090921014SLuciano Coelho 11190921014SLuciano Coelho if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) 11290921014SLuciano Coelho cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; 11390921014SLuciano Coelho else 11490921014SLuciano Coelho cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY; 11590921014SLuciano Coelho 11690921014SLuciano Coelho cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS 11790921014SLuciano Coelho | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; 11890921014SLuciano Coelho 11990921014SLuciano Coelho crc[0] = cmd[1]; 12090921014SLuciano Coelho crc[1] = cmd[0]; 12190921014SLuciano Coelho crc[2] = cmd[7]; 12290921014SLuciano Coelho crc[3] = cmd[6]; 12390921014SLuciano Coelho crc[4] = cmd[5]; 12490921014SLuciano Coelho 12590921014SLuciano Coelho cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1; 12690921014SLuciano Coelho cmd[4] |= WSPI_INIT_CMD_END; 12790921014SLuciano Coelho 12890921014SLuciano Coelho t.tx_buf = cmd; 12990921014SLuciano Coelho t.len = WSPI_INIT_CMD_LEN; 13090921014SLuciano Coelho spi_message_add_tail(&t, &m); 13190921014SLuciano Coelho 13290921014SLuciano Coelho spi_sync(wl_to_spi(wl), &m); 13390921014SLuciano Coelho 13490921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); 135a859e4d6SGrazvydas Ignotas 136a859e4d6SGrazvydas Ignotas kfree(cmd); 13790921014SLuciano Coelho } 13890921014SLuciano Coelho 13990921014SLuciano Coelho static void wl1251_spi_reset_wake(struct wl1251 *wl) 14090921014SLuciano Coelho { 14190921014SLuciano Coelho wl1251_spi_reset(wl); 14290921014SLuciano Coelho wl1251_spi_wake(wl); 14390921014SLuciano Coelho } 14490921014SLuciano Coelho 14590921014SLuciano Coelho static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf, 14690921014SLuciano Coelho size_t len) 14790921014SLuciano Coelho { 14890921014SLuciano Coelho struct spi_transfer t[3]; 14990921014SLuciano Coelho struct spi_message m; 15090921014SLuciano Coelho u8 *busy_buf; 15190921014SLuciano Coelho u32 *cmd; 15290921014SLuciano Coelho 15390921014SLuciano Coelho cmd = &wl->buffer_cmd; 15490921014SLuciano Coelho busy_buf = wl->buffer_busyword; 15590921014SLuciano Coelho 15690921014SLuciano Coelho *cmd = 0; 15790921014SLuciano Coelho *cmd |= WSPI_CMD_READ; 15890921014SLuciano Coelho *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; 15990921014SLuciano Coelho *cmd |= addr & WSPI_CMD_BYTE_ADDR; 16090921014SLuciano Coelho 16190921014SLuciano Coelho spi_message_init(&m); 16290921014SLuciano Coelho memset(t, 0, sizeof(t)); 16390921014SLuciano Coelho 16490921014SLuciano Coelho t[0].tx_buf = cmd; 16590921014SLuciano Coelho t[0].len = 4; 16690921014SLuciano Coelho spi_message_add_tail(&t[0], &m); 16790921014SLuciano Coelho 16890921014SLuciano Coelho /* Busy and non busy words read */ 16990921014SLuciano Coelho t[1].rx_buf = busy_buf; 17090921014SLuciano Coelho t[1].len = WL1251_BUSY_WORD_LEN; 17190921014SLuciano Coelho spi_message_add_tail(&t[1], &m); 17290921014SLuciano Coelho 17390921014SLuciano Coelho t[2].rx_buf = buf; 17490921014SLuciano Coelho t[2].len = len; 17590921014SLuciano Coelho spi_message_add_tail(&t[2], &m); 17690921014SLuciano Coelho 17790921014SLuciano Coelho spi_sync(wl_to_spi(wl), &m); 17890921014SLuciano Coelho 17990921014SLuciano Coelho /* FIXME: check busy words */ 18090921014SLuciano Coelho 18190921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); 18290921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len); 18390921014SLuciano Coelho } 18490921014SLuciano Coelho 18590921014SLuciano Coelho static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf, 18690921014SLuciano Coelho size_t len) 18790921014SLuciano Coelho { 18890921014SLuciano Coelho struct spi_transfer t[2]; 18990921014SLuciano Coelho struct spi_message m; 19090921014SLuciano Coelho u32 *cmd; 19190921014SLuciano Coelho 19290921014SLuciano Coelho cmd = &wl->buffer_cmd; 19390921014SLuciano Coelho 19490921014SLuciano Coelho *cmd = 0; 19590921014SLuciano Coelho *cmd |= WSPI_CMD_WRITE; 19690921014SLuciano Coelho *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; 19790921014SLuciano Coelho *cmd |= addr & WSPI_CMD_BYTE_ADDR; 19890921014SLuciano Coelho 19990921014SLuciano Coelho spi_message_init(&m); 20090921014SLuciano Coelho memset(t, 0, sizeof(t)); 20190921014SLuciano Coelho 20290921014SLuciano Coelho t[0].tx_buf = cmd; 20390921014SLuciano Coelho t[0].len = sizeof(*cmd); 20490921014SLuciano Coelho spi_message_add_tail(&t[0], &m); 20590921014SLuciano Coelho 20690921014SLuciano Coelho t[1].tx_buf = buf; 20790921014SLuciano Coelho t[1].len = len; 20890921014SLuciano Coelho spi_message_add_tail(&t[1], &m); 20990921014SLuciano Coelho 21090921014SLuciano Coelho spi_sync(wl_to_spi(wl), &m); 21190921014SLuciano Coelho 21290921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); 21390921014SLuciano Coelho wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len); 21490921014SLuciano Coelho } 21590921014SLuciano Coelho 21690921014SLuciano Coelho static void wl1251_spi_enable_irq(struct wl1251 *wl) 21790921014SLuciano Coelho { 21890921014SLuciano Coelho return enable_irq(wl->irq); 21990921014SLuciano Coelho } 22090921014SLuciano Coelho 22190921014SLuciano Coelho static void wl1251_spi_disable_irq(struct wl1251 *wl) 22290921014SLuciano Coelho { 22390921014SLuciano Coelho return disable_irq(wl->irq); 22490921014SLuciano Coelho } 22590921014SLuciano Coelho 22690921014SLuciano Coelho static int wl1251_spi_set_power(struct wl1251 *wl, bool enable) 22790921014SLuciano Coelho { 2281d207cd3SSebastian Reichel if (gpio_is_valid(wl->power_gpio)) 2291d207cd3SSebastian Reichel gpio_set_value(wl->power_gpio, enable); 23090921014SLuciano Coelho 23190921014SLuciano Coelho return 0; 23290921014SLuciano Coelho } 23390921014SLuciano Coelho 23490921014SLuciano Coelho static const struct wl1251_if_operations wl1251_spi_ops = { 23590921014SLuciano Coelho .read = wl1251_spi_read, 23690921014SLuciano Coelho .write = wl1251_spi_write, 23790921014SLuciano Coelho .reset = wl1251_spi_reset_wake, 23890921014SLuciano Coelho .enable_irq = wl1251_spi_enable_irq, 23990921014SLuciano Coelho .disable_irq = wl1251_spi_disable_irq, 24090921014SLuciano Coelho .power = wl1251_spi_set_power, 24190921014SLuciano Coelho }; 24290921014SLuciano Coelho 243b74324d1SBill Pemberton static int wl1251_spi_probe(struct spi_device *spi) 24490921014SLuciano Coelho { 245*07bbca6fSSebastian Reichel struct wl1251_platform_data *pdata = dev_get_platdata(&spi->dev); 246*07bbca6fSSebastian Reichel struct device_node *np = spi->dev.of_node; 24790921014SLuciano Coelho struct ieee80211_hw *hw; 24890921014SLuciano Coelho struct wl1251 *wl; 24990921014SLuciano Coelho int ret; 25090921014SLuciano Coelho 251*07bbca6fSSebastian Reichel if (!np && !pdata) { 25290921014SLuciano Coelho wl1251_error("no platform data"); 25390921014SLuciano Coelho return -ENODEV; 25490921014SLuciano Coelho } 25590921014SLuciano Coelho 25690921014SLuciano Coelho hw = wl1251_alloc_hw(); 25790921014SLuciano Coelho if (IS_ERR(hw)) 25890921014SLuciano Coelho return PTR_ERR(hw); 25990921014SLuciano Coelho 26090921014SLuciano Coelho wl = hw->priv; 26190921014SLuciano Coelho 26290921014SLuciano Coelho SET_IEEE80211_DEV(hw, &spi->dev); 263c49b05acSJingoo Han spi_set_drvdata(spi, wl); 26490921014SLuciano Coelho wl->if_priv = spi; 26590921014SLuciano Coelho wl->if_ops = &wl1251_spi_ops; 26690921014SLuciano Coelho 26790921014SLuciano Coelho /* This is the only SPI value that we need to set here, the rest 268789e90e5SSachin Kamat * comes from the board-peripherals file 269789e90e5SSachin Kamat */ 27090921014SLuciano Coelho spi->bits_per_word = 32; 27190921014SLuciano Coelho 27290921014SLuciano Coelho ret = spi_setup(spi); 27390921014SLuciano Coelho if (ret < 0) { 27490921014SLuciano Coelho wl1251_error("spi_setup failed"); 27590921014SLuciano Coelho goto out_free; 27690921014SLuciano Coelho } 27790921014SLuciano Coelho 278*07bbca6fSSebastian Reichel if (np) { 279*07bbca6fSSebastian Reichel wl->use_eeprom = of_property_read_bool(np, "ti,wl1251-has-eeprom"); 280*07bbca6fSSebastian Reichel wl->power_gpio = of_get_named_gpio(np, "ti,power-gpio", 0); 281*07bbca6fSSebastian Reichel } else if (pdata) { 2821d207cd3SSebastian Reichel wl->power_gpio = pdata->power_gpio; 283*07bbca6fSSebastian Reichel wl->use_eeprom = pdata->use_eeprom; 284*07bbca6fSSebastian Reichel } 285*07bbca6fSSebastian Reichel 286*07bbca6fSSebastian Reichel if (wl->power_gpio == -EPROBE_DEFER) { 287*07bbca6fSSebastian Reichel ret = -EPROBE_DEFER; 288*07bbca6fSSebastian Reichel goto out_free; 289*07bbca6fSSebastian Reichel } 2901d207cd3SSebastian Reichel 2911d207cd3SSebastian Reichel if (gpio_is_valid(wl->power_gpio)) { 2921d207cd3SSebastian Reichel ret = devm_gpio_request_one(&spi->dev, wl->power_gpio, 2931d207cd3SSebastian Reichel GPIOF_OUT_INIT_LOW, "wl1251 power"); 2941d207cd3SSebastian Reichel if (ret) { 2951d207cd3SSebastian Reichel wl1251_error("Failed to request gpio: %d\n", ret); 2961d207cd3SSebastian Reichel goto out_free; 2971d207cd3SSebastian Reichel } 2981d207cd3SSebastian Reichel } else { 2991d207cd3SSebastian Reichel wl1251_error("set power gpio missing in platform data"); 3001d207cd3SSebastian Reichel ret = -ENODEV; 3011d207cd3SSebastian Reichel goto out_free; 30290921014SLuciano Coelho } 30390921014SLuciano Coelho 30490921014SLuciano Coelho wl->irq = spi->irq; 30590921014SLuciano Coelho if (wl->irq < 0) { 30690921014SLuciano Coelho wl1251_error("irq missing in platform data"); 3071d207cd3SSebastian Reichel ret = -ENODEV; 3081d207cd3SSebastian Reichel goto out_free; 30990921014SLuciano Coelho } 31090921014SLuciano Coelho 311f380f2c4SGrazvydas Ignotas irq_set_status_flags(wl->irq, IRQ_NOAUTOEN); 3121d207cd3SSebastian Reichel ret = devm_request_irq(&spi->dev, wl->irq, wl1251_irq, 0, 3131d207cd3SSebastian Reichel DRIVER_NAME, wl); 31490921014SLuciano Coelho if (ret < 0) { 31590921014SLuciano Coelho wl1251_error("request_irq() failed: %d", ret); 31690921014SLuciano Coelho goto out_free; 31790921014SLuciano Coelho } 31890921014SLuciano Coelho 31990921014SLuciano Coelho irq_set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING); 32090921014SLuciano Coelho 321e4c2e09eSSebastian Reichel wl->vio = devm_regulator_get(&spi->dev, "vio"); 322e4c2e09eSSebastian Reichel if (IS_ERR(wl->vio)) { 323e4c2e09eSSebastian Reichel ret = PTR_ERR(wl->vio); 324e4c2e09eSSebastian Reichel wl1251_error("vio regulator missing: %d", ret); 325e4c2e09eSSebastian Reichel goto out_free; 326e4c2e09eSSebastian Reichel } 327e4c2e09eSSebastian Reichel 328e4c2e09eSSebastian Reichel ret = regulator_enable(wl->vio); 32990921014SLuciano Coelho if (ret) 3301d207cd3SSebastian Reichel goto out_free; 33190921014SLuciano Coelho 332e4c2e09eSSebastian Reichel ret = wl1251_init_ieee80211(wl); 333e4c2e09eSSebastian Reichel if (ret) 334e4c2e09eSSebastian Reichel goto disable_regulator; 335e4c2e09eSSebastian Reichel 33690921014SLuciano Coelho return 0; 33790921014SLuciano Coelho 338e4c2e09eSSebastian Reichel disable_regulator: 339e4c2e09eSSebastian Reichel regulator_disable(wl->vio); 34090921014SLuciano Coelho out_free: 34190921014SLuciano Coelho ieee80211_free_hw(hw); 34290921014SLuciano Coelho 34390921014SLuciano Coelho return ret; 34490921014SLuciano Coelho } 34590921014SLuciano Coelho 346b74324d1SBill Pemberton static int wl1251_spi_remove(struct spi_device *spi) 34790921014SLuciano Coelho { 348c49b05acSJingoo Han struct wl1251 *wl = spi_get_drvdata(spi); 34990921014SLuciano Coelho 35090921014SLuciano Coelho free_irq(wl->irq, wl); 35190921014SLuciano Coelho wl1251_free_hw(wl); 352e4c2e09eSSebastian Reichel regulator_disable(wl->vio); 35390921014SLuciano Coelho 35490921014SLuciano Coelho return 0; 35590921014SLuciano Coelho } 35690921014SLuciano Coelho 35790921014SLuciano Coelho static struct spi_driver wl1251_spi_driver = { 35890921014SLuciano Coelho .driver = { 35990921014SLuciano Coelho .name = DRIVER_NAME, 36090921014SLuciano Coelho .owner = THIS_MODULE, 36190921014SLuciano Coelho }, 36290921014SLuciano Coelho 36390921014SLuciano Coelho .probe = wl1251_spi_probe, 364b74324d1SBill Pemberton .remove = wl1251_spi_remove, 36590921014SLuciano Coelho }; 36690921014SLuciano Coelho 36719a4e68aSSachin Kamat module_spi_driver(wl1251_spi_driver); 36890921014SLuciano Coelho 36990921014SLuciano Coelho MODULE_LICENSE("GPL"); 37090921014SLuciano Coelho MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>"); 37190921014SLuciano Coelho MODULE_ALIAS("spi:wl1251"); 372