1 // SPDX-License-Identifier: GPL-2.0
3 // CS42L43 SPI Controller Driver
5 // Copyright (C) 2022-2023 Cirrus Logic, Inc. and
6 // Cirrus Logic International Semiconductor Ltd.
8 #include <linux/acpi.h>
9 #include <linux/array_size.h>
10 #include <linux/bits.h>
11 #include <linux/bitfield.h>
12 #include <linux/cleanup.h>
13 #include <linux/device.h>
14 #include <linux/errno.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/gpio/machine.h>
17 #include <linux/gpio/property.h>
18 #include <linux/mfd/cs42l43.h>
19 #include <linux/mfd/cs42l43-regs.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/module.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/property.h>
26 #include <linux/regmap.h>
27 #include <linux/spi/spi.h>
28 #include <linux/units.h>
30 #define CS42L43_FIFO_SIZE 16
31 #define CS42L43_SPI_ROOT_HZ 49152000
32 #define CS42L43_SPI_MAX_LENGTH 65532
34 enum cs42l43_spi_cmd
{
41 struct regmap
*regmap
;
42 struct spi_controller
*ctlr
;
45 static const unsigned int cs42l43_clock_divs
[] = {
46 2, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30
49 static struct spi_board_info amp_info_template
= {
50 .modalias
= "cs35l56",
51 .max_speed_hz
= 11 * HZ_PER_MHZ
,
55 static const struct software_node cs42l43_gpiochip_swnode
= {
56 .name
= "cs42l43-pinctrl",
59 static const struct software_node_ref_args cs42l43_cs_refs
[] = {
60 SOFTWARE_NODE_REFERENCE(&cs42l43_gpiochip_swnode
, 0, GPIO_ACTIVE_LOW
),
61 SOFTWARE_NODE_REFERENCE(&swnode_gpio_undefined
),
64 static const struct property_entry cs42l43_cs_props
[] = {
65 PROPERTY_ENTRY_REF_ARRAY("cs-gpios", cs42l43_cs_refs
),
69 static int cs42l43_spi_tx(struct regmap
*regmap
, const u8
*buf
, unsigned int len
)
71 const u8
*end
= buf
+ len
;
76 const u8
*block
= min(buf
+ CS42L43_FIFO_SIZE
, end
);
79 const u8
*word
= min(buf
+ sizeof(u32
), block
);
80 int pad
= (buf
+ sizeof(u32
)) - word
;
83 val
>>= BITS_PER_BYTE
;
84 val
|= FIELD_PREP(GENMASK(31, 24), *buf
);
89 val
>>= pad
* BITS_PER_BYTE
;
91 regmap_write(regmap
, CS42L43_TX_DATA
, val
);
94 regmap_write(regmap
, CS42L43_TRAN_CONFIG8
, CS42L43_SPI_TX_DONE_MASK
);
96 ret
= regmap_read_poll_timeout(regmap
, CS42L43_TRAN_STATUS1
,
97 val
, (val
& CS42L43_SPI_TX_REQUEST_MASK
),
106 static int cs42l43_spi_rx(struct regmap
*regmap
, u8
*buf
, unsigned int len
)
113 u8
*block
= min(buf
+ CS42L43_FIFO_SIZE
, end
);
115 ret
= regmap_read_poll_timeout(regmap
, CS42L43_TRAN_STATUS1
,
116 val
, (val
& CS42L43_SPI_RX_REQUEST_MASK
),
121 while (buf
< block
) {
122 u8
*word
= min(buf
+ sizeof(u32
), block
);
124 ret
= regmap_read(regmap
, CS42L43_RX_DATA
, &val
);
129 *buf
= FIELD_GET(GENMASK(7, 0), val
);
131 val
>>= BITS_PER_BYTE
;
136 regmap_write(regmap
, CS42L43_TRAN_CONFIG8
, CS42L43_SPI_RX_DONE_MASK
);
142 static int cs42l43_transfer_one(struct spi_controller
*ctlr
, struct spi_device
*spi
,
143 struct spi_transfer
*tfr
)
145 struct cs42l43_spi
*priv
= spi_controller_get_devdata(spi
->controller
);
146 int i
, ret
= -EINVAL
;
148 for (i
= 0; i
< ARRAY_SIZE(cs42l43_clock_divs
); i
++) {
149 if (CS42L43_SPI_ROOT_HZ
/ cs42l43_clock_divs
[i
] <= tfr
->speed_hz
)
153 if (i
== ARRAY_SIZE(cs42l43_clock_divs
))
156 regmap_write(priv
->regmap
, CS42L43_SPI_CLK_CONFIG1
, i
);
159 regmap_write(priv
->regmap
, CS42L43_TRAN_CONFIG3
, CS42L43_WRITE
);
160 regmap_write(priv
->regmap
, CS42L43_TRAN_CONFIG4
, tfr
->len
- 1);
161 } else if (tfr
->rx_buf
) {
162 regmap_write(priv
->regmap
, CS42L43_TRAN_CONFIG3
, CS42L43_READ
);
163 regmap_write(priv
->regmap
, CS42L43_TRAN_CONFIG5
, tfr
->len
- 1);
166 regmap_write(priv
->regmap
, CS42L43_TRAN_CONFIG1
, CS42L43_SPI_START_MASK
);
169 ret
= cs42l43_spi_tx(priv
->regmap
, (const u8
*)tfr
->tx_buf
, tfr
->len
);
170 else if (tfr
->rx_buf
)
171 ret
= cs42l43_spi_rx(priv
->regmap
, (u8
*)tfr
->rx_buf
, tfr
->len
);
176 static void cs42l43_set_cs(struct spi_device
*spi
, bool is_high
)
178 struct cs42l43_spi
*priv
= spi_controller_get_devdata(spi
->controller
);
180 regmap_write(priv
->regmap
, CS42L43_SPI_CONFIG2
, !is_high
);
183 static int cs42l43_prepare_message(struct spi_controller
*ctlr
, struct spi_message
*msg
)
185 struct cs42l43_spi
*priv
= spi_controller_get_devdata(ctlr
);
186 struct spi_device
*spi
= msg
->spi
;
187 unsigned int spi_config1
= 0;
189 /* select another internal CS, which doesn't exist, so CS 0 is not used */
190 if (spi_get_csgpiod(spi
, 0))
191 spi_config1
|= 1 << CS42L43_SPI_SS_SEL_SHIFT
;
192 if (spi
->mode
& SPI_CPOL
)
193 spi_config1
|= CS42L43_SPI_CPOL_MASK
;
194 if (spi
->mode
& SPI_CPHA
)
195 spi_config1
|= CS42L43_SPI_CPHA_MASK
;
196 if (spi
->mode
& SPI_3WIRE
)
197 spi_config1
|= CS42L43_SPI_THREE_WIRE_MASK
;
199 regmap_write(priv
->regmap
, CS42L43_SPI_CONFIG1
, spi_config1
);
204 static int cs42l43_prepare_transfer_hardware(struct spi_controller
*ctlr
)
206 struct cs42l43_spi
*priv
= spi_controller_get_devdata(ctlr
);
209 ret
= regmap_write(priv
->regmap
, CS42L43_BLOCK_EN2
, CS42L43_SPI_MSTR_EN_MASK
);
211 dev_err(priv
->dev
, "Failed to enable SPI controller: %d\n", ret
);
216 static int cs42l43_unprepare_transfer_hardware(struct spi_controller
*ctlr
)
218 struct cs42l43_spi
*priv
= spi_controller_get_devdata(ctlr
);
221 ret
= regmap_write(priv
->regmap
, CS42L43_BLOCK_EN2
, 0);
223 dev_err(priv
->dev
, "Failed to disable SPI controller: %d\n", ret
);
228 static size_t cs42l43_spi_max_length(struct spi_device
*spi
)
230 return CS42L43_SPI_MAX_LENGTH
;
233 static int cs42l43_get_speaker_id_gpios(struct cs42l43_spi
*priv
, int *result
)
235 struct gpio_descs
*descs
;
239 descs
= gpiod_get_array_optional(priv
->dev
, "spk-id", GPIOD_IN
);
240 if (IS_ERR_OR_NULL(descs
))
241 return PTR_ERR(descs
);
244 for (i
= 0; i
< descs
->ndescs
; i
++) {
245 ret
= gpiod_get_value_cansleep(descs
->desc
[i
]);
252 dev_dbg(priv
->dev
, "spk-id-gpios = %d\n", spkid
);
255 gpiod_put_array(descs
);
260 static struct fwnode_handle
*cs42l43_find_xu_node(struct fwnode_handle
*fwnode
)
262 static const u32 func_smart_amp
= 0x1;
263 struct fwnode_handle
*child_fwnode
, *ext_fwnode
;
267 fwnode_for_each_child_node(fwnode
, child_fwnode
) {
268 acpi_handle handle
= ACPI_HANDLE_FWNODE(child_fwnode
);
270 ret
= acpi_get_local_address(handle
, &function
);
271 if (ret
|| function
!= func_smart_amp
)
274 ext_fwnode
= fwnode_get_named_child_node(child_fwnode
,
275 "mipi-sdca-function-expansion-subproperties");
279 fwnode_handle_put(child_fwnode
);
287 static struct spi_board_info
*cs42l43_create_bridge_amp(struct cs42l43_spi
*priv
,
288 const char * const name
,
291 struct property_entry
*props
= NULL
;
292 struct software_node
*swnode
;
293 struct spi_board_info
*info
;
296 props
= devm_kmalloc(priv
->dev
, sizeof(*props
), GFP_KERNEL
);
300 *props
= PROPERTY_ENTRY_U32("cirrus,speaker-id", spkid
);
303 swnode
= devm_kmalloc(priv
->dev
, sizeof(*swnode
), GFP_KERNEL
);
307 *swnode
= SOFTWARE_NODE(name
, props
, NULL
);
309 info
= devm_kmemdup(priv
->dev
, &_info_template
,
310 sizeof(amp_info_template
), GFP_KERNEL
);
314 info
->chip_select
= cs
;
315 info
->swnode
= swnode
;
320 static void cs42l43_release_of_node(void *data
)
322 fwnode_handle_put(data
);
325 static void cs42l43_release_sw_node(void *data
)
327 software_node_unregister(&cs42l43_gpiochip_swnode
);
330 static int cs42l43_spi_probe(struct platform_device
*pdev
)
332 struct cs42l43
*cs42l43
= dev_get_drvdata(pdev
->dev
.parent
);
333 struct cs42l43_spi
*priv
;
334 struct fwnode_handle
*fwnode
= dev_fwnode(cs42l43
->dev
);
335 struct fwnode_handle
*xu_fwnode
__free(fwnode_handle
) = cs42l43_find_xu_node(fwnode
);
340 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_KERNEL
);
344 priv
->ctlr
= devm_spi_alloc_host(&pdev
->dev
, sizeof(*priv
->ctlr
));
348 spi_controller_set_devdata(priv
->ctlr
, priv
);
350 priv
->dev
= &pdev
->dev
;
351 priv
->regmap
= cs42l43
->regmap
;
353 priv
->ctlr
->prepare_message
= cs42l43_prepare_message
;
354 priv
->ctlr
->prepare_transfer_hardware
= cs42l43_prepare_transfer_hardware
;
355 priv
->ctlr
->unprepare_transfer_hardware
= cs42l43_unprepare_transfer_hardware
;
356 priv
->ctlr
->transfer_one
= cs42l43_transfer_one
;
357 priv
->ctlr
->set_cs
= cs42l43_set_cs
;
358 priv
->ctlr
->max_transfer_size
= cs42l43_spi_max_length
;
359 priv
->ctlr
->mode_bits
= SPI_3WIRE
| SPI_MODE_X_MASK
;
360 priv
->ctlr
->flags
= SPI_CONTROLLER_HALF_DUPLEX
;
361 priv
->ctlr
->bits_per_word_mask
= SPI_BPW_MASK(8) | SPI_BPW_MASK(16) |
363 priv
->ctlr
->min_speed_hz
= CS42L43_SPI_ROOT_HZ
/
364 cs42l43_clock_divs
[ARRAY_SIZE(cs42l43_clock_divs
) - 1];
365 priv
->ctlr
->max_speed_hz
= CS42L43_SPI_ROOT_HZ
/ cs42l43_clock_divs
[0];
366 priv
->ctlr
->use_gpio_descriptors
= true;
367 priv
->ctlr
->auto_runtime_pm
= true;
369 ret
= devm_pm_runtime_enable(priv
->dev
);
373 pm_runtime_idle(priv
->dev
);
375 regmap_write(priv
->regmap
, CS42L43_TRAN_CONFIG6
, CS42L43_FIFO_SIZE
- 1);
376 regmap_write(priv
->regmap
, CS42L43_TRAN_CONFIG7
, CS42L43_FIFO_SIZE
- 1);
378 // Disable Watchdog timer and enable stall
379 regmap_write(priv
->regmap
, CS42L43_SPI_CONFIG3
, 0);
380 regmap_write(priv
->regmap
, CS42L43_SPI_CONFIG4
, CS42L43_SPI_STALL_ENA_MASK
);
382 if (is_of_node(fwnode
)) {
383 fwnode
= fwnode_get_named_child_node(fwnode
, "spi");
384 ret
= devm_add_action_or_reset(priv
->dev
, cs42l43_release_of_node
, fwnode
);
389 fwnode_property_read_u32(xu_fwnode
, "01fa-sidecar-instances", &nsidecars
);
392 ret
= fwnode_property_read_u32(xu_fwnode
, "01fa-spk-id-val", &spkid
);
394 dev_dbg(priv
->dev
, "01fa-spk-id-val = %d\n", spkid
);
395 } else if (ret
!= -EINVAL
) {
396 return dev_err_probe(priv
->dev
, ret
, "Failed to get spk-id-val\n");
398 ret
= cs42l43_get_speaker_id_gpios(priv
, &spkid
);
400 return dev_err_probe(priv
->dev
, ret
,
401 "Failed to get spk-id-gpios\n");
404 ret
= software_node_register(&cs42l43_gpiochip_swnode
);
406 return dev_err_probe(priv
->dev
, ret
,
407 "Failed to register gpio swnode\n");
409 ret
= devm_add_action_or_reset(priv
->dev
, cs42l43_release_sw_node
, NULL
);
413 ret
= device_create_managed_software_node(&priv
->ctlr
->dev
,
414 cs42l43_cs_props
, NULL
);
416 return dev_err_probe(priv
->dev
, ret
, "Failed to add swnode\n");
418 device_set_node(&priv
->ctlr
->dev
, fwnode
);
421 ret
= devm_spi_register_controller(priv
->dev
, priv
->ctlr
);
423 return dev_err_probe(priv
->dev
, ret
,
424 "Failed to register SPI controller\n");
427 struct spi_board_info
*ampl_info
;
428 struct spi_board_info
*ampr_info
;
430 ampl_info
= cs42l43_create_bridge_amp(priv
, "cs35l56-left", 0, spkid
);
434 ampr_info
= cs42l43_create_bridge_amp(priv
, "cs35l56-right", 1, spkid
);
438 if (!spi_new_device(priv
->ctlr
, ampl_info
))
439 return dev_err_probe(priv
->dev
, -ENODEV
,
440 "Failed to create left amp slave\n");
442 if (!spi_new_device(priv
->ctlr
, ampr_info
))
443 return dev_err_probe(priv
->dev
, -ENODEV
,
444 "Failed to create right amp slave\n");
450 static const struct platform_device_id cs42l43_spi_id_table
[] = {
454 MODULE_DEVICE_TABLE(platform
, cs42l43_spi_id_table
);
456 static struct platform_driver cs42l43_spi_driver
= {
458 .name
= "cs42l43-spi",
460 .probe
= cs42l43_spi_probe
,
461 .id_table
= cs42l43_spi_id_table
,
463 module_platform_driver(cs42l43_spi_driver
);
465 MODULE_IMPORT_NS("GPIO_SWNODE");
466 MODULE_DESCRIPTION("CS42L43 SPI Driver");
467 MODULE_AUTHOR("Lucas Tanure <tanureal@opensource.cirrus.com>");
468 MODULE_AUTHOR("Maciej Strozek <mstrozek@opensource.cirrus.com>");
469 MODULE_LICENSE("GPL");