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/delay.h>
26 #include <linux/i2c.h>
27 #include <linux/platform_device.h>
28 #include <linux/slab.h>
29 #include <sound/soc-dapm.h>
30 #include <sound/initval.h>
31 #include <sound/tlv.h>
33 #define AK4642_VERSION "0.0.1"
73 #define AK4642_CACHEREGNUM 0x25
76 #define HPMTN (1 << 6)
77 #define PMHPL (1 << 5)
78 #define PMHPR (1 << 4)
79 #define MS (1 << 3) /* master/slave select */
81 #define PMPLL (1 << 0)
83 #define PMHP_MASK (PMHPL | PMHPR)
84 #define PMHP PMHP_MASK
91 #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
93 #define BCKO_MASK (1 << 3)
94 #define BCKO_64 BCKO_MASK
101 #define FS_MASK (FS0 | FS1 | FS2 | FS3)
105 * Playback Volume (table 39)
107 * max : 0x00 : +12.0 dB
109 * min : 0xFE : -115.0 dB
112 static const DECLARE_TLV_DB_SCALE(out_tlv, -11500, 50, 1);
114 static const struct snd_kcontrol_new ak4642_snd_controls[] = {
116 SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC, R_DVC,
117 0, 0xFF, 1, out_tlv),
121 /* codec private data */
124 enum snd_soc_control_type control_type;
129 * ak4642 register cache
131 static const u16 ak4642_reg[AK4642_CACHEREGNUM] = {
132 0x0000, 0x0000, 0x0001, 0x0000,
133 0x0002, 0x0000, 0x0000, 0x0000,
134 0x00e1, 0x00e1, 0x0018, 0x0000,
135 0x00e1, 0x0018, 0x0011, 0x0008,
136 0x0000, 0x0000, 0x0000, 0x0000,
137 0x0000, 0x0000, 0x0000, 0x0000,
138 0x0000, 0x0000, 0x0000, 0x0000,
139 0x0000, 0x0000, 0x0000, 0x0000,
140 0x0000, 0x0000, 0x0000, 0x0000,
145 * read ak4642 register cache
147 static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec *codec,
150 u16 *cache = codec->reg_cache;
151 if (reg >= AK4642_CACHEREGNUM)
157 * write ak4642 register cache
159 static inline void ak4642_write_reg_cache(struct snd_soc_codec *codec,
160 u16 reg, unsigned int value)
162 u16 *cache = codec->reg_cache;
163 if (reg >= AK4642_CACHEREGNUM)
170 * write to the AK4642 register space
172 static int ak4642_write(struct snd_soc_codec *codec, unsigned int reg,
178 * D15..D8 AK4642 register offset
179 * D7...D0 register data
181 data[0] = reg & 0xff;
182 data[1] = value & 0xff;
184 if (codec->hw_write(codec->control_data, data, 2) == 2) {
185 ak4642_write_reg_cache(codec, reg, value);
191 static int ak4642_sync(struct snd_soc_codec *codec)
193 u16 *cache = codec->reg_cache;
196 for (i = 0; i < AK4642_CACHEREGNUM; i++)
197 r |= ak4642_write(codec, i, cache[i]);
202 static int ak4642_dai_startup(struct snd_pcm_substream *substream,
203 struct snd_soc_dai *dai)
205 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
206 struct snd_soc_codec *codec = dai->codec;
210 * start headphone output
213 * Audio I/F Format :MSB justified (ADC & DAC)
214 * Bass Boost Level : Middle
216 * This operation came from example code of
217 * "ASAHI KASEI AK4642" (japanese) manual p97.
219 ak4642_write(codec, 0x0f, 0x09);
220 ak4642_write(codec, 0x0e, 0x19);
221 ak4642_write(codec, 0x09, 0x91);
222 ak4642_write(codec, 0x0c, 0x91);
223 ak4642_write(codec, 0x00, 0x64);
224 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, PMHP);
225 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, HPMTN);
231 * Audio I/F Format:MSB justified (ADC & DAC)
234 * ALC setting:Refer to Table 35
237 * This operation came from example code of
238 * "ASAHI KASEI AK4642" (japanese) manual p94.
240 ak4642_write(codec, 0x02, 0x05);
241 ak4642_write(codec, 0x06, 0x3c);
242 ak4642_write(codec, 0x08, 0xe1);
243 ak4642_write(codec, 0x0b, 0x00);
244 ak4642_write(codec, 0x07, 0x21);
245 ak4642_write(codec, 0x00, 0x41);
246 ak4642_write(codec, 0x10, 0x01);
252 static void ak4642_dai_shutdown(struct snd_pcm_substream *substream,
253 struct snd_soc_dai *dai)
255 int is_play = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
256 struct snd_soc_codec *codec = dai->codec;
259 /* stop headphone output */
260 snd_soc_update_bits(codec, PW_MGMT2, HPMTN, 0);
261 snd_soc_update_bits(codec, PW_MGMT2, PMHP_MASK, 0);
262 ak4642_write(codec, 0x00, 0x40);
263 ak4642_write(codec, 0x0e, 0x11);
264 ak4642_write(codec, 0x0f, 0x08);
266 /* stop stereo input */
267 ak4642_write(codec, 0x00, 0x40);
268 ak4642_write(codec, 0x10, 0x00);
269 ak4642_write(codec, 0x07, 0x01);
273 static int ak4642_dai_set_sysclk(struct snd_soc_dai *codec_dai,
274 int clk_id, unsigned int freq, int dir)
276 struct snd_soc_codec *codec = codec_dai->codec;
290 pll = PLL2 | PLL1 | PLL0;
296 pll = PLL3 | PLL2 | PLL0;
301 snd_soc_update_bits(codec, MD_CTL1, PLL_MASK, pll);
306 static int ak4642_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
308 struct snd_soc_codec *codec = dai->codec;
312 data = MCKO | PMPLL; /* use MCKO */
315 /* set master/slave audio interface */
316 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
317 case SND_SOC_DAIFMT_CBM_CFM:
321 case SND_SOC_DAIFMT_CBS_CFS:
326 snd_soc_update_bits(codec, PW_MGMT2, MS, data);
327 snd_soc_update_bits(codec, MD_CTL1, BCKO_MASK, bcko);
332 static int ak4642_dai_hw_params(struct snd_pcm_substream *substream,
333 struct snd_pcm_hw_params *params,
334 struct snd_soc_dai *dai)
336 struct snd_soc_codec *codec = dai->codec;
339 switch (params_rate(params)) {
359 rate = FS2 | FS1 | FS0;
365 rate = FS3 | FS2 | FS1;
371 rate = FS3 | FS2 | FS1 | FS0;
374 rate = FS3 | FS1 | FS0;
380 snd_soc_update_bits(codec, MD_CTL2, FS_MASK, rate);
385 static struct snd_soc_dai_ops ak4642_dai_ops = {
386 .startup = ak4642_dai_startup,
387 .shutdown = ak4642_dai_shutdown,
388 .set_sysclk = ak4642_dai_set_sysclk,
389 .set_fmt = ak4642_dai_set_fmt,
390 .hw_params = ak4642_dai_hw_params,
393 static struct snd_soc_dai_driver ak4642_dai = {
394 .name = "ak4642-hifi",
396 .stream_name = "Playback",
399 .rates = SNDRV_PCM_RATE_8000_48000,
400 .formats = SNDRV_PCM_FMTBIT_S16_LE },
402 .stream_name = "Capture",
405 .rates = SNDRV_PCM_RATE_8000_48000,
406 .formats = SNDRV_PCM_FMTBIT_S16_LE },
407 .ops = &ak4642_dai_ops,
408 .symmetric_rates = 1,
411 static int ak4642_resume(struct snd_soc_codec *codec)
418 static int ak4642_probe(struct snd_soc_codec *codec)
420 struct ak4642_priv *ak4642 = snd_soc_codec_get_drvdata(codec);
422 dev_info(codec->dev, "AK4642 Audio Codec %s", AK4642_VERSION);
424 codec->hw_write = (hw_write_t)i2c_master_send;
425 codec->control_data = ak4642->control_data;
427 snd_soc_add_controls(codec, ak4642_snd_controls,
428 ARRAY_SIZE(ak4642_snd_controls));
433 static struct snd_soc_codec_driver soc_codec_dev_ak4642 = {
434 .probe = ak4642_probe,
435 .resume = ak4642_resume,
436 .read = ak4642_read_reg_cache,
437 .write = ak4642_write,
438 .reg_cache_size = ARRAY_SIZE(ak4642_reg),
439 .reg_word_size = sizeof(u8),
440 .reg_cache_default = ak4642_reg,
443 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
444 static __devinit int ak4642_i2c_probe(struct i2c_client *i2c,
445 const struct i2c_device_id *id)
447 struct ak4642_priv *ak4642;
450 ak4642 = kzalloc(sizeof(struct ak4642_priv), GFP_KERNEL);
454 i2c_set_clientdata(i2c, ak4642);
455 ak4642->control_data = i2c;
456 ak4642->control_type = SND_SOC_I2C;
458 ret = snd_soc_register_codec(&i2c->dev,
459 &soc_codec_dev_ak4642, &ak4642_dai, 1);
465 static __devexit int ak4642_i2c_remove(struct i2c_client *client)
467 snd_soc_unregister_codec(&client->dev);
468 kfree(i2c_get_clientdata(client));
472 static const struct i2c_device_id ak4642_i2c_id[] = {
477 MODULE_DEVICE_TABLE(i2c, ak4642_i2c_id);
479 static struct i2c_driver ak4642_i2c_driver = {
481 .name = "ak4642-codec",
482 .owner = THIS_MODULE,
484 .probe = ak4642_i2c_probe,
485 .remove = __devexit_p(ak4642_i2c_remove),
486 .id_table = ak4642_i2c_id,
490 static int __init ak4642_modinit(void)
493 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
494 ret = i2c_add_driver(&ak4642_i2c_driver);
499 module_init(ak4642_modinit);
501 static void __exit ak4642_exit(void)
503 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
504 i2c_del_driver(&ak4642_i2c_driver);
508 module_exit(ak4642_exit);
510 MODULE_DESCRIPTION("Soc AK4642 driver");
511 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
512 MODULE_LICENSE("GPL");