1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file is part of wl1251
5 * Copyright (C) 2008 Nokia Corporation
8 #include <linux/interrupt.h>
10 #include <linux/module.h>
11 #include <linux/slab.h>
12 #include <linux/swab.h>
13 #include <linux/crc7.h>
14 #include <linux/spi/spi.h>
15 #include <linux/wl12xx.h>
16 #include <linux/gpio.h>
18 #include <linux/of_gpio.h>
19 #include <linux/regulator/consumer.h>
25 static irqreturn_t
wl1251_irq(int irq
, void *cookie
)
29 wl1251_debug(DEBUG_IRQ
, "IRQ");
33 ieee80211_queue_work(wl
->hw
, &wl
->irq_work
);
38 static struct spi_device
*wl_to_spi(struct wl1251
*wl
)
43 static void wl1251_spi_reset(struct wl1251
*wl
)
46 struct spi_transfer t
;
49 cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
51 wl1251_error("could not allocate cmd for spi reset");
55 memset(&t
, 0, sizeof(t
));
58 memset(cmd
, 0xff, WSPI_INIT_CMD_LEN
);
61 t
.len
= WSPI_INIT_CMD_LEN
;
62 spi_message_add_tail(&t
, &m
);
64 spi_sync(wl_to_spi(wl
), &m
);
66 wl1251_dump(DEBUG_SPI
, "spi reset -> ", cmd
, WSPI_INIT_CMD_LEN
);
71 static void wl1251_spi_wake(struct wl1251
*wl
)
73 struct spi_transfer t
;
75 u8
*cmd
= kzalloc(WSPI_INIT_CMD_LEN
, GFP_KERNEL
);
78 wl1251_error("could not allocate cmd for spi init");
82 memset(&t
, 0, sizeof(t
));
85 /* Set WSPI_INIT_COMMAND
86 * the data is being send from the MSB to LSB
90 cmd
[2] = WSPI_INIT_CMD_START
| WSPI_INIT_CMD_TX
;
93 cmd
[5] = HW_ACCESS_WSPI_INIT_CMD_MASK
<< 3;
94 cmd
[5] |= HW_ACCESS_WSPI_FIXED_BUSY_LEN
& WSPI_INIT_CMD_FIXEDBUSY_LEN
;
96 cmd
[6] = WSPI_INIT_CMD_IOD
| WSPI_INIT_CMD_IP
| WSPI_INIT_CMD_CS
97 | WSPI_INIT_CMD_WSPI
| WSPI_INIT_CMD_WS
;
99 if (HW_ACCESS_WSPI_FIXED_BUSY_LEN
== 0)
100 cmd
[6] |= WSPI_INIT_CMD_DIS_FIXEDBUSY
;
102 cmd
[6] |= WSPI_INIT_CMD_EN_FIXEDBUSY
;
104 cmd
[7] = crc7_be(0, cmd
+2, WSPI_INIT_CMD_CRC_LEN
) | WSPI_INIT_CMD_END
;
106 * The above is the logical order; it must actually be stored
107 * in the buffer byte-swapped.
109 __swab32s((u32
*)cmd
);
110 __swab32s((u32
*)cmd
+1);
113 t
.len
= WSPI_INIT_CMD_LEN
;
114 spi_message_add_tail(&t
, &m
);
116 spi_sync(wl_to_spi(wl
), &m
);
118 wl1251_dump(DEBUG_SPI
, "spi init -> ", cmd
, WSPI_INIT_CMD_LEN
);
123 static void wl1251_spi_reset_wake(struct wl1251
*wl
)
125 wl1251_spi_reset(wl
);
129 static void wl1251_spi_read(struct wl1251
*wl
, int addr
, void *buf
,
132 struct spi_transfer t
[3];
133 struct spi_message m
;
137 cmd
= &wl
->buffer_cmd
;
138 busy_buf
= wl
->buffer_busyword
;
141 *cmd
|= WSPI_CMD_READ
;
142 *cmd
|= (len
<< WSPI_CMD_BYTE_LENGTH_OFFSET
) & WSPI_CMD_BYTE_LENGTH
;
143 *cmd
|= addr
& WSPI_CMD_BYTE_ADDR
;
145 spi_message_init(&m
);
146 memset(t
, 0, sizeof(t
));
150 spi_message_add_tail(&t
[0], &m
);
152 /* Busy and non busy words read */
153 t
[1].rx_buf
= busy_buf
;
154 t
[1].len
= WL1251_BUSY_WORD_LEN
;
155 spi_message_add_tail(&t
[1], &m
);
159 spi_message_add_tail(&t
[2], &m
);
161 spi_sync(wl_to_spi(wl
), &m
);
163 /* FIXME: check busy words */
165 wl1251_dump(DEBUG_SPI
, "spi_read cmd -> ", cmd
, sizeof(*cmd
));
166 wl1251_dump(DEBUG_SPI
, "spi_read buf <- ", buf
, len
);
169 static void wl1251_spi_write(struct wl1251
*wl
, int addr
, void *buf
,
172 struct spi_transfer t
[2];
173 struct spi_message m
;
176 cmd
= &wl
->buffer_cmd
;
179 *cmd
|= WSPI_CMD_WRITE
;
180 *cmd
|= (len
<< WSPI_CMD_BYTE_LENGTH_OFFSET
) & WSPI_CMD_BYTE_LENGTH
;
181 *cmd
|= addr
& WSPI_CMD_BYTE_ADDR
;
183 spi_message_init(&m
);
184 memset(t
, 0, sizeof(t
));
187 t
[0].len
= sizeof(*cmd
);
188 spi_message_add_tail(&t
[0], &m
);
192 spi_message_add_tail(&t
[1], &m
);
194 spi_sync(wl_to_spi(wl
), &m
);
196 wl1251_dump(DEBUG_SPI
, "spi_write cmd -> ", cmd
, sizeof(*cmd
));
197 wl1251_dump(DEBUG_SPI
, "spi_write buf -> ", buf
, len
);
200 static void wl1251_spi_enable_irq(struct wl1251
*wl
)
202 return enable_irq(wl
->irq
);
205 static void wl1251_spi_disable_irq(struct wl1251
*wl
)
207 return disable_irq(wl
->irq
);
210 static int wl1251_spi_set_power(struct wl1251
*wl
, bool enable
)
212 if (gpio_is_valid(wl
->power_gpio
))
213 gpio_set_value(wl
->power_gpio
, enable
);
218 static const struct wl1251_if_operations wl1251_spi_ops
= {
219 .read
= wl1251_spi_read
,
220 .write
= wl1251_spi_write
,
221 .reset
= wl1251_spi_reset_wake
,
222 .enable_irq
= wl1251_spi_enable_irq
,
223 .disable_irq
= wl1251_spi_disable_irq
,
224 .power
= wl1251_spi_set_power
,
227 static int wl1251_spi_probe(struct spi_device
*spi
)
229 struct wl1251_platform_data
*pdata
= dev_get_platdata(&spi
->dev
);
230 struct device_node
*np
= spi
->dev
.of_node
;
231 struct ieee80211_hw
*hw
;
236 wl1251_error("no platform data");
240 hw
= wl1251_alloc_hw();
246 SET_IEEE80211_DEV(hw
, &spi
->dev
);
247 spi_set_drvdata(spi
, wl
);
249 wl
->if_ops
= &wl1251_spi_ops
;
251 /* This is the only SPI value that we need to set here, the rest
252 * comes from the board-peripherals file
254 spi
->bits_per_word
= 32;
256 ret
= spi_setup(spi
);
258 wl1251_error("spi_setup failed");
263 wl
->use_eeprom
= of_property_read_bool(np
, "ti,wl1251-has-eeprom");
264 wl
->power_gpio
= of_get_named_gpio(np
, "ti,power-gpio", 0);
266 wl
->power_gpio
= pdata
->power_gpio
;
267 wl
->use_eeprom
= pdata
->use_eeprom
;
270 if (wl
->power_gpio
== -EPROBE_DEFER
) {
275 if (gpio_is_valid(wl
->power_gpio
)) {
276 ret
= devm_gpio_request_one(&spi
->dev
, wl
->power_gpio
,
277 GPIOF_OUT_INIT_LOW
, "wl1251 power");
279 wl1251_error("Failed to request gpio: %d\n", ret
);
283 wl1251_error("set power gpio missing in platform data");
290 wl1251_error("irq missing in platform data");
295 irq_set_status_flags(wl
->irq
, IRQ_NOAUTOEN
);
296 ret
= devm_request_irq(&spi
->dev
, wl
->irq
, wl1251_irq
, 0,
299 wl1251_error("request_irq() failed: %d", ret
);
303 irq_set_irq_type(wl
->irq
, IRQ_TYPE_EDGE_RISING
);
305 wl
->vio
= devm_regulator_get(&spi
->dev
, "vio");
306 if (IS_ERR(wl
->vio
)) {
307 ret
= PTR_ERR(wl
->vio
);
308 wl1251_error("vio regulator missing: %d", ret
);
312 ret
= regulator_enable(wl
->vio
);
316 ret
= wl1251_init_ieee80211(wl
);
318 goto disable_regulator
;
323 regulator_disable(wl
->vio
);
325 ieee80211_free_hw(hw
);
330 static int wl1251_spi_remove(struct spi_device
*spi
)
332 struct wl1251
*wl
= spi_get_drvdata(spi
);
335 regulator_disable(wl
->vio
);
340 static struct spi_driver wl1251_spi_driver
= {
345 .probe
= wl1251_spi_probe
,
346 .remove
= wl1251_spi_remove
,
349 module_spi_driver(wl1251_spi_driver
);
351 MODULE_LICENSE("GPL");
352 MODULE_AUTHOR("Kalle Valo <kvalo@adurom.com>");
353 MODULE_ALIAS("spi:wl1251");