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
85 #define HPMTN (1 << 6)
86 #define PMHPL (1 << 5)
87 #define PMHPR (1 << 4)
88 #define MS (1 << 3) /* master/slave select */
90 #define PMPLL (1 << 0)
92 #define PMHP_MASK (PMHPL | PMHPR)
93 #define PMHP PMHP_MASK
100 #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
102 #define BCKO_MASK (1 << 3)
103 #define BCKO_64 BCKO_MASK
110 #define FS_MASK (FS0 | FS1 | FS2 | FS3)
112 struct snd_soc_codec_device soc_codec_dev_ak4642
;
114 /* codec private data */
116 struct snd_soc_codec codec
;
119 static struct snd_soc_codec
*ak4642_codec
;
122 * ak4642 register cache
124 static const u16 ak4642_reg
[AK4642_CACHEREGNUM
] = {
125 0x0000, 0x0000, 0x0001, 0x0000,
126 0x0002, 0x0000, 0x0000, 0x0000,
127 0x00e1, 0x00e1, 0x0018, 0x0000,
128 0x00e1, 0x0018, 0x0011, 0x0008,
129 0x0000, 0x0000, 0x0000, 0x0000,
130 0x0000, 0x0000, 0x0000, 0x0000,
131 0x0000, 0x0000, 0x0000, 0x0000,
132 0x0000, 0x0000, 0x0000, 0x0000,
133 0x0000, 0x0000, 0x0000, 0x0000,
138 * read ak4642 register cache
140 static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec
*codec
,
143 u16
*cache
= codec
->reg_cache
;
144 if (reg
>= AK4642_CACHEREGNUM
)
150 * write ak4642 register cache
152 static inline void ak4642_write_reg_cache(struct snd_soc_codec
*codec
,
153 u16 reg
, unsigned int value
)
155 u16
*cache
= codec
->reg_cache
;
156 if (reg
>= AK4642_CACHEREGNUM
)
163 * write to the AK4642 register space
165 static int ak4642_write(struct snd_soc_codec
*codec
, unsigned int reg
,
171 * D15..D8 AK4642 register offset
172 * D7...D0 register data
174 data
[0] = reg
& 0xff;
175 data
[1] = value
& 0xff;
177 if (codec
->hw_write(codec
->control_data
, data
, 2) == 2) {
178 ak4642_write_reg_cache(codec
, reg
, value
);
184 static int ak4642_sync(struct snd_soc_codec
*codec
)
186 u16
*cache
= codec
->reg_cache
;
189 for (i
= 0; i
< AK4642_CACHEREGNUM
; i
++)
190 r
|= ak4642_write(codec
, i
, cache
[i
]);
195 static int ak4642_dai_startup(struct snd_pcm_substream
*substream
,
196 struct snd_soc_dai
*dai
)
198 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
199 struct snd_soc_codec
*codec
= dai
->codec
;
203 * start headphone output
206 * Audio I/F Format :MSB justified (ADC & DAC)
207 * Digital Volume: -8dB
208 * Bass Boost Level : Middle
210 * This operation came from example code of
211 * "ASAHI KASEI AK4642" (japanese) manual p97.
213 ak4642_write(codec
, 0x0f, 0x09);
214 ak4642_write(codec
, 0x0e, 0x19);
215 ak4642_write(codec
, 0x09, 0x91);
216 ak4642_write(codec
, 0x0c, 0x91);
217 ak4642_write(codec
, 0x0a, 0x28);
218 ak4642_write(codec
, 0x0d, 0x28);
219 ak4642_write(codec
, 0x00, 0x64);
220 snd_soc_update_bits(codec
, PW_MGMT2
, PMHP_MASK
, PMHP
);
221 snd_soc_update_bits(codec
, PW_MGMT2
, HPMTN
, HPMTN
);
227 * Audio I/F Format:MSB justified (ADC & DAC)
230 * ALC setting:Refer to Table 35
233 * This operation came from example code of
234 * "ASAHI KASEI AK4642" (japanese) manual p94.
236 ak4642_write(codec
, 0x02, 0x05);
237 ak4642_write(codec
, 0x06, 0x3c);
238 ak4642_write(codec
, 0x08, 0xe1);
239 ak4642_write(codec
, 0x0b, 0x00);
240 ak4642_write(codec
, 0x07, 0x21);
241 ak4642_write(codec
, 0x00, 0x41);
242 ak4642_write(codec
, 0x10, 0x01);
248 static void ak4642_dai_shutdown(struct snd_pcm_substream
*substream
,
249 struct snd_soc_dai
*dai
)
251 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
252 struct snd_soc_codec
*codec
= dai
->codec
;
255 /* stop headphone output */
256 snd_soc_update_bits(codec
, PW_MGMT2
, HPMTN
, 0);
257 snd_soc_update_bits(codec
, PW_MGMT2
, PMHP_MASK
, 0);
258 ak4642_write(codec
, 0x00, 0x40);
259 ak4642_write(codec
, 0x0e, 0x11);
260 ak4642_write(codec
, 0x0f, 0x08);
262 /* stop stereo input */
263 ak4642_write(codec
, 0x00, 0x40);
264 ak4642_write(codec
, 0x10, 0x00);
265 ak4642_write(codec
, 0x07, 0x01);
269 static int ak4642_dai_set_sysclk(struct snd_soc_dai
*codec_dai
,
270 int clk_id
, unsigned int freq
, int dir
)
272 struct snd_soc_codec
*codec
= codec_dai
->codec
;
286 pll
= PLL2
| PLL1
| PLL0
;
292 pll
= PLL3
| PLL2
| PLL0
;
297 snd_soc_update_bits(codec
, MD_CTL1
, PLL_MASK
, pll
);
302 static int ak4642_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
304 struct snd_soc_codec
*codec
= dai
->codec
;
308 data
= MCKO
| PMPLL
; /* use MCKO */
311 /* set master/slave audio interface */
312 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
313 case SND_SOC_DAIFMT_CBM_CFM
:
317 case SND_SOC_DAIFMT_CBS_CFS
:
322 snd_soc_update_bits(codec
, PW_MGMT2
, MS
, data
);
323 snd_soc_update_bits(codec
, MD_CTL1
, BCKO_MASK
, bcko
);
328 static int ak4642_dai_hw_params(struct snd_pcm_substream
*substream
,
329 struct snd_pcm_hw_params
*params
,
330 struct snd_soc_dai
*dai
)
332 struct snd_soc_codec
*codec
= dai
->codec
;
335 switch (params_rate(params
)) {
355 rate
= FS2
| FS1
| FS0
;
361 rate
= FS3
| FS2
| FS1
;
367 rate
= FS3
| FS2
| FS1
| FS0
;
370 rate
= FS3
| FS1
| FS0
;
376 snd_soc_update_bits(codec
, MD_CTL2
, FS_MASK
, rate
);
381 static struct snd_soc_dai_ops ak4642_dai_ops
= {
382 .startup
= ak4642_dai_startup
,
383 .shutdown
= ak4642_dai_shutdown
,
384 .set_sysclk
= ak4642_dai_set_sysclk
,
385 .set_fmt
= ak4642_dai_set_fmt
,
386 .hw_params
= ak4642_dai_hw_params
,
389 struct snd_soc_dai ak4642_dai
= {
392 .stream_name
= "Playback",
395 .rates
= SNDRV_PCM_RATE_8000_48000
,
396 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
398 .stream_name
= "Capture",
401 .rates
= SNDRV_PCM_RATE_8000_48000
,
402 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
403 .ops
= &ak4642_dai_ops
,
404 .symmetric_rates
= 1,
406 EXPORT_SYMBOL_GPL(ak4642_dai
);
408 static int ak4642_resume(struct platform_device
*pdev
)
410 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
411 struct snd_soc_codec
*codec
= socdev
->card
->codec
;
418 * initialise the AK4642 driver
419 * register the mixer and dsp interfaces with the kernel
421 static int ak4642_init(struct ak4642_priv
*ak4642
)
423 struct snd_soc_codec
*codec
= &ak4642
->codec
;
427 dev_err(codec
->dev
, "Another ak4642 is registered\n");
431 mutex_init(&codec
->mutex
);
432 INIT_LIST_HEAD(&codec
->dapm_widgets
);
433 INIT_LIST_HEAD(&codec
->dapm_paths
);
435 snd_soc_codec_set_drvdata(codec
, ak4642
);
436 codec
->name
= "AK4642";
437 codec
->owner
= THIS_MODULE
;
438 codec
->read
= ak4642_read_reg_cache
;
439 codec
->write
= ak4642_write
;
440 codec
->dai
= &ak4642_dai
;
442 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
443 codec
->reg_cache_size
= ARRAY_SIZE(ak4642_reg
);
444 codec
->reg_cache
= kmemdup(ak4642_reg
,
445 sizeof(ak4642_reg
), GFP_KERNEL
);
447 if (!codec
->reg_cache
)
450 ak4642_dai
.dev
= codec
->dev
;
451 ak4642_codec
= codec
;
453 ret
= snd_soc_register_codec(codec
);
455 dev_err(codec
->dev
, "Failed to register codec: %d\n", ret
);
459 ret
= snd_soc_register_dai(&ak4642_dai
);
461 dev_err(codec
->dev
, "Failed to register DAI: %d\n", ret
);
462 snd_soc_unregister_codec(codec
);
469 kfree(codec
->reg_cache
);
470 codec
->reg_cache
= NULL
;
475 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
476 static int ak4642_i2c_probe(struct i2c_client
*i2c
,
477 const struct i2c_device_id
*id
)
479 struct ak4642_priv
*ak4642
;
480 struct snd_soc_codec
*codec
;
483 ak4642
= kzalloc(sizeof(struct ak4642_priv
), GFP_KERNEL
);
487 codec
= &ak4642
->codec
;
488 codec
->dev
= &i2c
->dev
;
490 i2c_set_clientdata(i2c
, ak4642
);
491 codec
->control_data
= i2c
;
493 ret
= ak4642_init(ak4642
);
495 printk(KERN_ERR
"failed to initialise AK4642\n");
500 static int ak4642_i2c_remove(struct i2c_client
*client
)
502 struct ak4642_priv
*ak4642
= i2c_get_clientdata(client
);
504 snd_soc_unregister_dai(&ak4642_dai
);
505 snd_soc_unregister_codec(&ak4642
->codec
);
506 kfree(ak4642
->codec
.reg_cache
);
513 static const struct i2c_device_id ak4642_i2c_id
[] = {
518 MODULE_DEVICE_TABLE(i2c
, ak4642_i2c_id
);
520 static struct i2c_driver ak4642_i2c_driver
= {
522 .name
= "AK4642 I2C Codec",
523 .owner
= THIS_MODULE
,
525 .probe
= ak4642_i2c_probe
,
526 .remove
= ak4642_i2c_remove
,
527 .id_table
= ak4642_i2c_id
,
532 static int ak4642_probe(struct platform_device
*pdev
)
534 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
538 dev_err(&pdev
->dev
, "Codec device not registered\n");
542 socdev
->card
->codec
= ak4642_codec
;
545 ret
= snd_soc_new_pcms(socdev
, SNDRV_DEFAULT_IDX1
, SNDRV_DEFAULT_STR1
);
547 printk(KERN_ERR
"ak4642: failed to create pcms\n");
551 dev_info(&pdev
->dev
, "AK4642 Audio Codec %s", AK4642_VERSION
);
559 /* power down chip */
560 static int ak4642_remove(struct platform_device
*pdev
)
562 struct snd_soc_device
*socdev
= platform_get_drvdata(pdev
);
564 snd_soc_free_pcms(socdev
);
565 snd_soc_dapm_free(socdev
);
570 struct snd_soc_codec_device soc_codec_dev_ak4642
= {
571 .probe
= ak4642_probe
,
572 .remove
= ak4642_remove
,
573 .resume
= ak4642_resume
,
575 EXPORT_SYMBOL_GPL(soc_codec_dev_ak4642
);
577 static int __init
ak4642_modinit(void)
580 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
581 ret
= i2c_add_driver(&ak4642_i2c_driver
);
586 module_init(ak4642_modinit
);
588 static void __exit
ak4642_exit(void)
590 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
591 i2c_del_driver(&ak4642_i2c_driver
);
595 module_exit(ak4642_exit
);
597 MODULE_DESCRIPTION("Soc AK4642 driver");
598 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
599 MODULE_LICENSE("GPL");