1 // SPDX-License-Identifier: GPL-2.0
3 // ak4642.c -- AK4642/AK4643 ALSA Soc Audio driver
5 // Copyright (C) 2009 Renesas Solutions Corp.
6 // Kuninori Morimoto <morimoto.kuninori@renesas.com>
8 // Based on wm8731.c by Richard Purdie
9 // Based on ak4535.c by Richard Purdie
10 // Based on wm8753.c by Liam Girdwood
14 * This is very simple driver.
15 * It can use headphone output / stereo input only
22 #include <linux/clk.h>
23 #include <linux/clk-provider.h>
24 #include <linux/delay.h>
25 #include <linux/i2c.h>
26 #include <linux/slab.h>
27 #include <linux/of_device.h>
28 #include <linux/module.h>
29 #include <linux/regmap.h>
30 #include <sound/soc.h>
31 #include <sound/initval.h>
32 #include <sound/tlv.h>
65 #define FIL1_3 0x1f /* The maximum valid register for ak4642 */
70 #define SPK_MS 0x24 /* The maximum valid register for ak4643 */
71 #define EQ_FBEQAB 0x25
72 #define EQ_FBEQCD 0x26
73 #define EQ_FBEQE 0x27 /* The maximum valid register for ak4648 */
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 LOPS (1 << 6) /* Stero Line-out Power Save Mode */
105 #define ZTM(param) ((param & 0x3) << 4) /* ALC Zero Crossing TimeOut */
106 #define WTM(param) (((param & 0x4) << 4) | ((param & 0x3) << 2))
109 #define ALC (1 << 5) /* ALC Enable */
110 #define LMTH0 (1 << 0) /* ALC Limiter / Recovery Level */
113 #define PLL3 (1 << 7)
114 #define PLL2 (1 << 6)
115 #define PLL1 (1 << 5)
116 #define PLL0 (1 << 4)
117 #define PLL_MASK (PLL3 | PLL2 | PLL1 | PLL0)
119 #define BCKO_MASK (1 << 3)
120 #define BCKO_64 BCKO_MASK
122 #define DIF_MASK (3 << 0)
124 #define RIGHT_J (1 << 0)
125 #define LEFT_J (2 << 0)
129 #define FSs(val) (((val & 0x7) << 0) | ((val & 0x8) << 2))
130 #define PSs(val) ((val & 0x3) << 6)
133 #define BST1 (1 << 3)
136 #define DACH (1 << 0)
138 struct ak4642_drvdata
{
139 const struct regmap_config
*regmap_config
;
140 int extended_frequencies
;
144 const struct ak4642_drvdata
*drvdata
;
149 * Playback Volume (table 39)
151 * max : 0x00 : +12.0 dB
153 * min : 0xFE : -115.0 dB
156 static const DECLARE_TLV_DB_SCALE(out_tlv
, -11550, 50, 1);
158 static const struct snd_kcontrol_new ak4642_snd_controls
[] = {
160 SOC_DOUBLE_R_TLV("Digital Playback Volume", L_DVC
, R_DVC
,
161 0, 0xFF, 1, out_tlv
),
162 SOC_SINGLE("ALC Capture Switch", ALC_CTL1
, 5, 1, 0),
163 SOC_SINGLE("ALC Capture ZC Switch", ALC_CTL1
, 4, 1, 1),
166 static const struct snd_kcontrol_new ak4642_headphone_control
=
167 SOC_DAPM_SINGLE("Switch", PW_MGMT2
, 6, 1, 0);
169 static const struct snd_kcontrol_new ak4642_lout_mixer_controls
[] = {
170 SOC_DAPM_SINGLE("DACL", SG_SL1
, 4, 1, 0),
174 static int ak4642_lout_event(struct snd_soc_dapm_widget
*w
,
175 struct snd_kcontrol
*kcontrol
, int event
)
177 struct snd_soc_component
*component
= snd_soc_dapm_to_component(w
->dapm
);
180 case SND_SOC_DAPM_PRE_PMD
:
181 case SND_SOC_DAPM_PRE_PMU
:
182 /* Power save mode ON */
183 snd_soc_component_update_bits(component
, SG_SL2
, LOPS
, LOPS
);
185 case SND_SOC_DAPM_POST_PMU
:
186 case SND_SOC_DAPM_POST_PMD
:
187 /* Power save mode OFF */
189 snd_soc_component_update_bits(component
, SG_SL2
, LOPS
, 0);
196 static const struct snd_soc_dapm_widget ak4642_dapm_widgets
[] = {
199 SND_SOC_DAPM_OUTPUT("HPOUTL"),
200 SND_SOC_DAPM_OUTPUT("HPOUTR"),
201 SND_SOC_DAPM_OUTPUT("LINEOUT"),
203 SND_SOC_DAPM_PGA("HPL Out", PW_MGMT2
, 5, 0, NULL
, 0),
204 SND_SOC_DAPM_PGA("HPR Out", PW_MGMT2
, 4, 0, NULL
, 0),
205 SND_SOC_DAPM_SWITCH("Headphone Enable", SND_SOC_NOPM
, 0, 0,
206 &ak4642_headphone_control
),
208 SND_SOC_DAPM_PGA("DACH", MD_CTL4
, 0, 0, NULL
, 0),
210 SND_SOC_DAPM_MIXER_E("LINEOUT Mixer", PW_MGMT1
, 3, 0,
211 &ak4642_lout_mixer_controls
[0],
212 ARRAY_SIZE(ak4642_lout_mixer_controls
),
214 SND_SOC_DAPM_PRE_PMU
| SND_SOC_DAPM_POST_PMU
|
215 SND_SOC_DAPM_PRE_PMD
| SND_SOC_DAPM_POST_PMD
),
218 SND_SOC_DAPM_DAC("DAC", NULL
, PW_MGMT1
, 2, 0),
221 static const struct snd_soc_dapm_route ak4642_intercon
[] = {
224 {"HPOUTL", NULL
, "HPL Out"},
225 {"HPOUTR", NULL
, "HPR Out"},
226 {"LINEOUT", NULL
, "LINEOUT Mixer"},
228 {"HPL Out", NULL
, "Headphone Enable"},
229 {"HPR Out", NULL
, "Headphone Enable"},
231 {"Headphone Enable", "Switch", "DACH"},
233 {"DACH", NULL
, "DAC"},
235 {"LINEOUT Mixer", "DACL", "DAC"},
237 { "DAC", NULL
, "Playback" },
241 * ak4642 register cache
243 static const struct reg_default ak4643_reg
[] = {
244 { 0, 0x00 }, { 1, 0x00 }, { 2, 0x01 }, { 3, 0x00 },
245 { 4, 0x02 }, { 5, 0x00 }, { 6, 0x00 }, { 7, 0x00 },
246 { 8, 0xe1 }, { 9, 0xe1 }, { 10, 0x18 }, { 11, 0x00 },
247 { 12, 0xe1 }, { 13, 0x18 }, { 14, 0x11 }, { 15, 0x08 },
248 { 16, 0x00 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x00 },
249 { 20, 0x00 }, { 21, 0x00 }, { 22, 0x00 }, { 23, 0x00 },
250 { 24, 0x00 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0x00 },
251 { 28, 0x00 }, { 29, 0x00 }, { 30, 0x00 }, { 31, 0x00 },
252 { 32, 0x00 }, { 33, 0x00 }, { 34, 0x00 }, { 35, 0x00 },
256 /* The default settings for 0x0 ~ 0x1f registers are the same for ak4642
257 and ak4643. So we reuse the ak4643 reg_default for ak4642.
258 The valid registers for ak4642 are 0x0 ~ 0x1f which is a subset of ak4643,
259 so define NUM_AK4642_REG_DEFAULTS for ak4642.
261 #define ak4642_reg ak4643_reg
262 #define NUM_AK4642_REG_DEFAULTS (FIL1_3 + 1)
264 static const struct reg_default ak4648_reg
[] = {
265 { 0, 0x00 }, { 1, 0x00 }, { 2, 0x01 }, { 3, 0x00 },
266 { 4, 0x02 }, { 5, 0x00 }, { 6, 0x00 }, { 7, 0x00 },
267 { 8, 0xe1 }, { 9, 0xe1 }, { 10, 0x18 }, { 11, 0x00 },
268 { 12, 0xe1 }, { 13, 0x18 }, { 14, 0x11 }, { 15, 0xb8 },
269 { 16, 0x00 }, { 17, 0x00 }, { 18, 0x00 }, { 19, 0x00 },
270 { 20, 0x00 }, { 21, 0x00 }, { 22, 0x00 }, { 23, 0x00 },
271 { 24, 0x00 }, { 25, 0x00 }, { 26, 0x00 }, { 27, 0x00 },
272 { 28, 0x00 }, { 29, 0x00 }, { 30, 0x00 }, { 31, 0x00 },
273 { 32, 0x00 }, { 33, 0x00 }, { 34, 0x00 }, { 35, 0x00 },
274 { 36, 0x00 }, { 37, 0x88 }, { 38, 0x88 }, { 39, 0x08 },
277 static int ak4642_dai_startup(struct snd_pcm_substream
*substream
,
278 struct snd_soc_dai
*dai
)
280 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
281 struct snd_soc_component
*component
= dai
->component
;
285 * start headphone output
288 * Audio I/F Format :MSB justified (ADC & DAC)
289 * Bass Boost Level : Middle
291 * This operation came from example code of
292 * "ASAHI KASEI AK4642" (japanese) manual p97.
294 snd_soc_component_write(component
, L_IVC
, 0x91); /* volume */
295 snd_soc_component_write(component
, R_IVC
, 0x91); /* volume */
301 * Audio I/F Format:MSB justified (ADC & DAC)
304 * ALC setting:Refer to Table 35
307 * This operation came from example code of
308 * "ASAHI KASEI AK4642" (japanese) manual p94.
310 snd_soc_component_update_bits(component
, SG_SL1
, PMMP
| MGAIN0
, PMMP
| MGAIN0
);
311 snd_soc_component_write(component
, TIMER
, ZTM(0x3) | WTM(0x3));
312 snd_soc_component_write(component
, ALC_CTL1
, ALC
| LMTH0
);
313 snd_soc_component_update_bits(component
, PW_MGMT1
, PMADL
, PMADL
);
314 snd_soc_component_update_bits(component
, PW_MGMT3
, PMADR
, PMADR
);
320 static void ak4642_dai_shutdown(struct snd_pcm_substream
*substream
,
321 struct snd_soc_dai
*dai
)
323 int is_play
= substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
;
324 struct snd_soc_component
*component
= dai
->component
;
328 /* stop stereo input */
329 snd_soc_component_update_bits(component
, PW_MGMT1
, PMADL
, 0);
330 snd_soc_component_update_bits(component
, PW_MGMT3
, PMADR
, 0);
331 snd_soc_component_update_bits(component
, ALC_CTL1
, ALC
, 0);
335 static int ak4642_dai_set_sysclk(struct snd_soc_dai
*codec_dai
,
336 int clk_id
, unsigned int freq
, int dir
)
338 struct snd_soc_component
*component
= codec_dai
->component
;
339 struct ak4642_priv
*priv
= snd_soc_component_get_drvdata(component
);
341 int extended_freq
= 0;
354 pll
= PLL2
| PLL1
| PLL0
;
360 pll
= PLL3
| PLL2
| PLL0
;
367 pll
= PLL3
| PLL2
| PLL1
;
371 pll
= PLL3
| PLL2
| PLL1
| PLL0
;
378 if (extended_freq
&& !priv
->drvdata
->extended_frequencies
)
381 snd_soc_component_update_bits(component
, MD_CTL1
, PLL_MASK
, pll
);
386 static int ak4642_dai_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
388 struct snd_soc_component
*component
= dai
->component
;
392 data
= MCKO
| PMPLL
; /* use MCKO */
395 /* set master/slave audio interface */
396 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
397 case SND_SOC_DAIFMT_CBM_CFM
:
401 case SND_SOC_DAIFMT_CBS_CFS
:
406 snd_soc_component_update_bits(component
, PW_MGMT2
, MS
| MCKO
| PMPLL
, data
);
407 snd_soc_component_update_bits(component
, MD_CTL1
, BCKO_MASK
, bcko
);
411 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
412 case SND_SOC_DAIFMT_LEFT_J
:
415 case SND_SOC_DAIFMT_I2S
:
419 * Please add RIGHT_J / DSP support here
424 snd_soc_component_update_bits(component
, MD_CTL1
, DIF_MASK
, data
);
429 static int ak4642_set_mcko(struct snd_soc_component
*component
,
432 static const u32 fs_list
[] = {
446 static const u32 ps_list
[] = {
454 for (ps
= 0; ps
< ARRAY_SIZE(ps_list
); ps
++) {
455 for (fs
= 0; fs
< ARRAY_SIZE(fs_list
); fs
++) {
456 if (frequency
== ps_list
[ps
] * fs_list
[fs
]) {
457 snd_soc_component_write(component
, MD_CTL2
,
467 static int ak4642_dai_hw_params(struct snd_pcm_substream
*substream
,
468 struct snd_pcm_hw_params
*params
,
469 struct snd_soc_dai
*dai
)
471 struct snd_soc_component
*component
= dai
->component
;
472 struct ak4642_priv
*priv
= snd_soc_component_get_drvdata(component
);
473 u32 rate
= clk_get_rate(priv
->mcko
);
476 rate
= params_rate(params
) * 256;
478 return ak4642_set_mcko(component
, rate
);
481 static int ak4642_set_bias_level(struct snd_soc_component
*component
,
482 enum snd_soc_bias_level level
)
485 case SND_SOC_BIAS_OFF
:
486 snd_soc_component_write(component
, PW_MGMT1
, 0x00);
489 snd_soc_component_update_bits(component
, PW_MGMT1
, PMVCM
, PMVCM
);
496 static const struct snd_soc_dai_ops ak4642_dai_ops
= {
497 .startup
= ak4642_dai_startup
,
498 .shutdown
= ak4642_dai_shutdown
,
499 .set_sysclk
= ak4642_dai_set_sysclk
,
500 .set_fmt
= ak4642_dai_set_fmt
,
501 .hw_params
= ak4642_dai_hw_params
,
504 static struct snd_soc_dai_driver ak4642_dai
= {
505 .name
= "ak4642-hifi",
507 .stream_name
= "Playback",
510 .rates
= SNDRV_PCM_RATE_8000_48000
,
511 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
513 .stream_name
= "Capture",
516 .rates
= SNDRV_PCM_RATE_8000_48000
,
517 .formats
= SNDRV_PCM_FMTBIT_S16_LE
},
518 .ops
= &ak4642_dai_ops
,
519 .symmetric_rates
= 1,
522 static int ak4642_suspend(struct snd_soc_component
*component
)
524 struct regmap
*regmap
= dev_get_regmap(component
->dev
, NULL
);
526 regcache_cache_only(regmap
, true);
527 regcache_mark_dirty(regmap
);
531 static int ak4642_resume(struct snd_soc_component
*component
)
533 struct regmap
*regmap
= dev_get_regmap(component
->dev
, NULL
);
535 regcache_cache_only(regmap
, false);
536 regcache_sync(regmap
);
539 static int ak4642_probe(struct snd_soc_component
*component
)
541 struct ak4642_priv
*priv
= snd_soc_component_get_drvdata(component
);
544 ak4642_set_mcko(component
, clk_get_rate(priv
->mcko
));
549 static const struct snd_soc_component_driver soc_component_dev_ak4642
= {
550 .probe
= ak4642_probe
,
551 .suspend
= ak4642_suspend
,
552 .resume
= ak4642_resume
,
553 .set_bias_level
= ak4642_set_bias_level
,
554 .controls
= ak4642_snd_controls
,
555 .num_controls
= ARRAY_SIZE(ak4642_snd_controls
),
556 .dapm_widgets
= ak4642_dapm_widgets
,
557 .num_dapm_widgets
= ARRAY_SIZE(ak4642_dapm_widgets
),
558 .dapm_routes
= ak4642_intercon
,
559 .num_dapm_routes
= ARRAY_SIZE(ak4642_intercon
),
562 .non_legacy_dai_naming
= 1,
565 static const struct regmap_config ak4642_regmap
= {
568 .max_register
= FIL1_3
,
569 .reg_defaults
= ak4642_reg
,
570 .num_reg_defaults
= NUM_AK4642_REG_DEFAULTS
,
571 .cache_type
= REGCACHE_RBTREE
,
574 static const struct regmap_config ak4643_regmap
= {
577 .max_register
= SPK_MS
,
578 .reg_defaults
= ak4643_reg
,
579 .num_reg_defaults
= ARRAY_SIZE(ak4643_reg
),
580 .cache_type
= REGCACHE_RBTREE
,
583 static const struct regmap_config ak4648_regmap
= {
586 .max_register
= EQ_FBEQE
,
587 .reg_defaults
= ak4648_reg
,
588 .num_reg_defaults
= ARRAY_SIZE(ak4648_reg
),
589 .cache_type
= REGCACHE_RBTREE
,
592 static const struct ak4642_drvdata ak4642_drvdata
= {
593 .regmap_config
= &ak4642_regmap
,
596 static const struct ak4642_drvdata ak4643_drvdata
= {
597 .regmap_config
= &ak4643_regmap
,
600 static const struct ak4642_drvdata ak4648_drvdata
= {
601 .regmap_config
= &ak4648_regmap
,
602 .extended_frequencies
= 1,
605 #ifdef CONFIG_COMMON_CLK
606 static struct clk
*ak4642_of_parse_mcko(struct device
*dev
)
608 struct device_node
*np
= dev
->of_node
;
610 const char *clk_name
= np
->name
;
611 const char *parent_clk_name
= NULL
;
614 if (of_property_read_u32(np
, "clock-frequency", &rate
))
617 if (of_property_read_bool(np
, "clocks"))
618 parent_clk_name
= of_clk_get_parent_name(np
, 0);
620 of_property_read_string(np
, "clock-output-names", &clk_name
);
622 clk
= clk_register_fixed_rate(dev
, clk_name
, parent_clk_name
, 0, rate
);
624 of_clk_add_provider(np
, of_clk_src_simple_get
, clk
);
629 #define ak4642_of_parse_mcko(d) 0
632 static const struct of_device_id ak4642_of_match
[];
633 static int ak4642_i2c_probe(struct i2c_client
*i2c
,
634 const struct i2c_device_id
*id
)
636 struct device
*dev
= &i2c
->dev
;
637 struct device_node
*np
= dev
->of_node
;
638 const struct ak4642_drvdata
*drvdata
= NULL
;
639 struct regmap
*regmap
;
640 struct ak4642_priv
*priv
;
641 struct clk
*mcko
= NULL
;
644 const struct of_device_id
*of_id
;
646 mcko
= ak4642_of_parse_mcko(dev
);
650 of_id
= of_match_device(ak4642_of_match
, dev
);
652 drvdata
= of_id
->data
;
654 drvdata
= (const struct ak4642_drvdata
*)id
->driver_data
;
658 dev_err(dev
, "Unknown device type\n");
662 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
666 priv
->drvdata
= drvdata
;
669 i2c_set_clientdata(i2c
, priv
);
671 regmap
= devm_regmap_init_i2c(i2c
, drvdata
->regmap_config
);
673 return PTR_ERR(regmap
);
675 return devm_snd_soc_register_component(dev
,
676 &soc_component_dev_ak4642
, &ak4642_dai
, 1);
679 static const struct of_device_id ak4642_of_match
[] = {
680 { .compatible
= "asahi-kasei,ak4642", .data
= &ak4642_drvdata
},
681 { .compatible
= "asahi-kasei,ak4643", .data
= &ak4643_drvdata
},
682 { .compatible
= "asahi-kasei,ak4648", .data
= &ak4648_drvdata
},
685 MODULE_DEVICE_TABLE(of
, ak4642_of_match
);
687 static const struct i2c_device_id ak4642_i2c_id
[] = {
688 { "ak4642", (kernel_ulong_t
)&ak4642_drvdata
},
689 { "ak4643", (kernel_ulong_t
)&ak4643_drvdata
},
690 { "ak4648", (kernel_ulong_t
)&ak4648_drvdata
},
693 MODULE_DEVICE_TABLE(i2c
, ak4642_i2c_id
);
695 static struct i2c_driver ak4642_i2c_driver
= {
697 .name
= "ak4642-codec",
698 .of_match_table
= ak4642_of_match
,
700 .probe
= ak4642_i2c_probe
,
701 .id_table
= ak4642_i2c_id
,
704 module_i2c_driver(ak4642_i2c_driver
);
706 MODULE_DESCRIPTION("Soc AK4642 driver");
707 MODULE_AUTHOR("Kuninori Morimoto <morimoto.kuninori@renesas.com>");
708 MODULE_LICENSE("GPL v2");