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 <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 #define DRV_NAME "ak4104"
50 struct ak4104_private
{
51 struct snd_soc_codec codec
;
52 u8 reg_cache
[AK4104_NUM_REGS
];
55 static int ak4104_fill_cache(struct snd_soc_codec
*codec
)
58 u8
*reg_cache
= codec
->reg_cache
;
59 struct spi_device
*spi
= codec
->control_data
;
61 for (i
= 0; i
< codec
->reg_cache_size
; i
++) {
62 int ret
= spi_w8r8(spi
, i
| AK4104_READ
);
64 dev_err(&spi
->dev
, "SPI write failure\n");
74 static unsigned int ak4104_read_reg_cache(struct snd_soc_codec
*codec
,
77 u8
*reg_cache
= codec
->reg_cache
;
79 if (reg
>= codec
->reg_cache_size
)
82 return reg_cache
[reg
];
85 static int ak4104_spi_write(struct snd_soc_codec
*codec
, unsigned int reg
,
88 u8
*cache
= codec
->reg_cache
;
89 struct spi_device
*spi
= codec
->control_data
;
91 if (reg
>= codec
->reg_cache_size
)
94 /* only write to the hardware if value has changed */
95 if (cache
[reg
] != value
) {
96 u8 tmp
[2] = { (reg
& AK4104_REG_MASK
) | AK4104_WRITE
, value
};
98 if (spi_write(spi
, tmp
, sizeof(tmp
))) {
99 dev_err(&spi
->dev
, "SPI write failed\n");
109 static int ak4104_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
112 struct snd_soc_codec
*codec
= codec_dai
->codec
;
115 val
= ak4104_read_reg_cache(codec
, AK4104_REG_CONTROL1
);
119 val
&= ~(AK4104_CONTROL1_DIF0
| AK4104_CONTROL1_DIF1
);
122 switch (format
& SND_SOC_DAIFMT_FORMAT_MASK
) {
123 case SND_SOC_DAIFMT_RIGHT_J
:
125 case SND_SOC_DAIFMT_LEFT_J
:
126 val
|= AK4104_CONTROL1_DIF0
;
128 case SND_SOC_DAIFMT_I2S
:
129 val
|= AK4104_CONTROL1_DIF0
| AK4104_CONTROL1_DIF1
;
132 dev_err(codec
->dev
, "invalid dai format\n");
136 /* This device can only be slave */
137 if ((format
& SND_SOC_DAIFMT_MASTER_MASK
) != SND_SOC_DAIFMT_CBS_CFS
)
140 return ak4104_spi_write(codec
, AK4104_REG_CONTROL1
, val
);
143 static int ak4104_hw_params(struct snd_pcm_substream
*substream
,
144 struct snd_pcm_hw_params
*params
,
145 struct snd_soc_dai
*dai
)
147 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
148 struct snd_soc_device
*socdev
= rtd
->socdev
;
149 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
152 /* set the IEC958 bits: consumer mode, no copyright bit */
153 val
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
154 ak4104_spi_write(codec
, AK4104_REG_CHN_STATUS(0), val
);
158 switch (params_rate(params
)) {
160 val
|= IEC958_AES3_CON_FS_44100
;
163 val
|= IEC958_AES3_CON_FS_48000
;
166 val
|= IEC958_AES3_CON_FS_32000
;
169 dev_err(codec
->dev
, "unsupported sampling rate\n");
173 return ak4104_spi_write(codec
, AK4104_REG_CHN_STATUS(3), val
);
176 static struct snd_soc_dai_ops ak4101_dai_ops
= {
177 .hw_params
= ak4104_hw_params
,
178 .set_fmt
= ak4104_set_dai_fmt
,
181 struct snd_soc_dai ak4104_dai
= {
184 .stream_name
= "Playback",
187 .rates
= SNDRV_PCM_RATE_8000_192000
,
188 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
189 SNDRV_PCM_FMTBIT_S24_3LE
|
190 SNDRV_PCM_FMTBIT_S24_LE
192 .ops
= &ak4101_dai_ops
,
195 static struct snd_soc_codec
*ak4104_codec
;
197 static int ak4104_spi_probe(struct spi_device
*spi
)
199 struct snd_soc_codec
*codec
;
200 struct ak4104_private
*ak4104
;
203 spi
->bits_per_word
= 8;
204 spi
->mode
= SPI_MODE_0
;
205 ret
= spi_setup(spi
);
209 ak4104
= kzalloc(sizeof(struct ak4104_private
), GFP_KERNEL
);
211 dev_err(&spi
->dev
, "could not allocate codec\n");
215 codec
= &ak4104
->codec
;
216 mutex_init(&codec
->mutex
);
217 INIT_LIST_HEAD(&codec
->dapm_widgets
);
218 INIT_LIST_HEAD(&codec
->dapm_paths
);
220 codec
->dev
= &spi
->dev
;
221 codec
->name
= DRV_NAME
;
222 codec
->owner
= THIS_MODULE
;
223 codec
->dai
= &ak4104_dai
;
225 snd_soc_codec_set_drvdata(codec
, ak4104
);
226 codec
->control_data
= spi
;
227 codec
->reg_cache
= ak4104
->reg_cache
;
228 codec
->reg_cache_size
= AK4104_NUM_REGS
;
230 /* read all regs and fill the cache */
231 ret
= ak4104_fill_cache(codec
);
233 dev_err(&spi
->dev
, "failed to fill register cache\n");
237 /* read the 'reserved' register - according to the datasheet, it
238 * should contain 0x5b. Not a good way to verify the presence of
239 * the device, but there is no hardware ID register. */
240 if (ak4104_read_reg_cache(codec
, AK4104_REG_RESERVED
) !=
241 AK4104_RESERVED_VAL
) {
243 goto error_free_codec
;
246 /* set power-up and non-reset bits */
247 val
= ak4104_read_reg_cache(codec
, AK4104_REG_CONTROL1
);
248 val
|= AK4104_CONTROL1_PW
| AK4104_CONTROL1_RSTN
;
249 ret
= ak4104_spi_write(codec
, AK4104_REG_CONTROL1
, val
);
251 goto error_free_codec
;
253 /* enable transmitter */
254 val
= ak4104_read_reg_cache(codec
, AK4104_REG_TX
);
255 val
|= AK4104_TX_TXE
;
256 ret
= ak4104_spi_write(codec
, AK4104_REG_TX
, val
);
258 goto error_free_codec
;
260 ak4104_codec
= codec
;
261 ret
= snd_soc_register_dai(&ak4104_dai
);
263 dev_err(&spi
->dev
, "failed to register DAI\n");
264 goto error_free_codec
;
267 spi_set_drvdata(spi
, ak4104
);
268 dev_info(&spi
->dev
, "SPI device initialized\n");
273 ak4104_dai
.dev
= NULL
;
277 static int __devexit
ak4104_spi_remove(struct spi_device
*spi
)
280 struct ak4104_private
*ak4104
= spi_get_drvdata(spi
);
282 val
= ak4104_read_reg_cache(&ak4104
->codec
, AK4104_REG_CONTROL1
);
286 /* clear power-up and non-reset bits */
287 val
&= ~(AK4104_CONTROL1_PW
| AK4104_CONTROL1_RSTN
);
288 ret
= ak4104_spi_write(&ak4104
->codec
, AK4104_REG_CONTROL1
, val
);
297 static int ak4104_probe(struct platform_device
*pdev
)
299 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
300 struct snd_soc_codec
*codec
= ak4104_codec
;
303 /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */
304 socdev
->card
->codec
= codec
;
307 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
309 dev_err(codec
->dev
, "failed to create pcms\n");
316 static int ak4104_remove(struct platform_device
*pdev
)
318 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
319 snd_soc_free_pcms(socdev
);
323 struct snd_soc_codec_device soc_codec_device_ak4104
= {
324 .probe
= ak4104_probe
,
325 .remove
= ak4104_remove
327 EXPORT_SYMBOL_GPL(soc_codec_device_ak4104
);
329 static struct spi_driver ak4104_spi_driver
= {
332 .owner
= THIS_MODULE
,
334 .probe
= ak4104_spi_probe
,
335 .remove
= __devexit_p(ak4104_spi_remove
),
338 static int __init
ak4104_init(void)
340 pr_info("Asahi Kasei AK4104 ALSA SoC Codec Driver\n");
341 return spi_register_driver(&ak4104_spi_driver
);
343 module_init(ak4104_init
);
345 static void __exit
ak4104_exit(void)
347 spi_unregister_driver(&ak4104_spi_driver
);
349 module_exit(ak4104_exit
);
351 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
352 MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
353 MODULE_LICENSE("GPL");