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 static const struct wfx_platform_data pdata_wf200
= {
27 .file_fw
= "wfx/wfm_wf200",
28 .file_pds
= "wfx/wf200.pds",
29 .use_rising_clk
= true,
32 static const struct wfx_platform_data pdata_brd4001a
= {
33 .file_fw
= "wfx/wfm_wf200",
34 .file_pds
= "wfx/brd4001a.pds",
35 .use_rising_clk
= true,
38 static const struct wfx_platform_data pdata_brd8022a
= {
39 .file_fw
= "wfx/wfm_wf200",
40 .file_pds
= "wfx/brd8022a.pds",
41 .use_rising_clk
= true,
44 static const struct wfx_platform_data pdata_brd8023a
= {
45 .file_fw
= "wfx/wfm_wf200",
46 .file_pds
= "wfx/brd8023a.pds",
47 .use_rising_clk
= true,
51 struct spi_device
*func
;
53 struct gpio_desc
*gpio_reset
;
57 /* The chip reads 16bits of data at time and place them directly into (little endian) CPU register.
58 * So, the chip expects bytes order to be "B1 B0 B3 B2" (while LE is "B0 B1 B2 B3" and BE is
61 * A little endian host with bits_per_word == 16 should do the right job natively. The code below to
62 * support big endian host and commonly used SPI 8bits.
64 static int wfx_spi_copy_from_io(void *priv
, unsigned int addr
, void *dst
, size_t count
)
66 struct wfx_spi_priv
*bus
= priv
;
67 u16 regaddr
= (addr
<< 12) | (count
/ 2) | SET_READ
;
69 struct spi_transfer t_addr
= {
71 .len
= sizeof(regaddr
),
73 struct spi_transfer t_msg
= {
80 WARN(count
% 2, "buffer size must be a multiple of 2");
82 cpu_to_le16s(®addr
);
87 spi_message_add_tail(&t_addr
, &m
);
88 spi_message_add_tail(&t_msg
, &m
);
89 ret
= spi_sync(bus
->func
, &m
);
91 if (bus
->need_swab
&& addr
== WFX_REG_CONFIG
)
92 for (i
= 0; i
< count
/ 2; i
++)
97 static int wfx_spi_copy_to_io(void *priv
, unsigned int addr
, const void *src
, size_t count
)
99 struct wfx_spi_priv
*bus
= priv
;
100 u16 regaddr
= (addr
<< 12) | (count
/ 2);
101 /* FIXME: use a bounce buffer */
102 u16
*src16
= (void *)src
;
104 struct spi_message m
;
105 struct spi_transfer t_addr
= {
107 .len
= sizeof(regaddr
),
109 struct spi_transfer t_msg
= {
114 WARN(count
% 2, "buffer size must be a multiple of 2");
115 WARN(regaddr
& SET_READ
, "bad addr or size overflow");
117 cpu_to_le16s(®addr
);
119 /* Register address and CONFIG content always use 16bit big endian
124 if (bus
->need_swab
&& addr
== WFX_REG_CONFIG
)
125 for (i
= 0; i
< count
/ 2; i
++)
128 spi_message_init(&m
);
129 spi_message_add_tail(&t_addr
, &m
);
130 spi_message_add_tail(&t_msg
, &m
);
131 ret
= spi_sync(bus
->func
, &m
);
133 if (bus
->need_swab
&& addr
== WFX_REG_CONFIG
)
134 for (i
= 0; i
< count
/ 2; i
++)
139 static void wfx_spi_lock(void *priv
)
143 static void wfx_spi_unlock(void *priv
)
147 static irqreturn_t
wfx_spi_irq_handler(int irq
, void *priv
)
149 struct wfx_spi_priv
*bus
= priv
;
151 wfx_bh_request_rx(bus
->core
);
155 static int wfx_spi_irq_subscribe(void *priv
)
157 struct wfx_spi_priv
*bus
= priv
;
160 flags
= irq_get_trigger_type(bus
->func
->irq
);
162 flags
= IRQF_TRIGGER_HIGH
;
163 flags
|= IRQF_ONESHOT
;
164 return devm_request_threaded_irq(&bus
->func
->dev
, bus
->func
->irq
, NULL
,
165 wfx_spi_irq_handler
, flags
, "wfx", bus
);
168 static int wfx_spi_irq_unsubscribe(void *priv
)
170 struct wfx_spi_priv
*bus
= priv
;
172 devm_free_irq(&bus
->func
->dev
, bus
->func
->irq
, bus
);
176 static size_t wfx_spi_align_size(void *priv
, size_t size
)
178 /* Most of SPI controllers avoid DMA if buffer size is not 32bit aligned */
179 return ALIGN(size
, 4);
182 static const struct wfx_hwbus_ops wfx_spi_hwbus_ops
= {
183 .copy_from_io
= wfx_spi_copy_from_io
,
184 .copy_to_io
= wfx_spi_copy_to_io
,
185 .irq_subscribe
= wfx_spi_irq_subscribe
,
186 .irq_unsubscribe
= wfx_spi_irq_unsubscribe
,
187 .lock
= wfx_spi_lock
,
188 .unlock
= wfx_spi_unlock
,
189 .align_size
= wfx_spi_align_size
,
192 static int wfx_spi_probe(struct spi_device
*func
)
194 struct wfx_platform_data
*pdata
;
195 struct wfx_spi_priv
*bus
;
198 if (!func
->bits_per_word
)
199 func
->bits_per_word
= 16;
200 ret
= spi_setup(func
);
203 pdata
= (struct wfx_platform_data
*)spi_get_device_id(func
)->driver_data
;
205 dev_err(&func
->dev
, "unable to retrieve driver data (please report)\n");
209 /* Trace below is also displayed by spi_setup() if compiled with DEBUG */
210 dev_dbg(&func
->dev
, "SPI params: CS=%d, mode=%d bits/word=%d speed=%d\n",
211 spi_get_chipselect(func
, 0), func
->mode
, func
->bits_per_word
, func
->max_speed_hz
);
212 if (func
->bits_per_word
!= 16 && func
->bits_per_word
!= 8)
213 dev_warn(&func
->dev
, "unusual bits/word value: %d\n", func
->bits_per_word
);
214 if (func
->max_speed_hz
> 50000000)
215 dev_warn(&func
->dev
, "%dHz is a very high speed\n", func
->max_speed_hz
);
217 bus
= devm_kzalloc(&func
->dev
, sizeof(*bus
), GFP_KERNEL
);
221 if (func
->bits_per_word
== 8 || IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
))
222 bus
->need_swab
= true;
223 spi_set_drvdata(func
, bus
);
225 bus
->gpio_reset
= devm_gpiod_get_optional(&func
->dev
, "reset", GPIOD_OUT_LOW
);
226 if (IS_ERR(bus
->gpio_reset
))
227 return PTR_ERR(bus
->gpio_reset
);
228 if (!bus
->gpio_reset
) {
229 dev_warn(&func
->dev
, "gpio reset is not defined, trying to load firmware anyway\n");
231 gpiod_set_consumer_name(bus
->gpio_reset
, "wfx reset");
232 gpiod_set_value_cansleep(bus
->gpio_reset
, 1);
233 usleep_range(100, 150);
234 gpiod_set_value_cansleep(bus
->gpio_reset
, 0);
235 usleep_range(2000, 2500);
238 bus
->core
= wfx_init_common(&func
->dev
, pdata
, &wfx_spi_hwbus_ops
, bus
);
242 return wfx_probe(bus
->core
);
245 static void wfx_spi_remove(struct spi_device
*func
)
247 struct wfx_spi_priv
*bus
= spi_get_drvdata(func
);
249 wfx_release(bus
->core
);
252 /* For dynamic driver binding, kernel does not use OF to match driver. It only
253 * use modalias and modalias is a copy of 'compatible' DT node with vendor
256 static const struct spi_device_id wfx_spi_id
[] = {
257 { "wf200", (kernel_ulong_t
)&pdata_wf200
},
258 { "brd4001a", (kernel_ulong_t
)&pdata_brd4001a
},
259 { "brd8022a", (kernel_ulong_t
)&pdata_brd8022a
},
260 { "brd8023a", (kernel_ulong_t
)&pdata_brd8023a
},
263 MODULE_DEVICE_TABLE(spi
, wfx_spi_id
);
266 static const struct of_device_id wfx_spi_of_match
[] = {
267 { .compatible
= "silabs,wf200" },
268 { .compatible
= "silabs,brd4001a" },
269 { .compatible
= "silabs,brd8022a" },
270 { .compatible
= "silabs,brd8023a" },
273 MODULE_DEVICE_TABLE(of
, wfx_spi_of_match
);
276 struct spi_driver wfx_spi_driver
= {
279 .of_match_table
= of_match_ptr(wfx_spi_of_match
),
281 .id_table
= wfx_spi_id
,
282 .probe
= wfx_spi_probe
,
283 .remove
= wfx_spi_remove
,