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
83 struct snd_soc_codec_device soc_codec_dev_ak4642
;
85 /* codec private data */
87 struct snd_soc_codec codec
;
91 static struct snd_soc_codec
*ak4642_codec
;
94 * ak4642 register cache
96 static const u16 ak4642_reg
[AK4642_CACHEREGNUM
] = {
97 0x0000, 0x0000, 0x0001, 0x0000,
98 0x0002, 0x0000, 0x0000, 0x0000,
99 0x00e1, 0x00e1, 0x0018, 0x0000,
100 0x00e1, 0x0018, 0x0011, 0x0008,
101 0x0000, 0x0000, 0x0000, 0x0000,
102 0x0000, 0x0000, 0x0000, 0x0000,
103 0x0000, 0x0000, 0x0000, 0x0000,
104 0x0000, 0x0000, 0x0000, 0x0000,
105 0x0000, 0x0000, 0x0000, 0x0000,
110 * read ak4642 register cache
112 static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec
*codec
,
115 u16
*cache
= codec
->reg_cache
;
116 if (reg
>= AK4642_CACHEREGNUM
)
122 * write ak4642 register cache
124 static inline void ak4642_write_reg_cache(struct snd_soc_codec
*codec
,
125 u16 reg
, unsigned int value
)
127 u16
*cache
= codec
->reg_cache
;
128 if (reg
>= AK4642_CACHEREGNUM
)
135 * write to the AK4642 register space
137 static int ak4642_write(struct snd_soc_codec
*codec
, unsigned int reg
,
143 * D15..D8 AK4642 register offset
144 * D7...D0 register data
146 data
[0] = reg
& 0xff;
147 data
[1] = value
& 0xff;
149 if (codec
->hw_write(codec
->control_data
, data
, 2) == 2) {
150 ak4642_write_reg_cache(codec
, reg
, value
);
156 static int ak4642_sync(struct snd_soc_codec
*codec
)
158 u16
*cache
= codec
->reg_cache
;
161 for (i
= 0; i
< AK4642_CACHEREGNUM
; i
++)
162 r
|= ak4642_write(codec
, i
, cache
[i
]);
167 static int ak4642_dai_startup(struct snd_pcm_substream
*substream
,
168 struct snd_soc_dai
*dai
)
170 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
171 struct snd_soc_codec
*codec
= dai
->codec
;
175 * start headphone output
178 * Audio I/F Format :MSB justified (ADC & DAC)
179 * Sampling Frequency: 44.1kHz
180 * Digital Volume: −8dB
181 * Bass Boost Level : Middle
183 * This operation came from example code of
184 * "ASAHI KASEI AK4642" (japanese) manual p97.
186 * Example code use 0x39, 0x79 value for 0x01 address,
187 * But we need MCKO (0x02) bit now
189 ak4642_write(codec
, 0x05, 0x27);
190 ak4642_write(codec
, 0x0f, 0x09);
191 ak4642_write(codec
, 0x0e, 0x19);
192 ak4642_write(codec
, 0x09, 0x91);
193 ak4642_write(codec
, 0x0c, 0x91);
194 ak4642_write(codec
, 0x0a, 0x28);
195 ak4642_write(codec
, 0x0d, 0x28);
196 ak4642_write(codec
, 0x00, 0x64);
197 ak4642_write(codec
, 0x01, 0x3b); /* + MCKO bit */
198 ak4642_write(codec
, 0x01, 0x7b); /* + MCKO bit */
204 * Audio I/F Format:MSB justified (ADC & DAC)
205 * Sampling Frequency:44.1kHz
208 * ALC setting:Refer to Table 35
211 * This operation came from example code of
212 * "ASAHI KASEI AK4642" (japanese) manual p94.
214 ak4642_write(codec
, 0x05, 0x27);
215 ak4642_write(codec
, 0x02, 0x05);
216 ak4642_write(codec
, 0x06, 0x3c);
217 ak4642_write(codec
, 0x08, 0xe1);
218 ak4642_write(codec
, 0x0b, 0x00);
219 ak4642_write(codec
, 0x07, 0x21);
220 ak4642_write(codec
, 0x00, 0x41);
221 ak4642_write(codec
, 0x10, 0x01);
227 static void ak4642_dai_shutdown(struct snd_pcm_substream
*substream
,
228 struct snd_soc_dai
*dai
)
230 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
231 struct snd_soc_codec
*codec
= dai
->codec
;
234 /* stop headphone output */
235 ak4642_write(codec
, 0x01, 0x3b);
236 ak4642_write(codec
, 0x01, 0x0b);
237 ak4642_write(codec
, 0x00, 0x40);
238 ak4642_write(codec
, 0x0e, 0x11);
239 ak4642_write(codec
, 0x0f, 0x08);
241 /* stop stereo input */
242 ak4642_write(codec
, 0x00, 0x40);
243 ak4642_write(codec
, 0x10, 0x00);
244 ak4642_write(codec
, 0x07, 0x01);
248 static int ak4642_dai_set_sysclk(struct snd_soc_dai
*codec_dai
,
249 int clk_id
, unsigned int freq
, int dir
)
251 struct snd_soc_codec
*codec
= codec_dai
->codec
;
252 struct ak4642_priv
*ak4642
= codec
->private_data
;
254 ak4642
->sysclk
= freq
;
258 static struct snd_soc_dai_ops ak4642_dai_ops
= {
259 .startup
= ak4642_dai_startup
,
260 .shutdown
= ak4642_dai_shutdown
,
261 .set_sysclk
= ak4642_dai_set_sysclk
,
264 struct snd_soc_dai ak4642_dai
= {
267 .stream_name
= "Playback",
270 .rates
= SNDRV_PCM_RATE_8000_48000
,
271 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
273 .stream_name
= "Capture",
276 .rates
= SNDRV_PCM_RATE_8000_48000
,
277 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
278 .ops
= &ak4642_dai_ops
,
280 EXPORT_SYMBOL_GPL(ak4642_dai
);
282 static int ak4642_resume(struct platform_device
*pdev
)
284 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
285 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
292 * initialise the AK4642 driver
293 * register the mixer and dsp interfaces with the kernel
295 static int ak4642_init(struct ak4642_priv
*ak4642
)
297 struct snd_soc_codec
*codec
= &ak4642
->codec
;
301 dev_err(codec
->dev
, "Another ak4642 is registered\n");
305 mutex_init(&codec
->mutex
);
306 INIT_LIST_HEAD(&codec
->dapm_widgets
);
307 INIT_LIST_HEAD(&codec
->dapm_paths
);
309 codec
->private_data
= ak4642
;
310 codec
->name
= "AK4642";
311 codec
->owner
= THIS_MODULE
;
312 codec
->read
= ak4642_read_reg_cache
;
313 codec
->write
= ak4642_write
;
314 codec
->dai
= &ak4642_dai
;
316 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
317 codec
->reg_cache_size
= ARRAY_SIZE(ak4642_reg
);
318 codec
->reg_cache
= kmemdup(ak4642_reg
,
319 sizeof(ak4642_reg
), GFP_KERNEL
);
321 if (!codec
->reg_cache
)
324 ak4642_dai
.dev
= codec
->dev
;
325 ak4642_codec
= codec
;
327 ret
= snd_soc_register_codec(codec
);
329 dev_err(codec
->dev
, "Failed to register codec: %d\n", ret
);
333 ret
= snd_soc_register_dai(&ak4642_dai
);
335 dev_err(codec
->dev
, "Failed to register DAI: %d\n", ret
);
336 snd_soc_unregister_codec(codec
);
343 * Audio I/F Format: MSB justified (ADC & DAC)
344 * BICK frequency at Master Mode: 64fs
345 * Input Master Clock Select at PLL Mode: 11.2896MHz
347 * Sampling Frequency: 44.1kHz
349 * This operation came from example code of
350 * "ASAHI KASEI AK4642" (japanese) manual p89.
354 ak4642_write(codec
, 0x01, 0x08);
355 ak4642_write(codec
, 0x04, 0x4a);
356 ak4642_write(codec
, 0x05, 0x27);
357 ak4642_write(codec
, 0x00, 0x40);
358 ak4642_write(codec
, 0x01, 0x0b);
363 kfree(codec
->reg_cache
);
364 codec
->reg_cache
= NULL
;
369 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
370 static int ak4642_i2c_probe(struct i2c_client
*i2c
,
371 const struct i2c_device_id
*id
)
373 struct ak4642_priv
*ak4642
;
374 struct snd_soc_codec
*codec
;
377 ak4642
= kzalloc(sizeof(struct ak4642_priv
), GFP_KERNEL
);
381 codec
= &ak4642
->codec
;
382 codec
->dev
= &i2c
->dev
;
384 i2c_set_clientdata(i2c
, ak4642
);
385 codec
->control_data
= i2c
;
387 ret
= ak4642_init(ak4642
);
389 printk(KERN_ERR
"failed to initialise AK4642\n");
394 static int ak4642_i2c_remove(struct i2c_client
*client
)
396 struct ak4642_priv
*ak4642
= i2c_get_clientdata(client
);
398 snd_soc_unregister_dai(&ak4642_dai
);
399 snd_soc_unregister_codec(&ak4642
->codec
);
400 kfree(ak4642
->codec
.reg_cache
);
407 static const struct i2c_device_id ak4642_i2c_id
[] = {
412 MODULE_DEVICE_TABLE(i2c
, ak4642_i2c_id
);
414 static struct i2c_driver ak4642_i2c_driver
= {
416 .name
= "AK4642 I2C Codec",
417 .owner
= THIS_MODULE
,
419 .probe
= ak4642_i2c_probe
,
420 .remove
= ak4642_i2c_remove
,
421 .id_table
= ak4642_i2c_id
,
426 static int ak4642_probe(struct platform_device
*pdev
)
428 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
432 dev_err(&pdev
->dev
, "Codec device not registered\n");
436 socdev
->card
->codec
= ak4642_codec
;
439 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
441 printk(KERN_ERR
"ak4642: failed to create pcms\n");
445 ret
= snd_soc_init_card(socdev
);
447 printk(KERN_ERR
"ak4642: failed to register card\n");
451 dev_info(&pdev
->dev
, "AK4642 Audio Codec %s", AK4642_VERSION
);
455 snd_soc_free_pcms(socdev
);
456 snd_soc_dapm_free(socdev
);
462 /* power down chip */
463 static int ak4642_remove(struct platform_device
*pdev
)
465 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
467 snd_soc_free_pcms(socdev
);
468 snd_soc_dapm_free(socdev
);
473 struct snd_soc_codec_device soc_codec_dev_ak4642
= {
474 .probe
= ak4642_probe
,
475 .remove
= ak4642_remove
,
476 .resume
= ak4642_resume
,
478 EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642
);
480 static int __init
ak4642_modinit(void)
483 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
484 ret
= i2c_add_driver(&ak4642_i2c_driver
);
489 module_init(ak4642_modinit
);
491 static void __exit
ak4642_exit(void)
493 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
494 i2c_del_driver(&ak4642_i2c_driver
);
498 module_exit(ak4642_exit
);
500 MODULE_DESCRIPTION("Soc AK4642 driver");
501 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
502 MODULE_LICENSE("GPL");