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/of_device.h>
30 #include <linux/module.h>
31 #include <sound/soc.h>
32 #include <sound/initval.h>
33 #include <sound/tlv.h>
74 #define PMVCM (1 << 6) /* VCOM Power Management */
75 #define PMMIN (1 << 5) /* MIN Input Power Management */
76 #define PMDAC (1 << 2) /* DAC Power Management */
77 #define PMADL (1 << 0) /* MIC Amp Lch and ADC Lch Power Management */
80 #define HPMTN (1 << 6)
81 #define PMHPL (1 << 5)
82 #define PMHPR (1 << 4)
83 #define MS (1 << 3) /* master/slave select */
85 #define PMPLL (1 << 0)
87 #define PMHP_MASK (PMHPL | PMHPR)
88 #define PMHP PMHP_MASK
91 #define PMADR (1 << 0) /* MIC L / ADC R Power Management */
94 #define MINS (1 << 6) /* Switch from MIN to Speaker */
95 #define DACL (1 << 4) /* Switch from DAC to Stereo or Receiver */
96 #define PMMP (1 << 2) /* MPWR pin Power Management */
97 #define MGAIN0 (1 << 0) /* MIC amp gain*/
100 #define ZTM(param) ((param & 0x3) << 4) /* ALC Zoro Crossing TimeOut */
101 #define WTM(param) (((param & 0x4) << 4) | ((param & 0x3) << 2))
104 #define ALC (1 << 5) /* ALC Enable */
105 #define LMTH0 (1 << 0) /* ALC Limiter / Recovery Level */
108 #define PLL3 (1 << 7)
109 #define PLL2 (1 << 6)
110 #define PLL1 (1 << 5)
111 #define PLL0 (1 << 4)
112 #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
114 #define BCKO_MASK (1 << 3)
115 #define BCKO_64 BCKO_MASK
117 #define DIF_MASK (3 << 0)
119 #define RIGHT_J (1 << 0)
120 #define LEFT_J (2 << 0)
128 #define FS_MASK (FS0 | FS1 | FS2 | FS3)
131 #define BST1 (1 << 3)
134 #define DACH (1 << 0)
137 * Playback Volume (table 39)
139 * max : 0x00 : +12.0 dB
141 * min : 0xFE : -115.0 dB
144 static const DECLARE_TLV_DB_SCALE(out_tlv
, -11550, 50, 1);
146 static const struct snd_kcontrol_new ak4642_snd_controls
[] = {
148 SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC
, R_DVC
,
149 0, 0xFF, 1, out_tlv
),
152 static const struct snd_kcontrol_new ak4642_headphone_control
=
153 SOC_DAPM_SINGLE("Switch", PW_MGMT2
, 6, 1, 0);
155 static const struct snd_kcontrol_new ak4642_lout_mixer_controls
[] = {
156 SOC_DAPM_SINGLE("DACL", SG_SL1
, 4, 1, 0),
159 static const struct snd_soc_dapm_widget ak4642_dapm_widgets
[] = {
162 SND_SOC_DAPM_OUTPUT("HPOUTL"),
163 SND_SOC_DAPM_OUTPUT("HPOUTR"),
164 SND_SOC_DAPM_OUTPUT("LINEOUT"),
166 SND_SOC_DAPM_PGA("HPL Out", PW_MGMT2
, 5, 0, NULL
, 0),
167 SND_SOC_DAPM_PGA("HPR Out", PW_MGMT2
, 4, 0, NULL
, 0),
168 SND_SOC_DAPM_SWITCH("Headphone Enable", SND_SOC_NOPM
, 0, 0,
169 &ak4642_headphone_control
),
171 SND_SOC_DAPM_PGA("DACH", MD_CTL4
, 0, 0, NULL
, 0),
173 SND_SOC_DAPM_MIXER("LINEOUT Mixer", PW_MGMT1
, 3, 0,
174 &ak4642_lout_mixer_controls
[0],
175 ARRAY_SIZE(ak4642_lout_mixer_controls
)),
178 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", PW_MGMT1
, 2, 0),
181 static const struct snd_soc_dapm_route ak4642_intercon
[] = {
184 {"HPOUTL", NULL
, "HPL Out"},
185 {"HPOUTR", NULL
, "HPR Out"},
186 {"LINEOUT", NULL
, "LINEOUT Mixer"},
188 {"HPL Out", NULL
, "Headphone Enable"},
189 {"HPR Out", NULL
, "Headphone Enable"},
191 {"Headphone Enable", "Switch", "DACH"},
193 {"DACH", NULL
, "DAC"},
195 {"LINEOUT Mixer", "DACL", "DAC"},
199 * ak4642 register cache
201 static const u8 ak4642_reg
[] = {
202 0x00, 0x00, 0x01, 0x00,
203 0x02, 0x00, 0x00, 0x00,
204 0xe1, 0xe1, 0x18, 0x00,
205 0xe1, 0x18, 0x11, 0x08,
206 0x00, 0x00, 0x00, 0x00,
207 0x00, 0x00, 0x00, 0x00,
208 0x00, 0x00, 0x00, 0x00,
209 0x00, 0x00, 0x00, 0x00,
210 0x00, 0x00, 0x00, 0x00,
214 static const u8 ak4648_reg
[] = {
215 0x00, 0x00, 0x01, 0x00,
216 0x02, 0x00, 0x00, 0x00,
217 0xe1, 0xe1, 0x18, 0x00,
218 0xe1, 0x18, 0x11, 0xb8,
219 0x00, 0x00, 0x00, 0x00,
220 0x00, 0x00, 0x00, 0x00,
221 0x00, 0x00, 0x00, 0x00,
222 0x00, 0x00, 0x00, 0x00,
223 0x00, 0x00, 0x00, 0x00,
224 0x00, 0x88, 0x88, 0x08,
227 static int ak4642_dai_startup(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
;
235 * start headphone output
238 * Audio I/F Format :MSB justified (ADC & DAC)
239 * Bass Boost Level : Middle
241 * This operation came from example code of
242 * "ASAHI KASEI AK4642" (japanese) manual p97.
244 snd_soc_write(codec
, L_IVC
, 0x91); /* volume */
245 snd_soc_write(codec
, R_IVC
, 0x91); /* volume */
251 * Audio I/F Format:MSB justified (ADC & DAC)
254 * ALC setting:Refer to Table 35
257 * This operation came from example code of
258 * "ASAHI KASEI AK4642" (japanese) manual p94.
260 snd_soc_write(codec
, SG_SL1
, PMMP
| MGAIN0
);
261 snd_soc_write(codec
, TIMER
, ZTM(0x3) | WTM(0x3));
262 snd_soc_write(codec
, ALC_CTL1
, ALC
| LMTH0
);
263 snd_soc_update_bits(codec
, PW_MGMT1
, PMADL
, PMADL
);
264 snd_soc_update_bits(codec
, PW_MGMT3
, PMADR
, PMADR
);
270 static void ak4642_dai_shutdown(struct snd_pcm_substream
*substream
,
271 struct snd_soc_dai
*dai
)
273 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
274 struct snd_soc_codec
*codec
= dai
->codec
;
278 /* stop stereo input */
279 snd_soc_update_bits(codec
, PW_MGMT1
, PMADL
, 0);
280 snd_soc_update_bits(codec
, PW_MGMT3
, PMADR
, 0);
281 snd_soc_update_bits(codec
, ALC_CTL1
, ALC
, 0);
285 static int ak4642_dai_set_sysclk(struct snd_soc_dai
*codec_dai
,
286 int clk_id
, unsigned int freq
, int dir
)
288 struct snd_soc_codec
*codec
= codec_dai
->codec
;
302 pll
= PLL2
| PLL1
| PLL0
;
308 pll
= PLL3
| PLL2
| PLL0
;
313 snd_soc_update_bits(codec
, MD_CTL1
, PLL_MASK
, pll
);
318 static int ak4642_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
320 struct snd_soc_codec
*codec
= dai
->codec
;
324 data
= MCKO
| PMPLL
; /* use MCKO */
327 /* set master/slave audio interface */
328 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
329 case SND_SOC_DAIFMT_CBM_CFM
:
333 case SND_SOC_DAIFMT_CBS_CFS
:
338 snd_soc_update_bits(codec
, PW_MGMT2
, MS
| MCKO
| PMPLL
, data
);
339 snd_soc_update_bits(codec
, MD_CTL1
, BCKO_MASK
, bcko
);
343 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
344 case SND_SOC_DAIFMT_LEFT_J
:
347 case SND_SOC_DAIFMT_I2S
:
351 * Please add RIGHT_J / DSP support here
357 snd_soc_update_bits(codec
, MD_CTL1
, DIF_MASK
, data
);
362 static int ak4642_dai_hw_params(struct snd_pcm_substream
*substream
,
363 struct snd_pcm_hw_params
*params
,
364 struct snd_soc_dai
*dai
)
366 struct snd_soc_codec
*codec
= dai
->codec
;
369 switch (params_rate(params
)) {
389 rate
= FS2
| FS1
| FS0
;
395 rate
= FS3
| FS2
| FS1
;
401 rate
= FS3
| FS2
| FS1
| FS0
;
404 rate
= FS3
| FS1
| FS0
;
410 snd_soc_update_bits(codec
, MD_CTL2
, FS_MASK
, rate
);
415 static int ak4642_set_bias_level(struct snd_soc_codec
*codec
,
416 enum snd_soc_bias_level level
)
419 case SND_SOC_BIAS_OFF
:
420 snd_soc_write(codec
, PW_MGMT1
, 0x00);
423 snd_soc_update_bits(codec
, PW_MGMT1
, PMVCM
, PMVCM
);
426 codec
->dapm
.bias_level
= level
;
431 static const struct snd_soc_dai_ops ak4642_dai_ops
= {
432 .startup
= ak4642_dai_startup
,
433 .shutdown
= ak4642_dai_shutdown
,
434 .set_sysclk
= ak4642_dai_set_sysclk
,
435 .set_fmt
= ak4642_dai_set_fmt
,
436 .hw_params
= ak4642_dai_hw_params
,
439 static struct snd_soc_dai_driver ak4642_dai
= {
440 .name
= "ak4642-hifi",
442 .stream_name
= "Playback",
445 .rates
= SNDRV_PCM_RATE_8000_48000
,
446 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
448 .stream_name
= "Capture",
451 .rates
= SNDRV_PCM_RATE_8000_48000
,
452 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
453 .ops
= &ak4642_dai_ops
,
454 .symmetric_rates
= 1,
457 static int ak4642_resume(struct snd_soc_codec
*codec
)
459 snd_soc_cache_sync(codec
);
464 static int ak4642_probe(struct snd_soc_codec
*codec
)
468 ret
= snd_soc_codec_set_cache_io(codec
, 8, 8, SND_SOC_I2C
);
470 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
474 snd_soc_add_codec_controls(codec
, ak4642_snd_controls
,
475 ARRAY_SIZE(ak4642_snd_controls
));
477 ak4642_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
482 static int ak4642_remove(struct snd_soc_codec
*codec
)
484 ak4642_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
488 static struct snd_soc_codec_driver soc_codec_dev_ak4642
= {
489 .probe
= ak4642_probe
,
490 .remove
= ak4642_remove
,
491 .resume
= ak4642_resume
,
492 .set_bias_level
= ak4642_set_bias_level
,
493 .reg_cache_default
= ak4642_reg
, /* ak4642 reg */
494 .reg_cache_size
= ARRAY_SIZE(ak4642_reg
), /* ak4642 reg */
495 .reg_word_size
= sizeof(u8
),
496 .dapm_widgets
= ak4642_dapm_widgets
,
497 .num_dapm_widgets
= ARRAY_SIZE(ak4642_dapm_widgets
),
498 .dapm_routes
= ak4642_intercon
,
499 .num_dapm_routes
= ARRAY_SIZE(ak4642_intercon
),
502 static struct snd_soc_codec_driver soc_codec_dev_ak4648
= {
503 .probe
= ak4642_probe
,
504 .remove
= ak4642_remove
,
505 .resume
= ak4642_resume
,
506 .set_bias_level
= ak4642_set_bias_level
,
507 .reg_cache_default
= ak4648_reg
, /* ak4648 reg */
508 .reg_cache_size
= ARRAY_SIZE(ak4648_reg
), /* ak4648 reg */
509 .reg_word_size
= sizeof(u8
),
510 .dapm_widgets
= ak4642_dapm_widgets
,
511 .num_dapm_widgets
= ARRAY_SIZE(ak4642_dapm_widgets
),
512 .dapm_routes
= ak4642_intercon
,
513 .num_dapm_routes
= ARRAY_SIZE(ak4642_intercon
),
516 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
517 static struct of_device_id ak4642_of_match
[];
518 static int ak4642_i2c_probe(struct i2c_client
*i2c
,
519 const struct i2c_device_id
*id
)
521 struct device_node
*np
= i2c
->dev
.of_node
;
522 const struct snd_soc_codec_driver
*driver
;
526 const struct of_device_id
*of_id
;
528 of_id
= of_match_device(ak4642_of_match
, &i2c
->dev
);
530 driver
= of_id
->data
;
532 driver
= (struct snd_soc_codec_driver
*)id
->driver_data
;
536 dev_err(&i2c
->dev
, "no driver\n");
540 return snd_soc_register_codec(&i2c
->dev
,
541 driver
, &ak4642_dai
, 1);
544 static int ak4642_i2c_remove(struct i2c_client
*client
)
546 snd_soc_unregister_codec(&client
->dev
);
550 static struct of_device_id ak4642_of_match
[] = {
551 { .compatible
= "asahi-kasei,ak4642", .data
= &soc_codec_dev_ak4642
},
552 { .compatible
= "asahi-kasei,ak4643", .data
= &soc_codec_dev_ak4642
},
553 { .compatible
= "asahi-kasei,ak4648", .data
= &soc_codec_dev_ak4648
},
556 MODULE_DEVICE_TABLE(of
, ak4642_of_match
);
558 static const struct i2c_device_id ak4642_i2c_id
[] = {
559 { "ak4642", (kernel_ulong_t
)&soc_codec_dev_ak4642
},
560 { "ak4643", (kernel_ulong_t
)&soc_codec_dev_ak4642
},
561 { "ak4648", (kernel_ulong_t
)&soc_codec_dev_ak4648
},
564 MODULE_DEVICE_TABLE(i2c
, ak4642_i2c_id
);
566 static struct i2c_driver ak4642_i2c_driver
= {
568 .name
= "ak4642-codec",
569 .owner
= THIS_MODULE
,
570 .of_match_table
= ak4642_of_match
,
572 .probe
= ak4642_i2c_probe
,
573 .remove
= ak4642_i2c_remove
,
574 .id_table
= ak4642_i2c_id
,
578 static int __init
ak4642_modinit(void)
581 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
582 ret
= i2c_add_driver(&ak4642_i2c_driver
);
587 module_init(ak4642_modinit
);
589 static void __exit
ak4642_exit(void)
591 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
592 i2c_del_driver(&ak4642_i2c_driver
);
596 module_exit(ak4642_exit
);
598 MODULE_DESCRIPTION("Soc AK4642 driver");
599 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
600 MODULE_LICENSE("GPL");