2 * ak4642.c -- AK4642/AK4643 ALSA Soc Audio driver
4 * Copyright (C) 2009 Renesas Solutions Corp.
5 * Kuninori Morimoto <morimoto.kuninori@renesas.com>
7 * Based on wm8731.c by Richard Purdie
8 * Based on ak4535.c by Richard Purdie
9 * Based on wm8753.c by Liam Girdwood
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
18 * This is very simple driver.
19 * It can use headphone output / stereo input only
21 * AK4642 is not tested.
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/delay.h>
30 #include <linux/i2c.h>
31 #include <linux/platform_device.h>
32 #include <sound/core.h>
33 #include <sound/pcm.h>
34 #include <sound/pcm_params.h>
35 #include <sound/soc.h>
36 #include <sound/soc-dapm.h>
37 #include <sound/initval.h>
41 #define AK4642_VERSION "0.0.1"
81 #define AK4642_CACHEREGNUM 0x25
84 #define HPMTN (1 << 6)
85 #define PMHPL (1 << 5)
86 #define PMHPR (1 << 4)
87 #define MS (1 << 3) /* master/slave select */
89 #define PMPLL (1 << 0)
91 #define PMHP_MASK (PMHPL | PMHPR)
92 #define PMHP PMHP_MASK
99 #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
101 #define BCKO_MASK (1 << 3)
102 #define BCKO_64 BCKO_MASK
104 struct snd_soc_codec_device soc_codec_dev_ak4642;
106 /* codec private data */
108 struct snd_soc_codec codec;
111 static struct snd_soc_codec *ak4642_codec;
114 * ak4642 register cache
116 static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
117 0x0000, 0x0000, 0x0001, 0x0000,
118 0x0002, 0x0000, 0x0000, 0x0000,
119 0x00e1, 0x00e1, 0x0018, 0x0000,
120 0x00e1, 0x0018, 0x0011, 0x0008,
121 0x0000, 0x0000, 0x0000, 0x0000,
122 0x0000, 0x0000, 0x0000, 0x0000,
123 0x0000, 0x0000, 0x0000, 0x0000,
124 0x0000, 0x0000, 0x0000, 0x0000,
125 0x0000, 0x0000, 0x0000, 0x0000,
130 * read ak4642 register cache
132 static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
135 u16 *cache = codec->reg_cache;
136 if (reg >= AK4642_CACHEREGNUM)
142 * write ak4642 register cache
144 static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
145 u16 reg, unsigned int value)
147 u16 *cache = codec->reg_cache;
148 if (reg >= AK4642_CACHEREGNUM)
155 * write to the AK4642 register space
157 static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
163 * D15..D8 AK4642 register offset
164 * D7...D0 register data
166 data[0] = reg & 0xff;
167 data[1] = value & 0xff;
169 if (codec->hw_write(codec->control_data, data, 2) == 2) {
170 ak4642_write_reg_cache(codec, reg, value);
176 static int ak4642_sync(struct snd_soc_codec *codec)
178 u16 *cache = codec->reg_cache;
181 for (i = 0; i < AK4642_CACHEREGNUM; i++)
182 r |= ak4642_write(codec, i, cache[i]);
187 static int ak4642_dai_startup(struct snd_pcm_substream *substream,
188 struct snd_soc_dai *dai)
190 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
191 struct snd_soc_codec *codec = dai->codec;
195 * start headphone output
198 * Audio I/F Format :MSB justified (ADC & DAC)
199 * Sampling Frequency: 44.1kHz
200 * Digital Volume: −8dB
201 * Bass Boost Level : Middle
203 * This operation came from example code of
204 * "ASAHI KASEI AK4642" (japanese) manual p97.
206 ak4642_write(codec, 0x05, 0x27);
207 ak4642_write(codec, 0x0f, 0x09);
208 ak4642_write(codec, 0x0e, 0x19);
209 ak4642_write(codec, 0x09, 0x91);
210 ak4642_write(codec, 0x0c, 0x91);
211 ak4642_write(codec, 0x0a, 0x28);
212 ak4642_write(codec, 0x0d, 0x28);
213 ak4642_write(codec, 0x00, 0x64);
214 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP);
215 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN);
221 * Audio I/F Format:MSB justified (ADC & DAC)
222 * Sampling Frequency:44.1kHz
225 * ALC setting:Refer to Table 35
228 * This operation came from example code of
229 * "ASAHI KASEI AK4642" (japanese) manual p94.
231 ak4642_write(codec, 0x05, 0x27);
232 ak4642_write(codec, 0x02, 0x05);
233 ak4642_write(codec, 0x06, 0x3c);
234 ak4642_write(codec, 0x08, 0xe1);
235 ak4642_write(codec, 0x0b, 0x00);
236 ak4642_write(codec, 0x07, 0x21);
237 ak4642_write(codec, 0x00, 0x41);
238 ak4642_write(codec, 0x10, 0x01);
244 static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
245 struct snd_soc_dai *dai)
247 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
248 struct snd_soc_codec *codec = dai->codec;
251 /* stop headphone output */
252 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0);
253 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0);
254 ak4642_write(codec, 0x00, 0x40);
255 ak4642_write(codec, 0x0e, 0x11);
256 ak4642_write(codec, 0x0f, 0x08);
258 /* stop stereo input */
259 ak4642_write(codec, 0x00, 0x40);
260 ak4642_write(codec, 0x10, 0x00);
261 ak4642_write(codec, 0x07, 0x01);
265 static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
266 int clk_id, unsigned int freq, int dir)
268 struct snd_soc_codec *codec = codec_dai->codec;
282 pll = PLL2 | PLL1 | PLL0;
288 pll = PLL3 | PLL2 | PLL0;
293 snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
298 static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
300 struct snd_soc_codec *codec = dai->codec;
304 data = MCKO | PMPLL; /* use MCKO */
307 /* set master/slave audio interface */
308 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
309 case SND_SOC_DAIFMT_CBM_CFM:
313 case SND_SOC_DAIFMT_CBS_CFS:
318 snd_soc_update_bits(codec, PW_MGMT2, MS, data);
319 snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
324 static struct snd_soc_dai_ops ak4642_dai_ops = {
325 .startup = ak4642_dai_startup,
326 .shutdown = ak4642_dai_shutdown,
327 .set_sysclk = ak4642_dai_set_sysclk,
328 .set_fmt = ak4642_dai_set_fmt,
331 struct snd_soc_dai ak4642_dai = {
334 .stream_name = "Playback",
337 .rates = SNDRV_PCM_RATE_8000_48000,
338 .formats = SNDRV_PCM_FMTBIT_S16_LE },
340 .stream_name = "Capture",
343 .rates = SNDRV_PCM_RATE_8000_48000,
344 .formats = SNDRV_PCM_FMTBIT_S16_LE },
345 .ops = &ak4642_dai_ops,
347 EXPORT_SYMBOL_GPL(ak4642_dai);
349 static int ak4642_resume(struct platform_device *pdev)
351 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
352 struct snd_soc_codec *codec = socdev->card->codec;
359 * initialise the AK4642 driver
360 * register the mixer and dsp interfaces with the kernel
362 static int ak4642_init(struct ak4642_priv *ak4642)
364 struct snd_soc_codec *codec = &ak4642->codec;
368 dev_err(codec->dev, "Another ak4642 is registered\n");
372 mutex_init(&codec->mutex);
373 INIT_LIST_HEAD(&codec->dapm_widgets);
374 INIT_LIST_HEAD(&codec->dapm_paths);
376 codec->private_data = ak4642;
377 codec->name = "AK4642";
378 codec->owner = THIS_MODULE;
379 codec->read = ak4642_read_reg_cache;
380 codec->write = ak4642_write;
381 codec->dai = &ak4642_dai;
383 codec->hw_write = (hw_write_t)i2c_master_send;
384 codec->reg_cache_size = ARRAY_SIZE(ak4642_reg);
385 codec->reg_cache = kmemdup(ak4642_reg,
386 sizeof(ak4642_reg), GFP_KERNEL);
388 if (!codec->reg_cache)
391 ak4642_dai.dev = codec->dev;
392 ak4642_codec = codec;
394 ret = snd_soc_register_codec(codec);
396 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
400 ret = snd_soc_register_dai(&ak4642_dai);
402 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
403 snd_soc_unregister_codec(codec);
410 kfree(codec->reg_cache);
411 codec->reg_cache = NULL;
416 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
417 static int ak4642_i2c_probe(struct i2c_client *i2c,
418 const struct i2c_device_id *id)
420 struct ak4642_priv *ak4642;
421 struct snd_soc_codec *codec;
424 ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
428 codec = &ak4642->codec;
429 codec->dev = &i2c->dev;
431 i2c_set_clientdata(i2c, ak4642);
432 codec->control_data = i2c;
434 ret = ak4642_init(ak4642);
436 printk(KERN_ERR "failed to initialise AK4642\n");
441 static int ak4642_i2c_remove(struct i2c_client *client)
443 struct ak4642_priv *ak4642 = i2c_get_clientdata(client);
445 snd_soc_unregister_dai(&ak4642_dai);
446 snd_soc_unregister_codec(&ak4642->codec);
447 kfree(ak4642->codec.reg_cache);
454 static const struct i2c_device_id ak4642_i2c_id[] = {
459 MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
461 static struct i2c_driver ak4642_i2c_driver = {
463 .name = "AK4642 I2C Codec",
464 .owner = THIS_MODULE,
466 .probe = ak4642_i2c_probe,
467 .remove = ak4642_i2c_remove,
468 .id_table = ak4642_i2c_id,
473 static int ak4642_probe(struct platform_device *pdev)
475 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
479 dev_err(&pdev->dev, "Codec device not registered\n");
483 socdev->card->codec = ak4642_codec;
486 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
488 printk(KERN_ERR "ak4642: failed to create pcms\n");
492 dev_info(&pdev->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
500 /* power down chip */
501 static int ak4642_remove(struct platform_device *pdev)
503 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
505 snd_soc_free_pcms(socdev);
506 snd_soc_dapm_free(socdev);
511 struct snd_soc_codec_device soc_codec_dev_ak4642 = {
512 .probe = ak4642_probe,
513 .remove = ak4642_remove,
514 .resume = ak4642_resume,
516 EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642);
518 static int __init ak4642_modinit(void)
521 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
522 ret = i2c_add_driver(&ak4642_i2c_driver);
527 module_init(ak4642_modinit);
529 static void __exit ak4642_exit(void)
531 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
532 i2c_del_driver(&ak4642_i2c_driver);
536 module_exit(ak4642_exit);
538 MODULE_DESCRIPTION("Soc AK4642 driver");
539 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
540 MODULE_LICENSE("GPL");