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
26 #include <linux/delay.h>
27 #include <linux/i2c.h>
28 #include <linux/slab.h>
29 #include <linux/module.h>
30 #include <sound/soc.h>
31 #include <sound/initval.h>
32 #include <sound/tlv.h>
73 #define PMVCM (1 << 6) /* VCOM Power Management */
74 #define PMMIN (1 << 5) /* MIN Input Power Management */
75 #define PMDAC (1 << 2) /* DAC Power Management */
76 #define PMADL (1 << 0) /* MIC Amp Lch and ADC Lch Power Management */
79 #define HPMTN (1 << 6)
80 #define PMHPL (1 << 5)
81 #define PMHPR (1 << 4)
82 #define MS (1 << 3) /* master/slave select */
84 #define PMPLL (1 << 0)
86 #define PMHP_MASK (PMHPL | PMHPR)
87 #define PMHP PMHP_MASK
90 #define PMADR (1 << 0) /* MIC L / ADC R Power Management */
93 #define MINS (1 << 6) /* Switch from MIN to Speaker */
94 #define DACL (1 << 4) /* Switch from DAC to Stereo or Receiver */
95 #define PMMP (1 << 2) /* MPWR pin Power Management */
96 #define MGAIN0 (1 << 0) /* MIC amp gain*/
99 #define ZTM(param) ((param & 0x3) << 4) /* ALC Zoro Crossing TimeOut */
100 #define WTM(param) (((param & 0x4) << 4) | ((param & 0x3) << 2))
103 #define ALC (1 << 5) /* ALC Enable */
104 #define LMTH0 (1 << 0) /* ALC Limiter / Recovery Level */
107 #define PLL3 (1 << 7)
108 #define PLL2 (1 << 6)
109 #define PLL1 (1 << 5)
110 #define PLL0 (1 << 4)
111 #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
113 #define BCKO_MASK (1 << 3)
114 #define BCKO_64 BCKO_MASK
116 #define DIF_MASK (3 << 0)
118 #define RIGHT_J (1 << 0)
119 #define LEFT_J (2 << 0)
127 #define FS_MASK (FS0 | FS1 | FS2 | FS3)
130 #define BST1 (1 << 3)
133 #define DACH (1 << 0)
136 * Playback Volume (table 39)
138 * max : 0x00 : +12.0 dB
140 * min : 0xFE : -115.0 dB
143 static const DECLARE_TLV_DB_SCALE(out_tlv
, -11500, 50, 1);
145 static const struct snd_kcontrol_new ak4642_snd_controls
[] = {
147 SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC
, R_DVC
,
148 0, 0xFF, 1, out_tlv
),
150 SOC_SINGLE("Headphone Switch", PW_MGMT2
, 6, 1, 0),
153 static const struct snd_kcontrol_new ak4642_hpout_mixer_controls
[] = {
154 SOC_DAPM_SINGLE("DACH", MD_CTL4
, 0, 1, 0),
157 static const struct snd_kcontrol_new ak4642_lout_mixer_controls
[] = {
158 SOC_DAPM_SINGLE("DACL", SG_SL1
, 4, 1, 0),
161 static const struct snd_soc_dapm_widget ak4642_dapm_widgets
[] = {
164 SND_SOC_DAPM_OUTPUT("HPOUTL"),
165 SND_SOC_DAPM_OUTPUT("HPOUTR"),
166 SND_SOC_DAPM_OUTPUT("LINEOUT"),
168 SND_SOC_DAPM_MIXER("HPOUTL Mixer", PW_MGMT2
, 5, 0,
169 &ak4642_hpout_mixer_controls
[0],
170 ARRAY_SIZE(ak4642_hpout_mixer_controls
)),
172 SND_SOC_DAPM_MIXER("HPOUTR Mixer", PW_MGMT2
, 4, 0,
173 &ak4642_hpout_mixer_controls
[0],
174 ARRAY_SIZE(ak4642_hpout_mixer_controls
)),
176 SND_SOC_DAPM_MIXER("LINEOUT Mixer", PW_MGMT1
, 3, 0,
177 &ak4642_lout_mixer_controls
[0],
178 ARRAY_SIZE(ak4642_lout_mixer_controls
)),
181 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", PW_MGMT1
, 2, 0),
184 static const struct snd_soc_dapm_route ak4642_intercon
[] = {
187 {"HPOUTL", NULL
, "HPOUTL Mixer"},
188 {"HPOUTR", NULL
, "HPOUTR Mixer"},
189 {"LINEOUT", NULL
, "LINEOUT Mixer"},
191 {"HPOUTL Mixer", "DACH", "DAC"},
192 {"HPOUTR Mixer", "DACH", "DAC"},
193 {"LINEOUT Mixer", "DACL", "DAC"},
196 /* codec private data */
199 enum snd_soc_control_type control_type
;
203 * ak4642 register cache
205 static const u8 ak4642_reg
[] = {
206 0x00, 0x00, 0x01, 0x00,
207 0x02, 0x00, 0x00, 0x00,
208 0xe1, 0xe1, 0x18, 0x00,
209 0xe1, 0x18, 0x11, 0x08,
210 0x00, 0x00, 0x00, 0x00,
211 0x00, 0x00, 0x00, 0x00,
212 0x00, 0x00, 0x00, 0x00,
213 0x00, 0x00, 0x00, 0x00,
214 0x00, 0x00, 0x00, 0x00,
218 static const u8 ak4648_reg
[] = {
219 0x00, 0x00, 0x01, 0x00,
220 0x02, 0x00, 0x00, 0x00,
221 0xe1, 0xe1, 0x18, 0x00,
222 0xe1, 0x18, 0x11, 0xb8,
223 0x00, 0x00, 0x00, 0x00,
224 0x00, 0x00, 0x00, 0x00,
225 0x00, 0x00, 0x00, 0x00,
226 0x00, 0x00, 0x00, 0x00,
227 0x00, 0x00, 0x00, 0x00,
228 0x00, 0x88, 0x88, 0x08,
231 static int ak4642_dai_startup(struct snd_pcm_substream
*substream
,
232 struct snd_soc_dai
*dai
)
234 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
235 struct snd_soc_codec
*codec
= dai
->codec
;
239 * start headphone output
242 * Audio I/F Format :MSB justified (ADC & DAC)
243 * Bass Boost Level : Middle
245 * This operation came from example code of
246 * "ASAHI KASEI AK4642" (japanese) manual p97.
248 snd_soc_write(codec
, L_IVC
, 0x91); /* volume */
249 snd_soc_write(codec
, R_IVC
, 0x91); /* volume */
255 * Audio I/F Format:MSB justified (ADC & DAC)
258 * ALC setting:Refer to Table 35
261 * This operation came from example code of
262 * "ASAHI KASEI AK4642" (japanese) manual p94.
264 snd_soc_write(codec
, SG_SL1
, PMMP
| MGAIN0
);
265 snd_soc_write(codec
, TIMER
, ZTM(0x3) | WTM(0x3));
266 snd_soc_write(codec
, ALC_CTL1
, ALC
| LMTH0
);
267 snd_soc_update_bits(codec
, PW_MGMT1
, PMADL
, PMADL
);
268 snd_soc_update_bits(codec
, PW_MGMT3
, PMADR
, PMADR
);
274 static void ak4642_dai_shutdown(struct snd_pcm_substream
*substream
,
275 struct snd_soc_dai
*dai
)
277 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
278 struct snd_soc_codec
*codec
= dai
->codec
;
282 /* stop stereo input */
283 snd_soc_update_bits(codec
, PW_MGMT1
, PMADL
, 0);
284 snd_soc_update_bits(codec
, PW_MGMT3
, PMADR
, 0);
285 snd_soc_update_bits(codec
, ALC_CTL1
, ALC
, 0);
289 static int ak4642_dai_set_sysclk(struct snd_soc_dai
*codec_dai
,
290 int clk_id
, unsigned int freq
, int dir
)
292 struct snd_soc_codec
*codec
= codec_dai
->codec
;
306 pll
= PLL2
| PLL1
| PLL0
;
312 pll
= PLL3
| PLL2
| PLL0
;
317 snd_soc_update_bits(codec
, MD_CTL1
, PLL_MASK
, pll
);
322 static int ak4642_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
324 struct snd_soc_codec
*codec
= dai
->codec
;
328 data
= MCKO
| PMPLL
; /* use MCKO */
331 /* set master/slave audio interface */
332 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
333 case SND_SOC_DAIFMT_CBM_CFM
:
337 case SND_SOC_DAIFMT_CBS_CFS
:
342 snd_soc_update_bits(codec
, PW_MGMT2
, MS
| MCKO
| PMPLL
, data
);
343 snd_soc_update_bits(codec
, MD_CTL1
, BCKO_MASK
, bcko
);
347 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
348 case SND_SOC_DAIFMT_LEFT_J
:
351 case SND_SOC_DAIFMT_I2S
:
355 * Please add RIGHT_J / DSP support here
361 snd_soc_update_bits(codec
, MD_CTL1
, DIF_MASK
, data
);
366 static int ak4642_dai_hw_params(struct snd_pcm_substream
*substream
,
367 struct snd_pcm_hw_params
*params
,
368 struct snd_soc_dai
*dai
)
370 struct snd_soc_codec
*codec
= dai
->codec
;
373 switch (params_rate(params
)) {
393 rate
= FS2
| FS1
| FS0
;
399 rate
= FS3
| FS2
| FS1
;
405 rate
= FS3
| FS2
| FS1
| FS0
;
408 rate
= FS3
| FS1
| FS0
;
414 snd_soc_update_bits(codec
, MD_CTL2
, FS_MASK
, rate
);
419 static int ak4642_set_bias_level(struct snd_soc_codec
*codec
,
420 enum snd_soc_bias_level level
)
423 case SND_SOC_BIAS_OFF
:
424 snd_soc_write(codec
, PW_MGMT1
, 0x00);
427 snd_soc_update_bits(codec
, PW_MGMT1
, PMVCM
, PMVCM
);
430 codec
->dapm
.bias_level
= level
;
435 static const struct snd_soc_dai_ops ak4642_dai_ops
= {
436 .startup
= ak4642_dai_startup
,
437 .shutdown
= ak4642_dai_shutdown
,
438 .set_sysclk
= ak4642_dai_set_sysclk
,
439 .set_fmt
= ak4642_dai_set_fmt
,
440 .hw_params
= ak4642_dai_hw_params
,
443 static struct snd_soc_dai_driver ak4642_dai
= {
444 .name
= "ak4642-hifi",
446 .stream_name
= "Playback",
449 .rates
= SNDRV_PCM_RATE_8000_48000
,
450 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
452 .stream_name
= "Capture",
455 .rates
= SNDRV_PCM_RATE_8000_48000
,
456 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
457 .ops
= &ak4642_dai_ops
,
458 .symmetric_rates
= 1,
461 static int ak4642_resume(struct snd_soc_codec
*codec
)
463 snd_soc_cache_sync(codec
);
468 static int ak4642_probe(struct snd_soc_codec
*codec
)
470 struct ak4642_priv
*ak4642
= snd_soc_codec_get_drvdata(codec
);
473 ret
= snd_soc_codec_set_cache_io(codec
, 8, 8, ak4642
->control_type
);
475 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
479 snd_soc_add_controls(codec
, ak4642_snd_controls
,
480 ARRAY_SIZE(ak4642_snd_controls
));
482 ak4642_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
487 static int ak4642_remove(struct snd_soc_codec
*codec
)
489 ak4642_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
493 static struct snd_soc_codec_driver soc_codec_dev_ak4642
= {
494 .probe
= ak4642_probe
,
495 .remove
= ak4642_remove
,
496 .resume
= ak4642_resume
,
497 .set_bias_level
= ak4642_set_bias_level
,
498 .reg_cache_default
= ak4642_reg
, /* ak4642 reg */
499 .reg_cache_size
= ARRAY_SIZE(ak4642_reg
), /* ak4642 reg */
500 .reg_word_size
= sizeof(u8
),
501 .dapm_widgets
= ak4642_dapm_widgets
,
502 .num_dapm_widgets
= ARRAY_SIZE(ak4642_dapm_widgets
),
503 .dapm_routes
= ak4642_intercon
,
504 .num_dapm_routes
= ARRAY_SIZE(ak4642_intercon
),
507 static struct snd_soc_codec_driver soc_codec_dev_ak4648
= {
508 .probe
= ak4642_probe
,
509 .remove
= ak4642_remove
,
510 .resume
= ak4642_resume
,
511 .set_bias_level
= ak4642_set_bias_level
,
512 .reg_cache_default
= ak4648_reg
, /* ak4648 reg */
513 .reg_cache_size
= ARRAY_SIZE(ak4648_reg
), /* ak4648 reg */
514 .reg_word_size
= sizeof(u8
),
515 .dapm_widgets
= ak4642_dapm_widgets
,
516 .num_dapm_widgets
= ARRAY_SIZE(ak4642_dapm_widgets
),
517 .dapm_routes
= ak4642_intercon
,
518 .num_dapm_routes
= ARRAY_SIZE(ak4642_intercon
),
521 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
522 static __devinit
int ak4642_i2c_probe(struct i2c_client
*i2c
,
523 const struct i2c_device_id
*id
)
525 struct ak4642_priv
*ak4642
;
528 ak4642
= devm_kzalloc(&i2c
->dev
, sizeof(struct ak4642_priv
),
533 i2c_set_clientdata(i2c
, ak4642
);
534 ak4642
->control_type
= SND_SOC_I2C
;
536 ret
= snd_soc_register_codec(&i2c
->dev
,
537 (struct snd_soc_codec_driver
*)id
->driver_data
,
542 static __devexit
int ak4642_i2c_remove(struct i2c_client
*client
)
544 snd_soc_unregister_codec(&client
->dev
);
548 static const struct i2c_device_id ak4642_i2c_id
[] = {
549 { "ak4642", (kernel_ulong_t
)&soc_codec_dev_ak4642
},
550 { "ak4643", (kernel_ulong_t
)&soc_codec_dev_ak4642
},
551 { "ak4648", (kernel_ulong_t
)&soc_codec_dev_ak4648
},
554 MODULE_DEVICE_TABLE(i2c
, ak4642_i2c_id
);
556 static struct i2c_driver ak4642_i2c_driver
= {
558 .name
= "ak4642-codec",
559 .owner
= THIS_MODULE
,
561 .probe
= ak4642_i2c_probe
,
562 .remove
= __devexit_p(ak4642_i2c_remove
),
563 .id_table
= ak4642_i2c_id
,
567 static int __init
ak4642_modinit(void)
570 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
571 ret
= i2c_add_driver(&ak4642_i2c_driver
);
576 module_init(ak4642_modinit
);
578 static void __exit
ak4642_exit(void)
580 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
581 i2c_del_driver(&ak4642_i2c_driver
);
585 module_exit(ak4642_exit
);
587 MODULE_DESCRIPTION("Soc AK4642 driver");
588 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
589 MODULE_LICENSE("GPL");