1 // SPDX-License-Identifier: GPL-2.0-only
3 * Mac80211 SPI driver for ST-Ericsson CW1200 device
5 * Copyright (c) 2011, Sagrad Inc.
6 * Author: Solomon Peachy <speachy@sagrad.com>
8 * Based on cw1200_sdio.c
9 * Copyright (c) 2010, ST-Ericsson
10 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
13 #include <linux/module.h>
14 #include <linux/gpio.h>
15 #include <linux/delay.h>
16 #include <linux/spinlock.h>
17 #include <linux/interrupt.h>
18 #include <net/mac80211.h>
20 #include <linux/spi/spi.h>
21 #include <linux/device.h>
25 #include <linux/platform_data/net-cw1200.h>
28 MODULE_AUTHOR("Solomon Peachy <speachy@sagrad.com>");
29 MODULE_DESCRIPTION("mac80211 ST-Ericsson CW1200 SPI driver");
30 MODULE_LICENSE("GPL");
31 MODULE_ALIAS("spi:cw1200_wlan_spi");
33 /* #define SPI_DEBUG */
36 struct spi_device
*func
;
37 struct cw1200_common
*core
;
38 const struct cw1200_platform_data_spi
*pdata
;
39 spinlock_t lock
; /* Serialize all bus operations */
44 #define SDIO_TO_SPI_ADDR(addr) ((addr & 0x1f)>>2)
45 #define SET_WRITE 0x7FFF /* usage: and operation */
46 #define SET_READ 0x8000 /* usage: or operation */
48 /* Notes on byte ordering:
52 Hardware expects 32-bit data to be written as 16-bit BE words:
57 static int cw1200_spi_memcpy_fromio(struct hwbus_priv
*self
,
65 struct spi_transfer t_addr
= {
67 .len
= sizeof(regaddr
),
69 struct spi_transfer t_msg
= {
74 regaddr
= (SDIO_TO_SPI_ADDR(addr
))<<12;
76 regaddr
|= (count
>>1);
79 pr_info("READ : %04d from 0x%02x (%04x)\n", count
, addr
, regaddr
);
83 regaddr
= cpu_to_le16(regaddr
);
85 /* We have to byteswap if the SPI bus is limited to 8b operation
86 or we are running on a Big Endian system
88 #if defined(__LITTLE_ENDIAN)
89 if (self
->func
->bits_per_word
== 8)
91 regaddr
= swab16(regaddr
);
94 spi_message_add_tail(&t_addr
, &m
);
95 spi_message_add_tail(&t_msg
, &m
);
96 ret
= spi_sync(self
->func
, &m
);
100 for (i
= 0; i
< t_addr
.len
; i
++)
101 printk("%02x ", ((u8
*)t_addr
.tx_buf
)[i
]);
103 for (i
= 0; i
< t_msg
.len
; i
++)
104 printk("%02x ", ((u8
*)t_msg
.rx_buf
)[i
]);
108 /* We have to byteswap if the SPI bus is limited to 8b operation
109 or we are running on a Big Endian system
111 #if defined(__LITTLE_ENDIAN)
112 if (self
->func
->bits_per_word
== 8)
115 uint16_t *buf
= (uint16_t *)dst
;
116 for (i
= 0; i
< ((count
+ 1) >> 1); i
++)
117 buf
[i
] = swab16(buf
[i
]);
123 static int cw1200_spi_memcpy_toio(struct hwbus_priv
*self
,
125 const void *src
, int count
)
129 struct spi_transfer t_addr
= {
131 .len
= sizeof(regaddr
),
133 struct spi_transfer t_msg
= {
137 struct spi_message m
;
139 regaddr
= (SDIO_TO_SPI_ADDR(addr
))<<12;
140 regaddr
&= SET_WRITE
;
141 regaddr
|= (count
>>1);
144 pr_info("WRITE: %04d to 0x%02x (%04x)\n", count
, addr
, regaddr
);
148 regaddr
= cpu_to_le16(regaddr
);
150 /* We have to byteswap if the SPI bus is limited to 8b operation
151 or we are running on a Big Endian system
153 #if defined(__LITTLE_ENDIAN)
154 if (self
->func
->bits_per_word
== 8)
157 uint16_t *buf
= (uint16_t *)src
;
158 regaddr
= swab16(regaddr
);
159 for (i
= 0; i
< ((count
+ 1) >> 1); i
++)
160 buf
[i
] = swab16(buf
[i
]);
165 for (i
= 0; i
< t_addr
.len
; i
++)
166 printk("%02x ", ((u8
*)t_addr
.tx_buf
)[i
]);
168 for (i
= 0; i
< t_msg
.len
; i
++)
169 printk("%02x ", ((u8
*)t_msg
.tx_buf
)[i
]);
173 spi_message_init(&m
);
174 spi_message_add_tail(&t_addr
, &m
);
175 spi_message_add_tail(&t_msg
, &m
);
176 rval
= spi_sync(self
->func
, &m
);
179 pr_info("WROTE: %d\n", m
.actual_length
);
182 #if defined(__LITTLE_ENDIAN)
183 /* We have to byteswap if the SPI bus is limited to 8b operation */
184 if (self
->func
->bits_per_word
== 8)
187 uint16_t *buf
= (uint16_t *)src
;
188 for (i
= 0; i
< ((count
+ 1) >> 1); i
++)
189 buf
[i
] = swab16(buf
[i
]);
194 static void cw1200_spi_lock(struct hwbus_priv
*self
)
198 DECLARE_WAITQUEUE(wait
, current
);
202 add_wait_queue(&self
->wq
, &wait
);
203 spin_lock_irqsave(&self
->lock
, flags
);
205 set_current_state(TASK_UNINTERRUPTIBLE
);
208 spin_unlock_irqrestore(&self
->lock
, flags
);
210 spin_lock_irqsave(&self
->lock
, flags
);
212 set_current_state(TASK_RUNNING
);
214 spin_unlock_irqrestore(&self
->lock
, flags
);
215 remove_wait_queue(&self
->wq
, &wait
);
220 static void cw1200_spi_unlock(struct hwbus_priv
*self
)
224 spin_lock_irqsave(&self
->lock
, flags
);
226 spin_unlock_irqrestore(&self
->lock
, flags
);
232 static irqreturn_t
cw1200_spi_irq_handler(int irq
, void *dev_id
)
234 struct hwbus_priv
*self
= dev_id
;
237 cw1200_spi_lock(self
);
238 cw1200_irq_handler(self
->core
);
239 cw1200_spi_unlock(self
);
246 static int cw1200_spi_irq_subscribe(struct hwbus_priv
*self
)
250 pr_debug("SW IRQ subscribe\n");
252 ret
= request_threaded_irq(self
->func
->irq
, NULL
,
253 cw1200_spi_irq_handler
,
254 IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
,
255 "cw1200_wlan_irq", self
);
256 if (WARN_ON(ret
< 0))
259 ret
= enable_irq_wake(self
->func
->irq
);
266 free_irq(self
->func
->irq
, self
);
271 static int cw1200_spi_irq_unsubscribe(struct hwbus_priv
*self
)
275 pr_debug("SW IRQ unsubscribe\n");
276 disable_irq_wake(self
->func
->irq
);
277 free_irq(self
->func
->irq
, self
);
282 static int cw1200_spi_off(const struct cw1200_platform_data_spi
*pdata
)
285 gpio_set_value(pdata
->reset
, 0);
286 msleep(30); /* Min is 2 * CLK32K cycles */
287 gpio_free(pdata
->reset
);
290 if (pdata
->power_ctrl
)
291 pdata
->power_ctrl(pdata
, false);
293 pdata
->clk_ctrl(pdata
, false);
298 static int cw1200_spi_on(const struct cw1200_platform_data_spi
*pdata
)
300 /* Ensure I/Os are pulled low */
302 gpio_request(pdata
->reset
, "cw1200_wlan_reset");
303 gpio_direction_output(pdata
->reset
, 0);
305 if (pdata
->powerup
) {
306 gpio_request(pdata
->powerup
, "cw1200_wlan_powerup");
307 gpio_direction_output(pdata
->powerup
, 0);
309 if (pdata
->reset
|| pdata
->powerup
)
310 msleep(10); /* Settle time? */
312 /* Enable 3v3 and 1v8 to hardware */
313 if (pdata
->power_ctrl
) {
314 if (pdata
->power_ctrl(pdata
, true)) {
315 pr_err("power_ctrl() failed!\n");
321 if (pdata
->clk_ctrl
) {
322 if (pdata
->clk_ctrl(pdata
, true)) {
323 pr_err("clk_ctrl() failed!\n");
326 msleep(10); /* Delay until clock is stable for 2 cycles */
329 /* Enable POWERUP signal */
330 if (pdata
->powerup
) {
331 gpio_set_value(pdata
->powerup
, 1);
332 msleep(250); /* or more..? */
334 /* Enable RSTn signal */
336 gpio_set_value(pdata
->reset
, 1);
337 msleep(50); /* Or more..? */
342 static size_t cw1200_spi_align_size(struct hwbus_priv
*self
, size_t size
)
344 return size
& 1 ? size
+ 1 : size
;
347 static int cw1200_spi_pm(struct hwbus_priv
*self
, bool suspend
)
349 return irq_set_irq_wake(self
->func
->irq
, suspend
);
352 static const struct hwbus_ops cw1200_spi_hwbus_ops
= {
353 .hwbus_memcpy_fromio
= cw1200_spi_memcpy_fromio
,
354 .hwbus_memcpy_toio
= cw1200_spi_memcpy_toio
,
355 .lock
= cw1200_spi_lock
,
356 .unlock
= cw1200_spi_unlock
,
357 .align_size
= cw1200_spi_align_size
,
358 .power_mgmt
= cw1200_spi_pm
,
361 /* Probe Function to be called by SPI stack when device is discovered */
362 static int cw1200_spi_probe(struct spi_device
*func
)
364 const struct cw1200_platform_data_spi
*plat_data
=
365 dev_get_platdata(&func
->dev
);
366 struct hwbus_priv
*self
;
369 /* Sanity check speed */
370 if (func
->max_speed_hz
> 52000000)
371 func
->max_speed_hz
= 52000000;
372 if (func
->max_speed_hz
< 1000000)
373 func
->max_speed_hz
= 1000000;
375 /* Fix up transfer size */
376 if (plat_data
->spi_bits_per_word
)
377 func
->bits_per_word
= plat_data
->spi_bits_per_word
;
378 if (!func
->bits_per_word
)
379 func
->bits_per_word
= 16;
382 func
->mode
= SPI_MODE_0
;
384 pr_info("cw1200_wlan_spi: Probe called (CS %d M %d BPW %d CLK %d)\n",
385 func
->chip_select
, func
->mode
, func
->bits_per_word
,
388 if (cw1200_spi_on(plat_data
)) {
389 pr_err("spi_on() failed!\n");
393 if (spi_setup(func
)) {
394 pr_err("spi_setup() failed!\n");
398 self
= devm_kzalloc(&func
->dev
, sizeof(*self
), GFP_KERNEL
);
400 pr_err("Can't allocate SPI hwbus_priv.");
404 self
->pdata
= plat_data
;
406 spin_lock_init(&self
->lock
);
408 spi_set_drvdata(func
, self
);
410 init_waitqueue_head(&self
->wq
);
412 status
= cw1200_spi_irq_subscribe(self
);
414 status
= cw1200_core_probe(&cw1200_spi_hwbus_ops
,
415 self
, &func
->dev
, &self
->core
,
416 self
->pdata
->ref_clk
,
417 self
->pdata
->macaddr
,
418 self
->pdata
->sdd_file
,
419 self
->pdata
->have_5ghz
);
422 cw1200_spi_irq_unsubscribe(self
);
423 cw1200_spi_off(plat_data
);
429 /* Disconnect Function to be called by SPI stack when device is disconnected */
430 static int cw1200_spi_disconnect(struct spi_device
*func
)
432 struct hwbus_priv
*self
= spi_get_drvdata(func
);
435 cw1200_spi_irq_unsubscribe(self
);
437 cw1200_core_release(self
->core
);
441 cw1200_spi_off(dev_get_platdata(&func
->dev
));
446 static int __maybe_unused
cw1200_spi_suspend(struct device
*dev
)
448 struct hwbus_priv
*self
= spi_get_drvdata(to_spi_device(dev
));
450 if (!cw1200_can_suspend(self
->core
))
453 /* XXX notify host that we have to keep CW1200 powered on? */
457 static SIMPLE_DEV_PM_OPS(cw1200_pm_ops
, cw1200_spi_suspend
, NULL
);
459 static struct spi_driver spi_driver
= {
460 .probe
= cw1200_spi_probe
,
461 .remove
= cw1200_spi_disconnect
,
463 .name
= "cw1200_wlan_spi",
464 .pm
= IS_ENABLED(CONFIG_PM
) ? &cw1200_pm_ops
: NULL
,
468 module_spi_driver(spi_driver
);