Skip to content

Commit ace6d14

Browse files
charleskeepaxlag-linaro
authored andcommitted
mfd: cs42l43: Add support for cs42l43 core driver
The CS42L43 is an audio CODEC with integrated MIPI SoundWire interface (Version 1.2.1 compliant), I2C, SPI, and I2S/TDM interfaces designed for portable applications. It provides a high dynamic range, stereo DAC for headphone output, two integrated Class D amplifiers for loudspeakers, and two ADCs for wired headset microphone input or stereo line input. PDM inputs are provided for digital microphones. The MFD component registers and initialises the device and provides PM/system power management. Signed-off-by: Charles Keepax <ckeepax@opensource.cirrus.com> Link: https://lore.kernel.org/r/20230804104602.395892-4-ckeepax@opensource.cirrus.com Signed-off-by: Lee Jones <lee@kernel.org>
1 parent ec77cad commit ace6d14

9 files changed

Lines changed: 2867 additions & 0 deletions

File tree

MAINTAINERS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4879,7 +4879,9 @@ L: alsa-devel@alsa-project.org (moderated for non-subscribers)
48794879
L: patches@opensource.cirrus.com
48804880
S: Maintained
48814881
F: Documentation/devicetree/bindings/sound/cirrus,cs*
4882+
F: drivers/mfd/cs42l43*
48824883
F: include/dt-bindings/sound/cs*
4884+
F: include/linux/mfd/cs42l43*
48834885
F: include/sound/cs*
48844886
F: sound/pci/hda/cs*
48854887
F: sound/pci/hda/hda_cs_dsp_ctl.*

drivers/mfd/Kconfig

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,29 @@ config MFD_CROS_EC_DEV
237237
To compile this driver as a module, choose M here: the module will be
238238
called cros-ec-dev.
239239

240+
config MFD_CS42L43
241+
tristate
242+
select MFD_CORE
243+
select REGMAP
244+
245+
config MFD_CS42L43_I2C
246+
tristate "Cirrus Logic CS42L43 (I2C)"
247+
depends on I2C
248+
select REGMAP_I2C
249+
select MFD_CS42L43
250+
help
251+
Select this to support the Cirrus Logic CS42L43 PC CODEC with
252+
headphone and class D speaker drivers over I2C.
253+
254+
config MFD_CS42L43_SDW
255+
tristate "Cirrus Logic CS42L43 (SoundWire)"
256+
depends on SOUNDWIRE
257+
select REGMAP_SOUNDWIRE
258+
select MFD_CS42L43
259+
help
260+
Select this to support the Cirrus Logic CS42L43 PC CODEC with
261+
headphone and class D speaker drivers over SoundWire.
262+
240263
config MFD_MADERA
241264
tristate "Cirrus Logic Madera codecs"
242265
select MFD_CORE

drivers/mfd/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ obj-$(CONFIG_ARCH_BCM2835) += bcm2835-pm.o
1313
obj-$(CONFIG_MFD_BCM590XX) += bcm590xx.o
1414
obj-$(CONFIG_MFD_BD9571MWV) += bd9571mwv.o
1515
obj-$(CONFIG_MFD_CROS_EC_DEV) += cros_ec_dev.o
16+
obj-$(CONFIG_MFD_CS42L43) += cs42l43.o
17+
obj-$(CONFIG_MFD_CS42L43_I2C) += cs42l43-i2c.o
18+
obj-$(CONFIG_MFD_CS42L43_SDW) += cs42l43-sdw.o
1619
obj-$(CONFIG_MFD_ENE_KB3930) += ene-kb3930.o
1720
obj-$(CONFIG_MFD_EXYNOS_LPASS) += exynos-lpass.o
1821
obj-$(CONFIG_MFD_GATEWORKS_GSC) += gateworks-gsc.o

drivers/mfd/cs42l43-i2c.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* CS42L43 I2C driver
4+
*
5+
* Copyright (C) 2022-2023 Cirrus Logic, Inc. and
6+
* Cirrus Logic International Semiconductor Ltd.
7+
*/
8+
9+
#include <linux/err.h>
10+
#include <linux/errno.h>
11+
#include <linux/i2c.h>
12+
#include <linux/mfd/cs42l43-regs.h>
13+
#include <linux/module.h>
14+
15+
#include "cs42l43.h"
16+
17+
static const struct regmap_config cs42l43_i2c_regmap = {
18+
.reg_bits = 32,
19+
.reg_stride = 4,
20+
.val_bits = 32,
21+
.reg_format_endian = REGMAP_ENDIAN_BIG,
22+
.val_format_endian = REGMAP_ENDIAN_BIG,
23+
24+
.max_register = CS42L43_MCU_RAM_MAX,
25+
.readable_reg = cs42l43_readable_register,
26+
.volatile_reg = cs42l43_volatile_register,
27+
.precious_reg = cs42l43_precious_register,
28+
29+
.cache_type = REGCACHE_MAPLE,
30+
.reg_defaults = cs42l43_reg_default,
31+
.num_reg_defaults = ARRAY_SIZE(cs42l43_reg_default),
32+
};
33+
34+
static int cs42l43_i2c_probe(struct i2c_client *i2c)
35+
{
36+
struct cs42l43 *cs42l43;
37+
int ret;
38+
39+
cs42l43 = devm_kzalloc(&i2c->dev, sizeof(*cs42l43), GFP_KERNEL);
40+
if (!cs42l43)
41+
return -ENOMEM;
42+
43+
cs42l43->dev = &i2c->dev;
44+
cs42l43->irq = i2c->irq;
45+
/* A device on an I2C is always attached by definition. */
46+
cs42l43->attached = true;
47+
48+
cs42l43->regmap = devm_regmap_init_i2c(i2c, &cs42l43_i2c_regmap);
49+
if (IS_ERR(cs42l43->regmap)) {
50+
ret = PTR_ERR(cs42l43->regmap);
51+
dev_err(cs42l43->dev, "Failed to allocate regmap: %d\n", ret);
52+
return ret;
53+
}
54+
55+
return cs42l43_dev_probe(cs42l43);
56+
}
57+
58+
static void cs42l43_i2c_remove(struct i2c_client *i2c)
59+
{
60+
struct cs42l43 *cs42l43 = dev_get_drvdata(&i2c->dev);
61+
62+
cs42l43_dev_remove(cs42l43);
63+
}
64+
65+
#if IS_ENABLED(CONFIG_OF)
66+
static const struct of_device_id cs42l43_of_match[] = {
67+
{ .compatible = "cirrus,cs42l43", },
68+
{}
69+
};
70+
MODULE_DEVICE_TABLE(of, cs42l43_of_match);
71+
#endif
72+
73+
#if IS_ENABLED(CONFIG_ACPI)
74+
static const struct acpi_device_id cs42l43_acpi_match[] = {
75+
{ "CSC4243", 0 },
76+
{}
77+
};
78+
MODULE_DEVICE_TABLE(acpi, cs42l43_acpi_match);
79+
#endif
80+
81+
static struct i2c_driver cs42l43_i2c_driver = {
82+
.driver = {
83+
.name = "cs42l43",
84+
.pm = pm_ptr(&cs42l43_pm_ops),
85+
.of_match_table = of_match_ptr(cs42l43_of_match),
86+
.acpi_match_table = ACPI_PTR(cs42l43_acpi_match),
87+
},
88+
89+
.probe = cs42l43_i2c_probe,
90+
.remove = cs42l43_i2c_remove,
91+
};
92+
module_i2c_driver(cs42l43_i2c_driver);
93+
94+
MODULE_IMPORT_NS(MFD_CS42L43);
95+
96+
MODULE_DESCRIPTION("CS42L43 I2C Driver");
97+
MODULE_AUTHOR("Charles Keepax <ckeepax@opensource.cirrus.com>");
98+
MODULE_LICENSE("GPL");

drivers/mfd/cs42l43-sdw.c

Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* CS42L43 SoundWire driver
4+
*
5+
* Copyright (C) 2022-2023 Cirrus Logic, Inc. and
6+
* Cirrus Logic International Semiconductor Ltd.
7+
*/
8+
9+
#include <linux/err.h>
10+
#include <linux/errno.h>
11+
#include <linux/mfd/cs42l43-regs.h>
12+
#include <linux/module.h>
13+
#include <linux/device.h>
14+
#include <linux/soundwire/sdw.h>
15+
#include <linux/soundwire/sdw_registers.h>
16+
#include <linux/soundwire/sdw_type.h>
17+
18+
#include "cs42l43.h"
19+
20+
enum cs42l43_sdw_ports {
21+
CS42L43_DMIC_DEC_ASP_PORT = 1,
22+
CS42L43_SPK_TX_PORT,
23+
CS42L43_SPDIF_HP_PORT,
24+
CS42L43_SPK_RX_PORT,
25+
CS42L43_ASP_PORT,
26+
};
27+
28+
static const struct regmap_config cs42l43_sdw_regmap = {
29+
.reg_bits = 32,
30+
.reg_stride = 4,
31+
.val_bits = 32,
32+
.reg_format_endian = REGMAP_ENDIAN_LITTLE,
33+
.val_format_endian = REGMAP_ENDIAN_LITTLE,
34+
35+
.max_register = CS42L43_MCU_RAM_MAX,
36+
.readable_reg = cs42l43_readable_register,
37+
.volatile_reg = cs42l43_volatile_register,
38+
.precious_reg = cs42l43_precious_register,
39+
40+
.cache_type = REGCACHE_MAPLE,
41+
.reg_defaults = cs42l43_reg_default,
42+
.num_reg_defaults = ARRAY_SIZE(cs42l43_reg_default),
43+
};
44+
45+
static int cs42l43_read_prop(struct sdw_slave *sdw)
46+
{
47+
struct sdw_slave_prop *prop = &sdw->prop;
48+
struct device *dev = &sdw->dev;
49+
struct sdw_dpn_prop *dpn;
50+
unsigned long addr;
51+
int nval;
52+
int i;
53+
u32 bit;
54+
55+
prop->use_domain_irq = true;
56+
prop->paging_support = true;
57+
prop->wake_capable = true;
58+
prop->source_ports = BIT(CS42L43_DMIC_DEC_ASP_PORT) | BIT(CS42L43_SPK_TX_PORT);
59+
prop->sink_ports = BIT(CS42L43_SPDIF_HP_PORT) |
60+
BIT(CS42L43_SPK_RX_PORT) | BIT(CS42L43_ASP_PORT);
61+
prop->quirks = SDW_SLAVE_QUIRKS_INVALID_INITIAL_PARITY;
62+
prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY |
63+
SDW_SCP_INT1_IMPL_DEF;
64+
65+
nval = hweight32(prop->source_ports);
66+
prop->src_dpn_prop = devm_kcalloc(dev, nval, sizeof(*prop->src_dpn_prop),
67+
GFP_KERNEL);
68+
if (!prop->src_dpn_prop)
69+
return -ENOMEM;
70+
71+
i = 0;
72+
dpn = prop->src_dpn_prop;
73+
addr = prop->source_ports;
74+
for_each_set_bit(bit, &addr, 32) {
75+
dpn[i].num = bit;
76+
dpn[i].max_ch = 2;
77+
dpn[i].type = SDW_DPN_FULL;
78+
dpn[i].max_word = 24;
79+
i++;
80+
}
81+
/*
82+
* All ports are 2 channels max, except the first one,
83+
* CS42L43_DMIC_DEC_ASP_PORT.
84+
*/
85+
dpn[CS42L43_DMIC_DEC_ASP_PORT].max_ch = 4;
86+
87+
nval = hweight32(prop->sink_ports);
88+
prop->sink_dpn_prop = devm_kcalloc(dev, nval, sizeof(*prop->sink_dpn_prop),
89+
GFP_KERNEL);
90+
if (!prop->sink_dpn_prop)
91+
return -ENOMEM;
92+
93+
i = 0;
94+
dpn = prop->sink_dpn_prop;
95+
addr = prop->sink_ports;
96+
for_each_set_bit(bit, &addr, 32) {
97+
dpn[i].num = bit;
98+
dpn[i].max_ch = 2;
99+
dpn[i].type = SDW_DPN_FULL;
100+
dpn[i].max_word = 24;
101+
i++;
102+
}
103+
104+
return 0;
105+
}
106+
107+
static int cs42l43_sdw_update_status(struct sdw_slave *sdw, enum sdw_slave_status status)
108+
{
109+
struct cs42l43 *cs42l43 = dev_get_drvdata(&sdw->dev);
110+
111+
switch (status) {
112+
case SDW_SLAVE_ATTACHED:
113+
dev_dbg(cs42l43->dev, "Device attach\n");
114+
115+
sdw_write_no_pm(sdw, CS42L43_GEN_INT_MASK_1,
116+
CS42L43_INT_STAT_GEN1_MASK);
117+
118+
cs42l43->attached = true;
119+
120+
complete(&cs42l43->device_attach);
121+
break;
122+
case SDW_SLAVE_UNATTACHED:
123+
dev_dbg(cs42l43->dev, "Device detach\n");
124+
125+
cs42l43->attached = false;
126+
127+
reinit_completion(&cs42l43->device_attach);
128+
complete(&cs42l43->device_detach);
129+
break;
130+
default:
131+
break;
132+
}
133+
134+
return 0;
135+
}
136+
137+
static int cs42l43_sdw_interrupt(struct sdw_slave *sdw,
138+
struct sdw_slave_intr_status *status)
139+
{
140+
/*
141+
* The IRQ itself was handled through the regmap_irq handler, this is
142+
* just clearing up the additional Cirrus SoundWire registers that are
143+
* not covered by the SoundWire framework or the IRQ handler itself.
144+
* There is only a single bit in GEN_INT_STAT_1 and it doesn't clear if
145+
* IRQs are still pending so doing a read/write here after handling the
146+
* IRQ is fine.
147+
*/
148+
sdw_read_no_pm(sdw, CS42L43_GEN_INT_STAT_1);
149+
sdw_write_no_pm(sdw, CS42L43_GEN_INT_STAT_1, CS42L43_INT_STAT_GEN1_MASK);
150+
151+
return 0;
152+
}
153+
154+
static int cs42l43_sdw_bus_config(struct sdw_slave *sdw,
155+
struct sdw_bus_params *params)
156+
{
157+
struct cs42l43 *cs42l43 = dev_get_drvdata(&sdw->dev);
158+
int ret = 0;
159+
160+
mutex_lock(&cs42l43->pll_lock);
161+
162+
if (cs42l43->sdw_freq != params->curr_dr_freq / 2) {
163+
if (cs42l43->sdw_pll_active) {
164+
dev_err(cs42l43->dev,
165+
"PLL active can't change SoundWire bus clock\n");
166+
ret = -EBUSY;
167+
} else {
168+
cs42l43->sdw_freq = params->curr_dr_freq / 2;
169+
}
170+
}
171+
172+
mutex_unlock(&cs42l43->pll_lock);
173+
174+
return ret;
175+
}
176+
177+
static const struct sdw_slave_ops cs42l43_sdw_ops = {
178+
.read_prop = cs42l43_read_prop,
179+
.update_status = cs42l43_sdw_update_status,
180+
.interrupt_callback = cs42l43_sdw_interrupt,
181+
.bus_config = cs42l43_sdw_bus_config,
182+
};
183+
184+
static int cs42l43_sdw_probe(struct sdw_slave *sdw, const struct sdw_device_id *id)
185+
{
186+
struct cs42l43 *cs42l43;
187+
struct device *dev = &sdw->dev;
188+
int ret;
189+
190+
cs42l43 = devm_kzalloc(dev, sizeof(*cs42l43), GFP_KERNEL);
191+
if (!cs42l43)
192+
return -ENOMEM;
193+
194+
cs42l43->dev = dev;
195+
cs42l43->sdw = sdw;
196+
197+
cs42l43->regmap = devm_regmap_init_sdw(sdw, &cs42l43_sdw_regmap);
198+
if (IS_ERR(cs42l43->regmap)) {
199+
ret = PTR_ERR(cs42l43->regmap);
200+
dev_err(cs42l43->dev, "Failed to allocate regmap: %d\n", ret);
201+
return ret;
202+
}
203+
204+
return cs42l43_dev_probe(cs42l43);
205+
}
206+
207+
static int cs42l43_sdw_remove(struct sdw_slave *sdw)
208+
{
209+
struct cs42l43 *cs42l43 = dev_get_drvdata(&sdw->dev);
210+
211+
cs42l43_dev_remove(cs42l43);
212+
213+
return 0;
214+
}
215+
216+
static const struct sdw_device_id cs42l43_sdw_id[] = {
217+
SDW_SLAVE_ENTRY(0x01FA, 0x4243, 0),
218+
{}
219+
};
220+
MODULE_DEVICE_TABLE(sdw, cs42l43_sdw_id);
221+
222+
static struct sdw_driver cs42l43_sdw_driver = {
223+
.driver = {
224+
.name = "cs42l43",
225+
.pm = pm_ptr(&cs42l43_pm_ops),
226+
},
227+
228+
.probe = cs42l43_sdw_probe,
229+
.remove = cs42l43_sdw_remove,
230+
.id_table = cs42l43_sdw_id,
231+
.ops = &cs42l43_sdw_ops,
232+
};
233+
module_sdw_driver(cs42l43_sdw_driver);
234+
235+
MODULE_IMPORT_NS(MFD_CS42L43);
236+
237+
MODULE_DESCRIPTION("CS42L43 SoundWire Driver");
238+
MODULE_AUTHOR("Lucas Tanure <tanureal@opensource.cirrus.com>");
239+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)