1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
6 * Copyright (c) 2011, Sagrad Inc.
7 * Copyright (c) 2010, ST-Ericsson
9 #include <linux/module.h>
10 #include <linux/delay.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/spi/spi.h>
13 #include <linux/interrupt.h>
14 #include <linux/irq.h>
23 #define SET_WRITE 0x7FFF /* usage: and operation */
24 #define SET_READ 0x8000 /* usage: or operation */
26 #define WFX_RESET_INVERTED 1
28 static const struct wfx_platform_data wfx_spi_pdata = {
29 .file_fw = "wfm_wf200",
30 .file_pds = "wf200.pds",
31 .use_rising_clk = true,
35 struct spi_device *func;
37 struct gpio_desc *gpio_reset;
41 /* The chip reads 16bits of data at time and place them directly into (little
42 * endian) CPU register. So, the chip expects bytes order to be "B1 B0 B3 B2"
43 * (while LE is "B0 B1 B2 B3" and BE is "B3 B2 B1 B0")
45 * A little endian host with bits_per_word == 16 should do the right job
46 * natively. The code below to support big endian host and commonly used SPI
49 static int wfx_spi_copy_from_io(void *priv, unsigned int addr,
50 void *dst, size_t count)
52 struct wfx_spi_priv *bus = priv;
53 u16 regaddr = (addr << 12) | (count / 2) | SET_READ;
55 struct spi_transfer t_addr = {
57 .len = sizeof(regaddr),
59 struct spi_transfer t_msg = {
66 WARN(count % 2, "buffer size must be a multiple of 2");
68 cpu_to_le16s(®addr);
73 spi_message_add_tail(&t_addr, &m);
74 spi_message_add_tail(&t_msg, &m);
75 ret = spi_sync(bus->func, &m);
77 if (bus->need_swab && addr == WFX_REG_CONFIG)
78 for (i = 0; i < count / 2; i++)
83 static int wfx_spi_copy_to_io(void *priv, unsigned int addr,
84 const void *src, size_t count)
86 struct wfx_spi_priv *bus = priv;
87 u16 regaddr = (addr << 12) | (count / 2);
88 /* FIXME: use a bounce buffer */
89 u16 *src16 = (void *)src;
92 struct spi_transfer t_addr = {
94 .len = sizeof(regaddr),
96 struct spi_transfer t_msg = {
101 WARN(count % 2, "buffer size must be a multiple of 2");
102 WARN(regaddr & SET_READ, "bad addr or size overflow");
104 cpu_to_le16s(®addr);
106 /* Register address and CONFIG content always use 16bit big endian
111 if (bus->need_swab && addr == WFX_REG_CONFIG)
112 for (i = 0; i < count / 2; i++)
115 spi_message_init(&m);
116 spi_message_add_tail(&t_addr, &m);
117 spi_message_add_tail(&t_msg, &m);
118 ret = spi_sync(bus->func, &m);
120 if (bus->need_swab && addr == WFX_REG_CONFIG)
121 for (i = 0; i < count / 2; i++)
126 static void wfx_spi_lock(void *priv)
130 static void wfx_spi_unlock(void *priv)
134 static irqreturn_t wfx_spi_irq_handler(int irq, void *priv)
136 struct wfx_spi_priv *bus = priv;
138 wfx_bh_request_rx(bus->core);
142 static int wfx_spi_irq_subscribe(void *priv)
144 struct wfx_spi_priv *bus = priv;
147 flags = irq_get_trigger_type(bus->func->irq);
149 flags = IRQF_TRIGGER_HIGH;
150 flags |= IRQF_ONESHOT;
151 return devm_request_threaded_irq(&bus->func->dev, bus->func->irq, NULL,
152 wfx_spi_irq_handler, IRQF_ONESHOT,
156 static int wfx_spi_irq_unsubscribe(void *priv)
158 struct wfx_spi_priv *bus = priv;
160 devm_free_irq(&bus->func->dev, bus->func->irq, bus);
164 static size_t wfx_spi_align_size(void *priv, size_t size)
166 /* Most of SPI controllers avoid DMA if buffer size is not 32bit aligned
168 return ALIGN(size, 4);
171 static const struct hwbus_ops wfx_spi_hwbus_ops = {
172 .copy_from_io = wfx_spi_copy_from_io,
173 .copy_to_io = wfx_spi_copy_to_io,
174 .irq_subscribe = wfx_spi_irq_subscribe,
175 .irq_unsubscribe = wfx_spi_irq_unsubscribe,
176 .lock = wfx_spi_lock,
177 .unlock = wfx_spi_unlock,
178 .align_size = wfx_spi_align_size,
181 static int wfx_spi_probe(struct spi_device *func)
183 struct wfx_spi_priv *bus;
186 if (!func->bits_per_word)
187 func->bits_per_word = 16;
188 ret = spi_setup(func);
191 /* Trace below is also displayed by spi_setup() if compiled with DEBUG */
192 dev_dbg(&func->dev, "SPI params: CS=%d, mode=%d bits/word=%d speed=%d\n",
193 func->chip_select, func->mode, func->bits_per_word,
195 if (func->bits_per_word != 16 && func->bits_per_word != 8)
196 dev_warn(&func->dev, "unusual bits/word value: %d\n",
197 func->bits_per_word);
198 if (func->max_speed_hz > 50000000)
199 dev_warn(&func->dev, "%dHz is a very high speed\n",
202 bus = devm_kzalloc(&func->dev, sizeof(*bus), GFP_KERNEL);
206 if (func->bits_per_word == 8 || IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
207 bus->need_swab = true;
208 spi_set_drvdata(func, bus);
210 bus->gpio_reset = devm_gpiod_get_optional(&func->dev, "reset",
212 if (IS_ERR(bus->gpio_reset))
213 return PTR_ERR(bus->gpio_reset);
214 if (!bus->gpio_reset) {
216 "gpio reset is not defined, trying to load firmware anyway\n");
218 gpiod_set_consumer_name(bus->gpio_reset, "wfx reset");
219 if (spi_get_device_id(func)->driver_data & WFX_RESET_INVERTED)
220 gpiod_toggle_active_low(bus->gpio_reset);
221 gpiod_set_value_cansleep(bus->gpio_reset, 1);
222 usleep_range(100, 150);
223 gpiod_set_value_cansleep(bus->gpio_reset, 0);
224 usleep_range(2000, 2500);
227 bus->core = wfx_init_common(&func->dev, &wfx_spi_pdata,
228 &wfx_spi_hwbus_ops, bus);
232 return wfx_probe(bus->core);
235 static void wfx_spi_remove(struct spi_device *func)
237 struct wfx_spi_priv *bus = spi_get_drvdata(func);
239 wfx_release(bus->core);
242 /* For dynamic driver binding, kernel does not use OF to match driver. It only
243 * use modalias and modalias is a copy of 'compatible' DT node with vendor
246 static const struct spi_device_id wfx_spi_id[] = {
247 { "wfx-spi", WFX_RESET_INVERTED },
251 MODULE_DEVICE_TABLE(spi, wfx_spi_id);
254 static const struct of_device_id wfx_spi_of_match[] = {
255 { .compatible = "silabs,wfx-spi", .data = (void *)WFX_RESET_INVERTED },
256 { .compatible = "silabs,wf200" },
259 MODULE_DEVICE_TABLE(of, wfx_spi_of_match);
262 struct spi_driver wfx_spi_driver = {
265 .of_match_table = of_match_ptr(wfx_spi_of_match),
267 .id_table = wfx_spi_id,
268 .probe = wfx_spi_probe,
269 .remove = wfx_spi_remove,