2 * AK4104 ALSA SoC (ASoC) driver
4 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version.
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
15 #include <linux/of_device.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/regulator/consumer.h>
18 #include <sound/asoundef.h>
19 #include <sound/core.h>
20 #include <sound/soc.h>
21 #include <sound/initval.h>
23 /* AK4104 registers addresses */
24 #define AK4104_REG_CONTROL1 0x00
25 #define AK4104_REG_RESERVED 0x01
26 #define AK4104_REG_CONTROL2 0x02
27 #define AK4104_REG_TX 0x03
28 #define AK4104_REG_CHN_STATUS(x) ((x) + 0x04)
29 #define AK4104_NUM_REGS 10
31 #define AK4104_REG_MASK 0x1f
32 #define AK4104_READ 0xc0
33 #define AK4104_WRITE 0xe0
34 #define AK4104_RESERVED_VAL 0x5b
36 /* Bit masks for AK4104 registers */
37 #define AK4104_CONTROL1_RSTN (1 << 0)
38 #define AK4104_CONTROL1_PW (1 << 1)
39 #define AK4104_CONTROL1_DIF0 (1 << 2)
40 #define AK4104_CONTROL1_DIF1 (1 << 3)
42 #define AK4104_CONTROL2_SEL0 (1 << 0)
43 #define AK4104_CONTROL2_SEL1 (1 << 1)
44 #define AK4104_CONTROL2_MODE (1 << 2)
46 #define AK4104_TX_TXE (1 << 0)
47 #define AK4104_TX_V (1 << 1)
49 struct ak4104_private
{
50 struct regmap
*regmap
;
51 struct regulator
*regulator
;
54 static const struct snd_soc_dapm_widget ak4104_dapm_widgets
[] = {
55 SND_SOC_DAPM_PGA("TXE", AK4104_REG_TX
, AK4104_TX_TXE
, 0, NULL
, 0),
57 SND_SOC_DAPM_OUTPUT("TX"),
60 static const struct snd_soc_dapm_route ak4104_dapm_routes
[] = {
61 { "TXE", NULL
, "Playback" },
62 { "TX", NULL
, "TXE" },
65 static int ak4104_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
68 struct snd_soc_component
*component
= codec_dai
->component
;
69 struct ak4104_private
*ak4104
= snd_soc_component_get_drvdata(component
);
74 switch (format
& SND_SOC_DAIFMT_FORMAT_MASK
) {
75 case SND_SOC_DAIFMT_RIGHT_J
:
77 case SND_SOC_DAIFMT_LEFT_J
:
78 val
|= AK4104_CONTROL1_DIF0
;
80 case SND_SOC_DAIFMT_I2S
:
81 val
|= AK4104_CONTROL1_DIF0
| AK4104_CONTROL1_DIF1
;
84 dev_err(component
->dev
, "invalid dai format\n");
88 /* This device can only be slave */
89 if ((format
& SND_SOC_DAIFMT_MASTER_MASK
) != SND_SOC_DAIFMT_CBS_CFS
)
92 ret
= regmap_update_bits(ak4104
->regmap
, AK4104_REG_CONTROL1
,
93 AK4104_CONTROL1_DIF0
| AK4104_CONTROL1_DIF1
,
101 static int ak4104_hw_params(struct snd_pcm_substream
*substream
,
102 struct snd_pcm_hw_params
*params
,
103 struct snd_soc_dai
*dai
)
105 struct snd_soc_component
*component
= dai
->component
;
106 struct ak4104_private
*ak4104
= snd_soc_component_get_drvdata(component
);
109 /* set the IEC958 bits: consumer mode, no copyright bit */
110 val
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
111 regmap_write(ak4104
->regmap
, AK4104_REG_CHN_STATUS(0), val
);
115 switch (params_rate(params
)) {
117 val
|= IEC958_AES3_CON_FS_22050
;
120 val
|= IEC958_AES3_CON_FS_24000
;
123 val
|= IEC958_AES3_CON_FS_32000
;
126 val
|= IEC958_AES3_CON_FS_44100
;
129 val
|= IEC958_AES3_CON_FS_48000
;
132 val
|= IEC958_AES3_CON_FS_88200
;
135 val
|= IEC958_AES3_CON_FS_96000
;
138 val
|= IEC958_AES3_CON_FS_176400
;
141 val
|= IEC958_AES3_CON_FS_192000
;
144 dev_err(component
->dev
, "unsupported sampling rate\n");
148 ret
= regmap_write(ak4104
->regmap
, AK4104_REG_CHN_STATUS(3), val
);
155 static const struct snd_soc_dai_ops ak4101_dai_ops
= {
156 .hw_params
= ak4104_hw_params
,
157 .set_fmt
= ak4104_set_dai_fmt
,
160 static struct snd_soc_dai_driver ak4104_dai
= {
161 .name
= "ak4104-hifi",
163 .stream_name
= "Playback",
166 .rates
= SNDRV_PCM_RATE_22050
| SNDRV_PCM_RATE_32000
|
167 SNDRV_PCM_RATE_44100
| SNDRV_PCM_RATE_48000
|
168 SNDRV_PCM_RATE_88200
| SNDRV_PCM_RATE_96000
|
169 SNDRV_PCM_RATE_176400
| SNDRV_PCM_RATE_192000
,
170 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
171 SNDRV_PCM_FMTBIT_S24_3LE
|
172 SNDRV_PCM_FMTBIT_S24_LE
174 .ops
= &ak4101_dai_ops
,
177 static int ak4104_probe(struct snd_soc_component
*component
)
179 struct ak4104_private
*ak4104
= snd_soc_component_get_drvdata(component
);
182 ret
= regulator_enable(ak4104
->regulator
);
184 dev_err(component
->dev
, "Unable to enable regulator: %d\n", ret
);
188 /* set power-up and non-reset bits */
189 ret
= regmap_update_bits(ak4104
->regmap
, AK4104_REG_CONTROL1
,
190 AK4104_CONTROL1_PW
| AK4104_CONTROL1_RSTN
,
191 AK4104_CONTROL1_PW
| AK4104_CONTROL1_RSTN
);
193 goto exit_disable_regulator
;
195 /* enable transmitter */
196 ret
= regmap_update_bits(ak4104
->regmap
, AK4104_REG_TX
,
197 AK4104_TX_TXE
, AK4104_TX_TXE
);
199 goto exit_disable_regulator
;
203 exit_disable_regulator
:
204 regulator_disable(ak4104
->regulator
);
208 static void ak4104_remove(struct snd_soc_component
*component
)
210 struct ak4104_private
*ak4104
= snd_soc_component_get_drvdata(component
);
212 regmap_update_bits(ak4104
->regmap
, AK4104_REG_CONTROL1
,
213 AK4104_CONTROL1_PW
| AK4104_CONTROL1_RSTN
, 0);
214 regulator_disable(ak4104
->regulator
);
218 static int ak4104_soc_suspend(struct snd_soc_component
*component
)
220 struct ak4104_private
*priv
= snd_soc_component_get_drvdata(component
);
222 regulator_disable(priv
->regulator
);
227 static int ak4104_soc_resume(struct snd_soc_component
*component
)
229 struct ak4104_private
*priv
= snd_soc_component_get_drvdata(component
);
232 ret
= regulator_enable(priv
->regulator
);
239 #define ak4104_soc_suspend NULL
240 #define ak4104_soc_resume NULL
241 #endif /* CONFIG_PM */
243 static const struct snd_soc_component_driver soc_component_device_ak4104
= {
244 .probe
= ak4104_probe
,
245 .remove
= ak4104_remove
,
246 .suspend
= ak4104_soc_suspend
,
247 .resume
= ak4104_soc_resume
,
248 .dapm_widgets
= ak4104_dapm_widgets
,
249 .num_dapm_widgets
= ARRAY_SIZE(ak4104_dapm_widgets
),
250 .dapm_routes
= ak4104_dapm_routes
,
251 .num_dapm_routes
= ARRAY_SIZE(ak4104_dapm_routes
),
253 .use_pmdown_time
= 1,
255 .non_legacy_dai_naming
= 1,
258 static const struct regmap_config ak4104_regmap
= {
262 .max_register
= AK4104_NUM_REGS
- 1,
263 .read_flag_mask
= AK4104_READ
,
264 .write_flag_mask
= AK4104_WRITE
,
266 .cache_type
= REGCACHE_RBTREE
,
269 static int ak4104_spi_probe(struct spi_device
*spi
)
271 struct ak4104_private
*ak4104
;
272 struct gpio_desc
*reset_gpiod
;
276 spi
->bits_per_word
= 8;
277 spi
->mode
= SPI_MODE_0
;
278 ret
= spi_setup(spi
);
282 ak4104
= devm_kzalloc(&spi
->dev
, sizeof(struct ak4104_private
),
287 ak4104
->regulator
= devm_regulator_get(&spi
->dev
, "vdd");
288 if (IS_ERR(ak4104
->regulator
)) {
289 ret
= PTR_ERR(ak4104
->regulator
);
290 dev_err(&spi
->dev
, "Unable to get Vdd regulator: %d\n", ret
);
294 ak4104
->regmap
= devm_regmap_init_spi(spi
, &ak4104_regmap
);
295 if (IS_ERR(ak4104
->regmap
)) {
296 ret
= PTR_ERR(ak4104
->regmap
);
300 reset_gpiod
= devm_gpiod_get_optional(&spi
->dev
, "reset",
302 if (IS_ERR(reset_gpiod
) &&
303 PTR_ERR(reset_gpiod
) == -EPROBE_DEFER
)
304 return -EPROBE_DEFER
;
306 /* read the 'reserved' register - according to the datasheet, it
307 * should contain 0x5b. Not a good way to verify the presence of
308 * the device, but there is no hardware ID register. */
309 ret
= regmap_read(ak4104
->regmap
, AK4104_REG_RESERVED
, &val
);
312 if (val
!= AK4104_RESERVED_VAL
)
315 spi_set_drvdata(spi
, ak4104
);
317 ret
= devm_snd_soc_register_component(&spi
->dev
,
318 &soc_component_device_ak4104
, &ak4104_dai
, 1);
322 static const struct of_device_id ak4104_of_match
[] = {
323 { .compatible
= "asahi-kasei,ak4104", },
326 MODULE_DEVICE_TABLE(of
, ak4104_of_match
);
328 static const struct spi_device_id ak4104_id_table
[] = {
332 MODULE_DEVICE_TABLE(spi
, ak4104_id_table
);
334 static struct spi_driver ak4104_spi_driver
= {
337 .of_match_table
= ak4104_of_match
,
339 .id_table
= ak4104_id_table
,
340 .probe
= ak4104_spi_probe
,
343 module_spi_driver(ak4104_spi_driver
);
345 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
346 MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
347 MODULE_LICENSE("GPL");