2 * AK4396 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>
19 #include <linux/delay.h>
21 /* AK4396 registers addresses */
22 #define AK4396_REG_CONTROL1 0x00
23 #define AK4396_REG_CONTROL2 0x01
24 #define AK4396_REG_CONTROL3 0x02
25 #define AK4396_REG_LCH_ATT 0x03
26 #define AK4396_REG_RCH_ATT 0x04
27 #define AK4396_NUM_REGS 5
29 #define AK4396_REG_MASK 0x1f
30 #define AK4396_WRITE 0x20 /*C1 C0 R/W A4 A3 A2 A1 A0 8bit==0010 0000 */
32 /* Bit masks for AK4396 registers */
33 #define AK4396_CONTROL1_RSTN (1 << 0)
34 #define AK4396_CONTROL1_DIF0 (1 << 1)
35 #define AK4396_CONTROL1_DIF1 (1 << 2)
36 #define AK4396_CONTROL1_DIF2 (1 << 3)
38 #define DRV_NAME "AK4396"
40 struct ak4396_private {
41 enum snd_soc_control_type control_type;
48 static const u16 ak4396_reg[AK4396_NUM_REGS] = {
49 0x87, 0x02, 0x00, 0xff, 0xff
52 static const u16 ak4396_reg[AK4396_NUM_REGS] = {
53 0x05, 0x02, 0x00, 0xff, 0xff
58 static void on_off_ext_amp(int i)
62 //gpio_direction_output(SPK_CTL, GPIO_LOW);
63 gpio_set_value(SPK_CTL, i);
64 printk("*** %s() SPEAKER set as %d\n", __FUNCTION__, i);
67 //gpio_direction_output(EAR_CON_PIN, GPIO_LOW);
68 gpio_set_value(EAR_CON_PIN, i);
69 printk("*** %s() HEADPHONE set as %d\n", __FUNCTION__, i);
74 static int ak4396_fill_cache(struct snd_soc_codec *codec)
77 u8 *reg_cache = codec->reg_cache;
78 struct spi_device *spi = codec->control_data;
80 for (i = 0; i < codec->driver->reg_cache_size; i++) {
81 int ret = spi_w8r8(spi, i);
83 dev_err(&spi->dev, "SPI write failure\n");
93 /* read the reg_cache */
94 static unsigned int ak4396_read_reg_cache(struct snd_soc_codec *codec,
97 u8 *reg_cache = codec->reg_cache;
99 if (reg >= codec->driver->reg_cache_size)
102 // printk("read reg_cache[%x]====%d\n", reg, reg_cache[reg]);
103 return reg_cache[reg];
106 static int ak4396_spi_write(struct snd_soc_codec *codec, unsigned int reg,
109 u8 *cache = codec->reg_cache;
110 struct spi_device *spi = codec->control_data;
112 if (reg >= codec->driver->reg_cache_size)
115 /* only write to the hardware if value has changed */
116 //if (cache[reg] != value)
118 u8 tmp[2] = { (reg & AK4396_REG_MASK) | AK4396_WRITE, value};
119 //printk("tmp[0]===%d\n", tmp[0]);
120 //printk("tmp[1]===%d\n", tmp[1]);
121 if (spi_write(spi, tmp, sizeof(tmp))) {
122 dev_err(&spi->dev, "SPI write failed\n");
131 /* write the register space */
132 static ak4396_write(struct snd_soc_codec *codec)
136 int addr[5] = {0x00, 0x01, 0x02, 0x03, 0x04};
137 int dat[5] = {0x87, 0x02, 0x00, 0xff, 0xff};
139 val |= AK4396_CONTROL1_RSTN;
140 ak4396_spi_write(codec, AK4396_REG_CONTROL1, val);
144 ret = ak4396_spi_write(codec, addr[i], dat[i]);
146 printk("ak4396_spi_write failed!\n");
148 printk("write %d time(s)\n", i);
154 * Note that this should be called from init rather than from hw_params.
156 static int ak4396_set_dai_sysclk(struct snd_soc_dai *codec_dai,
157 int clk_id, unsigned int freq, int dir)
159 struct snd_soc_codec *codec = codec_dai->codec;
160 struct ak4396_private *ak4396 = snd_soc_codec_get_drvdata(codec);
162 printk("Enter::%s----%d\n",__FUNCTION__,__LINE__);
163 printk("freq======%d\n", freq);
164 ak4396->sysclk = freq;
169 static int ak4396_set_dai_fmt(struct snd_soc_dai *codec_dai,
172 struct snd_soc_codec *codec = codec_dai->codec;
175 printk("%s----%d, format[%02x]\n",__FUNCTION__,__LINE__,format);
176 val = ak4396_read_reg_cache(codec, AK4396_REG_CONTROL1);
179 val &= ~(AK4396_CONTROL1_DIF0 | AK4396_CONTROL1_DIF1 | AK4396_CONTROL1_DIF2);
180 // printk("ak4396 val=%d\n", val);
183 switch (format & SND_SOC_DAIFMT_FORMAT_MASK) {
184 case SND_SOC_DAIFMT_RIGHT_J:
185 val |= AK4396_CONTROL1_DIF2 ;
186 printk("SND_SOC_DAIFMT_RIGHT_J: \n");
188 case SND_SOC_DAIFMT_LEFT_J:
189 val |= AK4396_CONTROL1_DIF1 ;
190 printk("SND_SOC_DAIFMT_LEFT_J: \n");
192 case SND_SOC_DAIFMT_I2S:
193 val |= AK4396_CONTROL1_DIF0 | AK4396_CONTROL1_DIF1 ;
196 printk("SND_SOC_DAIFMT_I2S is ok!\n");
199 dev_err(codec->dev, "invalid dai format\n");
203 /* This device can only be slave */
204 if ((format & SND_SOC_DAIFMT_MASTER_MASK) != SND_SOC_DAIFMT_CBS_CFS)
206 printk("%s failed!----%d\n",__FUNCTION__,__LINE__);
210 //val |= AK4396_CONTROL1_RSTN;
211 printk("AK4396 CONTROL1 val ==== %d\n", val);
212 ak4396_spi_write(codec, AK4396_REG_CONTROL1, val);
217 static int ak4396_hw_params(struct snd_pcm_substream *substream,
218 struct snd_pcm_hw_params *params,
219 struct snd_soc_dai *dai)
221 struct snd_soc_pcm_runtime *rtd = substream->private_data;
222 struct snd_soc_codec *codec = rtd->codec;
225 switch (params_rate(params)) {
228 val |= IEC958_AES3_CON_FS_176400;
229 printk("params_rate::=176400!\n");
233 val |= IEC958_AES3_CON_FS_192000;
234 printk("params_rate::=192000!\n");
238 val |= IEC958_AES3_CON_FS_88200;
239 printk("params_rate::=88200!\n");
244 val |= IEC958_AES3_CON_FS_96000;
245 printk("params_rate::=96000!\n");
249 val |= IEC958_AES3_CON_FS_44100;
250 printk("params_rate::=44100!\n");
253 val |= IEC958_AES3_CON_FS_48000;
256 val |= IEC958_AES3_CON_FS_32000;
259 dev_err(codec->dev, "unsupported sampling rate\n");
263 val = ak4396_read_reg_cache(codec, AK4396_REG_CONTROL1);
266 printk("val ==== %d\n", val);
267 ak4396_spi_write(codec, AK4396_REG_CONTROL1, val);
269 printk("val ==== %d\n", val);
270 ak4396_spi_write(codec, AK4396_REG_CONTROL1, val);
272 //printk("val === %d\n", val);
273 //ak4396_spi_write(codec, AK4396_REG_CONTROL2, val);
277 static struct snd_soc_dai_ops ak4396_dai_ops = {
278 .hw_params = ak4396_hw_params,
279 .set_fmt = ak4396_set_dai_fmt,
280 .set_sysclk = ak4396_set_dai_sysclk,
283 static struct snd_soc_dai_driver ak4396_dai = {
284 .name = "AK4396 HiFi",
286 .stream_name = "Playback",
289 .rates = SNDRV_PCM_RATE_8000_192000,
290 .formats = SNDRV_PCM_FMTBIT_S16_LE |
291 SNDRV_PCM_FMTBIT_S24_3LE |
292 SNDRV_PCM_FMTBIT_S24_LE |
293 SNDRV_PCM_FMTBIT_S32_LE
295 .ops = &ak4396_dai_ops,
298 struct snd_soc_codec *codec_temp;
299 static int ak4396_probe(struct snd_soc_codec *codec)
301 struct ak4396_private *ak4396 = snd_soc_codec_get_drvdata(codec);
303 printk("ak4396_probe begin!\n");
304 codec->control_data = ak4396->control_data;
307 /* read all regs and fill the cache */
308 ret = ak4396_fill_cache(codec);
310 dev_err(codec->dev, "failed to fill register cache\n");
314 /* write to ak4396_reg */
315 // ak4396_write(codec);
317 printk("ak4396_probe is ok!\n");
318 dev_info(codec->dev, "SPI device initialized\n");
322 static int ak4396_remove(struct snd_soc_codec *codec)
326 val = ak4396_read_reg_cache(codec, AK4396_REG_CONTROL1);
330 /* set non-reset bits */
331 val &= ~AK4396_CONTROL1_RSTN;
332 ret = ak4396_spi_write(codec, AK4396_REG_CONTROL1, val);
337 static int ak4396_suspend(struct snd_soc_codec *codec)
342 static int ak4396_resume(struct snd_soc_codec *codec)
344 //ak4396_write(codec);
348 static struct snd_soc_codec_driver soc_codec_device_ak4396 = {
349 .probe = ak4396_probe,
350 .remove = ak4396_remove,
351 .suspend = ak4396_suspend,
352 .resume = ak4396_resume,
353 .reg_cache_size = AK4396_NUM_REGS,
354 .reg_word_size = sizeof(u16),
355 .reg_cache_default = ak4396_reg,
358 static struct class *cls = NULL;
360 static ssize_t store_ak4396_reg(struct class *dev,
361 struct class_attribute *attr, const char *buf, size_t count)
364 // char buf[10] = "123 11";
367 printk("%s, the first dat is reg, the second dat is data, data type is dex\n", __FUNCTION__);
368 while (*start == ' ')
370 reg = simple_strtoull(start, &start, 16);
372 while (*start == ' ')
374 value = simple_strtoull(start, &start, 16);
377 ret = ak4396_spi_write(codec_temp, reg, value);
379 printk("ak4396_spi_write failed!\n");
381 printk("reg = %d, value =%d\n", reg, value);
384 static struct class_attribute attr[] = {
385 __ATTR(write_reg, 0644, NULL, store_ak4396_reg),
388 static int ak4396_spi_probe(struct spi_device *spi)
390 struct ak4396_private *ak4396;
392 printk("ak4396_spi_probe begin!\n");
394 #if 0 //defined(CONFIG_ARCH_RK3188)
398 printk("iomux_set is OK!!!\n");
401 spi->bits_per_word = 8;
402 spi->mode = SPI_MODE_0;
403 ret = spi_setup(spi);
407 ak4396 = kzalloc(sizeof(struct ak4396_private), GFP_KERNEL);
411 ak4396->control_data = spi;
412 ak4396->control_type = SND_SOC_SPI;
413 spi_set_drvdata(spi, ak4396);
415 cls = class_create(THIS_MODULE, DRV_NAME);
418 printk("class_create failed!\n");
420 ret = class_create_file(cls, attr);
423 printk("class_create_file failed!\n");
426 ret = snd_soc_register_codec(&spi->dev,
427 &soc_codec_device_ak4396, &ak4396_dai, 1);
431 printk("ak4396_spi_probe successful!\n");
435 static int ak4396_spi_remove(struct spi_device *spi)
437 snd_soc_unregister_codec(&spi->dev);
438 kfree(spi_get_drvdata(spi));
442 static struct spi_driver ak4396_spi_driver = {
445 .owner = THIS_MODULE,
447 .probe = ak4396_spi_probe,
448 .remove = ak4396_spi_remove,
451 static int __init ak4396_init(void)
453 printk("%s\n", __FUNCTION__);
454 return spi_register_driver(&ak4396_spi_driver);
456 module_init(ak4396_init);
458 static void __exit ak4396_exit(void)
460 spi_unregister_driver(&ak4396_spi_driver);
462 module_exit(ak4396_exit);
464 MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
465 MODULE_DESCRIPTION("Asahi Kasei AK4396 ALSA SoC driver");
466 MODULE_LICENSE("GPL");