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 <sound/core.h>
14 #include <sound/soc.h>
15 #include <sound/initval.h>
16 #include <linux/spi/spi.h>
17 #include <sound/asoundef.h>
21 /* AK4104 registers addresses */
22 #define AK4104_REG_CONTROL1 0x00
23 #define AK4104_REG_RESERVED 0x01
24 #define AK4104_REG_CONTROL2 0x02
25 #define AK4104_REG_TX 0x03
26 #define AK4104_REG_CHN_STATUS(x) ((x) + 0x04)
27 #define AK4104_NUM_REGS 10
29 #define AK4104_REG_MASK 0x1f
30 #define AK4104_READ 0xc0
31 #define AK4104_WRITE 0xe0
32 #define AK4104_RESERVED_VAL 0x5b
34 /* Bit masks for AK4104 registers */
35 #define AK4104_CONTROL1_RSTN (1 << 0)
36 #define AK4104_CONTROL1_PW (1 << 1)
37 #define AK4104_CONTROL1_DIF0 (1 << 2)
38 #define AK4104_CONTROL1_DIF1 (1 << 3)
40 #define AK4104_CONTROL2_SEL0 (1 << 0)
41 #define AK4104_CONTROL2_SEL1 (1 << 1)
42 #define AK4104_CONTROL2_MODE (1 << 2)
44 #define AK4104_TX_TXE (1 << 0)
45 #define AK4104_TX_V (1 << 1)
47 #define DRV_NAME "ak4104"
49 struct ak4104_private
{
50 struct snd_soc_codec codec
;
51 u8 reg_cache
[AK4104_NUM_REGS
];
54 static int ak4104_fill_cache(struct snd_soc_codec
*codec
)
57 u8
*reg_cache
= codec
->reg_cache
;
58 struct spi_device
*spi
= codec
->control_data
;
60 for (i
= 0; i
< codec
->reg_cache_size
; i
++) {
61 int ret
= spi_w8r8(spi
, i
| AK4104_READ
);
63 dev_err(&spi
->dev
, "SPI write failure\n");
73 static unsigned int ak4104_read_reg_cache(struct snd_soc_codec
*codec
,
76 u8
*reg_cache
= codec
->reg_cache
;
78 if (reg
>= codec
->reg_cache_size
)
81 return reg_cache
[reg
];
84 static int ak4104_spi_write(struct snd_soc_codec
*codec
, unsigned int reg
,
87 u8
*cache
= codec
->reg_cache
;
88 struct spi_device
*spi
= codec
->control_data
;
90 if (reg
>= codec
->reg_cache_size
)
93 /* only write to the hardware if value has changed */
94 if (cache
[reg
] != value
) {
95 u8 tmp
[2] = { (reg
& AK4104_REG_MASK
) | AK4104_WRITE
, value
};
97 if (spi_write(spi
, tmp
, sizeof(tmp
))) {
98 dev_err(&spi
->dev
, "SPI write failed\n");
108 static int ak4104_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
111 struct snd_soc_codec
*codec
= codec_dai
->codec
;
114 val
= ak4104_read_reg_cache(codec
, AK4104_REG_CONTROL1
);
118 val
&= ~(AK4104_CONTROL1_DIF0
| AK4104_CONTROL1_DIF1
);
121 switch (format
& SND_SOC_DAIFMT_FORMAT_MASK
) {
122 case SND_SOC_DAIFMT_RIGHT_J
:
124 case SND_SOC_DAIFMT_LEFT_J
:
125 val
|= AK4104_CONTROL1_DIF0
;
127 case SND_SOC_DAIFMT_I2S
:
128 val
|= AK4104_CONTROL1_DIF0
| AK4104_CONTROL1_DIF1
;
131 dev_err(codec
->dev
, "invalid dai format\n");
135 /* This device can only be slave */
136 if ((format
& SND_SOC_DAIFMT_MASTER_MASK
) != SND_SOC_DAIFMT_CBS_CFS
)
139 return ak4104_spi_write(codec
, AK4104_REG_CONTROL1
, val
);
142 static int ak4104_hw_params(struct snd_pcm_substream
*substream
,
143 struct snd_pcm_hw_params
*params
,
144 struct snd_soc_dai
*dai
)
146 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
147 struct snd_soc_device
*socdev
= rtd
->socdev
;
148 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
151 /* set the IEC958 bits: consumer mode, no copyright bit */
152 val
|= IEC958_AES0_CON_NOT_COPYRIGHT
;
153 ak4104_spi_write(codec
, AK4104_REG_CHN_STATUS(0), val
);
157 switch (params_rate(params
)) {
159 val
|= IEC958_AES3_CON_FS_44100
;
162 val
|= IEC958_AES3_CON_FS_48000
;
165 val
|= IEC958_AES3_CON_FS_32000
;
168 dev_err(codec
->dev
, "unsupported sampling rate\n");
172 return ak4104_spi_write(codec
, AK4104_REG_CHN_STATUS(3), val
);
175 static struct snd_soc_dai_ops ak4101_dai_ops
= {
176 .hw_params
= ak4104_hw_params
,
177 .set_fmt
= ak4104_set_dai_fmt
,
180 struct snd_soc_dai ak4104_dai
= {
183 .stream_name
= "Playback",
186 .rates
= SNDRV_PCM_RATE_44100
|
187 SNDRV_PCM_RATE_48000
|
188 SNDRV_PCM_RATE_32000
,
189 .formats
= SNDRV_PCM_FMTBIT_S16_LE
|
190 SNDRV_PCM_FMTBIT_S24_3LE
|
191 SNDRV_PCM_FMTBIT_S24_LE
193 .ops
= &ak4101_dai_ops
,
196 static struct snd_soc_codec
*ak4104_codec
;
198 static int ak4104_spi_probe(struct spi_device
*spi
)
200 struct snd_soc_codec
*codec
;
201 struct ak4104_private
*ak4104
;
204 spi
->bits_per_word
= 8;
205 spi
->mode
= SPI_MODE_0
;
206 ret
= spi_setup(spi
);
210 ak4104
= kzalloc(sizeof(struct ak4104_private
), GFP_KERNEL
);
212 dev_err(&spi
->dev
, "could not allocate codec\n");
216 codec
= &ak4104
->codec
;
217 mutex_init(&codec
->mutex
);
218 INIT_LIST_HEAD(&codec
->dapm_widgets
);
219 INIT_LIST_HEAD(&codec
->dapm_paths
);
221 codec
->dev
= &spi
->dev
;
222 codec
->name
= DRV_NAME
;
223 codec
->owner
= THIS_MODULE
;
224 codec
->dai
= &ak4104_dai
;
226 codec
->private_data
= ak4104
;
227 codec
->control_data
= spi
;
228 codec
->reg_cache
= ak4104
->reg_cache
;
229 codec
->reg_cache_size
= AK4104_NUM_REGS
;
231 /* read all regs and fill the cache */
232 ret
= ak4104_fill_cache(codec
);
234 dev_err(&spi
->dev
, "failed to fill register cache\n");
238 /* read the 'reserved' register - according to the datasheet, it
239 * should contain 0x5b. Not a good way to verify the presence of
240 * the device, but there is no hardware ID register. */
241 if (ak4104_read_reg_cache(codec
, AK4104_REG_RESERVED
) !=
242 AK4104_RESERVED_VAL
) {
244 goto error_free_codec
;
247 /* set power-up and non-reset bits */
248 val
= ak4104_read_reg_cache(codec
, AK4104_REG_CONTROL1
);
249 val
|= AK4104_CONTROL1_PW
| AK4104_CONTROL1_RSTN
;
250 ret
= ak4104_spi_write(codec
, AK4104_REG_CONTROL1
, val
);
252 goto error_free_codec
;
254 /* enable transmitter */
255 val
= ak4104_read_reg_cache(codec
, AK4104_REG_TX
);
256 val
|= AK4104_TX_TXE
;
257 ret
= ak4104_spi_write(codec
, AK4104_REG_TX
, val
);
259 goto error_free_codec
;
261 ak4104_codec
= codec
;
262 ret
= snd_soc_register_dai(&ak4104_dai
);
264 dev_err(&spi
->dev
, "failed to register DAI\n");
265 goto error_free_codec
;
268 spi_set_drvdata(spi
, ak4104
);
269 dev_info(&spi
->dev
, "SPI device initialized\n");
274 ak4104_dai
.dev
= NULL
;
278 static int __devexit
ak4104_spi_remove(struct spi_device
*spi
)
281 struct ak4104_private
*ak4104
= spi_get_drvdata(spi
);
283 val
= ak4104_read_reg_cache(&ak4104
->codec
, AK4104_REG_CONTROL1
);
287 /* clear power-up and non-reset bits */
288 val
&= ~(AK4104_CONTROL1_PW
| AK4104_CONTROL1_RSTN
);
289 ret
= ak4104_spi_write(&ak4104
->codec
, AK4104_REG_CONTROL1
, val
);
298 static int ak4104_probe(struct platform_device
*pdev
)
300 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
301 struct snd_soc_codec
*codec
= ak4104_codec
;
304 /* Connect the codec to the socdev. snd_soc_new_pcms() needs this. */
305 socdev
->card
->codec
= codec
;
308 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
310 dev_err(codec
->dev
, "failed to create pcms\n");
314 /* Register the socdev */
315 ret
= snd_soc_init_card(socdev
);
317 dev_err(codec
->dev
, "failed to register card\n");
318 snd_soc_free_pcms(socdev
);
325 static int ak4104_remove(struct platform_device
*pdev
)
327 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
328 snd_soc_free_pcms(socdev
);
332 struct snd_soc_codec_device soc_codec_device_ak4104
= {
333 .probe
= ak4104_probe
,
334 .remove
= ak4104_remove
336 EXPORT_SYMBOL_GPL(soc_codec_device_ak4104
);
338 static struct spi_driver ak4104_spi_driver
= {
341 .owner
= THIS_MODULE
,
343 .probe
= ak4104_spi_probe
,
344 .remove
= __devexit_p(ak4104_spi_remove
),
347 static int __init
ak4104_init(void)
349 pr_info("Asahi Kasei AK4104 ALSA SoC Codec Driver\n");
350 return spi_register_driver(&ak4104_spi_driver
);
352 module_init(ak4104_init
);
354 static void __exit
ak4104_exit(void)
356 spi_unregister_driver(&ak4104_spi_driver
);
358 module_exit(ak4104_exit
);
360 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
361 MODULE_DESCRIPTION("Asahi Kasei AK4104 ALSA SoC driver");
362 MODULE_LICENSE("GPL");