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 <sound/core.h>
15 #include <sound/soc.h>
16 #include <sound/initval.h>
17 #include <linux/spi/spi.h>
18 #include <linux/of_device.h>
19 #include <linux/of_gpio.h>
20 #include <sound/asoundef.h>
22 /* AK4104 registers addresses */
23 #define AK4104_REG_CONTROL1 0x00
24 #define AK4104_REG_RESERVED 0x01
25 #define AK4104_REG_CONTROL2 0x02
26 #define AK4104_REG_TX 0x03
27 #define AK4104_REG_CHN_STATUS(x) ((x) + 0x04)
28 #define AK4104_NUM_REGS 10
30 #define AK4104_REG_MASK 0x1f
31 #define AK4104_READ 0xc0
32 #define AK4104_WRITE 0xe0
33 #define AK4104_RESERVED_VAL 0x5b
35 /* Bit masks for AK4104 registers */
36 #define AK4104_CONTROL1_RSTN (1 << 0)
37 #define AK4104_CONTROL1_PW (1 << 1)
38 #define AK4104_CONTROL1_DIF0 (1 << 2)
39 #define AK4104_CONTROL1_DIF1 (1 << 3)
41 #define AK4104_CONTROL2_SEL0 (1 << 0)
42 #define AK4104_CONTROL2_SEL1 (1 << 1)
43 #define AK4104_CONTROL2_MODE (1 << 2)
45 #define AK4104_TX_TXE (1 << 0)
46 #define AK4104_TX_V (1 << 1)
48 struct ak4104_private
{
49 struct regmap
*regmap
;
52 static const struct snd_soc_dapm_widget ak4104_dapm_widgets
[] = {
53 SND_SOC_DAPM_PGA("TXE", AK4104_REG_TX
, AK4104_TX_TXE
, 0, NULL
, 0),
55 SND_SOC_DAPM_OUTPUT("TX"),
58 static const struct snd_soc_dapm_route ak4104_dapm_routes
[] = {
59 { "TXE", NULL
, "Playback" },
60 { "TX", NULL
, "TXE" },
63 static int ak4104_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
66 struct snd_soc_codec
*codec
= codec_dai
->codec
;
67 struct ak4104_private
*ak4104
= snd_soc_codec_get_drvdata(codec
);
72 switch (format
& SND_SOC_DAIFMT_FORMAT_MASK
) {
73 case SND_SOC_DAIFMT_RIGHT_J
:
75 case SND_SOC_DAIFMT_LEFT_J
:
76 val
|= AK4104_CONTROL1_DIF0
;
78 case SND_SOC_DAIFMT_I2S
:
79 val
|= AK4104_CONTROL1_DIF0
| AK4104_CONTROL1_DIF1
;
82 dev_err(codec
->dev
, "invalid dai format\n");
86 /* This device can only be slave */
87 if ((format
& SND_SOC_DAIFMT_MASTER_MASK
) != SND_SOC_DAIFMT_CBS_CFS
)
90 ret
= regmap_update_bits(ak4104
->regmap
, AK4104_REG_CONTROL1
,
91 AK4104_CONTROL1_DIF0
| AK4104_CONTROL1_DIF1
,
99 static int ak4104_hw_params(struct snd_pcm_substream
*substream
,
100 struct snd_pcm_hw_params
*params
,
101 struct snd_soc_dai
*dai
)
103 struct snd_soc_codec
*codec
= dai
->codec
;
104 struct ak4104_private
*ak4104
= snd_soc_codec_get_drvdata(codec
);
107 /* set the IEC958 bits: consumer mode, no copyright bit */
108 val
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
109 regmap_write(ak4104
->regmap
, AK4104_REG_CHN_STATUS(0), val
);
113 switch (params_rate(params
)) {
115 val
|= IEC958_AES3_CON_FS_22050
;
118 val
|= IEC958_AES3_CON_FS_24000
;
121 val
|= IEC958_AES3_CON_FS_32000
;
124 val
|= IEC958_AES3_CON_FS_44100
;
127 val
|= IEC958_AES3_CON_FS_48000
;
130 val
|= IEC958_AES3_CON_FS_88200
;
133 val
|= IEC958_AES3_CON_FS_96000
;
136 val
|= IEC958_AES3_CON_FS_176400
;
139 val
|= IEC958_AES3_CON_FS_192000
;
142 dev_err(codec
->dev
, "unsupported sampling rate\n");
146 ret
= regmap_write(ak4104
->regmap
, AK4104_REG_CHN_STATUS(3), val
);
153 static const struct snd_soc_dai_ops ak4101_dai_ops
= {
154 .hw_params
= ak4104_hw_params
,
155 .set_fmt
= ak4104_set_dai_fmt
,
158 static struct snd_soc_dai_driver ak4104_dai
= {
159 .name
= "ak4104-hifi",
161 .stream_name
= "Playback",
164 .rates
= SNDRV_PCM_RATE_8000_192000
,
165 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
166 SNDRV_PCM_FMTBIT_S24_3LE
|
167 SNDRV_PCM_FMTBIT_S24_LE
169 .ops
= &ak4101_dai_ops
,
172 static int ak4104_probe(struct snd_soc_codec
*codec
)
174 struct ak4104_private
*ak4104
= snd_soc_codec_get_drvdata(codec
);
177 codec
->control_data
= ak4104
->regmap
;
179 /* set power-up and non-reset bits */
180 ret
= regmap_update_bits(ak4104
->regmap
, AK4104_REG_CONTROL1
,
181 AK4104_CONTROL1_PW
| AK4104_CONTROL1_RSTN
,
182 AK4104_CONTROL1_PW
| AK4104_CONTROL1_RSTN
);
186 /* enable transmitter */
187 ret
= regmap_update_bits(ak4104
->regmap
, AK4104_REG_TX
,
188 AK4104_TX_TXE
, AK4104_TX_TXE
);
195 static int ak4104_remove(struct snd_soc_codec
*codec
)
197 struct ak4104_private
*ak4104
= snd_soc_codec_get_drvdata(codec
);
199 regmap_update_bits(ak4104
->regmap
, AK4104_REG_CONTROL1
,
200 AK4104_CONTROL1_PW
| AK4104_CONTROL1_RSTN
, 0);
205 static struct snd_soc_codec_driver soc_codec_device_ak4104
= {
206 .probe
= ak4104_probe
,
207 .remove
= ak4104_remove
,
209 .dapm_widgets
= ak4104_dapm_widgets
,
210 .num_dapm_widgets
= ARRAY_SIZE(ak4104_dapm_widgets
),
211 .dapm_routes
= ak4104_dapm_routes
,
212 .num_dapm_routes
= ARRAY_SIZE(ak4104_dapm_routes
),
215 static const struct regmap_config ak4104_regmap
= {
219 .max_register
= AK4104_NUM_REGS
- 1,
220 .read_flag_mask
= AK4104_READ
,
221 .write_flag_mask
= AK4104_WRITE
,
223 .cache_type
= REGCACHE_RBTREE
,
226 static int ak4104_spi_probe(struct spi_device
*spi
)
228 struct device_node
*np
= spi
->dev
.of_node
;
229 struct ak4104_private
*ak4104
;
233 spi
->bits_per_word
= 8;
234 spi
->mode
= SPI_MODE_0
;
235 ret
= spi_setup(spi
);
239 ak4104
= devm_kzalloc(&spi
->dev
, sizeof(struct ak4104_private
),
244 ak4104
->regmap
= devm_regmap_init_spi(spi
, &ak4104_regmap
);
245 if (IS_ERR(ak4104
->regmap
)) {
246 ret
= PTR_ERR(ak4104
->regmap
);
251 enum of_gpio_flags flags
;
252 int gpio
= of_get_named_gpio_flags(np
, "reset-gpio", 0, &flags
);
254 if (gpio_is_valid(gpio
)) {
255 ret
= devm_gpio_request_one(&spi
->dev
, gpio
,
256 flags
& OF_GPIO_ACTIVE_LOW
?
257 GPIOF_OUT_INIT_LOW
: GPIOF_OUT_INIT_HIGH
,
264 /* read the 'reserved' register - according to the datasheet, it
265 * should contain 0x5b. Not a good way to verify the presence of
266 * the device, but there is no hardware ID register. */
267 ret
= regmap_read(ak4104
->regmap
, AK4104_REG_RESERVED
, &val
);
270 if (val
!= AK4104_RESERVED_VAL
)
273 spi_set_drvdata(spi
, ak4104
);
275 ret
= snd_soc_register_codec(&spi
->dev
,
276 &soc_codec_device_ak4104
, &ak4104_dai
, 1);
280 static int ak4104_spi_remove(struct spi_device
*spi
)
282 snd_soc_unregister_codec(&spi
->dev
);
286 static const struct of_device_id ak4104_of_match
[] = {
287 { .compatible
= "asahi-kasei,ak4104", },
290 MODULE_DEVICE_TABLE(of
, ak4104_of_match
);
292 static const struct spi_device_id ak4104_id_table
[] = {
296 MODULE_DEVICE_TABLE(spi
, ak4104_id_table
);
298 static struct spi_driver ak4104_spi_driver
= {
301 .owner
= THIS_MODULE
,
302 .of_match_table
= ak4104_of_match
,
304 .id_table
= ak4104_id_table
,
305 .probe
= ak4104_spi_probe
,
306 .remove
= ak4104_spi_remove
,
309 module_spi_driver(ak4104_spi_driver
);
311 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
312 MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
313 MODULE_LICENSE("GPL");