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 <linux/slab.h>
33 #include <sound/core.h>
34 #include <sound/pcm.h>
35 #include <sound/pcm_params.h>
36 #include <sound/soc.h>
37 #include <sound/soc-dapm.h>
38 #include <sound/initval.h>
42 #define AK4642_VERSION "0.0.1"
82 #define AK4642_CACHEREGNUM 0x25
84 struct snd_soc_codec_device soc_codec_dev_ak4642
;
86 /* codec private data */
88 struct snd_soc_codec codec
;
92 static struct snd_soc_codec
*ak4642_codec
;
95 * ak4642 register cache
97 static const u16 ak4642_reg
[AK4642_CACHEREGNUM
] = {
98 0x0000, 0x0000, 0x0001, 0x0000,
99 0x0002, 0x0000, 0x0000, 0x0000,
100 0x00e1, 0x00e1, 0x0018, 0x0000,
101 0x00e1, 0x0018, 0x0011, 0x0008,
102 0x0000, 0x0000, 0x0000, 0x0000,
103 0x0000, 0x0000, 0x0000, 0x0000,
104 0x0000, 0x0000, 0x0000, 0x0000,
105 0x0000, 0x0000, 0x0000, 0x0000,
106 0x0000, 0x0000, 0x0000, 0x0000,
111 * read ak4642 register cache
113 static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec
*codec
,
116 u16
*cache
= codec
->reg_cache
;
117 if (reg
>= AK4642_CACHEREGNUM
)
123 * write ak4642 register cache
125 static inline void ak4642_write_reg_cache(struct snd_soc_codec
*codec
,
126 u16 reg
, unsigned int value
)
128 u16
*cache
= codec
->reg_cache
;
129 if (reg
>= AK4642_CACHEREGNUM
)
136 * write to the AK4642 register space
138 static int ak4642_write(struct snd_soc_codec
*codec
, unsigned int reg
,
144 * D15..D8 AK4642 register offset
145 * D7...D0 register data
147 data
[0] = reg
& 0xff;
148 data
[1] = value
& 0xff;
150 if (codec
->hw_write(codec
->control_data
, data
, 2) == 2) {
151 ak4642_write_reg_cache(codec
, reg
, value
);
157 static int ak4642_sync(struct snd_soc_codec
*codec
)
159 u16
*cache
= codec
->reg_cache
;
162 for (i
= 0; i
< AK4642_CACHEREGNUM
; i
++)
163 r
|= ak4642_write(codec
, i
, cache
[i
]);
168 static int ak4642_dai_startup(struct snd_pcm_substream
*substream
,
169 struct snd_soc_dai
*dai
)
171 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
172 struct snd_soc_codec
*codec
= dai
->codec
;
176 * start headphone output
179 * Audio I/F Format :MSB justified (ADC & DAC)
180 * Sampling Frequency: 44.1kHz
181 * Digital Volume: −8dB
182 * Bass Boost Level : Middle
184 * This operation came from example code of
185 * "ASAHI KASEI AK4642" (japanese) manual p97.
187 * Example code use 0x39, 0x79 value for 0x01 address,
188 * But we need MCKO (0x02) bit now
190 ak4642_write(codec
, 0x05, 0x27);
191 ak4642_write(codec
, 0x0f, 0x09);
192 ak4642_write(codec
, 0x0e, 0x19);
193 ak4642_write(codec
, 0x09, 0x91);
194 ak4642_write(codec
, 0x0c, 0x91);
195 ak4642_write(codec
, 0x0a, 0x28);
196 ak4642_write(codec
, 0x0d, 0x28);
197 ak4642_write(codec
, 0x00, 0x64);
198 ak4642_write(codec
, 0x01, 0x3b); /* + MCKO bit */
199 ak4642_write(codec
, 0x01, 0x7b); /* + MCKO bit */
205 * Audio I/F Format:MSB justified (ADC & DAC)
206 * Sampling Frequency:44.1kHz
209 * ALC setting:Refer to Table 35
212 * This operation came from example code of
213 * "ASAHI KASEI AK4642" (japanese) manual p94.
215 ak4642_write(codec
, 0x05, 0x27);
216 ak4642_write(codec
, 0x02, 0x05);
217 ak4642_write(codec
, 0x06, 0x3c);
218 ak4642_write(codec
, 0x08, 0xe1);
219 ak4642_write(codec
, 0x0b, 0x00);
220 ak4642_write(codec
, 0x07, 0x21);
221 ak4642_write(codec
, 0x00, 0x41);
222 ak4642_write(codec
, 0x10, 0x01);
228 static void ak4642_dai_shutdown(struct snd_pcm_substream
*substream
,
229 struct snd_soc_dai
*dai
)
231 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
232 struct snd_soc_codec
*codec
= dai
->codec
;
235 /* stop headphone output */
236 ak4642_write(codec
, 0x01, 0x3b);
237 ak4642_write(codec
, 0x01, 0x0b);
238 ak4642_write(codec
, 0x00, 0x40);
239 ak4642_write(codec
, 0x0e, 0x11);
240 ak4642_write(codec
, 0x0f, 0x08);
242 /* stop stereo input */
243 ak4642_write(codec
, 0x00, 0x40);
244 ak4642_write(codec
, 0x10, 0x00);
245 ak4642_write(codec
, 0x07, 0x01);
249 static int ak4642_dai_set_sysclk(struct snd_soc_dai
*codec_dai
,
250 int clk_id
, unsigned int freq
, int dir
)
252 struct snd_soc_codec
*codec
= codec_dai
->codec
;
253 struct ak4642_priv
*ak4642
= codec
->private_data
;
255 ak4642
->sysclk
= freq
;
259 static struct snd_soc_dai_ops ak4642_dai_ops
= {
260 .startup
= ak4642_dai_startup
,
261 .shutdown
= ak4642_dai_shutdown
,
262 .set_sysclk
= ak4642_dai_set_sysclk
,
265 struct snd_soc_dai ak4642_dai
= {
268 .stream_name
= "Playback",
271 .rates
= SNDRV_PCM_RATE_8000_48000
,
272 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
274 .stream_name
= "Capture",
277 .rates
= SNDRV_PCM_RATE_8000_48000
,
278 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
279 .ops
= &ak4642_dai_ops
,
281 EXPORT_SYMBOL_GPL(ak4642_dai
);
283 static int ak4642_resume(struct platform_device
*pdev
)
285 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
286 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
293 * initialise the AK4642 driver
294 * register the mixer and dsp interfaces with the kernel
296 static int ak4642_init(struct ak4642_priv
*ak4642
)
298 struct snd_soc_codec
*codec
= &ak4642
->codec
;
302 dev_err(codec
->dev
, "Another ak4642 is registered\n");
306 mutex_init(&codec
->mutex
);
307 INIT_LIST_HEAD(&codec
->dapm_widgets
);
308 INIT_LIST_HEAD(&codec
->dapm_paths
);
310 codec
->private_data
= ak4642
;
311 codec
->name
= "AK4642";
312 codec
->owner
= THIS_MODULE
;
313 codec
->read
= ak4642_read_reg_cache
;
314 codec
->write
= ak4642_write
;
315 codec
->dai
= &ak4642_dai
;
317 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
318 codec
->reg_cache_size
= ARRAY_SIZE(ak4642_reg
);
319 codec
->reg_cache
= kmemdup(ak4642_reg
,
320 sizeof(ak4642_reg
), GFP_KERNEL
);
322 if (!codec
->reg_cache
)
325 ak4642_dai
.dev
= codec
->dev
;
326 ak4642_codec
= codec
;
328 ret
= snd_soc_register_codec(codec
);
330 dev_err(codec
->dev
, "Failed to register codec: %d\n", ret
);
334 ret
= snd_soc_register_dai(&ak4642_dai
);
336 dev_err(codec
->dev
, "Failed to register DAI: %d\n", ret
);
337 snd_soc_unregister_codec(codec
);
344 * Audio I/F Format: MSB justified (ADC & DAC)
345 * BICK frequency at Master Mode: 64fs
346 * Input Master Clock Select at PLL Mode: 11.2896MHz
348 * Sampling Frequency: 44.1kHz
350 * This operation came from example code of
351 * "ASAHI KASEI AK4642" (japanese) manual p89.
355 ak4642_write(codec
, 0x01, 0x08);
356 ak4642_write(codec
, 0x04, 0x4a);
357 ak4642_write(codec
, 0x05, 0x27);
358 ak4642_write(codec
, 0x00, 0x40);
359 ak4642_write(codec
, 0x01, 0x0b);
364 kfree(codec
->reg_cache
);
365 codec
->reg_cache
= NULL
;
370 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
371 static int ak4642_i2c_probe(struct i2c_client
*i2c
,
372 const struct i2c_device_id
*id
)
374 struct ak4642_priv
*ak4642
;
375 struct snd_soc_codec
*codec
;
378 ak4642
= kzalloc(sizeof(struct ak4642_priv
), GFP_KERNEL
);
382 codec
= &ak4642
->codec
;
383 codec
->dev
= &i2c
->dev
;
385 i2c_set_clientdata(i2c
, ak4642
);
386 codec
->control_data
= i2c
;
388 ret
= ak4642_init(ak4642
);
390 printk(KERN_ERR
"failed to initialise AK4642\n");
395 static int ak4642_i2c_remove(struct i2c_client
*client
)
397 struct ak4642_priv
*ak4642
= i2c_get_clientdata(client
);
399 snd_soc_unregister_dai(&ak4642_dai
);
400 snd_soc_unregister_codec(&ak4642
->codec
);
401 kfree(ak4642
->codec
.reg_cache
);
408 static const struct i2c_device_id ak4642_i2c_id
[] = {
413 MODULE_DEVICE_TABLE(i2c
, ak4642_i2c_id
);
415 static struct i2c_driver ak4642_i2c_driver
= {
417 .name
= "AK4642 I2C Codec",
418 .owner
= THIS_MODULE
,
420 .probe
= ak4642_i2c_probe
,
421 .remove
= ak4642_i2c_remove
,
422 .id_table
= ak4642_i2c_id
,
427 static int ak4642_probe(struct platform_device
*pdev
)
429 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
433 dev_err(&pdev
->dev
, "Codec device not registered\n");
437 socdev
->card
->codec
= ak4642_codec
;
440 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
442 printk(KERN_ERR
"ak4642: failed to create pcms\n");
446 dev_info(&pdev
->dev
, "AK4642 Audio Codec %s", AK4642_VERSION
);
454 /* power down chip */
455 static int ak4642_remove(struct platform_device
*pdev
)
457 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
459 snd_soc_free_pcms(socdev
);
460 snd_soc_dapm_free(socdev
);
465 struct snd_soc_codec_device soc_codec_dev_ak4642
= {
466 .probe
= ak4642_probe
,
467 .remove
= ak4642_remove
,
468 .resume
= ak4642_resume
,
470 EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642
);
472 static int __init
ak4642_modinit(void)
475 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
476 ret
= i2c_add_driver(&ak4642_i2c_driver
);
481 module_init(ak4642_modinit
);
483 static void __exit
ak4642_exit(void)
485 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
486 i2c_del_driver(&ak4642_i2c_driver
);
490 module_exit(ak4642_exit
);
492 MODULE_DESCRIPTION("Soc AK4642 driver");
493 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
494 MODULE_LICENSE("GPL");