Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Mac80211 SPI driver for ST-Ericsson CW1200 device |
| 3 | * |
| 4 | * Copyright (c) 2011, Sagrad Inc. |
| 5 | * Author: Solomon Peachy <speachy@sagrad.com> |
| 6 | * |
| 7 | * Based on cw1200_sdio.c |
| 8 | * Copyright (c) 2010, ST-Ericsson |
| 9 | * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
| 15 | |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
| 17 | #include <linux/gpio.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/spinlock.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <net/mac80211.h> |
| 22 | |
| 23 | #include <linux/spi/spi.h> |
| 24 | #include <linux/device.h> |
| 25 | |
| 26 | #include "cw1200.h" |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 27 | #include "hwbus.h" |
Solomon Peachy | 4da2a54a | 2013-06-02 11:35:32 -0400 | [diff] [blame] | 28 | #include <linux/platform_data/net-cw1200.h> |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 29 | #include "hwio.h" |
| 30 | |
| 31 | MODULE_AUTHOR("Solomon Peachy <speachy@sagrad.com>"); |
| 32 | MODULE_DESCRIPTION("mac80211 ST-Ericsson CW1200 SPI driver"); |
| 33 | MODULE_LICENSE("GPL"); |
| 34 | MODULE_ALIAS("spi:cw1200_wlan_spi"); |
| 35 | |
| 36 | /* #define SPI_DEBUG */ |
| 37 | |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 38 | struct hwbus_priv { |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 39 | struct spi_device *func; |
| 40 | struct cw1200_common *core; |
| 41 | const struct cw1200_platform_data_spi *pdata; |
| 42 | spinlock_t lock; /* Serialize all bus operations */ |
Solomon Peachy | 85ba8f5 | 2013-08-27 20:29:47 -0400 | [diff] [blame] | 43 | wait_queue_head_t wq; |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 44 | int claimed; |
| 45 | }; |
| 46 | |
| 47 | #define SDIO_TO_SPI_ADDR(addr) ((addr & 0x1f)>>2) |
| 48 | #define SET_WRITE 0x7FFF /* usage: and operation */ |
| 49 | #define SET_READ 0x8000 /* usage: or operation */ |
| 50 | |
Solomon Peachy | 8b3e7be | 2013-06-11 09:49:40 -0400 | [diff] [blame] | 51 | /* Notes on byte ordering: |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 52 | LE: B0 B1 B2 B3 |
| 53 | BE: B3 B2 B1 B0 |
| 54 | |
| 55 | Hardware expects 32-bit data to be written as 16-bit BE words: |
| 56 | |
| 57 | B1 B0 B3 B2 |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 58 | */ |
| 59 | |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 60 | static int cw1200_spi_memcpy_fromio(struct hwbus_priv *self, |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 61 | unsigned int addr, |
| 62 | void *dst, int count) |
| 63 | { |
| 64 | int ret, i; |
Solomon Peachy | 7258416 | 2013-06-20 23:03:12 -0400 | [diff] [blame] | 65 | u16 regaddr; |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 66 | struct spi_message m; |
| 67 | |
| 68 | struct spi_transfer t_addr = { |
| 69 | .tx_buf = ®addr, |
| 70 | .len = sizeof(regaddr), |
| 71 | }; |
| 72 | struct spi_transfer t_msg = { |
| 73 | .rx_buf = dst, |
| 74 | .len = count, |
| 75 | }; |
| 76 | |
| 77 | regaddr = (SDIO_TO_SPI_ADDR(addr))<<12; |
| 78 | regaddr |= SET_READ; |
| 79 | regaddr |= (count>>1); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 80 | |
| 81 | #ifdef SPI_DEBUG |
Solomon Peachy | 7258416 | 2013-06-20 23:03:12 -0400 | [diff] [blame] | 82 | pr_info("READ : %04d from 0x%02x (%04x)\n", count, addr, regaddr); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 83 | #endif |
| 84 | |
Solomon Peachy | 7258416 | 2013-06-20 23:03:12 -0400 | [diff] [blame] | 85 | /* Header is LE16 */ |
| 86 | regaddr = cpu_to_le16(regaddr); |
| 87 | |
| 88 | /* We have to byteswap if the SPI bus is limited to 8b operation |
| 89 | or we are running on a Big Endian system |
| 90 | */ |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 91 | #if defined(__LITTLE_ENDIAN) |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 92 | if (self->func->bits_per_word == 8) |
| 93 | #endif |
| 94 | regaddr = swab16(regaddr); |
| 95 | |
| 96 | spi_message_init(&m); |
| 97 | spi_message_add_tail(&t_addr, &m); |
| 98 | spi_message_add_tail(&t_msg, &m); |
| 99 | ret = spi_sync(self->func, &m); |
| 100 | |
| 101 | #ifdef SPI_DEBUG |
| 102 | pr_info("READ : "); |
| 103 | for (i = 0; i < t_addr.len; i++) |
| 104 | printk("%02x ", ((u8 *)t_addr.tx_buf)[i]); |
| 105 | printk(" : "); |
| 106 | for (i = 0; i < t_msg.len; i++) |
| 107 | printk("%02x ", ((u8 *)t_msg.rx_buf)[i]); |
| 108 | printk("\n"); |
| 109 | #endif |
| 110 | |
Solomon Peachy | 7258416 | 2013-06-20 23:03:12 -0400 | [diff] [blame] | 111 | /* We have to byteswap if the SPI bus is limited to 8b operation |
| 112 | or we are running on a Big Endian system |
| 113 | */ |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 114 | #if defined(__LITTLE_ENDIAN) |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 115 | if (self->func->bits_per_word == 8) |
| 116 | #endif |
| 117 | { |
| 118 | uint16_t *buf = (uint16_t *)dst; |
| 119 | for (i = 0; i < ((count + 1) >> 1); i++) |
| 120 | buf[i] = swab16(buf[i]); |
| 121 | } |
| 122 | |
| 123 | return ret; |
| 124 | } |
| 125 | |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 126 | static int cw1200_spi_memcpy_toio(struct hwbus_priv *self, |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 127 | unsigned int addr, |
| 128 | const void *src, int count) |
| 129 | { |
| 130 | int rval, i; |
Solomon Peachy | 7258416 | 2013-06-20 23:03:12 -0400 | [diff] [blame] | 131 | u16 regaddr; |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 132 | struct spi_transfer t_addr = { |
| 133 | .tx_buf = ®addr, |
| 134 | .len = sizeof(regaddr), |
| 135 | }; |
| 136 | struct spi_transfer t_msg = { |
| 137 | .tx_buf = src, |
| 138 | .len = count, |
| 139 | }; |
| 140 | struct spi_message m; |
| 141 | |
| 142 | regaddr = (SDIO_TO_SPI_ADDR(addr))<<12; |
| 143 | regaddr &= SET_WRITE; |
| 144 | regaddr |= (count>>1); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 145 | |
| 146 | #ifdef SPI_DEBUG |
Solomon Peachy | 7258416 | 2013-06-20 23:03:12 -0400 | [diff] [blame] | 147 | pr_info("WRITE: %04d to 0x%02x (%04x)\n", count, addr, regaddr); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 148 | #endif |
| 149 | |
Solomon Peachy | 7258416 | 2013-06-20 23:03:12 -0400 | [diff] [blame] | 150 | /* Header is LE16 */ |
| 151 | regaddr = cpu_to_le16(regaddr); |
| 152 | |
| 153 | /* We have to byteswap if the SPI bus is limited to 8b operation |
| 154 | or we are running on a Big Endian system |
| 155 | */ |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 156 | #if defined(__LITTLE_ENDIAN) |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 157 | if (self->func->bits_per_word == 8) |
| 158 | #endif |
| 159 | { |
| 160 | uint16_t *buf = (uint16_t *)src; |
Solomon Peachy | 7258416 | 2013-06-20 23:03:12 -0400 | [diff] [blame] | 161 | regaddr = swab16(regaddr); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 162 | for (i = 0; i < ((count + 1) >> 1); i++) |
| 163 | buf[i] = swab16(buf[i]); |
| 164 | } |
| 165 | |
| 166 | #ifdef SPI_DEBUG |
| 167 | pr_info("WRITE: "); |
| 168 | for (i = 0; i < t_addr.len; i++) |
| 169 | printk("%02x ", ((u8 *)t_addr.tx_buf)[i]); |
| 170 | printk(" : "); |
| 171 | for (i = 0; i < t_msg.len; i++) |
| 172 | printk("%02x ", ((u8 *)t_msg.tx_buf)[i]); |
| 173 | printk("\n"); |
| 174 | #endif |
| 175 | |
| 176 | spi_message_init(&m); |
| 177 | spi_message_add_tail(&t_addr, &m); |
| 178 | spi_message_add_tail(&t_msg, &m); |
| 179 | rval = spi_sync(self->func, &m); |
| 180 | |
| 181 | #ifdef SPI_DEBUG |
| 182 | pr_info("WROTE: %d\n", m.actual_length); |
| 183 | #endif |
| 184 | |
| 185 | #if defined(__LITTLE_ENDIAN) |
| 186 | /* We have to byteswap if the SPI bus is limited to 8b operation */ |
| 187 | if (self->func->bits_per_word == 8) |
| 188 | #endif |
| 189 | { |
| 190 | uint16_t *buf = (uint16_t *)src; |
| 191 | for (i = 0; i < ((count + 1) >> 1); i++) |
| 192 | buf[i] = swab16(buf[i]); |
| 193 | } |
| 194 | return rval; |
| 195 | } |
| 196 | |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 197 | static void cw1200_spi_lock(struct hwbus_priv *self) |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 198 | { |
| 199 | unsigned long flags; |
| 200 | |
Solomon Peachy | 85ba8f5 | 2013-08-27 20:29:47 -0400 | [diff] [blame] | 201 | DECLARE_WAITQUEUE(wait, current); |
| 202 | |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 203 | might_sleep(); |
| 204 | |
Solomon Peachy | 85ba8f5 | 2013-08-27 20:29:47 -0400 | [diff] [blame] | 205 | add_wait_queue(&self->wq, &wait); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 206 | spin_lock_irqsave(&self->lock, flags); |
| 207 | while (1) { |
| 208 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 209 | if (!self->claimed) |
| 210 | break; |
| 211 | spin_unlock_irqrestore(&self->lock, flags); |
| 212 | schedule(); |
| 213 | spin_lock_irqsave(&self->lock, flags); |
| 214 | } |
| 215 | set_current_state(TASK_RUNNING); |
| 216 | self->claimed = 1; |
| 217 | spin_unlock_irqrestore(&self->lock, flags); |
Solomon Peachy | 85ba8f5 | 2013-08-27 20:29:47 -0400 | [diff] [blame] | 218 | remove_wait_queue(&self->wq, &wait); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 219 | |
| 220 | return; |
| 221 | } |
| 222 | |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 223 | static void cw1200_spi_unlock(struct hwbus_priv *self) |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 224 | { |
| 225 | unsigned long flags; |
| 226 | |
| 227 | spin_lock_irqsave(&self->lock, flags); |
| 228 | self->claimed = 0; |
| 229 | spin_unlock_irqrestore(&self->lock, flags); |
Solomon Peachy | 85ba8f5 | 2013-08-27 20:29:47 -0400 | [diff] [blame] | 230 | wake_up(&self->wq); |
| 231 | |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
| 235 | static irqreturn_t cw1200_spi_irq_handler(int irq, void *dev_id) |
| 236 | { |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 237 | struct hwbus_priv *self = dev_id; |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 238 | |
| 239 | if (self->core) { |
| 240 | cw1200_irq_handler(self->core); |
| 241 | return IRQ_HANDLED; |
| 242 | } else { |
| 243 | return IRQ_NONE; |
| 244 | } |
| 245 | } |
| 246 | |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 247 | static int cw1200_spi_irq_subscribe(struct hwbus_priv *self) |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 248 | { |
| 249 | int ret; |
| 250 | |
| 251 | pr_debug("SW IRQ subscribe\n"); |
| 252 | |
Solomon Peachy | 87421cb | 2013-09-23 16:00:04 -0400 | [diff] [blame] | 253 | ret = request_threaded_irq(self->func->irq, NULL, |
| 254 | cw1200_spi_irq_handler, |
| 255 | IRQF_TRIGGER_HIGH | IRQF_ONESHOT, |
| 256 | "cw1200_wlan_irq", self); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 257 | if (WARN_ON(ret < 0)) |
| 258 | goto exit; |
| 259 | |
| 260 | ret = enable_irq_wake(self->func->irq); |
| 261 | if (WARN_ON(ret)) |
| 262 | goto free_irq; |
| 263 | |
| 264 | return 0; |
| 265 | |
| 266 | free_irq: |
| 267 | free_irq(self->func->irq, self); |
| 268 | exit: |
| 269 | return ret; |
| 270 | } |
| 271 | |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 272 | static int cw1200_spi_irq_unsubscribe(struct hwbus_priv *self) |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 273 | { |
Solomon Peachy | c4fb19d | 2013-09-23 16:00:03 -0400 | [diff] [blame] | 274 | int ret = 0; |
| 275 | |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 276 | pr_debug("SW IRQ unsubscribe\n"); |
| 277 | disable_irq_wake(self->func->irq); |
| 278 | free_irq(self->func->irq, self); |
| 279 | |
Solomon Peachy | c4fb19d | 2013-09-23 16:00:03 -0400 | [diff] [blame] | 280 | return ret; |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static int cw1200_spi_off(const struct cw1200_platform_data_spi *pdata) |
| 284 | { |
Solomon Peachy | 6dd64a3 | 2013-06-02 09:53:03 -0400 | [diff] [blame] | 285 | if (pdata->reset) { |
| 286 | gpio_set_value(pdata->reset, 0); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 287 | msleep(30); /* Min is 2 * CLK32K cycles */ |
Solomon Peachy | 6dd64a3 | 2013-06-02 09:53:03 -0400 | [diff] [blame] | 288 | gpio_free(pdata->reset); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | if (pdata->power_ctrl) |
| 292 | pdata->power_ctrl(pdata, false); |
| 293 | if (pdata->clk_ctrl) |
| 294 | pdata->clk_ctrl(pdata, false); |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | static int cw1200_spi_on(const struct cw1200_platform_data_spi *pdata) |
| 300 | { |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 301 | /* Ensure I/Os are pulled low */ |
Solomon Peachy | 6dd64a3 | 2013-06-02 09:53:03 -0400 | [diff] [blame] | 302 | if (pdata->reset) { |
| 303 | gpio_request(pdata->reset, "cw1200_wlan_reset"); |
| 304 | gpio_direction_output(pdata->reset, 0); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 305 | } |
Solomon Peachy | 6dd64a3 | 2013-06-02 09:53:03 -0400 | [diff] [blame] | 306 | if (pdata->powerup) { |
| 307 | gpio_request(pdata->powerup, "cw1200_wlan_powerup"); |
| 308 | gpio_direction_output(pdata->powerup, 0); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 309 | } |
Solomon Peachy | 6dd64a3 | 2013-06-02 09:53:03 -0400 | [diff] [blame] | 310 | if (pdata->reset || pdata->powerup) |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 311 | msleep(10); /* Settle time? */ |
| 312 | |
| 313 | /* Enable 3v3 and 1v8 to hardware */ |
| 314 | if (pdata->power_ctrl) { |
| 315 | if (pdata->power_ctrl(pdata, true)) { |
| 316 | pr_err("power_ctrl() failed!\n"); |
| 317 | return -1; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | /* Enable CLK32K */ |
| 322 | if (pdata->clk_ctrl) { |
| 323 | if (pdata->clk_ctrl(pdata, true)) { |
| 324 | pr_err("clk_ctrl() failed!\n"); |
| 325 | return -1; |
| 326 | } |
| 327 | msleep(10); /* Delay until clock is stable for 2 cycles */ |
| 328 | } |
| 329 | |
| 330 | /* Enable POWERUP signal */ |
Solomon Peachy | 6dd64a3 | 2013-06-02 09:53:03 -0400 | [diff] [blame] | 331 | if (pdata->powerup) { |
| 332 | gpio_set_value(pdata->powerup, 1); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 333 | msleep(250); /* or more..? */ |
| 334 | } |
| 335 | /* Enable RSTn signal */ |
Solomon Peachy | 6dd64a3 | 2013-06-02 09:53:03 -0400 | [diff] [blame] | 336 | if (pdata->reset) { |
| 337 | gpio_set_value(pdata->reset, 1); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 338 | msleep(50); /* Or more..? */ |
| 339 | } |
| 340 | return 0; |
| 341 | } |
| 342 | |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 343 | static size_t cw1200_spi_align_size(struct hwbus_priv *self, size_t size) |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 344 | { |
| 345 | return size & 1 ? size + 1 : size; |
| 346 | } |
| 347 | |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 348 | static int cw1200_spi_pm(struct hwbus_priv *self, bool suspend) |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 349 | { |
| 350 | return irq_set_irq_wake(self->func->irq, suspend); |
| 351 | } |
| 352 | |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 353 | static struct hwbus_ops cw1200_spi_hwbus_ops = { |
| 354 | .hwbus_memcpy_fromio = cw1200_spi_memcpy_fromio, |
| 355 | .hwbus_memcpy_toio = cw1200_spi_memcpy_toio, |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 356 | .lock = cw1200_spi_lock, |
| 357 | .unlock = cw1200_spi_unlock, |
| 358 | .align_size = cw1200_spi_align_size, |
| 359 | .power_mgmt = cw1200_spi_pm, |
| 360 | }; |
| 361 | |
| 362 | /* Probe Function to be called by SPI stack when device is discovered */ |
| 363 | static int cw1200_spi_probe(struct spi_device *func) |
| 364 | { |
| 365 | const struct cw1200_platform_data_spi *plat_data = |
| 366 | func->dev.platform_data; |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 367 | struct hwbus_priv *self; |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 368 | int status; |
| 369 | |
| 370 | /* Sanity check speed */ |
| 371 | if (func->max_speed_hz > 52000000) |
| 372 | func->max_speed_hz = 52000000; |
| 373 | if (func->max_speed_hz < 1000000) |
| 374 | func->max_speed_hz = 1000000; |
| 375 | |
| 376 | /* Fix up transfer size */ |
| 377 | if (plat_data->spi_bits_per_word) |
| 378 | func->bits_per_word = plat_data->spi_bits_per_word; |
| 379 | if (!func->bits_per_word) |
| 380 | func->bits_per_word = 16; |
| 381 | |
| 382 | /* And finally.. */ |
| 383 | func->mode = SPI_MODE_0; |
| 384 | |
| 385 | pr_info("cw1200_wlan_spi: Probe called (CS %d M %d BPW %d CLK %d)\n", |
| 386 | func->chip_select, func->mode, func->bits_per_word, |
| 387 | func->max_speed_hz); |
| 388 | |
| 389 | if (cw1200_spi_on(plat_data)) { |
| 390 | pr_err("spi_on() failed!\n"); |
| 391 | return -1; |
| 392 | } |
| 393 | |
| 394 | if (spi_setup(func)) { |
| 395 | pr_err("spi_setup() failed!\n"); |
| 396 | return -1; |
| 397 | } |
| 398 | |
| 399 | self = kzalloc(sizeof(*self), GFP_KERNEL); |
| 400 | if (!self) { |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 401 | pr_err("Can't allocate SPI hwbus_priv."); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 402 | return -ENOMEM; |
| 403 | } |
| 404 | |
| 405 | self->pdata = plat_data; |
| 406 | self->func = func; |
| 407 | spin_lock_init(&self->lock); |
| 408 | |
| 409 | spi_set_drvdata(func, self); |
| 410 | |
Solomon Peachy | 85ba8f5 | 2013-08-27 20:29:47 -0400 | [diff] [blame] | 411 | init_waitqueue_head(&self->wq); |
| 412 | |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 413 | status = cw1200_spi_irq_subscribe(self); |
| 414 | |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 415 | status = cw1200_core_probe(&cw1200_spi_hwbus_ops, |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 416 | self, &func->dev, &self->core, |
| 417 | self->pdata->ref_clk, |
| 418 | self->pdata->macaddr, |
| 419 | self->pdata->sdd_file, |
| 420 | self->pdata->have_5ghz); |
| 421 | |
| 422 | if (status) { |
| 423 | cw1200_spi_irq_unsubscribe(self); |
| 424 | cw1200_spi_off(plat_data); |
| 425 | kfree(self); |
| 426 | } |
| 427 | |
| 428 | return status; |
| 429 | } |
| 430 | |
| 431 | /* Disconnect Function to be called by SPI stack when device is disconnected */ |
| 432 | static int cw1200_spi_disconnect(struct spi_device *func) |
| 433 | { |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 434 | struct hwbus_priv *self = spi_get_drvdata(func); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 435 | |
| 436 | if (self) { |
| 437 | cw1200_spi_irq_unsubscribe(self); |
| 438 | if (self->core) { |
| 439 | cw1200_core_release(self->core); |
| 440 | self->core = NULL; |
| 441 | } |
| 442 | kfree(self); |
| 443 | } |
| 444 | cw1200_spi_off(func->dev.platform_data); |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
Solomon Peachy | 4e17b87 | 2013-05-29 22:22:05 -0400 | [diff] [blame] | 449 | #ifdef CONFIG_PM |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 450 | static int cw1200_spi_suspend(struct device *dev, pm_message_t state) |
| 451 | { |
Solomon Peachy | 911373c | 2013-06-01 08:08:42 -0400 | [diff] [blame] | 452 | struct hwbus_priv *self = spi_get_drvdata(to_spi_device(dev)); |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 453 | |
| 454 | if (!cw1200_can_suspend(self->core)) |
| 455 | return -EAGAIN; |
| 456 | |
| 457 | /* XXX notify host that we have to keep CW1200 powered on? */ |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | static int cw1200_spi_resume(struct device *dev) |
| 462 | { |
| 463 | return 0; |
| 464 | } |
Solomon Peachy | 4e17b87 | 2013-05-29 22:22:05 -0400 | [diff] [blame] | 465 | #endif |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 466 | |
| 467 | static struct spi_driver spi_driver = { |
| 468 | .probe = cw1200_spi_probe, |
| 469 | .remove = cw1200_spi_disconnect, |
| 470 | .driver = { |
| 471 | .name = "cw1200_wlan_spi", |
| 472 | .bus = &spi_bus_type, |
| 473 | .owner = THIS_MODULE, |
Solomon Peachy | 4e17b87 | 2013-05-29 22:22:05 -0400 | [diff] [blame] | 474 | #ifdef CONFIG_PM |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 475 | .suspend = cw1200_spi_suspend, |
| 476 | .resume = cw1200_spi_resume, |
Solomon Peachy | 4e17b87 | 2013-05-29 22:22:05 -0400 | [diff] [blame] | 477 | #endif |
Solomon Peachy | a910e4a | 2013-05-24 20:04:38 -0400 | [diff] [blame] | 478 | }, |
| 479 | }; |
| 480 | |
Wei Yongjun | d071c04 | 2013-05-30 19:42:54 +0800 | [diff] [blame] | 481 | module_spi_driver(spi_driver); |