1ef75e767SLucas Tanure // SPDX-License-Identifier: GPL-2.0
2ef75e767SLucas Tanure //
3ef75e767SLucas Tanure // CS42L43 SPI Controller Driver
4ef75e767SLucas Tanure //
5ef75e767SLucas Tanure // Copyright (C) 2022-2023 Cirrus Logic, Inc. and
6ef75e767SLucas Tanure // Cirrus Logic International Semiconductor Ltd.
7ef75e767SLucas Tanure
8ef75e767SLucas Tanure #include <linux/bits.h>
9ef75e767SLucas Tanure #include <linux/bitfield.h>
10ef75e767SLucas Tanure #include <linux/device.h>
11ef75e767SLucas Tanure #include <linux/errno.h>
12ef75e767SLucas Tanure #include <linux/mfd/cs42l43.h>
13ef75e767SLucas Tanure #include <linux/mfd/cs42l43-regs.h>
14ef75e767SLucas Tanure #include <linux/module.h>
15ef75e767SLucas Tanure #include <linux/platform_device.h>
16ef75e767SLucas Tanure #include <linux/pm_runtime.h>
17ef75e767SLucas Tanure #include <linux/regmap.h>
18ef75e767SLucas Tanure #include <linux/spi/spi.h>
19ef75e767SLucas Tanure #include <linux/units.h>
20ef75e767SLucas Tanure
21ef75e767SLucas Tanure #define CS42L43_FIFO_SIZE 16
22*9f06731cSCharles Keepax #define CS42L43_SPI_ROOT_HZ 49152000
23ef75e767SLucas Tanure #define CS42L43_SPI_MAX_LENGTH 65532
24ef75e767SLucas Tanure
25ef75e767SLucas Tanure enum cs42l43_spi_cmd {
26ef75e767SLucas Tanure CS42L43_WRITE,
27ef75e767SLucas Tanure CS42L43_READ
28ef75e767SLucas Tanure };
29ef75e767SLucas Tanure
30ef75e767SLucas Tanure struct cs42l43_spi {
31ef75e767SLucas Tanure struct device *dev;
32ef75e767SLucas Tanure struct regmap *regmap;
33ef75e767SLucas Tanure struct spi_controller *ctlr;
34ef75e767SLucas Tanure };
35ef75e767SLucas Tanure
36ef75e767SLucas Tanure static const unsigned int cs42l43_clock_divs[] = {
37ef75e767SLucas Tanure 2, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30
38ef75e767SLucas Tanure };
39ef75e767SLucas Tanure
cs42l43_spi_tx(struct regmap * regmap,const u8 * buf,unsigned int len)40ef75e767SLucas Tanure static int cs42l43_spi_tx(struct regmap *regmap, const u8 *buf, unsigned int len)
41ef75e767SLucas Tanure {
42ef75e767SLucas Tanure const u8 *end = buf + len;
43ef75e767SLucas Tanure u32 val = 0;
44ef75e767SLucas Tanure int ret;
45ef75e767SLucas Tanure
46ef75e767SLucas Tanure while (buf < end) {
47ef75e767SLucas Tanure const u8 *block = min(buf + CS42L43_FIFO_SIZE, end);
48ef75e767SLucas Tanure
49ef75e767SLucas Tanure while (buf < block) {
50ef75e767SLucas Tanure const u8 *word = min(buf + sizeof(u32), block);
51ef75e767SLucas Tanure int pad = (buf + sizeof(u32)) - word;
52ef75e767SLucas Tanure
53ef75e767SLucas Tanure while (buf < word) {
54ef75e767SLucas Tanure val >>= BITS_PER_BYTE;
55ef75e767SLucas Tanure val |= FIELD_PREP(GENMASK(31, 24), *buf);
56ef75e767SLucas Tanure
57ef75e767SLucas Tanure buf++;
58ef75e767SLucas Tanure }
59ef75e767SLucas Tanure
60ef75e767SLucas Tanure val >>= pad * BITS_PER_BYTE;
61ef75e767SLucas Tanure
62ef75e767SLucas Tanure regmap_write(regmap, CS42L43_TX_DATA, val);
63ef75e767SLucas Tanure }
64ef75e767SLucas Tanure
65ef75e767SLucas Tanure regmap_write(regmap, CS42L43_TRAN_CONFIG8, CS42L43_SPI_TX_DONE_MASK);
66ef75e767SLucas Tanure
67ef75e767SLucas Tanure ret = regmap_read_poll_timeout(regmap, CS42L43_TRAN_STATUS1,
68ef75e767SLucas Tanure val, (val & CS42L43_SPI_TX_REQUEST_MASK),
69ef75e767SLucas Tanure 1000, 5000);
70ef75e767SLucas Tanure if (ret)
71ef75e767SLucas Tanure return ret;
72ef75e767SLucas Tanure }
73ef75e767SLucas Tanure
74ef75e767SLucas Tanure return 0;
75ef75e767SLucas Tanure }
76ef75e767SLucas Tanure
cs42l43_spi_rx(struct regmap * regmap,u8 * buf,unsigned int len)77ef75e767SLucas Tanure static int cs42l43_spi_rx(struct regmap *regmap, u8 *buf, unsigned int len)
78ef75e767SLucas Tanure {
79ef75e767SLucas Tanure u8 *end = buf + len;
80ef75e767SLucas Tanure u32 val;
81ef75e767SLucas Tanure int ret;
82ef75e767SLucas Tanure
83ef75e767SLucas Tanure while (buf < end) {
84ef75e767SLucas Tanure u8 *block = min(buf + CS42L43_FIFO_SIZE, end);
85ef75e767SLucas Tanure
86ef75e767SLucas Tanure ret = regmap_read_poll_timeout(regmap, CS42L43_TRAN_STATUS1,
87ef75e767SLucas Tanure val, (val & CS42L43_SPI_RX_REQUEST_MASK),
88ef75e767SLucas Tanure 1000, 5000);
89ef75e767SLucas Tanure if (ret)
90ef75e767SLucas Tanure return ret;
91ef75e767SLucas Tanure
92ef75e767SLucas Tanure while (buf < block) {
93ef75e767SLucas Tanure u8 *word = min(buf + sizeof(u32), block);
94ef75e767SLucas Tanure
95ef75e767SLucas Tanure ret = regmap_read(regmap, CS42L43_RX_DATA, &val);
96ef75e767SLucas Tanure if (ret)
97ef75e767SLucas Tanure return ret;
98ef75e767SLucas Tanure
99ef75e767SLucas Tanure while (buf < word) {
100ef75e767SLucas Tanure *buf = FIELD_GET(GENMASK(7, 0), val);
101ef75e767SLucas Tanure
102ef75e767SLucas Tanure val >>= BITS_PER_BYTE;
103ef75e767SLucas Tanure buf++;
104ef75e767SLucas Tanure }
105ef75e767SLucas Tanure }
106ef75e767SLucas Tanure
107ef75e767SLucas Tanure regmap_write(regmap, CS42L43_TRAN_CONFIG8, CS42L43_SPI_RX_DONE_MASK);
108ef75e767SLucas Tanure }
109ef75e767SLucas Tanure
110ef75e767SLucas Tanure return 0;
111ef75e767SLucas Tanure }
112ef75e767SLucas Tanure
cs42l43_transfer_one(struct spi_controller * ctlr,struct spi_device * spi,struct spi_transfer * tfr)113ef75e767SLucas Tanure static int cs42l43_transfer_one(struct spi_controller *ctlr, struct spi_device *spi,
114ef75e767SLucas Tanure struct spi_transfer *tfr)
115ef75e767SLucas Tanure {
116ef75e767SLucas Tanure struct cs42l43_spi *priv = spi_controller_get_devdata(spi->controller);
117ef75e767SLucas Tanure int i, ret = -EINVAL;
118ef75e767SLucas Tanure
119ef75e767SLucas Tanure for (i = 0; i < ARRAY_SIZE(cs42l43_clock_divs); i++) {
120ef75e767SLucas Tanure if (CS42L43_SPI_ROOT_HZ / cs42l43_clock_divs[i] <= tfr->speed_hz)
121ef75e767SLucas Tanure break;
122ef75e767SLucas Tanure }
123ef75e767SLucas Tanure
124ef75e767SLucas Tanure if (i == ARRAY_SIZE(cs42l43_clock_divs))
125ef75e767SLucas Tanure return -EINVAL;
126ef75e767SLucas Tanure
127ef75e767SLucas Tanure regmap_write(priv->regmap, CS42L43_SPI_CLK_CONFIG1, i);
128ef75e767SLucas Tanure
129ef75e767SLucas Tanure if (tfr->tx_buf) {
130ef75e767SLucas Tanure regmap_write(priv->regmap, CS42L43_TRAN_CONFIG3, CS42L43_WRITE);
131ef75e767SLucas Tanure regmap_write(priv->regmap, CS42L43_TRAN_CONFIG4, tfr->len - 1);
132ef75e767SLucas Tanure } else if (tfr->rx_buf) {
133ef75e767SLucas Tanure regmap_write(priv->regmap, CS42L43_TRAN_CONFIG3, CS42L43_READ);
134ef75e767SLucas Tanure regmap_write(priv->regmap, CS42L43_TRAN_CONFIG5, tfr->len - 1);
135ef75e767SLucas Tanure }
136ef75e767SLucas Tanure
137ef75e767SLucas Tanure regmap_write(priv->regmap, CS42L43_TRAN_CONFIG1, CS42L43_SPI_START_MASK);
138ef75e767SLucas Tanure
139ef75e767SLucas Tanure if (tfr->tx_buf)
140ef75e767SLucas Tanure ret = cs42l43_spi_tx(priv->regmap, (const u8 *)tfr->tx_buf, tfr->len);
141ef75e767SLucas Tanure else if (tfr->rx_buf)
142ef75e767SLucas Tanure ret = cs42l43_spi_rx(priv->regmap, (u8 *)tfr->rx_buf, tfr->len);
143ef75e767SLucas Tanure
144ef75e767SLucas Tanure return ret;
145ef75e767SLucas Tanure }
146ef75e767SLucas Tanure
cs42l43_set_cs(struct spi_device * spi,bool is_high)147ef75e767SLucas Tanure static void cs42l43_set_cs(struct spi_device *spi, bool is_high)
148ef75e767SLucas Tanure {
149ef75e767SLucas Tanure struct cs42l43_spi *priv = spi_controller_get_devdata(spi->controller);
150ef75e767SLucas Tanure
151ef75e767SLucas Tanure if (spi_get_chipselect(spi, 0) == 0)
152ef75e767SLucas Tanure regmap_write(priv->regmap, CS42L43_SPI_CONFIG2, !is_high);
153ef75e767SLucas Tanure }
154ef75e767SLucas Tanure
cs42l43_prepare_message(struct spi_controller * ctlr,struct spi_message * msg)155ef75e767SLucas Tanure static int cs42l43_prepare_message(struct spi_controller *ctlr, struct spi_message *msg)
156ef75e767SLucas Tanure {
157ef75e767SLucas Tanure struct cs42l43_spi *priv = spi_controller_get_devdata(ctlr);
158ef75e767SLucas Tanure struct spi_device *spi = msg->spi;
159ef75e767SLucas Tanure unsigned int spi_config1 = 0;
160ef75e767SLucas Tanure
161ef75e767SLucas Tanure /* select another internal CS, which doesn't exist, so CS 0 is not used */
162ef75e767SLucas Tanure if (spi_get_csgpiod(spi, 0))
163ef75e767SLucas Tanure spi_config1 |= 1 << CS42L43_SPI_SS_SEL_SHIFT;
164ef75e767SLucas Tanure if (spi->mode & SPI_CPOL)
165ef75e767SLucas Tanure spi_config1 |= CS42L43_SPI_CPOL_MASK;
166ef75e767SLucas Tanure if (spi->mode & SPI_CPHA)
167ef75e767SLucas Tanure spi_config1 |= CS42L43_SPI_CPHA_MASK;
168ef75e767SLucas Tanure if (spi->mode & SPI_3WIRE)
169ef75e767SLucas Tanure spi_config1 |= CS42L43_SPI_THREE_WIRE_MASK;
170ef75e767SLucas Tanure
171ef75e767SLucas Tanure regmap_write(priv->regmap, CS42L43_SPI_CONFIG1, spi_config1);
172ef75e767SLucas Tanure
173ef75e767SLucas Tanure return 0;
174ef75e767SLucas Tanure }
175ef75e767SLucas Tanure
cs42l43_prepare_transfer_hardware(struct spi_controller * ctlr)176ef75e767SLucas Tanure static int cs42l43_prepare_transfer_hardware(struct spi_controller *ctlr)
177ef75e767SLucas Tanure {
178ef75e767SLucas Tanure struct cs42l43_spi *priv = spi_controller_get_devdata(ctlr);
179ef75e767SLucas Tanure int ret;
180ef75e767SLucas Tanure
181ef75e767SLucas Tanure ret = regmap_write(priv->regmap, CS42L43_BLOCK_EN2, CS42L43_SPI_MSTR_EN_MASK);
182ef75e767SLucas Tanure if (ret)
183ef75e767SLucas Tanure dev_err(priv->dev, "Failed to enable SPI controller: %d\n", ret);
184ef75e767SLucas Tanure
185ef75e767SLucas Tanure return ret;
186ef75e767SLucas Tanure }
187ef75e767SLucas Tanure
cs42l43_unprepare_transfer_hardware(struct spi_controller * ctlr)188ef75e767SLucas Tanure static int cs42l43_unprepare_transfer_hardware(struct spi_controller *ctlr)
189ef75e767SLucas Tanure {
190ef75e767SLucas Tanure struct cs42l43_spi *priv = spi_controller_get_devdata(ctlr);
191ef75e767SLucas Tanure int ret;
192ef75e767SLucas Tanure
193ef75e767SLucas Tanure ret = regmap_write(priv->regmap, CS42L43_BLOCK_EN2, 0);
194ef75e767SLucas Tanure if (ret)
195ef75e767SLucas Tanure dev_err(priv->dev, "Failed to disable SPI controller: %d\n", ret);
196ef75e767SLucas Tanure
197ef75e767SLucas Tanure return ret;
198ef75e767SLucas Tanure }
199ef75e767SLucas Tanure
cs42l43_spi_max_length(struct spi_device * spi)200ef75e767SLucas Tanure static size_t cs42l43_spi_max_length(struct spi_device *spi)
201ef75e767SLucas Tanure {
202ef75e767SLucas Tanure return CS42L43_SPI_MAX_LENGTH;
203ef75e767SLucas Tanure }
204ef75e767SLucas Tanure
cs42l43_spi_probe(struct platform_device * pdev)205ef75e767SLucas Tanure static int cs42l43_spi_probe(struct platform_device *pdev)
206ef75e767SLucas Tanure {
207ef75e767SLucas Tanure struct cs42l43 *cs42l43 = dev_get_drvdata(pdev->dev.parent);
208ef75e767SLucas Tanure struct cs42l43_spi *priv;
209ef75e767SLucas Tanure struct fwnode_handle *fwnode = dev_fwnode(cs42l43->dev);
210ef75e767SLucas Tanure int ret;
211ef75e767SLucas Tanure
212ef75e767SLucas Tanure priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
213ef75e767SLucas Tanure if (!priv)
214ef75e767SLucas Tanure return -ENOMEM;
215ef75e767SLucas Tanure
216ef75e767SLucas Tanure priv->ctlr = devm_spi_alloc_master(&pdev->dev, sizeof(*priv->ctlr));
217ef75e767SLucas Tanure if (!priv->ctlr)
218ef75e767SLucas Tanure return -ENOMEM;
219ef75e767SLucas Tanure
220ef75e767SLucas Tanure spi_controller_set_devdata(priv->ctlr, priv);
221ef75e767SLucas Tanure
222ef75e767SLucas Tanure priv->dev = &pdev->dev;
223ef75e767SLucas Tanure priv->regmap = cs42l43->regmap;
224ef75e767SLucas Tanure
225ef75e767SLucas Tanure priv->ctlr->prepare_message = cs42l43_prepare_message;
226ef75e767SLucas Tanure priv->ctlr->prepare_transfer_hardware = cs42l43_prepare_transfer_hardware;
227ef75e767SLucas Tanure priv->ctlr->unprepare_transfer_hardware = cs42l43_unprepare_transfer_hardware;
228ef75e767SLucas Tanure priv->ctlr->transfer_one = cs42l43_transfer_one;
229ef75e767SLucas Tanure priv->ctlr->set_cs = cs42l43_set_cs;
230ef75e767SLucas Tanure priv->ctlr->max_transfer_size = cs42l43_spi_max_length;
231ef75e767SLucas Tanure
232ef75e767SLucas Tanure if (is_of_node(fwnode))
233ef75e767SLucas Tanure fwnode = fwnode_get_named_child_node(fwnode, "spi");
234ef75e767SLucas Tanure
235ef75e767SLucas Tanure device_set_node(&priv->ctlr->dev, fwnode);
236ef75e767SLucas Tanure
237ef75e767SLucas Tanure priv->ctlr->mode_bits = SPI_3WIRE | SPI_MODE_X_MASK;
238ef75e767SLucas Tanure priv->ctlr->flags = SPI_CONTROLLER_HALF_DUPLEX;
239ef75e767SLucas Tanure priv->ctlr->bits_per_word_mask = SPI_BPW_MASK(8) | SPI_BPW_MASK(16) |
240ef75e767SLucas Tanure SPI_BPW_MASK(32);
241ef75e767SLucas Tanure priv->ctlr->min_speed_hz = CS42L43_SPI_ROOT_HZ /
242ef75e767SLucas Tanure cs42l43_clock_divs[ARRAY_SIZE(cs42l43_clock_divs) - 1];
243ef75e767SLucas Tanure priv->ctlr->max_speed_hz = CS42L43_SPI_ROOT_HZ / cs42l43_clock_divs[0];
244ef75e767SLucas Tanure priv->ctlr->use_gpio_descriptors = true;
245ef75e767SLucas Tanure priv->ctlr->auto_runtime_pm = true;
246ef75e767SLucas Tanure
247ab7318c7SCharles Keepax ret = devm_pm_runtime_enable(priv->dev);
248ab7318c7SCharles Keepax if (ret)
249ab7318c7SCharles Keepax return ret;
250ab7318c7SCharles Keepax
251ef75e767SLucas Tanure pm_runtime_idle(priv->dev);
252ef75e767SLucas Tanure
253ef75e767SLucas Tanure regmap_write(priv->regmap, CS42L43_TRAN_CONFIG6, CS42L43_FIFO_SIZE - 1);
254ef75e767SLucas Tanure regmap_write(priv->regmap, CS42L43_TRAN_CONFIG7, CS42L43_FIFO_SIZE - 1);
255ef75e767SLucas Tanure
256ef75e767SLucas Tanure // Disable Watchdog timer and enable stall
257ef75e767SLucas Tanure regmap_write(priv->regmap, CS42L43_SPI_CONFIG3, 0);
258ef75e767SLucas Tanure regmap_write(priv->regmap, CS42L43_SPI_CONFIG4, CS42L43_SPI_STALL_ENA_MASK);
259ef75e767SLucas Tanure
260ef75e767SLucas Tanure ret = devm_spi_register_controller(priv->dev, priv->ctlr);
261ef75e767SLucas Tanure if (ret) {
262ef75e767SLucas Tanure dev_err(priv->dev, "Failed to register SPI controller: %d\n", ret);
263ef75e767SLucas Tanure }
264ef75e767SLucas Tanure
265ef75e767SLucas Tanure return ret;
266ef75e767SLucas Tanure }
267ef75e767SLucas Tanure
268ef75e767SLucas Tanure static const struct platform_device_id cs42l43_spi_id_table[] = {
269ef75e767SLucas Tanure { "cs42l43-spi", },
270ef75e767SLucas Tanure {}
271ef75e767SLucas Tanure };
272ef75e767SLucas Tanure MODULE_DEVICE_TABLE(platform, cs42l43_spi_id_table);
273ef75e767SLucas Tanure
274ef75e767SLucas Tanure static struct platform_driver cs42l43_spi_driver = {
275ef75e767SLucas Tanure .driver = {
276ef75e767SLucas Tanure .name = "cs42l43-spi",
277ef75e767SLucas Tanure },
278ef75e767SLucas Tanure .probe = cs42l43_spi_probe,
279ef75e767SLucas Tanure .id_table = cs42l43_spi_id_table,
280ef75e767SLucas Tanure };
281ef75e767SLucas Tanure module_platform_driver(cs42l43_spi_driver);
282ef75e767SLucas Tanure
283ef75e767SLucas Tanure MODULE_DESCRIPTION("CS42L43 SPI Driver");
284ef75e767SLucas Tanure MODULE_AUTHOR("Lucas Tanure <tanureal@opensource.cirrus.com>");
285ef75e767SLucas Tanure MODULE_AUTHOR("Maciej Strozek <mstrozek@opensource.cirrus.com>");
286ef75e767SLucas Tanure MODULE_LICENSE("GPL");
287