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/clk.h>
27 #include <linux/clk-provider.h>
28 #include <linux/delay.h>
29 #include <linux/i2c.h>
30 #include <linux/slab.h>
31 #include <linux/of_device.h>
32 #include <linux/module.h>
33 #include <linux/regmap.h>
34 #include <sound/soc.h>
35 #include <sound/initval.h>
36 #include <sound/tlv.h>
69 #define FIL1_3 0x1f /* The maximum valid register for ak4642 */
74 #define SPK_MS 0x24 /* The maximum valid register for ak4643 */
75 #define EQ_FBEQAB 0x25
76 #define EQ_FBEQCD 0x26
77 #define EQ_FBEQE 0x27 /* The maximum valid register for ak4648 */
80 #define PMVCM (1 << 6) /* VCOM Power Management */
81 #define PMMIN (1 << 5) /* MIN Input Power Management */
82 #define PMDAC (1 << 2) /* DAC Power Management */
83 #define PMADL (1 << 0) /* MIC Amp Lch and ADC Lch Power Management */
86 #define HPMTN (1 << 6)
87 #define PMHPL (1 << 5)
88 #define PMHPR (1 << 4)
89 #define MS (1 << 3) /* master/slave select */
91 #define PMPLL (1 << 0)
93 #define PMHP_MASK (PMHPL | PMHPR)
94 #define PMHP PMHP_MASK
97 #define PMADR (1 << 0) /* MIC L / ADC R Power Management */
100 #define MINS (1 << 6) /* Switch from MIN to Speaker */
101 #define DACL (1 << 4) /* Switch from DAC to Stereo or Receiver */
102 #define PMMP (1 << 2) /* MPWR pin Power Management */
103 #define MGAIN0 (1 << 0) /* MIC amp gain*/
106 #define LOPS (1 << 6) /* Stero Line-out Power Save Mode */
109 #define ZTM(param) ((param & 0x3) << 4) /* ALC Zero Crossing TimeOut */
110 #define WTM(param) (((param & 0x4) << 4) | ((param & 0x3) << 2))
113 #define ALC (1 << 5) /* ALC Enable */
114 #define LMTH0 (1 << 0) /* ALC Limiter / Recovery Level */
117 #define PLL3 (1 << 7)
118 #define PLL2 (1 << 6)
119 #define PLL1 (1 << 5)
120 #define PLL0 (1 << 4)
121 #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
123 #define BCKO_MASK (1 << 3)
124 #define BCKO_64 BCKO_MASK
126 #define DIF_MASK (3 << 0)
128 #define RIGHT_J (1 << 0)
129 #define LEFT_J (2 << 0)
133 #define FSs(val) (((val & 0x7) << 0) | ((val & 0x8) << 2))
134 #define PSs(val) ((val & 0x3) << 6)
137 #define BST1 (1 << 3)
140 #define DACH (1 << 0)
142 struct ak4642_drvdata
{
143 const struct regmap_config
*regmap_config
;
144 int extended_frequencies
;
148 const struct ak4642_drvdata
*drvdata
;
153 * Playback Volume (table 39)
155 * max : 0x00 : +12.0 dB
157 * min : 0xFE : -115.0 dB
160 static const DECLARE_TLV_DB_SCALE(out_tlv
, -11550, 50, 1);
162 static const struct snd_kcontrol_new ak4642_snd_controls
[] = {
164 SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC
, R_DVC
,
165 0, 0xFF, 1, out_tlv
),
166 SOC_SINGLE("ALC Capture Switch", ALC_CTL1
, 5, 1, 0),
167 SOC_SINGLE("ALC Capture ZC Switch", ALC_CTL1
, 4, 1, 1),
170 static const struct snd_kcontrol_new ak4642_headphone_control
=
171 SOC_DAPM_SINGLE("Switch", PW_MGMT2
, 6, 1, 0);
173 static const struct snd_kcontrol_new ak4642_lout_mixer_controls
[] = {
174 SOC_DAPM_SINGLE("DACL", SG_SL1
, 4, 1, 0),
178 static int ak4642_lout_event(struct snd_soc_dapm_widget
*w
,
179 struct snd_kcontrol
*kcontrol
, int event
)
181 struct snd_soc_codec
*codec
= snd_soc_dapm_to_codec(w
->dapm
);
184 case SND_SOC_DAPM_PRE_PMD
:
185 case SND_SOC_DAPM_PRE_PMU
:
186 /* Power save mode ON */
187 snd_soc_update_bits(codec
, SG_SL2
, LOPS
, LOPS
);
189 case SND_SOC_DAPM_POST_PMU
:
190 case SND_SOC_DAPM_POST_PMD
:
191 /* Power save mode OFF */
193 snd_soc_update_bits(codec
, SG_SL2
, LOPS
, 0);
200 static const struct snd_soc_dapm_widget ak4642_dapm_widgets
[] = {
203 SND_SOC_DAPM_OUTPUT("HPOUTL"),
204 SND_SOC_DAPM_OUTPUT("HPOUTR"),
205 SND_SOC_DAPM_OUTPUT("LINEOUT"),
207 SND_SOC_DAPM_PGA("HPL Out", PW_MGMT2
, 5, 0, NULL
, 0),
208 SND_SOC_DAPM_PGA("HPR Out", PW_MGMT2
, 4, 0, NULL
, 0),
209 SND_SOC_DAPM_SWITCH("Headphone Enable", SND_SOC_NOPM
, 0, 0,
210 &ak4642_headphone_control
),
212 SND_SOC_DAPM_PGA("DACH", MD_CTL4
, 0, 0, NULL
, 0),
214 SND_SOC_DAPM_MIXER_E("LINEOUT Mixer", PW_MGMT1
, 3, 0,
215 &ak4642_lout_mixer_controls
[0],
216 ARRAY_SIZE(ak4642_lout_mixer_controls
),
218 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMU
|
219 SND_SOC_DAPM_PRE_PMD
| SND_SOC_DAPM_POST_PMD
),
222 SND_SOC_DAPM_DAC("DAC", NULL
, PW_MGMT1
, 2, 0),
225 static const struct snd_soc_dapm_route ak4642_intercon
[] = {
228 {"HPOUTL", NULL
, "HPL Out"},
229 {"HPOUTR", NULL
, "HPR Out"},
230 {"LINEOUT", NULL
, "LINEOUT Mixer"},
232 {"HPL Out", NULL
, "Headphone Enable"},
233 {"HPR Out", NULL
, "Headphone Enable"},
235 {"Headphone Enable", "Switch", "DACH"},
237 {"DACH", NULL
, "DAC"},
239 {"LINEOUT Mixer", "DACL", "DAC"},
241 { "DAC", NULL
, "Playback" },
245 * ak4642 register cache
247 static const struct reg_default ak4643_reg
[] = {
248 { 0, 0x00 }, { 1, 0x00 }, { 2, 0x01 }, { 3, 0x00 },
249 { 4, 0x02 }, { 5, 0x00 }, { 6, 0x00 }, { 7, 0x00 },
250 { 8, 0xe1 }, { 9, 0xe1 }, { 10, 0x18 }, { 11, 0x00 },
251 { 12, 0xe1 }, { 13, 0x18 }, { 14, 0x11 }, { 15, 0x08 },
252 { 16, 0x00 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x00 },
253 { 20, 0x00 }, { 21, 0x00 }, { 22, 0x00 }, { 23, 0x00 },
254 { 24, 0x00 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0x00 },
255 { 28, 0x00 }, { 29, 0x00 }, { 30, 0x00 }, { 31, 0x00 },
256 { 32, 0x00 }, { 33, 0x00 }, { 34, 0x00 }, { 35, 0x00 },
260 /* The default settings for 0x0 ~ 0x1f registers are the same for ak4642
261 and ak4643. So we reuse the ak4643 reg_default for ak4642.
262 The valid registers for ak4642 are 0x0 ~ 0x1f which is a subset of ak4643,
263 so define NUM_AK4642_REG_DEFAULTS for ak4642.
265 #define ak4642_reg ak4643_reg
266 #define NUM_AK4642_REG_DEFAULTS (FIL1_3 + 1)
268 static const struct reg_default ak4648_reg
[] = {
269 { 0, 0x00 }, { 1, 0x00 }, { 2, 0x01 }, { 3, 0x00 },
270 { 4, 0x02 }, { 5, 0x00 }, { 6, 0x00 }, { 7, 0x00 },
271 { 8, 0xe1 }, { 9, 0xe1 }, { 10, 0x18 }, { 11, 0x00 },
272 { 12, 0xe1 }, { 13, 0x18 }, { 14, 0x11 }, { 15, 0xb8 },
273 { 16, 0x00 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x00 },
274 { 20, 0x00 }, { 21, 0x00 }, { 22, 0x00 }, { 23, 0x00 },
275 { 24, 0x00 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0x00 },
276 { 28, 0x00 }, { 29, 0x00 }, { 30, 0x00 }, { 31, 0x00 },
277 { 32, 0x00 }, { 33, 0x00 }, { 34, 0x00 }, { 35, 0x00 },
278 { 36, 0x00 }, { 37, 0x88 }, { 38, 0x88 }, { 39, 0x08 },
281 static int ak4642_dai_startup(struct snd_pcm_substream
*substream
,
282 struct snd_soc_dai
*dai
)
284 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
285 struct snd_soc_codec
*codec
= dai
->codec
;
289 * start headphone output
292 * Audio I/F Format :MSB justified (ADC & DAC)
293 * Bass Boost Level : Middle
295 * This operation came from example code of
296 * "ASAHI KASEI AK4642" (japanese) manual p97.
298 snd_soc_write(codec
, L_IVC
, 0x91); /* volume */
299 snd_soc_write(codec
, R_IVC
, 0x91); /* volume */
305 * Audio I/F Format:MSB justified (ADC & DAC)
308 * ALC setting:Refer to Table 35
311 * This operation came from example code of
312 * "ASAHI KASEI AK4642" (japanese) manual p94.
314 snd_soc_update_bits(codec
, SG_SL1
, PMMP
| MGAIN0
, PMMP
| MGAIN0
);
315 snd_soc_write(codec
, TIMER
, ZTM(0x3) | WTM(0x3));
316 snd_soc_write(codec
, ALC_CTL1
, ALC
| LMTH0
);
317 snd_soc_update_bits(codec
, PW_MGMT1
, PMADL
, PMADL
);
318 snd_soc_update_bits(codec
, PW_MGMT3
, PMADR
, PMADR
);
324 static void ak4642_dai_shutdown(struct snd_pcm_substream
*substream
,
325 struct snd_soc_dai
*dai
)
327 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
328 struct snd_soc_codec
*codec
= dai
->codec
;
332 /* stop stereo input */
333 snd_soc_update_bits(codec
, PW_MGMT1
, PMADL
, 0);
334 snd_soc_update_bits(codec
, PW_MGMT3
, PMADR
, 0);
335 snd_soc_update_bits(codec
, ALC_CTL1
, ALC
, 0);
339 static int ak4642_dai_set_sysclk(struct snd_soc_dai
*codec_dai
,
340 int clk_id
, unsigned int freq
, int dir
)
342 struct snd_soc_codec
*codec
= codec_dai
->codec
;
343 struct ak4642_priv
*priv
= snd_soc_codec_get_drvdata(codec
);
345 int extended_freq
= 0;
358 pll
= PLL2
| PLL1
| PLL0
;
364 pll
= PLL3
| PLL2
| PLL0
;
371 pll
= PLL3
| PLL2
| PLL1
;
375 pll
= PLL3
| PLL2
| PLL1
| PLL0
;
382 if (extended_freq
&& !priv
->drvdata
->extended_frequencies
)
385 snd_soc_update_bits(codec
, MD_CTL1
, PLL_MASK
, pll
);
390 static int ak4642_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
392 struct snd_soc_codec
*codec
= dai
->codec
;
396 data
= MCKO
| PMPLL
; /* use MCKO */
399 /* set master/slave audio interface */
400 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
401 case SND_SOC_DAIFMT_CBM_CFM
:
405 case SND_SOC_DAIFMT_CBS_CFS
:
410 snd_soc_update_bits(codec
, PW_MGMT2
, MS
| MCKO
| PMPLL
, data
);
411 snd_soc_update_bits(codec
, MD_CTL1
, BCKO_MASK
, bcko
);
415 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
416 case SND_SOC_DAIFMT_LEFT_J
:
419 case SND_SOC_DAIFMT_I2S
:
423 * Please add RIGHT_J / DSP support here
428 snd_soc_update_bits(codec
, MD_CTL1
, DIF_MASK
, data
);
433 static int ak4642_set_mcko(struct snd_soc_codec
*codec
,
436 static const u32 fs_list
[] = {
450 static const u32 ps_list
[] = {
458 for (ps
= 0; ps
< ARRAY_SIZE(ps_list
); ps
++) {
459 for (fs
= 0; fs
< ARRAY_SIZE(fs_list
); fs
++) {
460 if (frequency
== ps_list
[ps
] * fs_list
[fs
]) {
461 snd_soc_write(codec
, MD_CTL2
,
471 static int ak4642_dai_hw_params(struct snd_pcm_substream
*substream
,
472 struct snd_pcm_hw_params
*params
,
473 struct snd_soc_dai
*dai
)
475 struct snd_soc_codec
*codec
= dai
->codec
;
476 struct ak4642_priv
*priv
= snd_soc_codec_get_drvdata(codec
);
477 u32 rate
= clk_get_rate(priv
->mcko
);
480 rate
= params_rate(params
) * 256;
482 return ak4642_set_mcko(codec
, rate
);
485 static int ak4642_set_bias_level(struct snd_soc_codec
*codec
,
486 enum snd_soc_bias_level level
)
489 case SND_SOC_BIAS_OFF
:
490 snd_soc_write(codec
, PW_MGMT1
, 0x00);
493 snd_soc_update_bits(codec
, PW_MGMT1
, PMVCM
, PMVCM
);
500 static const struct snd_soc_dai_ops ak4642_dai_ops
= {
501 .startup
= ak4642_dai_startup
,
502 .shutdown
= ak4642_dai_shutdown
,
503 .set_sysclk
= ak4642_dai_set_sysclk
,
504 .set_fmt
= ak4642_dai_set_fmt
,
505 .hw_params
= ak4642_dai_hw_params
,
508 static struct snd_soc_dai_driver ak4642_dai
= {
509 .name
= "ak4642-hifi",
511 .stream_name
= "Playback",
514 .rates
= SNDRV_PCM_RATE_8000_48000
,
515 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
517 .stream_name
= "Capture",
520 .rates
= SNDRV_PCM_RATE_8000_48000
,
521 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
522 .ops
= &ak4642_dai_ops
,
523 .symmetric_rates
= 1,
526 static int ak4642_suspend(struct snd_soc_codec
*codec
)
528 struct regmap
*regmap
= dev_get_regmap(codec
->dev
, NULL
);
530 regcache_cache_only(regmap
, true);
531 regcache_mark_dirty(regmap
);
535 static int ak4642_resume(struct snd_soc_codec
*codec
)
537 struct regmap
*regmap
= dev_get_regmap(codec
->dev
, NULL
);
539 regcache_cache_only(regmap
, false);
540 regcache_sync(regmap
);
543 static int ak4642_probe(struct snd_soc_codec
*codec
)
545 struct ak4642_priv
*priv
= snd_soc_codec_get_drvdata(codec
);
548 ak4642_set_mcko(codec
, clk_get_rate(priv
->mcko
));
553 static const struct snd_soc_codec_driver soc_codec_dev_ak4642
= {
554 .probe
= ak4642_probe
,
555 .suspend
= ak4642_suspend
,
556 .resume
= ak4642_resume
,
557 .set_bias_level
= ak4642_set_bias_level
,
558 .component_driver
= {
559 .controls
= ak4642_snd_controls
,
560 .num_controls
= ARRAY_SIZE(ak4642_snd_controls
),
561 .dapm_widgets
= ak4642_dapm_widgets
,
562 .num_dapm_widgets
= ARRAY_SIZE(ak4642_dapm_widgets
),
563 .dapm_routes
= ak4642_intercon
,
564 .num_dapm_routes
= ARRAY_SIZE(ak4642_intercon
),
568 static const struct regmap_config ak4642_regmap
= {
571 .max_register
= FIL1_3
,
572 .reg_defaults
= ak4642_reg
,
573 .num_reg_defaults
= NUM_AK4642_REG_DEFAULTS
,
574 .cache_type
= REGCACHE_RBTREE
,
577 static const struct regmap_config ak4643_regmap
= {
580 .max_register
= SPK_MS
,
581 .reg_defaults
= ak4643_reg
,
582 .num_reg_defaults
= ARRAY_SIZE(ak4643_reg
),
583 .cache_type
= REGCACHE_RBTREE
,
586 static const struct regmap_config ak4648_regmap
= {
589 .max_register
= EQ_FBEQE
,
590 .reg_defaults
= ak4648_reg
,
591 .num_reg_defaults
= ARRAY_SIZE(ak4648_reg
),
592 .cache_type
= REGCACHE_RBTREE
,
595 static const struct ak4642_drvdata ak4642_drvdata
= {
596 .regmap_config
= &ak4642_regmap
,
599 static const struct ak4642_drvdata ak4643_drvdata
= {
600 .regmap_config
= &ak4643_regmap
,
603 static const struct ak4642_drvdata ak4648_drvdata
= {
604 .regmap_config
= &ak4648_regmap
,
605 .extended_frequencies
= 1,
608 #ifdef CONFIG_COMMON_CLK
609 static struct clk
*ak4642_of_parse_mcko(struct device
*dev
)
611 struct device_node
*np
= dev
->of_node
;
613 const char *clk_name
= np
->name
;
614 const char *parent_clk_name
= NULL
;
617 if (of_property_read_u32(np
, "clock-frequency", &rate
))
620 if (of_property_read_bool(np
, "clocks"))
621 parent_clk_name
= of_clk_get_parent_name(np
, 0);
623 of_property_read_string(np
, "clock-output-names", &clk_name
);
625 clk
= clk_register_fixed_rate(dev
, clk_name
, parent_clk_name
, 0, rate
);
627 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
632 #define ak4642_of_parse_mcko(d) 0
635 static const struct of_device_id ak4642_of_match
[];
636 static int ak4642_i2c_probe(struct i2c_client
*i2c
,
637 const struct i2c_device_id
*id
)
639 struct device
*dev
= &i2c
->dev
;
640 struct device_node
*np
= dev
->of_node
;
641 const struct ak4642_drvdata
*drvdata
= NULL
;
642 struct regmap
*regmap
;
643 struct ak4642_priv
*priv
;
644 struct clk
*mcko
= NULL
;
647 const struct of_device_id
*of_id
;
649 mcko
= ak4642_of_parse_mcko(dev
);
653 of_id
= of_match_device(ak4642_of_match
, dev
);
655 drvdata
= of_id
->data
;
657 drvdata
= (const struct ak4642_drvdata
*)id
->driver_data
;
661 dev_err(dev
, "Unknown device type\n");
665 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
669 priv
->drvdata
= drvdata
;
672 i2c_set_clientdata(i2c
, priv
);
674 regmap
= devm_regmap_init_i2c(i2c
, drvdata
->regmap_config
);
676 return PTR_ERR(regmap
);
678 return snd_soc_register_codec(dev
,
679 &soc_codec_dev_ak4642
, &ak4642_dai
, 1);
682 static int ak4642_i2c_remove(struct i2c_client
*client
)
684 snd_soc_unregister_codec(&client
->dev
);
688 static const struct of_device_id ak4642_of_match
[] = {
689 { .compatible
= "asahi-kasei,ak4642", .data
= &ak4642_drvdata
},
690 { .compatible
= "asahi-kasei,ak4643", .data
= &ak4643_drvdata
},
691 { .compatible
= "asahi-kasei,ak4648", .data
= &ak4648_drvdata
},
694 MODULE_DEVICE_TABLE(of
, ak4642_of_match
);
696 static const struct i2c_device_id ak4642_i2c_id
[] = {
697 { "ak4642", (kernel_ulong_t
)&ak4642_drvdata
},
698 { "ak4643", (kernel_ulong_t
)&ak4643_drvdata
},
699 { "ak4648", (kernel_ulong_t
)&ak4648_drvdata
},
702 MODULE_DEVICE_TABLE(i2c
, ak4642_i2c_id
);
704 static struct i2c_driver ak4642_i2c_driver
= {
706 .name
= "ak4642-codec",
707 .of_match_table
= ak4642_of_match
,
709 .probe
= ak4642_i2c_probe
,
710 .remove
= ak4642_i2c_remove
,
711 .id_table
= ak4642_i2c_id
,
714 module_i2c_driver(ak4642_i2c_driver
);
716 MODULE_DESCRIPTION("Soc AK4642 driver");
717 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
718 MODULE_LICENSE("GPL");