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
109 #define FS_MASK (FS0 | FS1 | FS2 | FS3)
111 struct snd_soc_codec_device soc_codec_dev_ak4642;
113 /* codec private data */
115 struct snd_soc_codec codec;
118 static struct snd_soc_codec *ak4642_codec;
121 * ak4642 register cache
123 static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
124 0x0000, 0x0000, 0x0001, 0x0000,
125 0x0002, 0x0000, 0x0000, 0x0000,
126 0x00e1, 0x00e1, 0x0018, 0x0000,
127 0x00e1, 0x0018, 0x0011, 0x0008,
128 0x0000, 0x0000, 0x0000, 0x0000,
129 0x0000, 0x0000, 0x0000, 0x0000,
130 0x0000, 0x0000, 0x0000, 0x0000,
131 0x0000, 0x0000, 0x0000, 0x0000,
132 0x0000, 0x0000, 0x0000, 0x0000,
137 * read ak4642 register cache
139 static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
142 u16 *cache = codec->reg_cache;
143 if (reg >= AK4642_CACHEREGNUM)
149 * write ak4642 register cache
151 static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
152 u16 reg, unsigned int value)
154 u16 *cache = codec->reg_cache;
155 if (reg >= AK4642_CACHEREGNUM)
162 * write to the AK4642 register space
164 static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
170 * D15..D8 AK4642 register offset
171 * D7...D0 register data
173 data[0] = reg & 0xff;
174 data[1] = value & 0xff;
176 if (codec->hw_write(codec->control_data, data, 2) == 2) {
177 ak4642_write_reg_cache(codec, reg, value);
183 static int ak4642_sync(struct snd_soc_codec *codec)
185 u16 *cache = codec->reg_cache;
188 for (i = 0; i < AK4642_CACHEREGNUM; i++)
189 r |= ak4642_write(codec, i, cache[i]);
194 static int ak4642_dai_startup(struct snd_pcm_substream *substream,
195 struct snd_soc_dai *dai)
197 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
198 struct snd_soc_codec *codec = dai->codec;
202 * start headphone output
205 * Audio I/F Format :MSB justified (ADC & DAC)
206 * Digital Volume: -8dB
207 * Bass Boost Level : Middle
209 * This operation came from example code of
210 * "ASAHI KASEI AK4642" (japanese) manual p97.
212 ak4642_write(codec, 0x0f, 0x09);
213 ak4642_write(codec, 0x0e, 0x19);
214 ak4642_write(codec, 0x09, 0x91);
215 ak4642_write(codec, 0x0c, 0x91);
216 ak4642_write(codec, 0x0a, 0x28);
217 ak4642_write(codec, 0x0d, 0x28);
218 ak4642_write(codec, 0x00, 0x64);
219 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP);
220 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN);
226 * Audio I/F Format:MSB justified (ADC & DAC)
229 * ALC setting:Refer to Table 35
232 * This operation came from example code of
233 * "ASAHI KASEI AK4642" (japanese) manual p94.
235 ak4642_write(codec, 0x02, 0x05);
236 ak4642_write(codec, 0x06, 0x3c);
237 ak4642_write(codec, 0x08, 0xe1);
238 ak4642_write(codec, 0x0b, 0x00);
239 ak4642_write(codec, 0x07, 0x21);
240 ak4642_write(codec, 0x00, 0x41);
241 ak4642_write(codec, 0x10, 0x01);
247 static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
248 struct snd_soc_dai *dai)
250 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
251 struct snd_soc_codec *codec = dai->codec;
254 /* stop headphone output */
255 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0);
256 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0);
257 ak4642_write(codec, 0x00, 0x40);
258 ak4642_write(codec, 0x0e, 0x11);
259 ak4642_write(codec, 0x0f, 0x08);
261 /* stop stereo input */
262 ak4642_write(codec, 0x00, 0x40);
263 ak4642_write(codec, 0x10, 0x00);
264 ak4642_write(codec, 0x07, 0x01);
268 static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
269 int clk_id, unsigned int freq, int dir)
271 struct snd_soc_codec *codec = codec_dai->codec;
285 pll = PLL2 | PLL1 | PLL0;
291 pll = PLL3 | PLL2 | PLL0;
296 snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
301 static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
303 struct snd_soc_codec *codec = dai->codec;
307 data = MCKO | PMPLL; /* use MCKO */
310 /* set master/slave audio interface */
311 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
312 case SND_SOC_DAIFMT_CBM_CFM:
316 case SND_SOC_DAIFMT_CBS_CFS:
321 snd_soc_update_bits(codec, PW_MGMT2, MS, data);
322 snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
327 static int ak4642_dai_hw_params(struct snd_pcm_substream *substream,
328 struct snd_pcm_hw_params *params,
329 struct snd_soc_dai *dai)
331 struct snd_soc_codec *codec = dai->codec;
334 switch (params_rate(params)) {
354 rate = FS2 | FS1 | FS0;
360 rate = FS3 | FS2 | FS1;
366 rate = FS3 | FS2 | FS1 | FS0;
369 rate = FS3 | FS1 | FS0;
375 snd_soc_update_bits(codec, MD_CTL2, FS_MASK, rate);
380 static struct snd_soc_dai_ops ak4642_dai_ops = {
381 .startup = ak4642_dai_startup,
382 .shutdown = ak4642_dai_shutdown,
383 .set_sysclk = ak4642_dai_set_sysclk,
384 .set_fmt = ak4642_dai_set_fmt,
385 .hw_params = ak4642_dai_hw_params,
388 struct snd_soc_dai ak4642_dai = {
391 .stream_name = "Playback",
394 .rates = SNDRV_PCM_RATE_8000_48000,
395 .formats = SNDRV_PCM_FMTBIT_S16_LE },
397 .stream_name = "Capture",
400 .rates = SNDRV_PCM_RATE_8000_48000,
401 .formats = SNDRV_PCM_FMTBIT_S16_LE },
402 .ops = &ak4642_dai_ops,
403 .symmetric_rates = 1,
405 EXPORT_SYMBOL_GPL(ak4642_dai);
407 static int ak4642_resume(struct platform_device *pdev)
409 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
410 struct snd_soc_codec *codec = socdev->card->codec;
417 * initialise the AK4642 driver
418 * register the mixer and dsp interfaces with the kernel
420 static int ak4642_init(struct ak4642_priv *ak4642)
422 struct snd_soc_codec *codec = &ak4642->codec;
426 dev_err(codec->dev, "Another ak4642 is registered\n");
430 mutex_init(&codec->mutex);
431 INIT_LIST_HEAD(&codec->dapm_widgets);
432 INIT_LIST_HEAD(&codec->dapm_paths);
434 snd_soc_codec_set_drvdata(codec, ak4642);
435 codec->name = "AK4642";
436 codec->owner = THIS_MODULE;
437 codec->read = ak4642_read_reg_cache;
438 codec->write = ak4642_write;
439 codec->dai = &ak4642_dai;
441 codec->hw_write = (hw_write_t)i2c_master_send;
442 codec->reg_cache_size = ARRAY_SIZE(ak4642_reg);
443 codec->reg_cache = kmemdup(ak4642_reg,
444 sizeof(ak4642_reg), GFP_KERNEL);
446 if (!codec->reg_cache)
449 ak4642_dai.dev = codec->dev;
450 ak4642_codec = codec;
452 ret = snd_soc_register_codec(codec);
454 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
458 ret = snd_soc_register_dai(&ak4642_dai);
460 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
461 snd_soc_unregister_codec(codec);
468 kfree(codec->reg_cache);
469 codec->reg_cache = NULL;
474 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
475 static int ak4642_i2c_probe(struct i2c_client *i2c,
476 const struct i2c_device_id *id)
478 struct ak4642_priv *ak4642;
479 struct snd_soc_codec *codec;
482 ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
486 codec = &ak4642->codec;
487 codec->dev = &i2c->dev;
489 i2c_set_clientdata(i2c, ak4642);
490 codec->control_data = i2c;
492 ret = ak4642_init(ak4642);
494 printk(KERN_ERR "failed to initialise AK4642\n");
499 static int ak4642_i2c_remove(struct i2c_client *client)
501 struct ak4642_priv *ak4642 = i2c_get_clientdata(client);
503 snd_soc_unregister_dai(&ak4642_dai);
504 snd_soc_unregister_codec(&ak4642->codec);
505 kfree(ak4642->codec.reg_cache);
512 static const struct i2c_device_id ak4642_i2c_id[] = {
517 MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
519 static struct i2c_driver ak4642_i2c_driver = {
521 .name = "AK4642 I2C Codec",
522 .owner = THIS_MODULE,
524 .probe = ak4642_i2c_probe,
525 .remove = ak4642_i2c_remove,
526 .id_table = ak4642_i2c_id,
531 static int ak4642_probe(struct platform_device *pdev)
533 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
537 dev_err(&pdev->dev, "Codec device not registered\n");
541 socdev->card->codec = ak4642_codec;
544 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
546 printk(KERN_ERR "ak4642: failed to create pcms\n");
550 dev_info(&pdev->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
558 /* power down chip */
559 static int ak4642_remove(struct platform_device *pdev)
561 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
563 snd_soc_free_pcms(socdev);
564 snd_soc_dapm_free(socdev);
569 struct snd_soc_codec_device soc_codec_dev_ak4642 = {
570 .probe = ak4642_probe,
571 .remove = ak4642_remove,
572 .resume = ak4642_resume,
574 EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642);
576 static int __init ak4642_modinit(void)
579 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
580 ret = i2c_add_driver(&ak4642_i2c_driver);
585 module_init(ak4642_modinit);
587 static void __exit ak4642_exit(void)
589 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
590 i2c_del_driver(&ak4642_i2c_driver);
594 module_exit(ak4642_exit);
596 MODULE_DESCRIPTION("Soc AK4642 driver");
597 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
598 MODULE_LICENSE("GPL");