Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
1e942b5
Documentation
LICENSES
arch
block
certs
crypto
drivers
accel
accessibility
acpi
amba
android
ata
atm
auxdisplay
base
bcma
block
bluetooth
bus
cache
cdrom
cdx
char
clk
clocksource
comedi
connector
counter
cpufreq
cpuidle
crypto
cxl
dax
dca
devfreq
dio
dma-buf
dma
dpll
edac
eisa
extcon
firewire
firmware
fpga
fsi
gnss
gpio
gpu
greybus
hid
hsi
hte
hv
hwmon
hwspinlock
hwtracing
i2c
i3c
idle
iio
infiniband
input
interconnect
iommu
ipack
irqchip
isdn
leds
macintosh
mailbox
mcb
md
media
memory
memstick
message
mfd
misc
mmc
most
mtd
mux
net
nfc
ntb
nubus
nvdimm
nvme
nvmem
of
opp
parisc
parport
pci
pcmcia
peci
perf
phy
pinctrl
platform
pmdomain
pnp
power
powercap
pps
ps3
ptp
pwm
rapidio
ras
regulator
remoteproc
reset
rpmsg
rtc
s390
sbus
scsi
sh
siox
slimbus
soc
soundwire
spi
Kconfig
Makefile
atmel-quadspi.c
internals.h
spi-altera-core.c
spi-altera-dfl.c
spi-altera-platform.c
spi-amd.c
spi-amlogic-spifc-a1.c
spi-ar934x.c
spi-armada-3700.c
spi-aspeed-smc.c
spi-at91-usart.c
spi-ath79.c
spi-atmel.c
spi-au1550.c
spi-axi-spi-engine.c
spi-bcm-qspi.c
spi-bcm-qspi.h
spi-bcm2835.c
spi-bcm2835aux.c
spi-bcm63xx-hsspi.c
spi-bcm63xx.c
spi-bcmbca-hsspi.c
spi-bitbang-txrx.h
spi-bitbang.c
spi-brcmstb-qspi.c
spi-butterfly.c
spi-cadence-quadspi.c
spi-cadence-xspi.c
spi-cadence.c
spi-cavium-octeon.c
spi-cavium-thunderx.c
spi-cavium.c
spi-cavium.h
spi-clps711x.c
spi-coldfire-qspi.c
spi-cs42l43.c
spi-davinci.c
spi-dln2.c
spi-dw-bt1.c
spi-dw-core.c
spi-dw-dma.c
spi-dw-mmio.c
spi-dw-pci.c
spi-dw.h
spi-ep93xx.c
spi-falcon.c
spi-fsi.c
spi-fsl-cpm.c
spi-fsl-cpm.h
spi-fsl-dspi.c
spi-fsl-espi.c
spi-fsl-lib.c
spi-fsl-lib.h
spi-fsl-lpspi.c
spi-fsl-qspi.c
spi-fsl-spi.c
spi-fsl-spi.h
spi-geni-qcom.c
spi-gpio.c
spi-gxp.c
spi-hisi-kunpeng.c
spi-hisi-sfc-v3xx.c
spi-img-spfi.c
spi-imx.c
spi-ingenic.c
spi-intel-pci.c
spi-intel-platform.c
spi-intel.c
spi-intel.h
spi-iproc-qspi.c
spi-jcore.c
spi-lantiq-ssc.c
spi-ljca.c
spi-lm70llp.c
spi-loongson-core.c
spi-loongson-pci.c
spi-loongson-plat.c
spi-loongson.h
spi-loopback-test.c
spi-lp8841-rtc.c
spi-mem.c
spi-meson-spicc.c
spi-meson-spifc.c
spi-microchip-core-qspi.c
spi-microchip-core.c
spi-mpc512x-psc.c
spi-mpc52xx-psc.c
spi-mpc52xx.c
spi-mt65xx.c
spi-mt7621.c
spi-mtk-nor.c
spi-mtk-snfi.c
spi-mux.c
spi-mxic.c
spi-mxs.c
spi-npcm-fiu.c
spi-npcm-pspi.c
spi-nxp-fspi.c
spi-oc-tiny.c
spi-omap-uwire.c
spi-omap2-mcspi.c
spi-orion.c
spi-pci1xxxx.c
spi-pic32-sqi.c
spi-pic32.c
spi-pl022.c
spi-ppc4xx.c
spi-pxa2xx-dma.c
spi-pxa2xx-pci.c
spi-pxa2xx.c
spi-pxa2xx.h
spi-qcom-qspi.c
spi-qup.c
spi-rb4xx.c
spi-realtek-rtl.c
spi-rockchip-sfc.c
spi-rockchip.c
spi-rpc-if.c
spi-rspi.c
spi-rzv2m-csi.c
spi-s3c64xx.c
spi-sc18is602.c
spi-sh-hspi.c
spi-sh-msiof.c
spi-sh-sci.c
spi-sh.c
spi-sifive.c
spi-slave-mt27xx.c
spi-slave-system-control.c
spi-slave-time.c
spi-sn-f-ospi.c
spi-sprd-adi.c
spi-sprd.c
spi-st-ssc4.c
spi-stm32-qspi.c
spi-stm32.c
spi-sun4i.c
spi-sun6i.c
spi-sunplus-sp7021.c
spi-synquacer.c
spi-tegra114.c
spi-tegra20-sflash.c
spi-tegra20-slink.c
spi-tegra210-quad.c
spi-test.h
spi-ti-qspi.c
spi-tle62x0.c
spi-topcliff-pch.c
spi-uniphier.c
spi-wpcm-fiu.c
spi-xcomm.c
spi-xilinx.c
spi-xlp.c
spi-xtensa-xtfpga.c
spi-zynq-qspi.c
spi-zynqmp-gqspi.c
spi.c
spidev.c
spmi
ssb
staging
target
tc
tee
thermal
thunderbolt
tty
ufs
uio
usb
vdpa
vfio
vhost
video
virt
virtio
w1
watchdog
xen
zorro
Kconfig
Makefile
fs
include
init
io_uring
ipc
kernel
lib
mm
net
rust
samples
scripts
security
sound
tools
usr
virt
.clang-format
.cocciconfig
.editorconfig
.get_maintainer.ignore
.gitattributes
.gitignore
.mailmap
.rustfmt.toml
COPYING
CREDITS
Kbuild
Kconfig
MAINTAINERS
Makefile
README
Breadcrumbs
linux
/
drivers
/
spi
/
spi-cs42l43.c
Copy path
Blame
Blame
Latest commit
Charles Keepax
and
Mark Brown
spi: cs42l43: Clean up of firmware node
Feb 2, 2024
1e942b5
·
Feb 2, 2024
History
History
296 lines (233 loc) · 7.76 KB
Breadcrumbs
linux
/
drivers
/
spi
/
spi-cs42l43.c
Top
File metadata and controls
Code
Blame
296 lines (233 loc) · 7.76 KB
Raw
// SPDX-License-Identifier: GPL-2.0 // // CS42L43 SPI Controller Driver // // Copyright (C) 2022-2023 Cirrus Logic, Inc. and // Cirrus Logic International Semiconductor Ltd. #include <linux/bits.h> #include <linux/bitfield.h> #include <linux/device.h> #include <linux/errno.h> #include <linux/mfd/cs42l43.h> #include <linux/mfd/cs42l43-regs.h> #include <linux/module.h> #include <linux/platform_device.h> #include <linux/pm_runtime.h> #include <linux/regmap.h> #include <linux/spi/spi.h> #include <linux/units.h> #define CS42L43_FIFO_SIZE 16 #define CS42L43_SPI_ROOT_HZ (40 * HZ_PER_MHZ) #define CS42L43_SPI_MAX_LENGTH 65532 enum cs42l43_spi_cmd { CS42L43_WRITE, CS42L43_READ }; struct cs42l43_spi { struct device *dev; struct regmap *regmap; struct spi_controller *ctlr; }; static const unsigned int cs42l43_clock_divs[] = { 2, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30 }; static int cs42l43_spi_tx(struct regmap *regmap, const u8 *buf, unsigned int len) { const u8 *end = buf + len; u32 val = 0; int ret; while (buf < end) { const u8 *block = min(buf + CS42L43_FIFO_SIZE, end); while (buf < block) { const u8 *word = min(buf + sizeof(u32), block); int pad = (buf + sizeof(u32)) - word; while (buf < word) { val >>= BITS_PER_BYTE; val |= FIELD_PREP(GENMASK(31, 24), *buf); buf++; } val >>= pad * BITS_PER_BYTE; regmap_write(regmap, CS42L43_TX_DATA, val); } regmap_write(regmap, CS42L43_TRAN_CONFIG8, CS42L43_SPI_TX_DONE_MASK); ret = regmap_read_poll_timeout(regmap, CS42L43_TRAN_STATUS1, val, (val & CS42L43_SPI_TX_REQUEST_MASK), 1000, 5000); if (ret) return ret; } return 0; } static int cs42l43_spi_rx(struct regmap *regmap, u8 *buf, unsigned int len) { u8 *end = buf + len; u32 val; int ret; while (buf < end) { u8 *block = min(buf + CS42L43_FIFO_SIZE, end); ret = regmap_read_poll_timeout(regmap, CS42L43_TRAN_STATUS1, val, (val & CS42L43_SPI_RX_REQUEST_MASK), 1000, 5000); if (ret) return ret; while (buf < block) { u8 *word = min(buf + sizeof(u32), block); ret = regmap_read(regmap, CS42L43_RX_DATA, &val); if (ret) return ret; while (buf < word) { *buf = FIELD_GET(GENMASK(7, 0), val); val >>= BITS_PER_BYTE; buf++; } } regmap_write(regmap, CS42L43_TRAN_CONFIG8, CS42L43_SPI_RX_DONE_MASK); } return 0; } static int cs42l43_transfer_one(struct spi_controller *ctlr, struct spi_device *spi, struct spi_transfer *tfr) { struct cs42l43_spi *priv = spi_controller_get_devdata(spi->controller); int i, ret = -EINVAL; for (i = 0; i < ARRAY_SIZE(cs42l43_clock_divs); i++) { if (CS42L43_SPI_ROOT_HZ / cs42l43_clock_divs[i] <= tfr->speed_hz) break; } if (i == ARRAY_SIZE(cs42l43_clock_divs)) return -EINVAL; regmap_write(priv->regmap, CS42L43_SPI_CLK_CONFIG1, i); if (tfr->tx_buf) { regmap_write(priv->regmap, CS42L43_TRAN_CONFIG3, CS42L43_WRITE); regmap_write(priv->regmap, CS42L43_TRAN_CONFIG4, tfr->len - 1); } else if (tfr->rx_buf) { regmap_write(priv->regmap, CS42L43_TRAN_CONFIG3, CS42L43_READ); regmap_write(priv->regmap, CS42L43_TRAN_CONFIG5, tfr->len - 1); } regmap_write(priv->regmap, CS42L43_TRAN_CONFIG1, CS42L43_SPI_START_MASK); if (tfr->tx_buf) ret = cs42l43_spi_tx(priv->regmap, (const u8 *)tfr->tx_buf, tfr->len); else if (tfr->rx_buf) ret = cs42l43_spi_rx(priv->regmap, (u8 *)tfr->rx_buf, tfr->len); return ret; } static void cs42l43_set_cs(struct spi_device *spi, bool is_high) { struct cs42l43_spi *priv = spi_controller_get_devdata(spi->controller); if (spi_get_chipselect(spi, 0) == 0) regmap_write(priv->regmap, CS42L43_SPI_CONFIG2, !is_high); } static int cs42l43_prepare_message(struct spi_controller *ctlr, struct spi_message *msg) { struct cs42l43_spi *priv = spi_controller_get_devdata(ctlr); struct spi_device *spi = msg->spi; unsigned int spi_config1 = 0; /* select another internal CS, which doesn't exist, so CS 0 is not used */ if (spi_get_csgpiod(spi, 0)) spi_config1 |= 1 << CS42L43_SPI_SS_SEL_SHIFT; if (spi->mode & SPI_CPOL) spi_config1 |= CS42L43_SPI_CPOL_MASK; if (spi->mode & SPI_CPHA) spi_config1 |= CS42L43_SPI_CPHA_MASK; if (spi->mode & SPI_3WIRE) spi_config1 |= CS42L43_SPI_THREE_WIRE_MASK; regmap_write(priv->regmap, CS42L43_SPI_CONFIG1, spi_config1); return 0; } static int cs42l43_prepare_transfer_hardware(struct spi_controller *ctlr) { struct cs42l43_spi *priv = spi_controller_get_devdata(ctlr); int ret; ret = regmap_write(priv->regmap, CS42L43_BLOCK_EN2, CS42L43_SPI_MSTR_EN_MASK); if (ret) dev_err(priv->dev, "Failed to enable SPI controller: %d\n", ret); return ret; } static int cs42l43_unprepare_transfer_hardware(struct spi_controller *ctlr) { struct cs42l43_spi *priv = spi_controller_get_devdata(ctlr); int ret; ret = regmap_write(priv->regmap, CS42L43_BLOCK_EN2, 0); if (ret) dev_err(priv->dev, "Failed to disable SPI controller: %d\n", ret); return ret; } static size_t cs42l43_spi_max_length(struct spi_device *spi) { return CS42L43_SPI_MAX_LENGTH; } static void cs42l43_release_of_node(void *data) { fwnode_handle_put(data); } static int cs42l43_spi_probe(struct platform_device *pdev) { struct cs42l43 *cs42l43 = dev_get_drvdata(pdev->dev.parent); struct cs42l43_spi *priv; struct fwnode_handle *fwnode = dev_fwnode(cs42l43->dev); int ret; priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL); if (!priv) return -ENOMEM; priv->ctlr = devm_spi_alloc_host(&pdev->dev, sizeof(*priv->ctlr)); if (!priv->ctlr) return -ENOMEM; spi_controller_set_devdata(priv->ctlr, priv); priv->dev = &pdev->dev; priv->regmap = cs42l43->regmap; priv->ctlr->prepare_message = cs42l43_prepare_message; priv->ctlr->prepare_transfer_hardware = cs42l43_prepare_transfer_hardware; priv->ctlr->unprepare_transfer_hardware = cs42l43_unprepare_transfer_hardware; priv->ctlr->transfer_one = cs42l43_transfer_one; priv->ctlr->set_cs = cs42l43_set_cs; priv->ctlr->max_transfer_size = cs42l43_spi_max_length; priv->ctlr->mode_bits = SPI_3WIRE | SPI_MODE_X_MASK; priv->ctlr->flags = SPI_CONTROLLER_HALF_DUPLEX; priv->ctlr->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) | SPI_BPW_MASK(32); priv->ctlr->min_speed_hz = CS42L43_SPI_ROOT_HZ / cs42l43_clock_divs[ARRAY_SIZE(cs42l43_clock_divs) - 1]; priv->ctlr->max_speed_hz = CS42L43_SPI_ROOT_HZ / cs42l43_clock_divs[0]; priv->ctlr->use_gpio_descriptors = true; priv->ctlr->auto_runtime_pm = true; ret = devm_pm_runtime_enable(priv->dev); if (ret) return ret; pm_runtime_idle(priv->dev); regmap_write(priv->regmap, CS42L43_TRAN_CONFIG6, CS42L43_FIFO_SIZE - 1); regmap_write(priv->regmap, CS42L43_TRAN_CONFIG7, CS42L43_FIFO_SIZE - 1); // Disable Watchdog timer and enable stall regmap_write(priv->regmap, CS42L43_SPI_CONFIG3, 0); regmap_write(priv->regmap, CS42L43_SPI_CONFIG4, CS42L43_SPI_STALL_ENA_MASK); if (is_of_node(fwnode)) { fwnode = fwnode_get_named_child_node(fwnode, "spi"); ret = devm_add_action(priv->dev, cs42l43_release_of_node, fwnode); if (ret) { fwnode_handle_put(fwnode); return ret; } } device_set_node(&priv->ctlr->dev, fwnode); ret = devm_spi_register_controller(priv->dev, priv->ctlr); if (ret) { dev_err(priv->dev, "Failed to register SPI controller: %d\n", ret); } return ret; } static const struct platform_device_id cs42l43_spi_id_table[] = { { "cs42l43-spi", }, {} }; MODULE_DEVICE_TABLE(platform, cs42l43_spi_id_table); static struct platform_driver cs42l43_spi_driver = { .driver = { .name = "cs42l43-spi", }, .probe = cs42l43_spi_probe, .id_table = cs42l43_spi_id_table, }; module_platform_driver(cs42l43_spi_driver); MODULE_DESCRIPTION("CS42L43 SPI Driver"); MODULE_AUTHOR("Lucas Tanure <tanureal@opensource.cirrus.com>"); MODULE_AUTHOR("Maciej Strozek <mstrozek@opensource.cirrus.com>"); MODULE_LICENSE("GPL");
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
You can’t perform that action at this time.