Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 1 | /* |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 2 | * This file is part of wl1251 |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2008 Nokia Corporation |
| 5 | * |
| 6 | * Contact: Kalle Valo <kalle.valo@nokia.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * version 2 as published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 20 | * 02110-1301 USA |
| 21 | * |
| 22 | */ |
| 23 | |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 24 | #include <linux/irq.h> |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 25 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 27 | #include <linux/crc7.h> |
| 28 | #include <linux/spi/spi.h> |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 29 | #include <linux/spi/wl12xx.h> |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 30 | |
Kalle Valo | 1367411 | 2009-06-12 14:17:25 +0300 | [diff] [blame] | 31 | #include "wl1251.h" |
Kalle Valo | 29d904c | 2009-08-07 13:35:11 +0300 | [diff] [blame] | 32 | #include "wl1251_reg.h" |
Kalle Valo | ef2f8d4 | 2009-06-12 14:17:19 +0300 | [diff] [blame] | 33 | #include "wl1251_spi.h" |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 34 | |
Bob Copeland | b5ed9c1 | 2009-08-07 13:33:49 +0300 | [diff] [blame] | 35 | static irqreturn_t wl1251_irq(int irq, void *cookie) |
| 36 | { |
| 37 | struct wl1251 *wl; |
| 38 | |
| 39 | wl1251_debug(DEBUG_IRQ, "IRQ"); |
| 40 | |
| 41 | wl = cookie; |
| 42 | |
Kalle Valo | 16e711f | 2009-08-07 13:35:04 +0300 | [diff] [blame] | 43 | ieee80211_queue_work(wl->hw, &wl->irq_work); |
Bob Copeland | b5ed9c1 | 2009-08-07 13:33:49 +0300 | [diff] [blame] | 44 | |
| 45 | return IRQ_HANDLED; |
| 46 | } |
| 47 | |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame] | 48 | static struct spi_device *wl_to_spi(struct wl1251 *wl) |
| 49 | { |
| 50 | return wl->if_priv; |
| 51 | } |
| 52 | |
Bob Copeland | 08d9f57 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 53 | static void wl1251_spi_reset(struct wl1251 *wl) |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 54 | { |
| 55 | u8 *cmd; |
| 56 | struct spi_transfer t; |
| 57 | struct spi_message m; |
| 58 | |
| 59 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); |
| 60 | if (!cmd) { |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 61 | wl1251_error("could not allocate cmd for spi reset"); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | |
| 65 | memset(&t, 0, sizeof(t)); |
| 66 | spi_message_init(&m); |
| 67 | |
| 68 | memset(cmd, 0xff, WSPI_INIT_CMD_LEN); |
| 69 | |
| 70 | t.tx_buf = cmd; |
| 71 | t.len = WSPI_INIT_CMD_LEN; |
| 72 | spi_message_add_tail(&t, &m); |
| 73 | |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame] | 74 | spi_sync(wl_to_spi(wl), &m); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 75 | |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 76 | wl1251_dump(DEBUG_SPI, "spi reset -> ", cmd, WSPI_INIT_CMD_LEN); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 77 | } |
| 78 | |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 79 | static void wl1251_spi_wake(struct wl1251 *wl) |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 80 | { |
| 81 | u8 crc[WSPI_INIT_CMD_CRC_LEN], *cmd; |
| 82 | struct spi_transfer t; |
| 83 | struct spi_message m; |
| 84 | |
| 85 | cmd = kzalloc(WSPI_INIT_CMD_LEN, GFP_KERNEL); |
| 86 | if (!cmd) { |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 87 | wl1251_error("could not allocate cmd for spi init"); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | |
| 91 | memset(crc, 0, sizeof(crc)); |
| 92 | memset(&t, 0, sizeof(t)); |
| 93 | spi_message_init(&m); |
| 94 | |
| 95 | /* |
| 96 | * Set WSPI_INIT_COMMAND |
| 97 | * the data is being send from the MSB to LSB |
| 98 | */ |
| 99 | cmd[2] = 0xff; |
| 100 | cmd[3] = 0xff; |
| 101 | cmd[1] = WSPI_INIT_CMD_START | WSPI_INIT_CMD_TX; |
| 102 | cmd[0] = 0; |
| 103 | cmd[7] = 0; |
| 104 | cmd[6] |= HW_ACCESS_WSPI_INIT_CMD_MASK << 3; |
| 105 | cmd[6] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN & WSPI_INIT_CMD_FIXEDBUSY_LEN; |
| 106 | |
| 107 | if (HW_ACCESS_WSPI_FIXED_BUSY_LEN == 0) |
| 108 | cmd[5] |= WSPI_INIT_CMD_DIS_FIXEDBUSY; |
| 109 | else |
| 110 | cmd[5] |= WSPI_INIT_CMD_EN_FIXEDBUSY; |
| 111 | |
| 112 | cmd[5] |= WSPI_INIT_CMD_IOD | WSPI_INIT_CMD_IP | WSPI_INIT_CMD_CS |
| 113 | | WSPI_INIT_CMD_WSPI | WSPI_INIT_CMD_WS; |
| 114 | |
| 115 | crc[0] = cmd[1]; |
| 116 | crc[1] = cmd[0]; |
| 117 | crc[2] = cmd[7]; |
| 118 | crc[3] = cmd[6]; |
| 119 | crc[4] = cmd[5]; |
| 120 | |
| 121 | cmd[4] |= crc7(0, crc, WSPI_INIT_CMD_CRC_LEN) << 1; |
| 122 | cmd[4] |= WSPI_INIT_CMD_END; |
| 123 | |
| 124 | t.tx_buf = cmd; |
| 125 | t.len = WSPI_INIT_CMD_LEN; |
| 126 | spi_message_add_tail(&t, &m); |
| 127 | |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame] | 128 | spi_sync(wl_to_spi(wl), &m); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 129 | |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 130 | wl1251_dump(DEBUG_SPI, "spi init -> ", cmd, WSPI_INIT_CMD_LEN); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 131 | } |
| 132 | |
Bob Copeland | 08d9f57 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 133 | static void wl1251_spi_reset_wake(struct wl1251 *wl) |
| 134 | { |
| 135 | wl1251_spi_reset(wl); |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 136 | wl1251_spi_wake(wl); |
Bob Copeland | 08d9f57 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 137 | } |
| 138 | |
Bob Copeland | 08d9f57 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 139 | static void wl1251_spi_read(struct wl1251 *wl, int addr, void *buf, |
| 140 | size_t len) |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 141 | { |
| 142 | struct spi_transfer t[3]; |
| 143 | struct spi_message m; |
Kalle Valo | 5262c12 | 2009-06-12 14:14:55 +0300 | [diff] [blame] | 144 | u8 *busy_buf; |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 145 | u32 *cmd; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 146 | |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 147 | cmd = &wl->buffer_cmd; |
Kalle Valo | 5262c12 | 2009-06-12 14:14:55 +0300 | [diff] [blame] | 148 | busy_buf = wl->buffer_busyword; |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 149 | |
| 150 | *cmd = 0; |
| 151 | *cmd |= WSPI_CMD_READ; |
| 152 | *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; |
| 153 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 154 | |
| 155 | spi_message_init(&m); |
| 156 | memset(t, 0, sizeof(t)); |
| 157 | |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 158 | t[0].tx_buf = cmd; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 159 | t[0].len = 4; |
| 160 | spi_message_add_tail(&t[0], &m); |
| 161 | |
| 162 | /* Busy and non busy words read */ |
| 163 | t[1].rx_buf = busy_buf; |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 164 | t[1].len = WL1251_BUSY_WORD_LEN; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 165 | spi_message_add_tail(&t[1], &m); |
| 166 | |
| 167 | t[2].rx_buf = buf; |
| 168 | t[2].len = len; |
| 169 | spi_message_add_tail(&t[2], &m); |
| 170 | |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame] | 171 | spi_sync(wl_to_spi(wl), &m); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 172 | |
| 173 | /* FIXME: check busy words */ |
| 174 | |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 175 | wl1251_dump(DEBUG_SPI, "spi_read cmd -> ", cmd, sizeof(*cmd)); |
| 176 | wl1251_dump(DEBUG_SPI, "spi_read buf <- ", buf, len); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 177 | } |
| 178 | |
Bob Copeland | 08d9f57 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 179 | static void wl1251_spi_write(struct wl1251 *wl, int addr, void *buf, |
| 180 | size_t len) |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 181 | { |
| 182 | struct spi_transfer t[2]; |
| 183 | struct spi_message m; |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 184 | u32 *cmd; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 185 | |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 186 | cmd = &wl->buffer_cmd; |
| 187 | |
| 188 | *cmd = 0; |
| 189 | *cmd |= WSPI_CMD_WRITE; |
| 190 | *cmd |= (len << WSPI_CMD_BYTE_LENGTH_OFFSET) & WSPI_CMD_BYTE_LENGTH; |
| 191 | *cmd |= addr & WSPI_CMD_BYTE_ADDR; |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 192 | |
| 193 | spi_message_init(&m); |
| 194 | memset(t, 0, sizeof(t)); |
| 195 | |
Kalle Valo | 56343a3 | 2009-06-12 14:14:47 +0300 | [diff] [blame] | 196 | t[0].tx_buf = cmd; |
| 197 | t[0].len = sizeof(*cmd); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 198 | spi_message_add_tail(&t[0], &m); |
| 199 | |
| 200 | t[1].tx_buf = buf; |
| 201 | t[1].len = len; |
| 202 | spi_message_add_tail(&t[1], &m); |
| 203 | |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame] | 204 | spi_sync(wl_to_spi(wl), &m); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 205 | |
Kalle Valo | 80301cd | 2009-06-12 14:17:39 +0300 | [diff] [blame] | 206 | wl1251_dump(DEBUG_SPI, "spi_write cmd -> ", cmd, sizeof(*cmd)); |
| 207 | wl1251_dump(DEBUG_SPI, "spi_write buf -> ", buf, len); |
Kalle Valo | 2f01a1f | 2009-04-29 23:33:31 +0300 | [diff] [blame] | 208 | } |
Bob Copeland | 08d9f57 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 209 | |
Bob Copeland | b5ed9c1 | 2009-08-07 13:33:49 +0300 | [diff] [blame] | 210 | static void wl1251_spi_enable_irq(struct wl1251 *wl) |
| 211 | { |
| 212 | return enable_irq(wl->irq); |
| 213 | } |
| 214 | |
| 215 | static void wl1251_spi_disable_irq(struct wl1251 *wl) |
| 216 | { |
| 217 | return disable_irq(wl->irq); |
| 218 | } |
| 219 | |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame] | 220 | static const struct wl1251_if_operations wl1251_spi_ops = { |
Bob Copeland | 08d9f57 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 221 | .read = wl1251_spi_read, |
| 222 | .write = wl1251_spi_write, |
| 223 | .reset = wl1251_spi_reset_wake, |
Bob Copeland | b5ed9c1 | 2009-08-07 13:33:49 +0300 | [diff] [blame] | 224 | .enable_irq = wl1251_spi_enable_irq, |
| 225 | .disable_irq = wl1251_spi_disable_irq, |
Bob Copeland | 08d9f57 | 2009-08-07 13:33:11 +0300 | [diff] [blame] | 226 | }; |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 227 | |
| 228 | static int __devinit wl1251_spi_probe(struct spi_device *spi) |
| 229 | { |
| 230 | struct wl12xx_platform_data *pdata; |
| 231 | struct ieee80211_hw *hw; |
| 232 | struct wl1251 *wl; |
| 233 | int ret; |
| 234 | |
| 235 | pdata = spi->dev.platform_data; |
| 236 | if (!pdata) { |
| 237 | wl1251_error("no platform data"); |
| 238 | return -ENODEV; |
| 239 | } |
| 240 | |
| 241 | hw = wl1251_alloc_hw(); |
| 242 | if (IS_ERR(hw)) |
| 243 | return PTR_ERR(hw); |
| 244 | |
| 245 | wl = hw->priv; |
| 246 | |
| 247 | SET_IEEE80211_DEV(hw, &spi->dev); |
| 248 | dev_set_drvdata(&spi->dev, wl); |
Bob Copeland | af8c78e | 2009-08-07 13:33:34 +0300 | [diff] [blame] | 249 | wl->if_priv = spi; |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 250 | wl->if_ops = &wl1251_spi_ops; |
| 251 | |
| 252 | /* This is the only SPI value that we need to set here, the rest |
| 253 | * comes from the board-peripherals file */ |
| 254 | spi->bits_per_word = 32; |
| 255 | |
| 256 | ret = spi_setup(spi); |
| 257 | if (ret < 0) { |
| 258 | wl1251_error("spi_setup failed"); |
| 259 | goto out_free; |
| 260 | } |
| 261 | |
| 262 | wl->set_power = pdata->set_power; |
| 263 | if (!wl->set_power) { |
| 264 | wl1251_error("set power function missing in platform data"); |
| 265 | return -ENODEV; |
| 266 | } |
| 267 | |
| 268 | wl->irq = spi->irq; |
| 269 | if (wl->irq < 0) { |
| 270 | wl1251_error("irq missing in platform data"); |
| 271 | return -ENODEV; |
| 272 | } |
| 273 | |
David-John Willis | c95cf3d0 | 2009-11-17 18:50:09 +0200 | [diff] [blame] | 274 | wl->use_eeprom = pdata->use_eeprom; |
| 275 | |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 276 | ret = request_irq(wl->irq, wl1251_irq, 0, DRIVER_NAME, wl); |
| 277 | if (ret < 0) { |
| 278 | wl1251_error("request_irq() failed: %d", ret); |
| 279 | goto out_free; |
| 280 | } |
| 281 | |
| 282 | set_irq_type(wl->irq, IRQ_TYPE_EDGE_RISING); |
| 283 | |
| 284 | disable_irq(wl->irq); |
| 285 | |
| 286 | ret = wl1251_init_ieee80211(wl); |
| 287 | if (ret) |
| 288 | goto out_irq; |
| 289 | |
| 290 | return 0; |
| 291 | |
| 292 | out_irq: |
| 293 | free_irq(wl->irq, wl); |
| 294 | |
| 295 | out_free: |
| 296 | ieee80211_free_hw(hw); |
| 297 | |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | static int __devexit wl1251_spi_remove(struct spi_device *spi) |
| 302 | { |
| 303 | struct wl1251 *wl = dev_get_drvdata(&spi->dev); |
| 304 | |
Bob Copeland | b5ed9c1 | 2009-08-07 13:33:49 +0300 | [diff] [blame] | 305 | free_irq(wl->irq, wl); |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 306 | wl1251_free_hw(wl); |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static struct spi_driver wl1251_spi_driver = { |
| 312 | .driver = { |
Kalle Valo | 7590a55 | 2010-04-02 15:31:46 +0300 | [diff] [blame] | 313 | .name = DRIVER_NAME, |
Bob Copeland | 8e639c0 | 2009-08-07 13:33:26 +0300 | [diff] [blame] | 314 | .bus = &spi_bus_type, |
| 315 | .owner = THIS_MODULE, |
| 316 | }, |
| 317 | |
| 318 | .probe = wl1251_spi_probe, |
| 319 | .remove = __devexit_p(wl1251_spi_remove), |
| 320 | }; |
| 321 | |
| 322 | static int __init wl1251_spi_init(void) |
| 323 | { |
| 324 | int ret; |
| 325 | |
| 326 | ret = spi_register_driver(&wl1251_spi_driver); |
| 327 | if (ret < 0) { |
| 328 | wl1251_error("failed to register spi driver: %d", ret); |
| 329 | goto out; |
| 330 | } |
| 331 | |
| 332 | out: |
| 333 | return ret; |
| 334 | } |
| 335 | |
| 336 | static void __exit wl1251_spi_exit(void) |
| 337 | { |
| 338 | spi_unregister_driver(&wl1251_spi_driver); |
| 339 | |
| 340 | wl1251_notice("unloaded"); |
| 341 | } |
| 342 | |
| 343 | module_init(wl1251_spi_init); |
| 344 | module_exit(wl1251_spi_exit); |
| 345 | |
| 346 | MODULE_LICENSE("GPL"); |
Kalle Valo | b1b0a2b | 2009-08-07 13:35:19 +0300 | [diff] [blame] | 347 | MODULE_AUTHOR("Kalle Valo <kalle.valo@nokia.com>"); |