xref: /linux/sound/soc/codecs/cs42xx8-spi.c (revision d2c9a99135da931377240942d44f3dea104cedb8)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Cirrus Logic CS42448/CS42888 Audio CODEC DAI SPI driver
4  *
5  * Copyright 2026 NXP
6  *
7  */
8 
9 #include <linux/module.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/regmap.h>
12 #include <linux/spi/spi.h>
13 #include <sound/soc.h>
14 
15 #include "cs42xx8.h"
16 
17 /*
18  * CS42448/CS42888 SPI register access (from datasheet Figure 23):
19  *
20  * The SPI frame is 3 bytes:
21  *   Byte 0: chip address [7:1] = 1001111, bit[0] = R/W (0=write, 1=read)
22  *           Write: 0x9E,  Read: 0x9F
23  *   Byte 1: MAP - Memory Address Pointer
24  *           bit[7] = INCR (auto-increment for burst), bits[6:0] = address
25  *   Byte 2: data byte
26  *
27  * We configure reg_bits=16 so that regmap treats the address field as 2 bytes
28  * (big-endian). The chip address byte (0x9E/0x9F) is placed in the high byte
29  * via write_flag_mask / read_flag_mask, and the MAP register address occupies
30  * the low byte. Currently INCR (MAP bit[7]) is not set and use_single_read/write
31  * are enabled. This produces the correct 3-byte on-wire frame without any
32  * custom bus implementation:
33  *
34  *   write: [0x9E, MAP_addr, data]
35  *   read:  [0x9F, MAP_addr] -> [data]
36  */
37 
cs42xx8_spi_probe(struct spi_device * spi)38 static int cs42xx8_spi_probe(struct spi_device *spi)
39 {
40 	struct cs42xx8_driver_data *drvdata;
41 	struct regmap_config config;
42 	int ret;
43 
44 	drvdata = (struct cs42xx8_driver_data *)spi_get_device_match_data(spi);
45 	if (!drvdata)
46 		return dev_err_probe(&spi->dev, -EINVAL,
47 				     "failed to find driver data\n");
48 
49 	config = cs42xx8_regmap_config;
50 	/*
51 	 * reg_bits=16 makes regmap send a 2-byte address field (big-endian).
52 	 * write_flag_mask/read_flag_mask are OR'd into that address field:
53 	 */
54 	config.reg_bits           = 16;
55 	config.write_flag_mask    = 0x9E;
56 	config.read_flag_mask     = 0x9F;
57 
58 	ret = cs42xx8_probe(&spi->dev,
59 			    devm_regmap_init_spi(spi, &config), drvdata);
60 	if (ret)
61 		return ret;
62 
63 	pm_runtime_enable(&spi->dev);
64 	pm_request_idle(&spi->dev);
65 
66 	return 0;
67 }
68 
cs42xx8_spi_remove(struct spi_device * spi)69 static void cs42xx8_spi_remove(struct spi_device *spi)
70 {
71 	pm_runtime_disable(&spi->dev);
72 }
73 
74 static const struct of_device_id cs42xx8_of_match[] = {
75 	{ .compatible = "cirrus,cs42448", .data = &cs42448_data, },
76 	{ .compatible = "cirrus,cs42888", .data = &cs42888_data, },
77 	{ /* sentinel */ }
78 };
79 MODULE_DEVICE_TABLE(of, cs42xx8_of_match);
80 
81 static const struct spi_device_id cs42xx8_spi_id[] = {
82 	{ .name = "cs42448", .driver_data = (kernel_ulong_t)&cs42448_data },
83 	{ .name = "cs42888", .driver_data = (kernel_ulong_t)&cs42888_data },
84 	{ }
85 };
86 MODULE_DEVICE_TABLE(spi, cs42xx8_spi_id);
87 
88 static struct spi_driver cs42xx8_spi_driver = {
89 	.driver = {
90 		.name = "cs42xx8",
91 		.pm = pm_ptr(&cs42xx8_pm),
92 		.of_match_table = cs42xx8_of_match,
93 	},
94 	.probe = cs42xx8_spi_probe,
95 	.remove = cs42xx8_spi_remove,
96 	.id_table = cs42xx8_spi_id,
97 };
98 
99 module_spi_driver(cs42xx8_spi_driver);
100 
101 MODULE_DESCRIPTION("Cirrus Logic CS42448/CS42888 ALSA SoC Codec SPI Driver");
102 MODULE_AUTHOR("Chancel Liu <chancel.liu@nxp.com>");
103 MODULE_LICENSE("GPL");
104