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.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 PMVCM (1 << 6) /* VCOM Power Management */
77 #define PMMIN (1 << 5) /* MIN Input Power Management */
78 #define PMDAC (1 << 2) /* DAC Power Management */
79 #define PMADL (1 << 0) /* MIC Amp Lch and ADC Lch Power Management */
82 #define HPMTN (1 << 6)
83 #define PMHPL (1 << 5)
84 #define PMHPR (1 << 4)
85 #define MS (1 << 3) /* master/slave select */
87 #define PMPLL (1 << 0)
89 #define PMHP_MASK (PMHPL | PMHPR)
90 #define PMHP PMHP_MASK
93 #define PMADR (1 << 0) /* MIC L / ADC R Power Management */
96 #define MINS (1 << 6) /* Switch from MIN to Speaker */
97 #define DACL (1 << 4) /* Switch from DAC to Stereo or Receiver */
98 #define PMMP (1 << 2) /* MPWR pin Power Management */
99 #define MGAIN0 (1 << 0) /* MIC amp gain*/
102 #define ZTM(param) ((param & 0x3) << 4) /* ALC Zoro Crossing TimeOut */
103 #define WTM(param) (((param & 0x4) << 4) | ((param & 0x3) << 2))
106 #define ALC (1 << 5) /* ALC Enable */
107 #define LMTH0 (1 << 0) /* ALC Limiter / Recovery Level */
110 #define PLL3 (1 << 7)
111 #define PLL2 (1 << 6)
112 #define PLL1 (1 << 5)
113 #define PLL0 (1 << 4)
114 #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
116 #define BCKO_MASK (1 << 3)
117 #define BCKO_64 BCKO_MASK
124 #define FS_MASK (FS0 | FS1 | FS2 | FS3)
127 #define BST1 (1 << 3)
130 #define DACH (1 << 0)
133 * Playback Volume (table 39)
135 * max : 0x00 : +12.0 dB
137 * min : 0xFE : -115.0 dB
140 static const DECLARE_TLV_DB_SCALE(out_tlv
, -11500, 50, 1);
142 static const struct snd_kcontrol_new ak4642_snd_controls
[] = {
144 SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC
, R_DVC
,
145 0, 0xFF, 1, out_tlv
),
149 /* codec private data */
152 enum snd_soc_control_type control_type
;
157 * ak4642 register cache
159 static const u16 ak4642_reg
[AK4642_CACHEREGNUM
] = {
160 0x0000, 0x0000, 0x0001, 0x0000,
161 0x0002, 0x0000, 0x0000, 0x0000,
162 0x00e1, 0x00e1, 0x0018, 0x0000,
163 0x00e1, 0x0018, 0x0011, 0x0008,
164 0x0000, 0x0000, 0x0000, 0x0000,
165 0x0000, 0x0000, 0x0000, 0x0000,
166 0x0000, 0x0000, 0x0000, 0x0000,
167 0x0000, 0x0000, 0x0000, 0x0000,
168 0x0000, 0x0000, 0x0000, 0x0000,
173 * read ak4642 register cache
175 static inline unsigned int ak4642_read_reg_cache(struct snd_soc_codec
*codec
,
178 u16
*cache
= codec
->reg_cache
;
179 if (reg
>= AK4642_CACHEREGNUM
)
185 * write ak4642 register cache
187 static inline void ak4642_write_reg_cache(struct snd_soc_codec
*codec
,
188 u16 reg
, unsigned int value
)
190 u16
*cache
= codec
->reg_cache
;
191 if (reg
>= AK4642_CACHEREGNUM
)
198 * write to the AK4642 register space
200 static int ak4642_write(struct snd_soc_codec
*codec
, unsigned int reg
,
206 * D15..D8 AK4642 register offset
207 * D7...D0 register data
209 data
[0] = reg
& 0xff;
210 data
[1] = value
& 0xff;
212 if (codec
->hw_write(codec
->control_data
, data
, 2) == 2) {
213 ak4642_write_reg_cache(codec
, reg
, value
);
219 static int ak4642_sync(struct snd_soc_codec
*codec
)
221 u16
*cache
= codec
->reg_cache
;
224 for (i
= 0; i
< AK4642_CACHEREGNUM
; i
++)
225 r
|= ak4642_write(codec
, i
, cache
[i
]);
230 static int ak4642_dai_startup(struct snd_pcm_substream
*substream
,
231 struct snd_soc_dai
*dai
)
233 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
234 struct snd_soc_codec
*codec
= dai
->codec
;
238 * start headphone output
241 * Audio I/F Format :MSB justified (ADC & DAC)
242 * Bass Boost Level : Middle
244 * This operation came from example code of
245 * "ASAHI KASEI AK4642" (japanese) manual p97.
247 snd_soc_update_bits(codec
, MD_CTL4
, DACH
, DACH
);
248 snd_soc_update_bits(codec
, MD_CTL3
, BST1
, BST1
);
249 ak4642_write(codec
, L_IVC
, 0x91); /* volume */
250 ak4642_write(codec
, R_IVC
, 0x91); /* volume */
251 snd_soc_update_bits(codec
, PW_MGMT1
, PMVCM
| PMMIN
| PMDAC
,
252 PMVCM
| PMMIN
| PMDAC
);
253 snd_soc_update_bits(codec
, PW_MGMT2
, PMHP_MASK
, PMHP
);
254 snd_soc_update_bits(codec
, PW_MGMT2
, HPMTN
, HPMTN
);
260 * Audio I/F Format:MSB justified (ADC & DAC)
263 * ALC setting:Refer to Table 35
266 * This operation came from example code of
267 * "ASAHI KASEI AK4642" (japanese) manual p94.
269 ak4642_write(codec
, SG_SL1
, PMMP
| MGAIN0
);
270 ak4642_write(codec
, TIMER
, ZTM(0x3) | WTM(0x3));
271 ak4642_write(codec
, ALC_CTL1
, ALC
| LMTH0
);
272 snd_soc_update_bits(codec
, PW_MGMT1
, PMVCM
| PMADL
,
274 snd_soc_update_bits(codec
, PW_MGMT3
, PMADR
, PMADR
);
280 static void ak4642_dai_shutdown(struct snd_pcm_substream
*substream
,
281 struct snd_soc_dai
*dai
)
283 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
284 struct snd_soc_codec
*codec
= dai
->codec
;
287 /* stop headphone output */
288 snd_soc_update_bits(codec
, PW_MGMT2
, HPMTN
, 0);
289 snd_soc_update_bits(codec
, PW_MGMT2
, PMHP_MASK
, 0);
290 snd_soc_update_bits(codec
, PW_MGMT1
, PMMIN
| PMDAC
, 0);
291 snd_soc_update_bits(codec
, MD_CTL3
, BST1
, 0);
292 snd_soc_update_bits(codec
, MD_CTL4
, DACH
, 0);
294 /* stop stereo input */
295 snd_soc_update_bits(codec
, PW_MGMT1
, PMADL
, 0);
296 snd_soc_update_bits(codec
, PW_MGMT3
, PMADR
, 0);
297 snd_soc_update_bits(codec
, ALC_CTL1
, ALC
, 0);
301 static int ak4642_dai_set_sysclk(struct snd_soc_dai
*codec_dai
,
302 int clk_id
, unsigned int freq
, int dir
)
304 struct snd_soc_codec
*codec
= codec_dai
->codec
;
318 pll
= PLL2
| PLL1
| PLL0
;
324 pll
= PLL3
| PLL2
| PLL0
;
329 snd_soc_update_bits(codec
, MD_CTL1
, PLL_MASK
, pll
);
334 static int ak4642_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
336 struct snd_soc_codec
*codec
= dai
->codec
;
340 data
= MCKO
| PMPLL
; /* use MCKO */
343 /* set master/slave audio interface */
344 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
345 case SND_SOC_DAIFMT_CBM_CFM
:
349 case SND_SOC_DAIFMT_CBS_CFS
:
354 snd_soc_update_bits(codec
, PW_MGMT2
, MS
, data
);
355 snd_soc_update_bits(codec
, MD_CTL1
, BCKO_MASK
, bcko
);
360 static int ak4642_dai_hw_params(struct snd_pcm_substream
*substream
,
361 struct snd_pcm_hw_params
*params
,
362 struct snd_soc_dai
*dai
)
364 struct snd_soc_codec
*codec
= dai
->codec
;
367 switch (params_rate(params
)) {
387 rate
= FS2
| FS1
| FS0
;
393 rate
= FS3
| FS2
| FS1
;
399 rate
= FS3
| FS2
| FS1
| FS0
;
402 rate
= FS3
| FS1
| FS0
;
408 snd_soc_update_bits(codec
, MD_CTL2
, FS_MASK
, rate
);
413 static struct snd_soc_dai_ops ak4642_dai_ops
= {
414 .startup
= ak4642_dai_startup
,
415 .shutdown
= ak4642_dai_shutdown
,
416 .set_sysclk
= ak4642_dai_set_sysclk
,
417 .set_fmt
= ak4642_dai_set_fmt
,
418 .hw_params
= ak4642_dai_hw_params
,
421 static struct snd_soc_dai_driver ak4642_dai
= {
422 .name
= "ak4642-hifi",
424 .stream_name
= "Playback",
427 .rates
= SNDRV_PCM_RATE_8000_48000
,
428 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
430 .stream_name
= "Capture",
433 .rates
= SNDRV_PCM_RATE_8000_48000
,
434 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
435 .ops
= &ak4642_dai_ops
,
436 .symmetric_rates
= 1,
439 static int ak4642_resume(struct snd_soc_codec
*codec
)
446 static int ak4642_probe(struct snd_soc_codec
*codec
)
448 struct ak4642_priv
*ak4642
= snd_soc_codec_get_drvdata(codec
);
450 dev_info(codec
->dev
, "AK4642 Audio Codec %s", AK4642_VERSION
);
452 codec
->hw_write
= (hw_write_t
)i2c_master_send
;
453 codec
->control_data
= ak4642
->control_data
;
455 snd_soc_add_controls(codec
, ak4642_snd_controls
,
456 ARRAY_SIZE(ak4642_snd_controls
));
461 static struct snd_soc_codec_driver soc_codec_dev_ak4642
= {
462 .probe
= ak4642_probe
,
463 .resume
= ak4642_resume
,
464 .read
= ak4642_read_reg_cache
,
465 .write
= ak4642_write
,
466 .reg_cache_size
= ARRAY_SIZE(ak4642_reg
),
467 .reg_word_size
= sizeof(u8
),
468 .reg_cache_default
= ak4642_reg
,
471 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
472 static __devinit
int ak4642_i2c_probe(struct i2c_client
*i2c
,
473 const struct i2c_device_id
*id
)
475 struct ak4642_priv
*ak4642
;
478 ak4642
= kzalloc(sizeof(struct ak4642_priv
), GFP_KERNEL
);
482 i2c_set_clientdata(i2c
, ak4642
);
483 ak4642
->control_data
= i2c
;
484 ak4642
->control_type
= SND_SOC_I2C
;
486 ret
= snd_soc_register_codec(&i2c
->dev
,
487 &soc_codec_dev_ak4642
, &ak4642_dai
, 1);
493 static __devexit
int ak4642_i2c_remove(struct i2c_client
*client
)
495 snd_soc_unregister_codec(&client
->dev
);
496 kfree(i2c_get_clientdata(client
));
500 static const struct i2c_device_id ak4642_i2c_id
[] = {
505 MODULE_DEVICE_TABLE(i2c
, ak4642_i2c_id
);
507 static struct i2c_driver ak4642_i2c_driver
= {
509 .name
= "ak4642-codec",
510 .owner
= THIS_MODULE
,
512 .probe
= ak4642_i2c_probe
,
513 .remove
= __devexit_p(ak4642_i2c_remove
),
514 .id_table
= ak4642_i2c_id
,
518 static int __init
ak4642_modinit(void)
521 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
522 ret
= i2c_add_driver(&ak4642_i2c_driver
);
527 module_init(ak4642_modinit
);
529 static void __exit
ak4642_exit(void)
531 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
532 i2c_del_driver(&ak4642_i2c_driver
);
536 module_exit(ak4642_exit
);
538 MODULE_DESCRIPTION("Soc AK4642 driver");
539 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
540 MODULE_LICENSE("GPL");