File Index
Files and directories
Sign in
FILE

bus_spi.c

File preview with syntax highlighting.

linux-6.19.14/drivers/net/wireless/silabs/wfx/bus_spi.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * SPI interface.
  4 *
  5 * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
  6 * Copyright (c) 2011, Sagrad Inc.
  7 * Copyright (c) 2010, ST-Ericsson
  8 */
  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>
 15#include <linux/of.h>
 16#include <linux/pm.h>
 17
 18#include "bus.h"
 19#include "wfx.h"
 20#include "hwio.h"
 21#include "main.h"
 22#include "bh.h"
 23
 24#define SET_WRITE 0x7FFF        /* usage: and operation */
 25#define SET_READ 0x8000         /* usage: or operation */
 26
 27static const struct wfx_platform_data pdata_wf200 = {
 28	.file_fw = "wfx/wfm_wf200",
 29	.file_pds = "wfx/wf200.pds",
 30	.use_rising_clk = true,
 31};
 32
 33static const struct wfx_platform_data pdata_brd4001a = {
 34	.file_fw = "wfx/wfm_wf200",
 35	.file_pds = "wfx/brd4001a.pds",
 36	.use_rising_clk = true,
 37};
 38
 39static const struct wfx_platform_data pdata_brd8022a = {
 40	.file_fw = "wfx/wfm_wf200",
 41	.file_pds = "wfx/brd8022a.pds",
 42	.use_rising_clk = true,
 43};
 44
 45static const struct wfx_platform_data pdata_brd8023a = {
 46	.file_fw = "wfx/wfm_wf200",
 47	.file_pds = "wfx/brd8023a.pds",
 48	.use_rising_clk = true,
 49};
 50
 51struct wfx_spi_priv {
 52	struct spi_device *func;
 53	struct wfx_dev *core;
 54	struct gpio_desc *gpio_reset;
 55	bool need_swab;
 56};
 57
 58/* The chip reads 16bits of data at time and place them directly into (little endian) CPU register.
 59 * So, the chip expects bytes order to be "B1 B0 B3 B2" (while LE is "B0 B1 B2 B3" and BE is
 60 * "B3 B2 B1 B0")
 61 *
 62 * A little endian host with bits_per_word == 16 should do the right job natively. The code below to
 63 * support big endian host and commonly used SPI 8bits.
 64 */
 65static int wfx_spi_copy_from_io(void *priv, unsigned int addr, void *dst, size_t count)
 66{
 67	struct wfx_spi_priv *bus = priv;
 68	u16 regaddr = (addr << 12) | (count / 2) | SET_READ;
 69	struct spi_message m;
 70	struct spi_transfer t_addr = {
 71		.tx_buf = &regaddr,
 72		.len = sizeof(regaddr),
 73	};
 74	struct spi_transfer t_msg = {
 75		.rx_buf = dst,
 76		.len = count,
 77	};
 78	u16 *dst16 = dst;
 79	int ret, i;
 80
 81	WARN(count % 2, "buffer size must be a multiple of 2");
 82
 83	cpu_to_le16s(&regaddr);
 84	if (bus->need_swab)
 85		swab16s(&regaddr);
 86
 87	spi_message_init(&m);
 88	spi_message_add_tail(&t_addr, &m);
 89	spi_message_add_tail(&t_msg, &m);
 90	ret = spi_sync(bus->func, &m);
 91
 92	if (bus->need_swab && addr == WFX_REG_CONFIG)
 93		for (i = 0; i < count / 2; i++)
 94			swab16s(&dst16[i]);
 95	return ret;
 96}
 97
 98static int wfx_spi_copy_to_io(void *priv, unsigned int addr, const void *src, size_t count)
 99{
100	struct wfx_spi_priv *bus = priv;
101	u16 regaddr = (addr << 12) | (count / 2);
102	/* FIXME: use a bounce buffer */
103	u16 *src16 = (void *)src;
104	int ret, i;
105	struct spi_message m;
106	struct spi_transfer t_addr = {
107		.tx_buf = &regaddr,
108		.len = sizeof(regaddr),
109	};
110	struct spi_transfer t_msg = {
111		.tx_buf = src,
112		.len = count,
113	};
114
115	WARN(count % 2, "buffer size must be a multiple of 2");
116	WARN(regaddr & SET_READ, "bad addr or size overflow");
117
118	cpu_to_le16s(&regaddr);
119
120	/* Register address and CONFIG content always use 16bit big endian
121	 * ("BADC" order)
122	 */
123	if (bus->need_swab)
124		swab16s(&regaddr);
125	if (bus->need_swab && addr == WFX_REG_CONFIG)
126		for (i = 0; i < count / 2; i++)
127			swab16s(&src16[i]);
128
129	spi_message_init(&m);
130	spi_message_add_tail(&t_addr, &m);
131	spi_message_add_tail(&t_msg, &m);
132	ret = spi_sync(bus->func, &m);
133
134	if (bus->need_swab && addr == WFX_REG_CONFIG)
135		for (i = 0; i < count / 2; i++)
136			swab16s(&src16[i]);
137	return ret;
138}
139
140static void wfx_spi_lock(void *priv)
141{
142}
143
144static void wfx_spi_unlock(void *priv)
145{
146}
147
148static irqreturn_t wfx_spi_irq_handler(int irq, void *priv)
149{
150	struct wfx_spi_priv *bus = priv;
151
152	wfx_bh_request_rx(bus->core);
153	return IRQ_HANDLED;
154}
155
156static int wfx_spi_irq_subscribe(void *priv)
157{
158	struct wfx_spi_priv *bus = priv;
159	u32 flags;
160
161	flags = irq_get_trigger_type(bus->func->irq);
162	if (!flags)
163		flags = IRQF_TRIGGER_HIGH;
164	flags |= IRQF_ONESHOT;
165	return devm_request_threaded_irq(&bus->func->dev, bus->func->irq, NULL,
166					 wfx_spi_irq_handler, flags, "wfx", bus);
167}
168
169static int wfx_spi_irq_unsubscribe(void *priv)
170{
171	struct wfx_spi_priv *bus = priv;
172
173	devm_free_irq(&bus->func->dev, bus->func->irq, bus);
174	return 0;
175}
176
177static size_t wfx_spi_align_size(void *priv, size_t size)
178{
179	/* Most of SPI controllers avoid DMA if buffer size is not 32bit aligned */
180	return ALIGN(size, 4);
181}
182
183static void wfx_spi_set_wakeup(void *priv, bool enabled)
184{
185	struct wfx_spi_priv *bus = priv;
186
187	device_set_wakeup_enable(&bus->func->dev, enabled);
188}
189
190static const struct wfx_hwbus_ops wfx_spi_hwbus_ops = {
191	.copy_from_io    = wfx_spi_copy_from_io,
192	.copy_to_io      = wfx_spi_copy_to_io,
193	.irq_subscribe   = wfx_spi_irq_subscribe,
194	.irq_unsubscribe = wfx_spi_irq_unsubscribe,
195	.lock            = wfx_spi_lock,
196	.unlock          = wfx_spi_unlock,
197	.align_size      = wfx_spi_align_size,
198	.set_wakeup      = wfx_spi_set_wakeup,
199};
200
201static int wfx_spi_suspend(struct device *dev)
202{
203	struct spi_device *func = to_spi_device(dev);
204	struct wfx_spi_priv *bus = spi_get_drvdata(func);
205
206	if (!device_may_wakeup(dev))
207		return 0;
208	flush_work(&bus->core->hif.bh);
209	return enable_irq_wake(func->irq);
210}
211
212static int wfx_spi_resume(struct device *dev)
213{
214	struct spi_device *func = to_spi_device(dev);
215
216	if (!device_may_wakeup(dev))
217		return 0;
218	return disable_irq_wake(func->irq);
219}
220
221static int wfx_spi_probe(struct spi_device *func)
222{
223	struct wfx_platform_data *pdata;
224	struct wfx_spi_priv *bus;
225	int ret;
226
227	if (!func->bits_per_word)
228		func->bits_per_word = 16;
229	ret = spi_setup(func);
230	if (ret)
231		return ret;
232	pdata = (struct wfx_platform_data *)spi_get_device_id(func)->driver_data;
233	if (!pdata) {
234		dev_err(&func->dev, "unable to retrieve driver data (please report)\n");
235		return -ENODEV;
236	}
237
238	/* Trace below is also displayed by spi_setup() if compiled with DEBUG */
239	dev_dbg(&func->dev, "SPI params: CS=%d, mode=%d bits/word=%d speed=%d\n",
240		spi_get_chipselect(func, 0), func->mode, func->bits_per_word, func->max_speed_hz);
241	if (func->bits_per_word != 16 && func->bits_per_word != 8)
242		dev_warn(&func->dev, "unusual bits/word value: %d\n", func->bits_per_word);
243	if (func->max_speed_hz > 50000000)
244		dev_warn(&func->dev, "%dHz is a very high speed\n", func->max_speed_hz);
245
246	bus = devm_kzalloc(&func->dev, sizeof(*bus), GFP_KERNEL);
247	if (!bus)
248		return -ENOMEM;
249	bus->func = func;
250	if (func->bits_per_word == 8 || IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
251		bus->need_swab = true;
252	spi_set_drvdata(func, bus);
253
254	bus->gpio_reset = devm_gpiod_get_optional(&func->dev, "reset", GPIOD_OUT_LOW);
255	if (IS_ERR(bus->gpio_reset))
256		return PTR_ERR(bus->gpio_reset);
257	if (!bus->gpio_reset) {
258		dev_warn(&func->dev, "gpio reset is not defined, trying to load firmware anyway\n");
259	} else {
260		gpiod_set_consumer_name(bus->gpio_reset, "wfx reset");
261		gpiod_set_value_cansleep(bus->gpio_reset, 1);
262		usleep_range(100, 150);
263		gpiod_set_value_cansleep(bus->gpio_reset, 0);
264		usleep_range(2000, 2500);
265	}
266
267	bus->core = wfx_init_common(&func->dev, pdata, &wfx_spi_hwbus_ops, bus);
268	if (!bus->core)
269		return -EIO;
270
271	ret = wfx_probe(bus->core);
272	if (ret)
273		return ret;
274
275	device_set_wakeup_capable(&func->dev, true);
276	return 0;
277}
278
279static void wfx_spi_remove(struct spi_device *func)
280{
281	struct wfx_spi_priv *bus = spi_get_drvdata(func);
282
283	wfx_release(bus->core);
284}
285
286/* For dynamic driver binding, kernel does not use OF to match driver. It only
287 * use modalias and modalias is a copy of 'compatible' DT node with vendor
288 * stripped.
289 */
290static const struct spi_device_id wfx_spi_id[] = {
291	{ "wf200",    (kernel_ulong_t)&pdata_wf200 },
292	{ "brd4001a", (kernel_ulong_t)&pdata_brd4001a },
293	{ "brd8022a", (kernel_ulong_t)&pdata_brd8022a },
294	{ "brd8023a", (kernel_ulong_t)&pdata_brd8023a },
295	{ },
296};
297MODULE_DEVICE_TABLE(spi, wfx_spi_id);
298
299#ifdef CONFIG_OF
300static const struct of_device_id wfx_spi_of_match[] = {
301	{ .compatible = "silabs,wf200" },
302	{ .compatible = "silabs,brd4001a" },
303	{ .compatible = "silabs,brd8022a" },
304	{ .compatible = "silabs,brd8023a" },
305	{ },
306};
307MODULE_DEVICE_TABLE(of, wfx_spi_of_match);
308#endif
309
310static DEFINE_SIMPLE_DEV_PM_OPS(wfx_spi_pm_ops, wfx_spi_suspend, wfx_spi_resume);
311
312struct spi_driver wfx_spi_driver = {
313	.id_table = wfx_spi_id,
314	.probe = wfx_spi_probe,
315	.remove = wfx_spi_remove,
316	.driver = {
317		.name = "wfx-spi",
318		.of_match_table = of_match_ptr(wfx_spi_of_match),
319		.pm = &wfx_spi_pm_ops,
320	},
321};
~/ Read-only file preview.