1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * AK4104 ALSA SoC (ASoC) driver
5 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
8 #include <linux/module.h>
9 #include <linux/slab.h>
10 #include <linux/spi/spi.h>
11 #include <linux/of_device.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/regulator/consumer.h>
14 #include <sound/asoundef.h>
15 #include <sound/core.h>
16 #include <sound/soc.h>
17 #include <sound/initval.h>
19 /* AK4104 registers addresses */
20 #define AK4104_REG_CONTROL1 0x00
21 #define AK4104_REG_RESERVED 0x01
22 #define AK4104_REG_CONTROL2 0x02
23 #define AK4104_REG_TX 0x03
24 #define AK4104_REG_CHN_STATUS(x) ((x) + 0x04)
25 #define AK4104_NUM_REGS 10
27 #define AK4104_REG_MASK 0x1f
28 #define AK4104_READ 0xc0
29 #define AK4104_WRITE 0xe0
30 #define AK4104_RESERVED_VAL 0x5b
32 /* Bit masks for AK4104 registers */
33 #define AK4104_CONTROL1_RSTN (1 << 0)
34 #define AK4104_CONTROL1_PW (1 << 1)
35 #define AK4104_CONTROL1_DIF0 (1 << 2)
36 #define AK4104_CONTROL1_DIF1 (1 << 3)
38 #define AK4104_CONTROL2_SEL0 (1 << 0)
39 #define AK4104_CONTROL2_SEL1 (1 << 1)
40 #define AK4104_CONTROL2_MODE (1 << 2)
42 #define AK4104_TX_TXE (1 << 0)
43 #define AK4104_TX_V (1 << 1)
45 struct ak4104_private {
46 struct regmap *regmap;
47 struct regulator *regulator;
50 static const struct snd_soc_dapm_widget ak4104_dapm_widgets[] = {
51 SND_SOC_DAPM_PGA("TXE", AK4104_REG_TX, AK4104_TX_TXE, 0, NULL, 0),
53 SND_SOC_DAPM_OUTPUT("TX"),
56 static const struct snd_soc_dapm_route ak4104_dapm_routes[] = {
57 { "TXE", NULL, "Playback" },
58 { "TX", NULL, "TXE" },
61 static int ak4104_set_dai_fmt(struct snd_soc_dai *codec_dai,
64 struct snd_soc_component *component = codec_dai->component;
65 struct ak4104_private *ak4104 = snd_soc_component_get_drvdata(component);
70 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
71 case SND_SOC_DAIFMT_RIGHT_J:
73 case SND_SOC_DAIFMT_LEFT_J:
74 val |= AK4104_CONTROL1_DIF0;
76 case SND_SOC_DAIFMT_I2S:
77 val |= AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1;
80 dev_err(component->dev, "invalid dai format\n");
84 /* This device can only be slave */
85 if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
88 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
89 AK4104_CONTROL1_DIF0 | AK4104_CONTROL1_DIF1,
97 static int ak4104_hw_params(struct snd_pcm_substream *substream,
98 struct snd_pcm_hw_params *params,
99 struct snd_soc_dai *dai)
101 struct snd_soc_component *component = dai->component;
102 struct ak4104_private *ak4104 = snd_soc_component_get_drvdata(component);
105 /* set the IEC958 bits: consumer mode, no copyright bit */
106 val |= IEC958_AES0_CON_NOT_COPYRIGHT;
107 regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(0), val);
111 switch (params_rate(params)) {
113 val |= IEC958_AES3_CON_FS_22050;
116 val |= IEC958_AES3_CON_FS_24000;
119 val |= IEC958_AES3_CON_FS_32000;
122 val |= IEC958_AES3_CON_FS_44100;
125 val |= IEC958_AES3_CON_FS_48000;
128 val |= IEC958_AES3_CON_FS_88200;
131 val |= IEC958_AES3_CON_FS_96000;
134 val |= IEC958_AES3_CON_FS_176400;
137 val |= IEC958_AES3_CON_FS_192000;
140 dev_err(component->dev, "unsupported sampling rate\n");
144 ret = regmap_write(ak4104->regmap, AK4104_REG_CHN_STATUS(3), val);
151 static const struct snd_soc_dai_ops ak4101_dai_ops = {
152 .hw_params = ak4104_hw_params,
153 .set_fmt = ak4104_set_dai_fmt,
156 static struct snd_soc_dai_driver ak4104_dai = {
157 .name = "ak4104-hifi",
159 .stream_name = "Playback",
162 .rates = SNDRV_PCM_RATE_22050 | SNDRV_PCM_RATE_32000 |
163 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 |
164 SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000 |
165 SNDRV_PCM_RATE_176400 | SNDRV_PCM_RATE_192000,
166 .formats = SNDRV_PCM_FMTBIT_S16_LE |
167 SNDRV_PCM_FMTBIT_S24_3LE |
168 SNDRV_PCM_FMTBIT_S24_LE
170 .ops = &ak4101_dai_ops,
173 static int ak4104_probe(struct snd_soc_component *component)
175 struct ak4104_private *ak4104 = snd_soc_component_get_drvdata(component);
178 ret = regulator_enable(ak4104->regulator);
180 dev_err(component->dev, "Unable to enable regulator: %d\n", ret);
184 /* set power-up and non-reset bits */
185 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
186 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN,
187 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN);
189 goto exit_disable_regulator;
191 /* enable transmitter */
192 ret = regmap_update_bits(ak4104->regmap, AK4104_REG_TX,
193 AK4104_TX_TXE, AK4104_TX_TXE);
195 goto exit_disable_regulator;
199 exit_disable_regulator:
200 regulator_disable(ak4104->regulator);
204 static void ak4104_remove(struct snd_soc_component *component)
206 struct ak4104_private *ak4104 = snd_soc_component_get_drvdata(component);
208 regmap_update_bits(ak4104->regmap, AK4104_REG_CONTROL1,
209 AK4104_CONTROL1_PW | AK4104_CONTROL1_RSTN, 0);
210 regulator_disable(ak4104->regulator);
214 static int ak4104_soc_suspend(struct snd_soc_component *component)
216 struct ak4104_private *priv = snd_soc_component_get_drvdata(component);
218 regulator_disable(priv->regulator);
223 static int ak4104_soc_resume(struct snd_soc_component *component)
225 struct ak4104_private *priv = snd_soc_component_get_drvdata(component);
228 ret = regulator_enable(priv->regulator);
235 #define ak4104_soc_suspend NULL
236 #define ak4104_soc_resume NULL
237 #endif /* CONFIG_PM */
239 static const struct snd_soc_component_driver soc_component_device_ak4104 = {
240 .probe = ak4104_probe,
241 .remove = ak4104_remove,
242 .suspend = ak4104_soc_suspend,
243 .resume = ak4104_soc_resume,
244 .dapm_widgets = ak4104_dapm_widgets,
245 .num_dapm_widgets = ARRAY_SIZE(ak4104_dapm_widgets),
246 .dapm_routes = ak4104_dapm_routes,
247 .num_dapm_routes = ARRAY_SIZE(ak4104_dapm_routes),
249 .use_pmdown_time = 1,
251 .non_legacy_dai_naming = 1,
254 static const struct regmap_config ak4104_regmap = {
258 .max_register = AK4104_NUM_REGS - 1,
259 .read_flag_mask = AK4104_READ,
260 .write_flag_mask = AK4104_WRITE,
262 .cache_type = REGCACHE_RBTREE,
265 static int ak4104_spi_probe(struct spi_device *spi)
267 struct ak4104_private *ak4104;
268 struct gpio_desc *reset_gpiod;
272 spi->bits_per_word = 8;
273 spi->mode = SPI_MODE_0;
274 ret = spi_setup(spi);
278 ak4104 = devm_kzalloc(&spi->dev, sizeof(struct ak4104_private),
283 ak4104->regulator = devm_regulator_get(&spi->dev, "vdd");
284 if (IS_ERR(ak4104->regulator)) {
285 ret = PTR_ERR(ak4104->regulator);
286 dev_err(&spi->dev, "Unable to get Vdd regulator: %d\n", ret);
290 ak4104->regmap = devm_regmap_init_spi(spi, &ak4104_regmap);
291 if (IS_ERR(ak4104->regmap)) {
292 ret = PTR_ERR(ak4104->regmap);
296 reset_gpiod = devm_gpiod_get_optional(&spi->dev, "reset",
298 if (PTR_ERR(reset_gpiod) == -EPROBE_DEFER)
299 return -EPROBE_DEFER;
301 /* read the 'reserved' register - according to the datasheet, it
302 * should contain 0x5b. Not a good way to verify the presence of
303 * the device, but there is no hardware ID register. */
304 ret = regmap_read(ak4104->regmap, AK4104_REG_RESERVED, &val);
307 if (val != AK4104_RESERVED_VAL)
310 spi_set_drvdata(spi, ak4104);
312 ret = devm_snd_soc_register_component(&spi->dev,
313 &soc_component_device_ak4104, &ak4104_dai, 1);
317 static const struct of_device_id ak4104_of_match[] = {
318 { .compatible = "asahi-kasei,ak4104", },
321 MODULE_DEVICE_TABLE(of, ak4104_of_match);
323 static const struct spi_device_id ak4104_id_table[] = {
327 MODULE_DEVICE_TABLE(spi, ak4104_id_table);
329 static struct spi_driver ak4104_spi_driver = {
332 .of_match_table = ak4104_of_match,
334 .id_table = ak4104_id_table,
335 .probe = ak4104_spi_probe,
338 module_spi_driver(ak4104_spi_driver);
340 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
341 MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
342 MODULE_LICENSE("GPL");